PHS2081¶
This documents the monashspa.PHS2081
library that you will import into code used in the PHS2081 unit.
-
monashspa.PHS2081.
get_fit_parameters
(fit_result)[source]¶ Returns the parameters from a fit result as a dictionary.
This function takes a
lmfit.model.ModelResult
object from the lmfit Python library and extracts the parameters of the fit along with their uncertainties. These are returned to you in a Python dictionary format. The format of the dictionary depends on the model used to perform the fit. For example, a linear fit would result in the following dictionary:parameters = { 'slope': <value>, 'u_slope': <value>, 'intercept': <value>, 'u_intercept': <value>, }
The parameter names always match those of the lmfit model, and the uncertainties are always the identical parameter name prefixed with
u_
.Parameters: fit_result – A lmfit.model.ModelResult
object from the lmfit Python library.Returns: A dictionary containing the fit parameters and their associated uncertainties.
-
monashspa.PHS2081.
linear_fit
(x, y, u_y=None, slope_guess=None, intercept_guess=None)[source]¶ General purpose linear fit function.
This function takes your x and y data (as numpy arrays) and returns a
lmfit.model.ModelResult
object from the lmfit Python library. It attempts to fit your data to a model define by:\(y=mx+c\)where \(m = slope\) and \(c = intercept\). If guesses for the slope and intercept are not explicitly provided when calling this function, they will be inferred from the provided data arrays.
Parameters: - x – A 1D numpy array of x data points
- y – A 1D numpy array of y data points
Keyword Arguments: - u_y – An optional argument for providing a 1D numpy array of uncertainty values for the y data points
- slope_guess – An optional argument for providing an initial guess for the value of the slope parameter
- intercept_guess – An optional argument for providing an initial guess for the value of the intercept parameter
Returns: A
lmfit.model.ModelResult
object from the lmfit Python library
-
monashspa.PHS2081.
make_lmfit_model
(expression, independent_vars=None, allow_constant_model=False, **kwargs)[source]¶ A convenience function for creating a lmfit Model from an equation in a string
This function takes an expression containing the right hand side of an equation you wish to use as your fitting model, and generates a
lmfit.model.Model
object from the lmfit Python libraryFor example, the expression
"m*x+c"
would create a model that would fit to linear data modelled by the equation \(y=m*x+c\). Note that the expression does not contain they
or=
symbolsStandard numpy and scipy.special functions are also available for use in your expression. For example, this is also a valid expression:
"sin(x)+c"
.The expression must always be valid Python code, and must be able to be evaluated with every parameter set to a random floating point number.
The independent variable is assumed to be
x
unless otherwise specified. All other variables are assumed to be parameters you wish the fitting routine to optimise. These parameters will be given an initial hint of 1 in the returned model, but can be overridden later usinglmfit.model.Model.set_param_hint()
,lmfit.model.Model.make_params()
, orlmfit.parameter.Parameters.add()
.Note: Additional keyword arguments are passed directly to
lmfit.model.Model
.Parameters: expression – A string containing the right-hand-side of the equation you wish to model (assumes the left hand side is equal to “y”).
Keyword Arguments: - independent_vars – a list of independent variable names that should
not be varied by lmfit. If set to
None
(the default) it assumes that the independent variables is just["x"]
- allow_constant_model – A Boolean to indicate whether to suppress the
exception raised if you don’t use the independent
variable(s) in your model equation. Defaults to
False
(raise the exception). If you do wish to use a constant model, we recommend leaving this as “False” and modifying your model equation to include an “x*0” (or similar) term as this also ensures the component can be plotted usinglmfit.model.ModelResult.eval_components()
without additional modification. However, you can also set this toTrue
to suppress the Exception and restore the default lmfit behaviour.
Returns: A
lmfit.model.Model
object to be used for fitting with the lmfit library.- independent_vars – a list of independent variable names that should
not be varied by lmfit. If set to
-
monashspa.PHS2081.
model_fit
(model, parameters, x, y, u_y=None, **kwargs)[source]¶ A wrapper for fitting to an arbitrary model using lmfit.
This function automatically inverts the array of standard errors to weights (which lmfit expects) and disables the scaling of the covariant matrix is the array of standard errors are provided.
- Note: Any additional keyword arguments passed to this function
- will be passed directly to
model.fit
.
Parameters: - model – a reference to a lmfit model.
- parameters – a reference to a
lmfit.parameters.Parameters
object for your model - x – A 1D numpy array of x data points
- y – A 1D numpy array of y data points
Keyword Arguments: u_y – An optional argument for providing a 1D numpy array of uncertainty values for the y data points
Returns: A
lmfit.model.ModelResult
object from the lmfit Python library
-
monashspa.PHS2081.
savefig
(fname, dpi=600, bbox_inches='tight', **kwargs)[source]¶ A wrapper for
matplotlib.pyplot.savefig()
with sensible defaults.By default, if a matplotlib legend is located outside of the plot axes, then
matplotlib.pyplot.savefig()
may cut off the legend when saving the figure. This function fixes this issue by settingbbox_inches='tight'
and settingbbox_extra_artists
to be a list of the current figure legends, unless the user chooses to define them otherwise. This function also sets the default fordpi
to 600, which is large enough for most purposes.This function accepts any keyword argument from
matplotlib.pyplot.savefig()
and returns the result of the call to that function. Seematplotlib.pyplot.savefig()
for usage.