From 7af96d6bf6003af29c2f90146843a0754756697e Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Fri, 26 Apr 2024 14:16:35 +0200 Subject: [PATCH] Add `iterativeMean` to `utils` Verified with: ```py myIterativeMean = iterativeMean() print(myIterativeMean.mean) myIterativeMean.addElement(2) print(myIterativeMean.mean) myIterativeMean.addElement(3) print(myIterativeMean.mean) ``` Note that cannot simplify as follows in the general case of using images as `element`s for instance: ```diff diff --git a/datasets/raise/utils.py b/datasets/raise/utils.py index fba5523..a116edb 100644 --- a/datasets/raise/utils.py +++ b/datasets/raise/utils.py @@ -27,12 +27,9 @@ def denoise(imageNpArray, denoiserName): return imageDenoisedNpArray class iterativeMean(): - mean = None + mean = 0 numberOfElementsInMean = 0 def addElement(self, element): - if self.mean is None: - self.mean = element - else: - self.mean = ((self.mean * self.numberOfElementsInMean) + element) / (self.numberOfElementsInMean + 1) + self.mean = ((self.mean * self.numberOfElementsInMean) + element) / (self.numberOfElementsInMean + 1) self.numberOfElementsInMean += 1 \ No newline at end of file ``` --- datasets/raise/utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/datasets/raise/utils.py b/datasets/raise/utils.py index 79fa11d..a116edb 100644 --- a/datasets/raise/utils.py +++ b/datasets/raise/utils.py @@ -24,4 +24,12 @@ def denoise(imageNpArray, denoiserName): imageDenoisedNpArray = skImageRestorationDenoise(imageNpArray, sigma_color=0.05, sigma_spatial=15) case 'tv_chambolle': imageDenoisedNpArray = skImageRestorationDenoise(imageNpArray, weight=0.2) - return imageDenoisedNpArray \ No newline at end of file + return imageDenoisedNpArray + +class iterativeMean(): + mean = 0 + numberOfElementsInMean = 0 + + def addElement(self, element): + self.mean = ((self.mean * self.numberOfElementsInMean) + element) / (self.numberOfElementsInMean + 1) + self.numberOfElementsInMean += 1 \ No newline at end of file