Axes can start from non zero now (2d bimodal fit)
This commit is contained in:
parent
a841f10379
commit
16c6ba7d86
@ -428,6 +428,8 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
# reshaping the image to 2D in the form [[a_00, a_01, .., a_0Y], [a_10,.., a_1Y], .., [a_X0, .., a_XY]], with a_xy
|
||||
x_width = len(np.unique(x))
|
||||
y_width = len(np.unique(y))
|
||||
x_1d = np.linspace(x[0], x[-1], x_width)
|
||||
y_1d = np.linspace(y[0], y[-1], y_width)
|
||||
|
||||
data = np.reshape(data, (y_width, x_width))
|
||||
data = data.T
|
||||
@ -440,13 +442,15 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
# binarizing image to guess BEC width and calculate center
|
||||
thresh = self.calc_thresh(data,thresh_val=0.5)
|
||||
# calculating center of cloud by statistical distribution of binarized image
|
||||
center = self.calc_cen(thresh)
|
||||
center_pix = self.calc_cen_pix(thresh)
|
||||
center = self.center_pix_conv(center_pix, x_1d, y_1d)
|
||||
# guessing BEC width, or better of width of center blob if no BEC is present
|
||||
BEC_width_guess = self.guess_BEC_width(thresh, center)
|
||||
BEC_width_guess = self.guess_BEC_width(thresh, center_pix)
|
||||
|
||||
# plot binarized image and center position for debugging
|
||||
if self.is_debug:
|
||||
plt.pcolormesh(thresh.T, cmap='jet')
|
||||
X, Y = np.meshgrid(x_1d,y_1d)
|
||||
plt.pcolormesh(X,Y, thresh.T, cmap='jet')
|
||||
plt.plot(center[0], center[1], marker='x', markersize=25, color='green')
|
||||
plt.gca().set_aspect('equal')
|
||||
plt.title(f'Binarized image for guessing BEC width + center position (BEC_width: x={BEC_width_guess[0]:.0f}, y={BEC_width_guess[1]:.0f} pix)')
|
||||
@ -459,17 +463,19 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
if self.is_debug:
|
||||
print(f'x smaller y, 1d fit along x')
|
||||
s_width_ind = 0
|
||||
x_fit = x_1d
|
||||
# slice of the image along the short BEC axis with width of BEC width is taken
|
||||
X_guess = np.sum(data[:, round(center[1] - BEC_width_guess[1]/2) : round(center[1] + BEC_width_guess[1]/2)], 1) / len(data[0,round(center[1] - BEC_width_guess[1]/2) : round(center[1] + BEC_width_guess[1]/2)])
|
||||
X_guess = np.sum(data[:, round(center_pix[1] - BEC_width_guess[1]/2) : round(center_pix[1] + BEC_width_guess[1]/2)], 1) / len(data[0,round(center_pix[1] - BEC_width_guess[1]/2) : round(center_pix[1] + BEC_width_guess[1]/2)])
|
||||
else:
|
||||
if self.is_debug:
|
||||
print(f'y smaller x, 1d fit along y')
|
||||
s_width_ind = 1
|
||||
X_guess = np.sum(data[round(center[0] - BEC_width_guess[0]/2) : round(center[0] + BEC_width_guess[0]/2), :], 0) / len(data[0,round(center[0] - BEC_width_guess[0]/2) : round(center[0] + BEC_width_guess[0]/2)])
|
||||
x_fit = y_1d
|
||||
X_guess = np.sum(data[round(center_pix[0] - BEC_width_guess[0]/2) : round(center_pix[0] + BEC_width_guess[0]/2), :], 0) / len(data[0,round(center_pix[0] - BEC_width_guess[0]/2) : round(center_pix[0] + BEC_width_guess[0]/2)])
|
||||
|
||||
|
||||
# Creating 1d fit init params + Performing fit
|
||||
x = np.linspace(0, len(X_guess), len(X_guess))
|
||||
|
||||
|
||||
max_val = np.max(X_guess)
|
||||
|
||||
@ -487,7 +493,7 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
params_1d.add('sigma_th', 3*BEC_width_guess[0], min=0, expr=f'0.632*sigma_bec + 0.518*deltax')
|
||||
|
||||
|
||||
res_1d = fitmodel_1d.fit(X_guess, x=x, params=params_1d)
|
||||
res_1d = fitmodel_1d.fit(X_guess, x=x_fit, params=params_1d)
|
||||
bval_1d = res_1d.best_values
|
||||
|
||||
if self.is_debug:
|
||||
@ -501,17 +507,16 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
print('1d fitted values')
|
||||
self.print_bval(res_1d)
|
||||
|
||||
x = np.linspace(0, len(X_guess), len(X_guess))
|
||||
plt.plot(x, X_guess, label='1d int. data')
|
||||
plt.plot(x, density_1d(x,**bval_1d), label='bimodal fit')
|
||||
plt.plot(x, thermal(x,x0=bval_1d['x0_th'], amp=bval_1d['amp_th'], sigma=bval_1d['sigma_th']), label='thermal part')
|
||||
plt.plot(x_fit, X_guess, label='1d int. data')
|
||||
plt.plot(x_fit, density_1d(x_fit,**bval_1d), label='bimodal fit')
|
||||
plt.plot(x_fit, thermal(x_fit,x0=bval_1d['x0_th'], amp=bval_1d['amp_th'], sigma=bval_1d['sigma_th']), label='thermal part')
|
||||
plt.legend()
|
||||
if s_width_ind==0:
|
||||
plt.title('1d fit of data along x-axis')
|
||||
plt.xlabel('x_axis (pix)')
|
||||
plt.xlabel('x_')
|
||||
else:
|
||||
plt.title('1d fit of data along y-axis')
|
||||
plt.xlabel('y_axis (pix)')
|
||||
plt.xlabel('y_axis')
|
||||
plt.show()
|
||||
|
||||
# scaling amplitudes of 1d fit with the maximum value of blurred 2d data
|
||||
@ -600,6 +605,7 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
|
||||
bval = res.best_values
|
||||
# Do described post_check if enabled
|
||||
|
||||
if res.params['amp_bec'].vary and res.params['amp_th'].vary and bval['amp_bec']>0.5*bval['amp_th'] and self.post_check:
|
||||
|
||||
# creating image by cutting out region around BEC and counting number of atoms
|
||||
@ -648,8 +654,8 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
|
||||
return thresh
|
||||
|
||||
def calc_cen(self, thresh1):
|
||||
"""Calculating the center of a blob (atom cloud) in a binarized image by first calculating the probability distribution along both axes and afterwards the expectation value
|
||||
def calc_cen_pix(self, thresh1):
|
||||
"""Calculating the center (in pixel) of a blob (atom cloud) in a binarized image by first calculating the probability distribution along both axes and afterwards the expectation value
|
||||
|
||||
:param thresh1: Binary 2D image in the form [[a_00, a_01, .., a_0Y], [a_10,.., a_1Y], .., [a_X0, .., a_XY]], with a_xy, x_dim=X, y_dim=Y
|
||||
:type thresh1: 2D numpy array
|
||||
@ -670,12 +676,29 @@ class DensityProfileBEC2dModel(lmfit.Model):
|
||||
cen[1] = np.sum(dy * np.arange(Y))
|
||||
return cen
|
||||
|
||||
def center_pix_conv(self, center_pix, x, y):
|
||||
"""Converts center in pixel to center in values of x and y
|
||||
|
||||
:param center_pix: pixel values of center
|
||||
:type center_pix: numpy array (shape=(1,2))
|
||||
:param x: x-axis
|
||||
:type x: 1d numpy array
|
||||
:param y: y-axis
|
||||
:type y: 1d numpy array
|
||||
:return: center coordinates in form [x_center, y_center] with respect to the axes
|
||||
:rtype: numpy array (shap=(1,2))
|
||||
"""
|
||||
center = np.empty(2)
|
||||
center[0] = x[round(center_pix[0])]
|
||||
center[1] = y[round(center_pix[1])]
|
||||
return center
|
||||
|
||||
def guess_BEC_width(self, thresh, center):
|
||||
""" returns width of blob in binarized image along both axis through the center
|
||||
|
||||
:param thresh: Binary 2D image in the form [[a_00, a_01, .., a_0Y], [a_10,.., a_1Y], .., [a_X0, .., a_XY]], with a_xy, x_dim=X, y_dim=Y
|
||||
:type thresh: 2d numpy array
|
||||
:param center: center of blob in image in form [x_center, y_center]
|
||||
:param center: center of blob in image in form [x_center, y_center] in pixel
|
||||
:type center: 1d numpy array (shape=(1,2))
|
||||
:return: width of blob in image as [x_width, y_width]
|
||||
:rtype: 1d numpy array (shape=(1,2))
|
||||
|
Loading…
Reference in New Issue
Block a user