PHS3000

This documents the monashspa.PHS3000 library that you will import into code used in the PHS3000 unit.

General purpose functions

monashspa.PHS3000.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.PHS3000.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.PHS3000.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 library

For 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 the y or = symbols

Standard 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 using lmfit.model.Model.set_param_hint(), lmfit.model.Model.make_params(), or lmfit.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 using lmfit.model.ModelResult.eval_components() without additional modification. However, you can also set this to True 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.

monashspa.PHS3000.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.PHS3000.read_csv(filepath, *args, **kwargs)[source]

A simple wrapper around the pandas csv reader.

This function wraps the pandas.read_csv() function and returns a numpy array. Any optional argument for the pandas function can be used here.

Examples

Standard csv files with a header row can be read using read_csv(filepath).

If your csv file has no header row, use: read_csv(filepath, header=None).

If your file is delimited by something other than a comma, you can specify the separator with the optional argument sep. For example, sep='|' if your file is delimited by the pipe symbol.

There are many options that may be useful, such as those that parse dates, ignore columns, or convert certain values to ‘nan’s. See the pandas documentation for pandas.read_csv() to see all optional arguments.

Parameters:filepath – The path to the file to read
Keyword Arguments:
 **kwargs – See the pandas documentation for pandas.read_csv()
Returns:A numpy array. This array will be 1D if the csv file contains only a single row or column of data. Otherwise the array will be 2D.
monashspa.PHS3000.read_mca_file(filepath)[source]

Imports data saved from the ADMCA software

Parameters:filepath – The path to the .mca file produced by the ADMCA acquisition software
Returns:A tuple (header, data) where header is a dictionary containing the key, value pairs from header rows of the .mca file and data is a 1D numpy array of the counts for each MCA channel.
monashspa.PHS3000.read_picoscope_csv(filepath)[source]

Reads the csv file saved from the PicoScope software

Note

The picoscope software will save overrange values in the csv file as either “Infinity” or the unicode symbol for infinity (which will show up in some software as several random characters). This Python function converts those instances to np.nan.

Note

This function attempts to return values in S.I. units without a prefix. For example, if the csv file contains time in milliseconds, this function will attempt to detect that and return the data in units of seconds. If the function fails to make this conversion, a warning message will be printed.

Parameters:filepath – A string containing the path to the csv file
Returns:A tuple (time, CHA, CHB) where each element is a 1D numpy array containing an array of time points (x-axis of the oscilloscope), and the y-values for channels A and B respectively. The units of these should be seconds, Volts and Volts respectively unless otherwise stated via a warning message printed to your terminal.
monashspa.PHS3000.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 setting bbox_inches='tight' and setting bbox_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 for dpi 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. See matplotlib.pyplot.savefig() for usage.