Table Of Contents

Previous topic

nmrglue.leastsqbound

Next topic

nmrglue.peakpick

This Page

nmrglue.linesh

Functions for fitting and simulating arbitrary dimensional lineshapes commonly found in NMR experiments

This module is imported as nmrglue.linesh and can be called as such

High-Level Functions

These are the functions the majority of users will use from the linesh module.

nmrglue.analysis.linesh.fit_spectrum(spectrum, lineshapes, params, amps, bounds, ampbounds, centers, rIDs, box_width, error_flag, verb=True, **kw)

Fit a spectrum by region which contain one or more peaks.

Parameters:

  • spectrum Slicable spectral data.

  • lineshape List of lineshapes by label (str) or a lineshape class.

    See fit_NDregion for details.

  • params P-length list (P is the number of peaks in region) of

    N-length lists of tuples where each each tuple is the optimiztion starting parameters for a given peak and dimension lineshape.

  • amps P-length list of amplitudes.

  • bounds List of bounds for parameter of same shape as params. If

    none of the parameters in a given dimension have limits None can be used, otherwise each dimension should have a list/tuple of (min,max) or None for each parameter. min or max may be None when there is no bound in a given direction.

  • ampbounds P-length list of bounds for the amplitude with format

    similar to bounds.

  • centers List of N-tuples indicating peak centers.

  • rIDs P-length list of region numbers (peak with the same

    region number are fit together).

  • box_width N-tuple indicating box width to add and subtract from

    peak centers to form region around peak to fit.

  • error_flag Set to True to estimate errors for each lineshape

    parameter and amplitude.

  • verb Set to True to print summary of each region fit, False

    supresses all printing.

  • kw Additional keywords passed to the scipy.optimize.leastsq

    function.

Returns: param_best,amp_best,iers if error_flag is False
param_best,amp_best,param_err,amp_err,iers if error_flag is True
  • params_best Optimal values for lineshape parameters with same format

    as params input parameter.

  • amp_best List of optimal peak amplitudes.

  • param_err Estimated lineshape parameter errors with same format

    as oarans inout parameter. (Optional)

  • amp_err Estimated peak amplitude errors.

  • iers List of interger flag from scipy.optimize.leastsq

    indicating if the solution was found for a given peak. 1,2,3,4 indicates that a solution was found. Other indicate an error.

nmrglue.analysis.linesh.fit_NDregion(region, lineshapes, params, amps, bounds=None, ampbounds=None, wmask=None, error_flag=False, **kw)

Fit a N-dimensional region.

Parameters:

  • region N-dimensional region to fit.

  • lineshapes List of lineshapes by label (str) or a lineshape class.

  • params P-length list (P is the number of peaks in region) of

    N-length lists of tuples where each each tuple is the optimiztion starting parameters for a given peak and dimension lineshape.

  • amps P-length list of amplitudes.

  • bounds List of bounds for parameter of same shape as params. If

    none of the parameters in a given dimension have limits None can be used, otherwise each dimension should have a list/tuple of (min,max) or None for each parameter. min or max may be None when there is no bound in a given direction.

  • ampbounds P-length list of bounds for the amplitude with format

    similar to bounds.

  • wmask Array with same shape as region which is used to weight

    points in the err calculation, typically a boolean array is used to exclude certain points in the region. Default of None will include all points in the region equally in the error calculation.

  • error_flag Set to True to estimate errors for each lineshape

    parameter and amplitude.

  • kw Additional keywords passed to the scipy.optimize.leastsq

    function.

Returns: param_best,amp_best,ier if error_flag is False
param_best,amp_best,param_err,amp_err,ier if error_flag is True
  • params_best Optimal values for lineshape parameters with same format

    as params input parameter.

  • amp_best List of optimal peak amplitudes.

  • param_err Estimated lineshape parameter errors with same format

    as oarans inout parameter. (Optional)

  • amp_err Estimated peak amplitude errors.

  • ier Interger flag from scipy.optimize.leastsq indicating if

    the solution was found. 1,2,3,4 indicates that a solution was found. Otherwise the solution was not found.

Note on the lineshape parameter:

Elements of the lineshape parameter list can be string indicating the lineshape of given dimension or an instance of a lineshape class which provide a sim method which takes two arguments, the first being the length of the lineshape the second being a list of lineshape parameters, and returns a simulated lineshape as well as a nparam method which when given the length of lineshape returns the number of parameters needed to describe the lineshape. Currently the following strings are allowed:

  • ‘g’ or ‘gauss’ Gaussian (normal) lineshape.
  • ‘l’ or ‘lorentz’ Lorentzian lineshape.
  • ‘s’ or ‘scale’ Scaled lineshape.
  • ‘p’ or ‘peak’ Gaussian lineshape which takes FWHM as a parameter.

