xrdfit package

xrdfit.plotting module

This module contain functions for plotting spectral data and the fits to it. None of these functions should be called directly by users - these functions are called from plot methods in spectrum_fitting.

xrdfit.plotting.plot_parameter(data: numpy.ndarray, fit_parameter: str, show_points: bool, show_error: bool, scale_by_error: bool = False, log_scale=False)[source]

Plot a parameter of a fit against time.

Parameters:
  • data – The data to plot, x data in the first column, y data in the second column and the y error in the third column.
  • fit_parameter – The name of the parameter being plotted, used to generate the y-axis label
  • show_points – Whether to show data points on the plot.
  • show_error – Whether to show error bars on the plot.
  • scale_by_error – If True auto scale the y-axis to the range of the error bars. If False, auto scale the y-axis to the range of the data.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
xrdfit.plotting.plot_peak_fit(peak_fit: PeakFit, time_step: str = None, file_name: str = None, title: str = None, label_angle: float = None, log_scale=False)[source]

Plot the result of a peak fit as well as the raw data.

Parameters:
  • peak_fit – The result of a peak fit
  • time_step – If provided, used to generate the title of the plot.
  • file_name – If provided used as a on disk location to save the plot.
  • title – If provided, can be used to override the auto generated plot title.
  • label_angle – The angle to rotate maxima labels.
  • log_scale – Whether to plot the y axis on a log or linear scale.
xrdfit.plotting.plot_peak_params(peak_params: List[PeakParams], x_range: Tuple[float, float], label_angle: float)[source]

A visualisation to show the PeakParams. Peak bounds are indicated by a shaded grey area. Maxima bounds are shown by a dashed green line for the min bound and a dashed red line for the max bound. This method is called with an active plot environment and plots the peak params on top.

Parameters:
  • peak_params – The peak params to plot.
  • x_range – If supplied, restricts the x-axis of the plot to this range.
  • label_angle – If supplied, the angle to rotate the maxima labels.
xrdfit.plotting.plot_polar_heat_map(num_cakes: int, rad: Union[numpy.ndarray, List[int]], z_data: numpy.ndarray, first_cake_angle: int, cake_order: str)[source]

Plot a polar heat map using matplotlib.

Parameters:
  • num_cakes – The number of segments the polar map is divided into.
  • rad – The radial bin edges.
  • z_data – A num_cakes by rad shaped array of data to plot.
  • first_cake_angle – The angle clockwise from vertical at which to label the first cake.
  • cake_order – The order of cakes in the input data. Valid options are clockwise or anticlockwise
xrdfit.plotting.plot_spectrum(data: numpy.ndarray, cakes_to_plot: List[int], merge_cakes: bool, show_points: bool, x_range: Union[None, Tuple[float, float]] = None, log_scale=False)[source]

Plot a raw spectrum using matplotlib.

Parameters:
  • data – The data to plot, x_data in column 0, y data in columns 1-N where N is the number of cakes in the dataset.
  • cakes_to_plot – Which cakes (columns of y data) to plot.
  • merge_cakes – If True plot the sum of the selected cakes as a single line. If False plot all selected cakes individually.
  • show_points – Whether to show data points on the plot.
  • x_range – If supplied, restricts the x-axis of the plot to this range.
  • log_scale – If True, plot y axis on log scale. If False use linear scale.

xrdfit.pv_fit module

This module contain functions implementing the Pseudo-Voigt fit in lmfit. None of these functions should be called directly by users - these functions are called from methods in spectrum_fitting.

xrdfit.pv_fit.do_pv_fit(peak_data: numpy.ndarray, peak_param: PeakParams) → lmfit.model.ModelResult[source]

Pseudo-Voigt fit to the lattice plane peak intensity.

Parameters:
  • peak_data – The data to be fitted, two theta values (x-data) in column 0 and intensity (y-data) in column 1.
  • peak_param – A PeakParams object describing the peak to be fitted.
xrdfit.pv_fit.guess_params(model: lmfit.model.Model, old_fit_params: lmfit.parameter.Parameters, x_data: numpy.ndarray, y_data: numpy.ndarray, maxima_params: List[MaximumParams]) → lmfit.parameter.Parameters[source]

Given a dataset and some information about where the maxima are, guess some good initial values for the Pseudo-Voigt fit.

