From 77e7d56a4ed2a0b0e9ff57b09d242a37e1e3a48a Mon Sep 17 00:00:00 2001 From: geegee Date: Sun, 20 Nov 2022 16:22:13 +0100 Subject: [PATCH 1/4] Using a most likelihood estimator to improve performance of the estimator. --- src/qudi/util/fit_models/gaussian.py | 59 ++++++++++++++++++---------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/qudi/util/fit_models/gaussian.py b/src/qudi/util/fit_models/gaussian.py index cb7a4fd45..3cb9d530d 100644 --- a/src/qudi/util/fit_models/gaussian.py +++ b/src/qudi/util/fit_models/gaussian.py @@ -351,29 +351,46 @@ def _model_function(xy, offset, amplitude, center_x, center_y, sigma_x, sigma_y, @estimator('Peak') def estimate_peak(self, data, xy): # ToDo: Not properly implemented, yet - x_range = abs(np.max(xy[0]) - np.min(xy[0])) - y_range = abs(np.max(xy[1]) - np.min(xy[1])) + stepsize_x = xy[0][1,0] - xy[0][0,0] + stepsize_y = xy[0][0,1] - xy[0][0,0] + x_axis = xy[0].flatten() + y_axis = xy[1].flatten() + + # TODO: Make good estimate for theta + + amplitude = float(data.max() - data.min()) + + # By calculating the log likelihood of the 2D gaussian pdf, one obtain for + # the minimization of the center_x or center_y values the following formula + # (which are in fact just the expectation/mean value formula): + norm = np.sum(data) + center_x = np.sum(x_axis * data) / norm + center_y = np.sum(y_axis * data) / norm + exp_of_xsquared = np.sum(x_axis ** 2 * data) / norm + exp_of_ysquared = np.sum(y_axis ** 2 * data) / norm + sigma_x = np.sqrt(exp_of_xsquared - center_x ** 2) + sigma_y = np.sqrt(exp_of_ysquared - center_y ** 2) + xrange = x_axis.max() - x_axis.min() + yrange = y_axis.max() - y_axis.min() + theta = 0.0 + offset = float(data.min()) + + # populate the parameter container: + params = self.make_params() + params['amplitude'].set(value=amplitude, min=100, max=1e7) + params['sigma_x'].set(value=sigma_x, min=1*stepsize_x, + max=3*xrange) + params['sigma_y'].set(value=sigma_y, min=1*stepsize_y, + max=3*yrange) + params['center_x'].set(value=center_x, min=center_x - xrange, + max=center_x + xrange) + params['center_y'].set(value=center_y, min=center_y - yrange, + max=center_y + yrange) + params['theta'].set(value=theta, min=0, max=np.pi) + params['offset'].set(value=offset, min=0, max=1e7) + return params - amplitude = np.max(data) - center_x = x_range / 2 + np.min(xy[0]) - center_y = y_range / 2 + np.min(xy[1]) - sigma_x = x_range / 10 - sigma_y = y_range / 10 - theta = 0 - estimate = self.make_params() - estimate['offset'].set(value=np.mean(data), min=-np.inf, max=np.max(data)) - estimate['amplitude'].set(value=amplitude, min=0, max=amplitude * 2) - estimate['center_x'].set(value=center_x, - min=np.min(xy[0]) - x_range / 2, - max=np.max(xy[0]) + x_range / 2) - estimate['center_y'].set(value=center_y, - min=np.min(xy[1]) - y_range / 2, - max=np.max(xy[1]) + y_range / 2) - estimate['sigma_x'].set(value=sigma_x, min=x_range / (xy[0].shape[0] - 1), max=x_range) - estimate['sigma_y'].set(value=sigma_y, min=y_range / (xy[0].shape[1] - 1), max=y_range) - estimate['theta'].set(value=theta, min=-np.pi, max=np.pi) - return estimate @estimator('Dip') def estimate_dip(self, data, xy): From 5b1d06cb61fc78c8c7479171e7aba3610417f876 Mon Sep 17 00:00:00 2001 From: geegee Date: Mon, 21 Nov 2022 09:46:09 +0100 Subject: [PATCH 2/4] Small bugfix in the determination in the stepsize in y. --- src/qudi/util/fit_models/gaussian.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/qudi/util/fit_models/gaussian.py b/src/qudi/util/fit_models/gaussian.py index 3cb9d530d..b3397c4b4 100644 --- a/src/qudi/util/fit_models/gaussian.py +++ b/src/qudi/util/fit_models/gaussian.py @@ -28,6 +28,8 @@ from qudi.util.fit_models.helpers import estimate_double_peaks, estimate_triple_peaks from qudi.util.fit_models.linear import Linear +from qudi.core.logger import get_logger +logger = get_logger(__name__) def multiple_gaussian(x, centers, sigmas, amplitudes): """ Mathematical definition of the sum of multiple gaussian functions without any bias. @@ -351,8 +353,11 @@ def _model_function(xy, offset, amplitude, center_x, center_y, sigma_x, sigma_y, @estimator('Peak') def estimate_peak(self, data, xy): # ToDo: Not properly implemented, yet + logger.info(f'have a look at the input: {xy[0]}') stepsize_x = xy[0][1,0] - xy[0][0,0] - stepsize_y = xy[0][0,1] - xy[0][0,0] + stepsize_y = xy[1][0,1] - xy[1][0,0] + logger.info(f'stepsize_x {stepsize_x} stepsize_y {stepsize_y}') + logger.info(f"length of xy {len(xy)}") x_axis = xy[0].flatten() y_axis = xy[1].flatten() From cc8149df95021c25a969b84673d53cc21e81bc28 Mon Sep 17 00:00:00 2001 From: geegee Date: Mon, 21 Nov 2022 09:55:36 +0100 Subject: [PATCH 3/4] Remove logging --- src/qudi/util/fit_models/gaussian.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/qudi/util/fit_models/gaussian.py b/src/qudi/util/fit_models/gaussian.py index b3397c4b4..c03725d72 100644 --- a/src/qudi/util/fit_models/gaussian.py +++ b/src/qudi/util/fit_models/gaussian.py @@ -353,11 +353,8 @@ def _model_function(xy, offset, amplitude, center_x, center_y, sigma_x, sigma_y, @estimator('Peak') def estimate_peak(self, data, xy): # ToDo: Not properly implemented, yet - logger.info(f'have a look at the input: {xy[0]}') stepsize_x = xy[0][1,0] - xy[0][0,0] stepsize_y = xy[1][0,1] - xy[1][0,0] - logger.info(f'stepsize_x {stepsize_x} stepsize_y {stepsize_y}') - logger.info(f"length of xy {len(xy)}") x_axis = xy[0].flatten() y_axis = xy[1].flatten() From 9632606fa34ea52e4fad5bf5525c417bc87c44de Mon Sep 17 00:00:00 2001 From: Nikolas Tomek Date: Thu, 18 Jun 2026 21:18:47 +0200 Subject: [PATCH 4/4] Remove unused logger from gaussian.py Removed unused logger initialization from gaussian.py --- src/qudi/util/fit_models/gaussian.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/qudi/util/fit_models/gaussian.py b/src/qudi/util/fit_models/gaussian.py index b42c8619e..f85fb805b 100644 --- a/src/qudi/util/fit_models/gaussian.py +++ b/src/qudi/util/fit_models/gaussian.py @@ -29,8 +29,6 @@ from qudi.util.fit_models.helpers import estimate_double_peaks, estimate_triple_peaks from qudi.util.fit_models.linear import Linear -from qudi.core.logger import get_logger -logger = get_logger(__name__) def multiple_gaussian(x, centers, sigmas, amplitudes): """ Mathematical definition of the sum of multiple gaussian functions without any bias.