Basic Data Objects

class nmrpy.data_objects.Fid(*args, **kwargs)[source]

The basic FID (Free Induction Decay) class contains all the data for a single spectrum (data), and the necessary methods to process these data.

baseline_correct(deg=2)[source]

Perform baseline correction by fitting specified baseline points (stored in _bl_ppm) with polynomial of specified degree (stored in _bl_ppm) and subtract this polynomial from data.

Parameters:deg – degree of fitted polynomial
baseliner()[source]

Instantiate a baseline-correction GUI widget. Right-click-dragging defines a range. Ctrl-Right click deletes previously selected range. Indices selected are stored in _bl_ppm, which is used for baseline-correction (see baseline_correction()).

calibrate()[source]

Instantiate a GUI widget to select a peak and calibrate spectrum. Left-clicking selects a peak. The user is then prompted to enter the PPM value of that peak for calibration.

clear_peaks()[source]

Clear peaks stored in peaks.

clear_ranges()[source]

Clear ranges stored in ranges.

data

The spectral data. This is the primary object upon which the processing and analysis functions work.

deconv(method='leastsq', frac_gauss=0.0)[source]

Deconvolute data object by fitting a series of peaks to the spectrum. These peaks are generated using the parameters in peaks. ranges splits data up into smaller portions. This significantly speeds up deconvolution time.

Parameters:
  • frac_gauss – (0-1) determines the Gaussian fraction of the peaks. Setting this argument to None will fit this parameter as well.
  • method

    The fitting method to use. Default is ‘leastsq’, the Levenberg-Marquardt algorithm, which is usually sufficient. Additional options include:

    Nelder-Mead (nelder)

    L-BFGS-B (l-bfgs-b)

    Conjugate Gradient (cg)

    Powell (powell)

    Newton-CG (newton)

deconvoluted_integrals

An array of integrals for each deconvoluted peak.

emhz(lb=5.0)[source]

Apply exponential line-broadening to data array data.

Parameters:lb – degree of line-broadening in Hz.
classmethod from_data(data)[source]

Instantiate a new Fid object by providing a spectral data object as argument. Eg.

fid = Fid.from_data(data) 
ft()[source]

Fourier Transform the data array data.

Calculates the Discrete Fourier Transform using the Fast Fourier Transform algorithm as implemented in NumPy (Cooley, James W., and John W. Tukey, 1965, ‘An algorithm for the machine calculation of complex Fourier series,’ Math. Comput. 19: 297-301.)

peakpick(thresh=0.1)[source]

Attempt to automatically identify peaks. Picked peaks are assigned to peaks.

Parameters:thresh – fractional threshold for peak-picking
peakpicker()[source]

Instantiate a peak-picking GUI widget. Left-clicking selects a peak. Right-click-dragging defines a range. Ctrl-left click deletes nearest peak; ctrl-right click deletes range. Peaks are stored in peaks; ranges are stored in ranges: both are used for deconvolution (see deconv()).

peaks

Picked peaks for deconvolution of data.

phase_correct(method='leastsq')[source]

Automatically phase-correct data by minimising total absolute area.

Parameters:method

The fitting method to use. Default is ‘leastsq’, the Levenberg-Marquardt algorithm, which is usually sufficient. Additional options include:

Nelder-Mead (nelder)

L-BFGS-B (l-bfgs-b)

Conjugate Gradient (cg)

Powell (powell)

Newton-CG (newton)

phaser()[source]

Instantiate a phase-correction GUI widget which applies to data.

plot_deconv(**kwargs)[source]

Plot data with deconvoluted peaks overlaid.

Parameters:
  • upper_ppm – upper spectral bound in ppm
  • lower_ppm – lower spectral bound in ppm
  • lw – linewidth of plot
  • colour – colour of the plot
  • peak_colour – colour of the deconvoluted peaks
  • residual_colour – colour of the residual signal after subtracting deconvoluted peaks
plot_ppm(**kwargs)[source]

Plot data.

Parameters:
  • upper_ppm – upper spectral bound in ppm
  • lower_ppm – lower spectral bound in ppm
  • lw – linewidth of plot
  • colour – colour of the plot
ps(p0=0.0, p1=0.0)[source]