Parameters:
  • model – The lmfit Model to guess the params for.
  • old_fit_params – Any params that are to be passed on from a previous fit
  • x_data – The x data to be fitted.
  • y_data – The y data to be fitted.
  • maxima_params – The MaximaParams specified by the user.
xrdfit.pv_fit.guess_sigma(x_data: numpy.ndarray, maximum_range: Tuple[float, float]) → Tuple[float, float, float][source]

Guess an initial value of sigma for the Pseudo-Voigt fit.

Parameters:
  • x_data – The x_data to be fitted.
  • maximum_range – Two floats indicating the range of values that the maximum falls within.
Returns:

A maximum possible value for sigma, a minimum possible value and the initial guess of sigma.

xrdfit.spectrum_fitting module

This module contains the main fitting functions of xrdfit.

class xrdfit.spectrum_fitting.FitExperiment(spectrum_time: float, file_string: str, first_cake_angle: int, cakes_to_fit: List[int], peak_params: Union[xrdfit.spectrum_fitting.PeakParams, List[xrdfit.spectrum_fitting.PeakParams]], merge_cakes: bool, frames_to_load: List[int] = None)[source]

Bases: object

Information about a series of fits to temporally spaced diffraction patterns.

Variables:
  • spectrum_time – Time between subsequent diffraction patterns.
  • file_string – String used to glob for the diffraction patterns.
  • first_cake_angle – The angle of the first cake in the data file in degrees clockwise from North.
  • cakes_to_fit – Which of the cakes in the diffraction pattern to fit.
  • peak_params – The provided PeakParams to use for fitting.
  • merge_cakes – If multiple cakes are requested, whether to merge them or fit them separately.
  • frames_to_load – If specified, which time steps to fit.
  • time_steps – A list of FitSpectrum one for each time step.
  • fit_report – A FitReport for this :class`FittingExperiment`.
Parameters:
  • spectrum_time – Time between subsequent diffraction patterns.
  • file_string – String used to glob for the diffraction patterns.
  • first_cake_angle – The angle of the first cake in the data file in degrees clockwise from North.
  • cakes_to_fit – Which of the cakes in the diffraction pattern to fit.
  • peak_params – The provided PeakParams to use for fitting.
  • merge_cakes – If multiple cakes are requested, whether to merge them or fit them separately.
  • frames_to_load – If specified, which time steps to fit.
fit_parameters(peak_name: str) → List[str][source]

List the names of the parameters of the fit for a specified peak. The names are modified from the internal lmfit names to the maxima names provided by the user.

Parameters:peak_name – The peak to list the parameters of.
get_fit_parameter(peak_name: str, fit_parameter: str) → Union[None, numpy.ndarray][source]

Get the raw values and error of a fitting parameter over time.

Parameters:
  • peak_name – The name of the peak to get the data for.
  • fit_parameter – The name of the fit parameter to get the data for.
Returns:

A NumPy array with x data in the first column, y data in the second column and the y-error in the third column.

peak_names() → List[str][source]

List the names of the peaks specified for fitting in the PeakParams.

plot_fit_parameter(peak_name: str, fit_parameter: str, show_points=False, show_error=True, scale_by_error: bool = False, log_scale=False)[source]

Plot a named parameter of a fit as a function of time.

Parameters:
  • peak_name – The name of the fit to plot.
  • fit_parameter – The name of the fit parameter to plot.
  • show_points – Whether to show data points on the plot.
  • show_error – Whether to show the y-error as a shaded area on the plot.
  • scale_by_error – If False the y-axis will be scaled to fit the data values. If True the y-axis will be scaled to fit the error values.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
plot_fits(num_time_steps: int = 5, peak_names: Union[List[str], str] = None, time_steps: List[int] = None, file_name: str = None, log_scale=False)[source]

Plot the calculated fits to the data for this FitExperiment instance.

Parameters:
  • num_time_steps – The number of time_steps to plot fits for. The function will plot this many time_steps, evenly spaced over the whole dataset. This value is ignored if time_steps is specified.
  • peak_names – The name of the peak to fit. If not specified, will plot all fitted peaks.
  • time_steps – If provided, a list of time_steps to plot the fits for.
  • file_name – If provided, outputs the plot to an image file with file_name as the image name stub.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
run_analysis(reuse_fits=False)[source]

Run a fit over multiple diffraction patterns.