The following are all valid lineshapes parameters for a 2D Gaussian peak:

[‘g’,’g’] [‘gauss’,’gauss’] [ng.analysisbase.gauss1D(),ng.analysisbase.gauss1D()]

An simple example of a lineshape class which simulates the function y=c: XXX

nmrglue.analysis.linesh.sim_NDregion(shape, lineshapes, params, amps)

Simulate a arbitrary dimensional region with one or more peaks.

Parameters:

  • shape tuple of region shape

  • lineshapes List of lineshapes by label (str) or a lineshape class.

    See fit_NDregion for additional documentation.

  • params P-length list (P is the number of peaks in region) of

    N-length lists of tuples where each each tuple is lineshape parameters for a given peak and dimension.

  • amps P-length of peak amplitudes.

Returns: array containing simulated region

nmrglue.analysis.linesh.add_to_table(rec, columns, column_names)

Add (append) multiple columns to a record array

Parameters:

  • rec Records array (table).
  • columns List of columns data to append to table.
  • column_names List of names of columns.

Returns: rec (Records array with columns added)

nmrglue.analysis.linesh.pack_table(pbest, abest, iers, rec, param_columns, amp_column, ier_column=None)

Pack fitting parameters into table

Parameters:

  • pbest List of best-fit parameters. See fit_NDregion for format.
  • abest List of best-fit amplitudes.
  • iers List of fitting error return values.
  • rec Records array (table) to save fitting parameters into.
  • param_columns List of parameter columns in rec. (format same as pbest)
  • amp_columns Name of amplitude column in rec.
  • ier_column Name of column in rec to save iers to, None to not save.

Return nothing, rec is updated in place.

nmrglue.analysis.linesh.unpack_table(rec, param_columns, amp_column)

Unpack initial fitting parameters from a table

Parameters:

  • rec Records array (table) holding parameters.

  • param_columns List of column names which hold lineshape parameters.

    See fit_NDregion for format.

  • amp_column Name of columns in rec holding initial amplitudes.

Returns: params,amps

nmrglue.analysis.linesh.estimate_scales(spectrum, centers, box_width, scale_axis=0)

Estimate scale parameter for peaks in a spectrum

Parameters:

  • spectrum Slicable spectral data.

  • centers List of N-tuples indicating peak centers.

  • box_width N-tuple indicating box width to add and subtract from

    peak centers to form region around peak to fit.

  • scale_axis Axis number to estimate scale parameters for.

Low-Level Functions

These functions are typically not used directly by users. They are called by high level functions. Developers and user who want fine control over lineshape fitting may be interested in these functions.

nmrglue.analysis.linesh.f_NDregion(region, ls_classes, p0, p_bounds, n_peaks, wmask, **kw)

Fit an arbitrary dimensional regions containing one or more peaks using a contrained Levenberg-Marquard optmization algorithm.

Parameters:

  • region N-dimensional region to fit
  • ls_classes List of lineshape classes
  • p0 Initial parameter guesses
  • p_bounds (min,max) pairs for each element of p0
  • n_peaks Number of peaks

Additional keyword are passed directly to leastsqbound and in turn passed to scipy.optimize.leastsq after variable transformation.

nmrglue.analysis.linesh.s_NDregion(p, shape, ls_classes, n_peaks)

Simulate a arbitrary dimensional region with one or more peaks.

Parameters:

  • p List (and must be a list) of parameters
  • shape tuple of region shape
  • ls_classes List of lineshape classes
  • n_peaks Number of peaks in region

p is modified by this functions, pass a copy if p should be retained.

nmrglue.analysis.linesh.s_single_NDregion(p, shape, ls_classes)

Simulate a arbitrary dimensional region with a single peak. Called repeatly by s_NDregion to build up a full ND region.

Parameters: * p List (and must be a list) of parameters * shape tuple of region shape * ls_classes List of lineshape classes

nmrglue.analysis.linesh.err_NDregion(p, region, shape, ls_classes, n_peaks, wmask)
Error functions for a NDregion, called by f_NDregion function
nmrglue.analysis.linesh.make_slist(l, t_sizes)

Create a list of tuples of given sizes from a list

Parameters:

  • l List/array to pack into shaped list.
  • t_sizes List of tuple sizes.
nmrglue.analysis.linesh.split_list(l, N)
Split list l into N sublists of equal size