Skip to content

Commit

Permalink
Reformatting fitting logic with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshpinto committed Sep 30, 2023
1 parent bc50244 commit 57e57c9
Show file tree
Hide file tree
Showing 10 changed files with 1,181 additions and 888 deletions.
8 changes: 5 additions & 3 deletions qudi_hira_analysis/_fitmethods/antibunchingmethods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


def make_antibunching_model(self, prefix=None):
def antibunching(x: np.ndarray, n: float, a: float, b: float, tau0: float, tau1: float, tau2: float) \
def antibunching(x: np.ndarray, n: float, a: float, b: float, tau0: float,
tau1: float, tau2: float) \
-> np.ndarray:
"""
Fit to function
Expand Down Expand Up @@ -34,7 +35,8 @@ def estimate_antibunching_dip(self, x_axis, data, params):
return error, params


def make_antibunching_fit(self, x_axis, data, estimator, units=None, add_params=None, **kwargs):
def make_antibunching_fit(self, x_axis, data, estimator, units=None, add_params=None,
**kwargs):
model, params = self.make_antibunching_model()

error, params = estimator(x_axis, data, params)
Expand All @@ -47,7 +49,7 @@ def make_antibunching_fit(self, x_axis, data, estimator, units=None, add_params=
except:
result = model.fit(data, x=x_axis, params=params, **kwargs)
self.log.warning('The 1D antibunching fit did not work. Error '
'message: {0}\n'.format(result.message))
f'message: {result.message}\n')

# Write the parameters to allow human-readable output to be generated
result_str_dict = OrderedDict()
Expand Down
178 changes: 85 additions & 93 deletions qudi_hira_analysis/_fitmethods/decaylikemethods.py

Large diffs are not rendered by default.

209 changes: 107 additions & 102 deletions qudi_hira_analysis/_fitmethods/gaussianlikemethods.py

Large diffs are not rendered by default.

290 changes: 148 additions & 142 deletions qudi_hira_analysis/_fitmethods/generalmethods.py

Large diffs are not rendered by default.

25 changes: 11 additions & 14 deletions qudi_hira_analysis/_fitmethods/hyperbolicsaturationmethods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
This file contains methods for hyperbolic saturation fitting, these methods
are imported by class FitLogic.
Expand All @@ -20,11 +19,9 @@
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/>
"""


import numpy as np
from lmfit.models import Model


################################################################################
# #
# Hyperbolic saturation models #
Expand Down Expand Up @@ -63,9 +60,9 @@ def hyperbolicsaturation_function(x, I_sat, P_sat):
return I_sat * (x / (x + P_sat))

if not isinstance(prefix, str) and prefix is not None:
self.log.error('The passed prefix <{0}> of type {1} is not a string and'
'cannot be used as a prefix and will be ignored for now.'
'Correct that!'.format(prefix, type(prefix)))
self.log.error('The passed prefix <{}> of type {} is not a string and'
'cannot be used as a prefix and will be ignored for now.'
'Correct that!'.format(prefix, type(prefix)))

mod_sat = Model(hyperbolicsaturation_function, independent_vars=['x'])
else:
Expand All @@ -80,7 +77,8 @@ def hyperbolicsaturation_function(x, I_sat, P_sat):
return complete_model, params


def make_hyperbolicsaturation_fit(self, x_axis, data, estimator, units=None, add_params=None, **kwargs):
def make_hyperbolicsaturation_fit(self, x_axis, data, estimator, units=None,
add_params=None, **kwargs):
""" Perform a fit on the provided data with a fluorescence depending function.
@param numpy.array x_axis: 1D axis values
Expand Down Expand Up @@ -128,23 +126,22 @@ def estimate_hyperbolicsaturation(self, x_axis, data, params):

error = self._check_1D_input(x_axis=x_axis, data=data, params=params)

x_axis_half = x_axis[len(x_axis)//2:]
data_half = data[len(x_axis)//2:]
x_axis_half = x_axis[len(x_axis) // 2:]
data_half = data[len(x_axis) // 2:]

results_lin = self.make_linear_fit(x_axis=x_axis_half, data=data_half,
estimator=self.estimate_linear)
estimator=self.estimate_linear)

est_slope = results_lin.params['slope'].value
est_offset = data.min()

data_red = data - est_slope*x_axis - est_offset
est_I_sat = np.mean(data_red[len(data_red)//2:])
est_P_sat = est_I_sat/2
data_red = data - est_slope * x_axis - est_offset
est_I_sat = np.mean(data_red[len(data_red) // 2:])
est_P_sat = est_I_sat / 2

params['I_sat'].value = est_I_sat
params['slope'].value = est_slope
params['offset'].value = est_offset
params['P_sat'].value = est_P_sat


return error, params
46 changes: 25 additions & 21 deletions qudi_hira_analysis/_fitmethods/linearmethods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
This file contains methods for linear fitting, these methods
are imported by class FitLogic. The functions can be used for amy
Expand Down Expand Up @@ -55,6 +54,7 @@ def make_constant_model(self, prefix=None):
For further information have a look in:
http://cars9.uchicago.edu/software/python/lmfit/builtin_models.html#models.GaussianModel
"""