Parameters:reuse_fits – If True, use the result of one time step to provide the initial parameters for the next fit. If False, guess the initial fit parameters from the data at each time step.
save(file_name: str)[source]

Dump the data and all fits to a compressed binary file using dill.

Parameters:file_name – The name of the file to save the data to.
class xrdfit.spectrum_fitting.FitReport(peak_names: List[str])[source]

Bases: object

Some details about the performance of a FitExperiment.

Variables:
  • fit_time – The cumulative time taken to fit each peak in the spectrum.
  • num_evaluations – The number of evaluations taken for the fit to minimise for each peak in the spectrum.
  • num_time_steps – The number of time steps in the FitExperiment.
Parameters:

peak_names – The names of the peaks being fitted in the FitExperiment.

print(evaluation_threshold: int = 500, detailed=False)[source]

Print the fit report to the console.

Parameters:
  • evaluation_threshold – The number of fitting iterations that the triggers the report to warn about slow fitting.
  • detailed – If True, also report the time taken to do the fits.
class xrdfit.spectrum_fitting.FitSpectrum(file_path: str, first_cake_angle: int = 90, delimiter='t', cake_order='clockwise')[source]

Bases: object

An object that stores data about a spectrum and its fitted peaks.

Variables:
  • first_cake_angle – The angle of the first cake in the data file in degrees clockwise from North. This angle is always clockwise regardless of the value of cake_order.
  • fitted_peaks – Fits to peaks in the spectrum.
  • num_evaluations – A dict of peak names and how many iterations the fit took to converge.
  • fit_time – A dict of peak names and the time taken to evaluate that fit.
  • spectral_data – Data for the whole diffraction pattern.
  • cake_order – The order of cakes in the file. Valid options are clockwise or anticlockwise
Parameters:
  • file_path – The path of the file containing scattering data to load.
  • first_cake_angle – The angle of the first cake in the data file in degrees clockwise from North.
  • delimiter – The delimiter between values in the input file.
fit_peaks(peak_params: Union[xrdfit.spectrum_fitting.PeakParams, List[xrdfit.spectrum_fitting.PeakParams]], cakes: Union[int, List[int]], merge_cakes: bool = False)[source]

Attempt to fit peaks within the ranges specified by PeakParams.

Parameters:
  • peak_params – A list of PeakParams describing the peaks to be fitted.
  • cakes – Which cakes to fit.
  • merge_cakes – If True and multiple cakes are specified then sum the cakes before fitting. Else do the fit to multiple cakes simultaneously.
get_fit(name: str) → xrdfit.spectrum_fitting.PeakFit[source]

Get a PeakFit by name.

Parameters:name – The name of the PeakFit to get.
highlight_cakes(cakes: Union[int, List[int]])[source]

Plot a circular map of diffraction pattern with the selected cakes highlighted.

Parameters:cakes – The cake numbers to be highlighted.
num_cakes

The number of cakes in the spectral data of this FitSpectrum.

plot(cakes_to_plot: Union[int, List[int]], x_range: Tuple[float, float] = None, merge_cakes: bool = False, show_points=False, log_scale=False)[source]

Plot the intensity as a function of two theta for a given cake.

Parameters:
  • cakes_to_plot – The numbers of one or more cakes to plot.
  • x_range – If supplied, restricts the x-axis of the plot to this range.
  • merge_cakes – If True plot the sum of the selected cakes as a single line. If False plot all selected cakes individually.
  • show_points – Whether to show data points on the plot.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
plot_fit(fit_name: str, time_step: str = None, file_name: str = None, label_angle: float = None, log_scale=False)[source]

Plot the result of a fit and the raw data.

Parameters:
  • fit_name – The name of the fit to plot.
  • time_step – If provided, the time_step of the fit which will be added to the title.
  • file_name – If provided, the stub of the file name to write the plot to, if not provided, the plot will be displayed on screen.
  • label_angle – The angle to rotate maxima labels.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
plot_peak_params(peak_params: Union[xrdfit.spectrum_fitting.PeakParams, List[xrdfit.spectrum_fitting.PeakParams]], cakes_to_plot: Union[int, List[int]], x_range: Tuple[float, float] = None, merge_cakes: bool = False, show_points=False, label_angle=None, log_scale=False)[source]

Plot a visualisation of the provided PeakParams over the raw data.

