Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
signal_data: Dependent variable
initial_parameters: Optional list with estimate of model parameters
remove_outliers: If True, then remove outliers in the fitting
Returns:
Tuple of fitted parameters and results dictionary
"""
if initial_parameters is None:
A0 = signal_data[0]
alpha0 = 1
initial_parameters = [A0, alpha0]
if np.any(frequencies == 0):
raise Exception('input data cannot contain 0')
lmfit_model = Model(power_law_model, name='Power law model')
lmfit_result = lmfit_model.fit(signal_data, frequency=frequencies, **
dict(zip(lmfit_model.param_names, initial_parameters)))
inliers = None
if remove_outliers:
inliers = get_outlier_mask(lmfit_result.residual)
logging.info(f'fit_power_law: outlier detection: number of outliers: {(inliers==False).sum()}')
lmfit_result = lmfit_model.fit(signal_data[inliers], frequency=frequencies[inliers], **lmfit_result.best_values)
result_dict = extract_lmfit_parameters(lmfit_model, lmfit_result)
result_dict['description'] = 'fit of power law model'
result_dict['inliers'] = inliers
return result_dict['fitted_parameters'], result_dict
def barestretchedexponentialdecay_function(x, beta, lifetime):
""" Function of a bare exponential decay.
@param numpy.array x: 1D array as the independent variable - e.g. time
@param float lifetime: constant lifetime
@return: bare exponential decay function: in order to use it as a model
"""
return np.exp(-np.power(x/lifetime, beta))
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)))
model = Model(barestretchedexponentialdecay_function,
independent_vars='x')
else:
model = Model(barestretchedexponentialdecay_function,
independent_vars='x', prefix=prefix)
params = model.make_params()
return model, params
@return: numpy.array with length equals to input x and with the values
of a lorentzian.
"""
return np.power(sigma, 2) / (np.power((center - x), 2) + np.power(sigma, 2))
amplitude_model, params = self.make_amplitude_model(prefix=prefix)
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)))
lorentz_model = Model(physical_lorentzian, independent_vars='x')
else:
lorentz_model = Model(
physical_lorentzian,
independent_vars='x',
prefix=prefix)
full_lorentz_model = amplitude_model * lorentz_model
params = full_lorentz_model.make_params()
# introduces a new parameter, which is solely depending on others and which
# will be not optimized:
if prefix is None:
prefix = ''
full_lorentz_model.set_param_hint(
'{0!s}fwhm'.format(prefix),
expr="2*{0!s}sigma".format(prefix))
# full_lorentz_model.set_param_hint('{0}contrast'.format(prefix),
# expr='(-100.0)')
def make_exponentialdecay_model(self): # pure exponential decay
#def bareexponentaildecay_function(x,lifetime):
#return np.exp(x/lifetime)
#def exponentialdecay_function(x,lifetime,amplitude,x_offset,y_offset):
#return amplitude*np.exp((x+x_offset)/lifetime) + y_offset
def exponentialdecay_function(x,lifetime):
return np.exp(-x/lifetime)
#amplitude_model, params = self.make_amplitude_model()
#constant_model, params = self.make_constant_model()
#model = amplitude_model * Model(exponentialdecay_function) + constant_model
model = Model(exponentialdecay_function)
params = model.make_params()
return model, params
@param numpy.array x: 1D array as the independent variable e.g. power
@param float I_sat: Saturation Intensity
@param float P_sat: Saturation power
@return: hyperbolicsaturation function: for using it as a model
"""
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)))
mod_sat = Model(hyperbolicsaturation_function, independent_vars='x')
else:
mod_sat = Model(hyperbolicsaturation_function, independent_vars='x',
prefix=prefix)
linear_model, params = self.make_linear_model(prefix=prefix)
complete_model = mod_sat + linear_model
params = complete_model.make_params()
return complete_model, params
"""
return self.poisson(x, mu)
amplitude_model, params = self.make_amplitude_model(prefix=prefix)
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)))
poissonian_model = Model(poisson_function, independent_vars='x')
else:
poissonian_model = Model(poisson_function, independent_vars='x',
prefix=prefix)
poissonian_ampl_model = amplitude_model * poissonian_model
params = poissonian_ampl_model.make_params()
return poissonian_ampl_model, params
b = -(np.sin(2 * theta)) / (4 * sigma_x ** 2) \
+ (np.sin(2 * theta)) / (4 * sigma_y ** 2)
c = (np.sin(theta) ** 2) / (2 * sigma_x ** 2) \
+ (np.cos(theta) ** 2) / (2 * sigma_y ** 2)
g = offset + amplitude * np.exp(- (a * ((u - center_x) ** 2)
+ 2 * b * (u - center_x) * (v - center_y)
+ c * ((v - center_y) ** 2)))
return g.ravel()
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)))
gaussian_2d_model = Model(twoDgaussian_function, independent_vars='x')
else:
gaussian_2d_model = Model(twoDgaussian_function, independent_vars='x',
prefix=prefix)
params = gaussian_2d_model.make_params()
return gaussian_2d_model, params
@param numpy.array x: 1D array as the independent variable e.g. power
@param float I_sat: Saturation Intensity
@param float P_sat: Saturation power
@return: hyperbolicsaturation function: for using it as a model
"""
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)))
mod_sat = Model(hyperbolicsaturation_function, independent_vars='x')
else:
mod_sat = Model(hyperbolicsaturation_function, independent_vars='x',
prefix=prefix)
linear_model, params = self.make_linear_model(prefix=prefix)
complete_model = mod_sat + linear_model
params = complete_model.make_params()
return complete_model, params
""" Function of a linear model.
@param numpy.array x: 1D array as the independent variable - e.g. time
@return: linear function, in order to use it as a model
"""
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)))
linear_mod = Model(linear_function, independent_vars='x')
else:
linear_mod = Model(linear_function, independent_vars='x', prefix=prefix)
slope, slope_param = self.make_slope_model(prefix=prefix)
constant, constant_param = self.make_constant_model(prefix=prefix)
model = slope * linear_mod + constant
params = model.make_params()
return model, params
@param numpy.array x: independant variable - e.g. time
@param float frequency: frequency
@param float phase: phase
@return: reference to method of a sine function in order to use it as a
model
"""
return np.sin(2*np.pi*frequency*x+phase)
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)))
model = Model(bare_sine_function, independent_vars='x')
else:
model = Model(bare_sine_function, independent_vars='x', prefix=prefix)
params = model.make_params()
return model, params