From 78c57ab1c58a8261825f4567857f7619e30e4f89 Mon Sep 17 00:00:00 2001 From: Ajinkya Kulkarni Date: Sun, 2 Apr 2023 19:28:06 +0200 Subject: [PATCH 1/2] Improve code readability and speed 1. Used np.apply_along_axis to calculate stack_ instead of using a list comprehension. 2. Used elif statements instead of multiple if statements to improve code readability. --- torchstain/numpy/normalizers/reinhard.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchstain/numpy/normalizers/reinhard.py b/torchstain/numpy/normalizers/reinhard.py index d299dd3..255141b 100644 --- a/torchstain/numpy/normalizers/reinhard.py +++ b/torchstain/numpy/normalizers/reinhard.py @@ -25,7 +25,7 @@ def fit(self, target): lab = rgb2lab(target) # get summary statistics - stack_ = np.array([get_mean_std(x) for x in lab_split(lab)]) + stack_ = np.apply_along_axis(get_mean_std, 1, lab_split(lab)) self.target_means = stack_[:, 0] self.target_stds = stack_[:, 1] @@ -38,7 +38,7 @@ def normalize(self, I): labs = lab_split(lab) # get summary statistics from LAB - stack_ = np.array([get_mean_std(x) for x in labs]) + stack_ = np.apply_along_axis(get_mean_std, 1, labs) mus = stack_[:, 0] stds = stack_[:, 1] @@ -62,7 +62,7 @@ def normalize(self, I): else: raise ValueError("Unsupported 'method' was chosen. Choose either {None, 'modified'}.") - + # rebuild LAB lab = lab_merge(*result) From 706bea10c04c05f2d09949c7524407bb5981c39b Mon Sep 17 00:00:00 2001 From: Ajinkya Kulkarni Date: Thu, 6 Apr 2023 19:42:13 +0200 Subject: [PATCH 2/2] Minor update to the `apply_along_axis` method --- torchstain/numpy/normalizers/reinhard.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/torchstain/numpy/normalizers/reinhard.py b/torchstain/numpy/normalizers/reinhard.py index 255141b..035c49f 100644 --- a/torchstain/numpy/normalizers/reinhard.py +++ b/torchstain/numpy/normalizers/reinhard.py @@ -25,7 +25,9 @@ def fit(self, target): lab = rgb2lab(target) # get summary statistics - stack_ = np.apply_along_axis(get_mean_std, 1, lab_split(lab)) +# stack_ = np.apply_along_axis(get_mean_std, 1, lab_split(lab)) + stack_ = np.apply_along_axis(get_mean_std, axis=1, arr=lab_split(lab)) + self.target_means = stack_[:, 0] self.target_stds = stack_[:, 1] @@ -38,7 +40,9 @@ def normalize(self, I): labs = lab_split(lab) # get summary statistics from LAB - stack_ = np.apply_along_axis(get_mean_std, 1, labs) +# stack_ = np.apply_along_axis(get_mean_std, 1, labs) + stack_ = np.apply_along_axis(get_mean_std, axis=1, arr=labs) + mus = stack_[:, 0] stds = stack_[:, 1]