Parameters:
  • peak_params – The PeakParams to plot.
  • cakes_to_plot – The numbers of one or more cakes to plot to raw data for.
  • x_range – If supplied, restricts the x-axis of the plot to this range.
  • merge_cakes – If True plot the sum of the selected cakes as a single line. If False plot all selected cakes individually.
  • show_points – Whether to show data points on the plot.
  • label_angle – The angle to rotate maxima labels.
  • log_scale – Whether to plot the y-axis on a log or linear scale.
plot_polar()[source]

Plot the whole diffraction pattern on polar axes.

class xrdfit.spectrum_fitting.MaximumParams(name: str, bounds: Tuple[float, float])[source]

Bases: object

An object representing information about a maximum within a peak.

Variables:
  • name – The name of the maximum.
  • bounds – An upper and lower bound for the position of the center of the maximum.
class xrdfit.spectrum_fitting.PeakFit(peak_params: xrdfit.spectrum_fitting.PeakParams)[source]

Bases: object

An object containing data on the fit to a peak.

Variables:
  • name – The name of the peak.
  • maxima_names – The names of the maxima in the peak.
  • raw_spectrum – The raw data to which the fit is made.
  • result – The lmfit result of the fit.
  • cake_numbers – The cake number each column in raw_spectrum refers to.
Parameters:

peak_params – A PeakParams object describing the peak to be fitted.

get_maxima_snrs() → List[float][source]

Get the signal to noise ratio for each maxima in a PeakFit.

plot(time_step: str = None, file_name: str = None, title: str = None, label_angle: float = None, log_scale=False)[source]

Plot the raw spectral data and the fit. :param time_step: If provided, a time step used to generate the title of the plot. :param file_name: If provided, save the plot to this file as well as displaying it. :param title: If provided, override the autogenerated plot title with this title. :param label_angle: The angle to rotate maxima labels. :param log_scale: Whether to plot the y-axis in a log or linear scale.

class xrdfit.spectrum_fitting.PeakParams(peak_bounds: Tuple[float, float], maxima_names: Union[str, List[str]], maxima_bounds: List[Tuple[float, float]] = None)[source]

Bases: object

An object containing information about a peak and its maxima.

Variables:
  • peak_bounds – Where in the spectrum the peak begins and ends. The fit will be done over this region.
  • maxima – A MaximumParams for each of the maxima.
  • peak_name – The name of the peak, made from compounding the maxima names.
  • previous_fit_parameters – If running multiple fits over time using a FitExperiment, the result of the previous fit.
Parameters:
  • peak_bounds – Where in the spectrum the peak begins and ends. The fit will be done over this region.
  • maxima_names – A name for each of the maxima.
  • maxima_bounds – If there is more than one maxima, a bounding box for each peak center.
adjust_maxima_bounds(fit_result: lmfit.model.ModelResult)[source]

Adjust maxima bounds to re-center the maximum in the maximum bounds.

Parameters:fit_result – The result of the previous fit.
adjust_peak_bounds(fit_result: lmfit.model.ModelResult)[source]

Adjust peak bounds to re-center the peak in the peak bounds.

Parameters:fit_result – The final parameters of the previous fit.
get_maxima_names() → List[str][source]
set_previous_fit(fit_params: lmfit.parameter.Parameters, maxima_snr: List[float], snr_cutoff: float)[source]

Peak fit parameters can be passed from the result of one fit to be initial parameters for the next fit. Parameters are only passed on if the previous fit was good as defined by the signal to noise ratio of the maxima. The signal to noise is assessed per maxima meaning that it may be the case that only a subset of the parameters are reused. In addition, the peak center may drift over time so the center parameter limits for the next fit are reset. The parameters to be used for the next fit are stored in the previous_fit_parameters variable of the PeakParams object.

Parameters:
  • fit_params – The final parameters of the previous fit.
  • maxima_snr – A measure of the signal to noise ratio for each maxima in the peak
  • snr_cutoff – The signal to noise ratio that defines whether a fit is good enough for parameters to be carried over to the next fit.
xrdfit.spectrum_fitting.load_dump(file_name: str) → xrdfit.spectrum_fitting.FitExperiment[source]

Load a FittingExperiment object saved using the FittingExperiment.save() method.

Parameters:file_name – The path of the file to load the data from.