def constant_function(x, offset):
""" Function of a constant value.
Expand All @@ -67,9 +67,10 @@ def constant_function(x, offset):
return offset

if not isinstance(prefix, str) and prefix is not None:
self.log.error('The passed prefix <{0}> of type {1} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
self.log.error(
'The passed prefix <{}> of type {} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
model = Model(constant_function, independent_vars=['x'])
else:
model = Model(constant_function, independent_vars=['x'], prefix=prefix)
Expand Down Expand Up @@ -103,9 +104,10 @@ def amplitude_function(x, amplitude):
return amplitude

if not isinstance(prefix, str) and prefix is not None:
self.log.error('The passed prefix <{0}> of type {1} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
self.log.error(
'The passed prefix <{}> of type {} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
model = Model(amplitude_function, independent_vars=['x'])
else:
model = Model(amplitude_function, independent_vars=['x'], prefix=prefix)
Expand Down Expand Up @@ -139,9 +141,10 @@ def slope_function(x, slope):
return slope

if not isinstance(prefix, str) and prefix is not None:
self.log.error('The passed prefix <{0}> of type {1} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
self.log.error(
'The passed prefix <{}> of type {} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
model = Model(slope_function, independent_vars=['x'])
else:
model = Model(slope_function, independent_vars=['x'], prefix=prefix)
Expand Down Expand Up @@ -174,9 +177,10 @@ def linear_function(x):
return x

if not isinstance(prefix, str) and prefix is not None:
self.log.error('The passed prefix <{0}> of type {1} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
self.log.error(
'The passed prefix <{}> of type {} is not a string and cannot be used as '
'a prefix and will be ignored for now. Correct that!'.format(prefix,
type(prefix)))
linear_mod = Model(linear_function, independent_vars=['x'])
else:
linear_mod = Model(linear_function, independent_vars=['x'], prefix=prefix)
Expand All @@ -190,7 +194,8 @@ def linear_function(x):
return model, params


def make_linear_fit(self, x_axis, data, estimator, units=None, add_params=None, **kwargs):
def make_linear_fit(self, x_axis, data, estimator, units=None, add_params=None,
**kwargs):
""" Performe a linear fit on the provided data.
@param numpy.array x_axis: 1D axis values
Expand Down Expand Up @@ -218,11 +223,11 @@ def make_linear_fit(self, x_axis, data, estimator, units=None, add_params=None,
if units is None:
units = ['arb. unit', 'arb. unit']

result_str_dict = dict()
result_str_dict = {}

result_str_dict['Slope'] = {'value': result.params['slope'].value,
'error': result.params['slope'].stderr,
'unit': '{0}/{1}'.format(units[1], units[0])}
'unit': f'{units[1]}/{units[0]}'}
result_str_dict['Offset'] = {'value': result.params['offset'].value,
'error': result.params['offset'].stderr,
'unit': units[1]}
Expand Down Expand Up @@ -256,10 +261,10 @@ def estimate_linear(self, x_axis, data, params):
x_mean = x_axis.mean()
data_mean = data.mean()

for i in range(0, len(x_axis)):
a_1 += (x_axis[i]-x_mean)*(data[i]-data_mean)
a_2 += np.power(x_axis[i]-x_mean, 2)
slope = a_1/a_2
for i in range(len(x_axis)):
a_1 += (x_axis[i] - x_mean) * (data[i] - data_mean)
a_2 += np.power(x_axis[i] - x_mean, 2)
slope = a_1 / a_2
intercept = data_mean - slope * x_mean
params['offset'].value = intercept
params['slope'].value = slope
Expand All @@ -269,4 +274,3 @@ def estimate_linear(self, x_axis, data, params):
params['offset'].value = 0

return error, params

Loading

0 comments on commit 57e57c9

Please sign in to comment.