Skip to content

Commit

Permalink
makes optimize routine compatible with changes to sample_counts
Browse files Browse the repository at this point in the history
  • Loading branch information
gadamc committed Feb 3, 2023
1 parent 7c18bb1 commit 265c5c8
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/qt3utils/datagenerators/piezoscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def optimize_position(self, axis, center_position, width = 2, step_size = 0.25):
example:
([r0, r1, ...], [x0, x1, ...], x_optimal, [C, mu, sigma])
In cases where the data cannot be succesfully fit to a gaussian function,
the optimial_position returned is the absolute brightest position in the scan,
In cases where the data cannot be successfully fit to a gaussian function,
the optimal_position returned is the absolute brightest position in the scan,
and the fit_coeff is set to None.
When the fit is sucessful, x_optimal = mu.
When the fit is successful, x_optimal = mu.
'''
min_val = center_position - width
Expand All @@ -171,16 +171,17 @@ def optimize_position(self, axis, center_position, width = 2, step_size = 0.25):
raw_counts = self.scan_axis(axis, min_val, max_val, step_size)
self.stop()
axis_vals = np.arange(min_val, max_val, step_size)
count_rate = self.sample_count_rate(raw_counts)
count_rates = [self.sample_count_rate(count) for count in raw_counts]

optimal_position = axis_vals[np.argmax(count_rate)]
optimal_position = axis_vals[np.argmax(count_rates)]
coeff = None
params = [np.max(count_rate), optimal_position, 1.0, np.min(count_rate)]
params = [np.max(count_rates), optimal_position, 1.0, np.min(count_rates)]
bounds = (len(params)*tuple((0,)), len(params)*tuple((np.inf,)))
try:
coeff, var_matrix = scipy.optimize.curve_fit(gauss, axis_vals, count_rate, p0=params)
coeff, var_matrix = scipy.optimize.curve_fit(gauss, axis_vals, count_rates, p0=params, bounds=bounds)
optimal_position = coeff[1]
except RuntimeError as e:
print(e)

return count_rate, axis_vals, optimal_position, coeff
return count_rates, axis_vals, optimal_position, coeff

0 comments on commit 265c5c8

Please sign in to comment.