Linear phase correction of data

Parameters:
  • p0 – Zero order phase in degrees
  • p1 – First order phase in degrees
ranges

Picked ranges for deconvolution of data.

real()[source]

Discard imaginary component of data.

zf()[source]

Apply a single degree of zero-filling to data array data.

Note: extends data to double length by appending zeroes. This results in an artificially increased resolution once Fourier-transformed.

class nmrpy.data_objects.FidArray(*args, **kwargs)[source]

This object collects several Fid objects into an array, and it contains all the processing methods necessary for bulk processing of these FIDs. It should be considered the parent object for any project. The class methods from_path() and from_data() will instantiate a new FidArray object from a Varian/Bruker .fid path or an iterable of data respectively. Each Fid object in the array will appear as an attribute of FidArray with a unique ID of the form ‘fidXX’, where ‘XX’ is an increasing integer .

add_fid(fid)[source]

Add an Fid object to this FidArray, using a unique id.

Parameters:fid – an Fid instance
add_fids(fids)[source]

Add a list of Fid objects to this FidArray.

Parameters:fids – a list of Fid instances
baseline_correct_fids(deg=2)[source]

Apply baseline-correction to all Fid objects owned by this FidArray

Parameters:deg – degree of the baseline polynomial (see baseline_correct())
baseliner_fids()[source]

Instantiate a baseline-correction GUI widget. Right-click-dragging defines a range. Ctrl-Right click deletes previously selected range. Indices selected are stored in _bl_ppm, which is used for baseline-correction (see baseline_correction()).

calibrate(fid_number=None, assign_only_to_index=False, voff=0.02)[source]

Instantiate a GUI widget to select a peak and calibrate spectra in a FidArray. Left-clicking selects a peak. The user is then prompted to enter the PPM value of that peak for calibration; this will be applied to all Fid objects owned by this FidArray. See also calibrate().

Parameters:fid_number – list or number, index of

Fid to use for calibration. If None, the whole data array is plotted.

Parameters:assign_only_to_index – if True, assigns calibration only

to Fid objects indexed by fid_number; if False, assigns to all.

Parameters:voff – vertical offset for spectra
clear_peaks()[source]

Calls clear_peaks() on every Fid object in this FidArray.

clear_ranges()[source]

Calls clear_ranges() on every Fid object in this FidArray.

data

An array of all data objects belonging to the Fid objects owned by this FidArray.

deconv_fids(mp=True, cpus=None, method='leastsq', frac_gauss=0.0)[source]

Apply deconvolution to all Fid objects owned by this FidArray, using the peaks and ranges attribute of each respective Fid.

Parameters:
  • method – see phase_correct()
  • mp – parallelise the phasing process over multiple processors, significantly reduces computation time
  • cpus – defines number of CPUs to utilise if ‘mp’ is set to True, default is n-1 cores
deconvoluted_integrals

Collected deconvoluted_integrals

del_fid(fid_id)[source]

Delete an Fid object belonging to this FidArray, using a unique id.

Parameters:fid_id – a string id for an Fid
emhz_fids(lb=5.0)[source]

Apply line-broadening (apodisation) to all nmrpy.~data_objects.Fid objects owned by this FidArray

Parameters:lb – degree of line-broadening in Hz.
classmethod from_data(data)[source]

Instantiate a new FidArray object from a 2D data set of spectral arrays.

Parameters:data – a 2D data array
classmethod from_path(fid_path='.', file_format=None, arrayset=None)[source]

Instantiate a new FidArray object from a .fid directory.

Parameters:
  • fid_path – filepath to .fid directory
  • file_format – ‘varian’ or ‘bruker’, usually unnecessary
  • arrayset – (int) array set for interleaved spectra, user is prompted if not specified
ft_fids(mp=True, cpus=None)[source]

Fourier-transform all FIDs.

Parameters:
  • mp – parallelise over multiple processors, significantly reducing computation time
  • cpus – defines number of CPUs to utilise if ‘mp’ is set to True
get_fid(id)[source]

Return an Fid object owned by this object, identified by unique ID. Eg.:

fid12 = fid_array.get_fid('fid12')
Parameters:id – a string id for an Fid
get_fids()[source]

