Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved gaussian2d estimator #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions src/qudi/util/fit_models/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -351,29 +353,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[1][0,1] - xy[1][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):
Expand Down