Return a list of all Fid objects owned by this FidArray.

get_integrals_from_traces()[source]

Returns a dictionary of integral values for all Fid objects calculated from trace dictionary integral_traces.

get_masked_integrals()[source]

After peakpicker_traces() and deconv_fids() this function returns a masked integral array.

integral_traces

Returns the dictionary of integral traces generated by select_integral_traces().

norm_fids()[source]

Normalise FIDs by maximum data value in data.

peakpicker(fid_number=None, assign_only_to_index=True, voff=0.02)[source]

Instantiate peak-picker widget for data, and apply selected peaks and ranges to all Fid objects owned by this FidArray. See peakpicker().

Parameters:fid_number – list or number, index of

Fid to use for peak-picking. If None, data array is plotted.

Parameters:assign_only_to_index – if True, assigns selections only

to Fid objects indexed by fid_number, if False, assigns to all

Parameters:voff – vertical offset for spectra
peakpicker_traces(voff=0.02, lw=1)[source]

Instantiates a widget to pick peaks and ranges employing a polygon shape (or ‘trace’). This is useful for picking peaks that are subject to drift and peaks that appear (or disappear) during the course of an experiment.

Parameters:
  • voff – vertical offset fraction (0.01)
  • lw – linewidth of plot (1)
phase_correct_fids(method='leastsq', mp=True, cpus=None)[source]

Apply automatic phase-correction to all Fid objects owned by this FidArray

Parameters:
  • method – see phase_correct()
  • mp – parallelise the phasing process over multiple processors, significantly reducing computation time
  • cpus – defines number of CPUs to utilise if ‘mp’ is set to True
plot_array(**kwargs)[source]

Plot data.

Parameters:
  • upper_index – upper index of array (None)
  • lower_index – lower index of array (None)
  • upper_ppm – upper spectral bound in ppm (None)
  • lower_ppm – lower spectral bound in ppm (None)
  • lw – linewidth of plot (0.5)
  • azim – starting azimuth of plot (-90)
  • elev – starting elevation of plot (40)
  • filled – True=filled vertices, False=lines (False)
  • show_zticks – show labels on z axis (False)
  • labels – under development (None)
  • colour – plot spectra with colour spectrum, False=black (True)
  • filename – save plot to .pdf file (None)
plot_deconv_array(**kwargs)[source]

Plot all data with deconvoluted peaks overlaid.

Parameters:
  • upper_index – upper index of Fids to plot
  • lower_index – lower index of Fids to plot
  • upper_ppm – upper spectral bound in ppm
  • lower_ppm – lower spectral bound in ppm
  • data_colour – colour of the plotted data (‘k’)
  • summed_peak_colour – colour of the plotted summed peaks (‘r’)
  • residual_colour – colour of the residual signal after subtracting deconvoluted peaks (‘g’)
  • data_filled – fill state of the plotted data (False)
  • summed_peak_filled – fill state of the plotted summed peaks (True)
  • residual_filled – fill state of the plotted residuals (False)
  • figsize – [x, y] size of plot ([15, 7.5])
  • lw – linewidth of plot (0.3)
  • azim – azimuth of 3D axes (-90)
  • elev – elevation of 3D axes (20)
ps_fids(p0=0.0, p1=0.0)[source]

Apply manual phase-correction to all Fid objects owned by this FidArray

Parameters:
  • p0 – Zero order phase in degrees
  • p1 – First order phase in degrees
real_fids()[source]

Discard imaginary component of FID data sets.

save_to_file(filename=None, overwrite=False)[source]

Save FidArray object to file, including all objects owned.

Parameters:
  • filename – filename to save FidArray to
  • overwrite – if True, overwrite existing file
select_integral_traces(voff=0.02, lw=1)[source]

Instantiate a trace-selection widget to identify deconvoluted peaks. This can be useful when data are subject to drift. Selected traces on the data array are translated into a set of nearest deconvoluted peaks, and saved in a dictionary: integral_traces.

Parameters:
  • voff – vertical offset fraction (0.01)
  • lw – linewidth of plot (1)
t

An array of the acquisition time for each FID.

zf_fids()[source]

Zero-fill all Fid objects owned by this FidArray