Skip to content

Public API

class Spectrum:

NMR spectrum.

The recommended way of creating Spectrum instances from spectrum files is by using the Spectrum.load() function. This automatically determines the format of the spectrum and selects the correct SpectrumReader.

Example:

spectrum = Spectrum.load('./spectrum_file.ucsf')
ndim: int

Number of dimensions of the spectrum.

nuclei: tuple[str, ...]

The type of nucleus (13C, 1H, etc.) associated with each axis.

data_source: bioshift.spectra.spectrumdatasource.SpectrumDataSource

Object responsible for lazy-loading and parsing spectrum data.

transform: bioshift.spectra.spectrumtransform.SpectrumTransform

Object storing the transformation from array coordinate space to chemical shift space.

experiment: bioshift.spectra.spectrum.NMRExperiment

Enum value for the type of NMR experiment the spectrum is from (e.g., HSQC, HNCACB)

shape: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Returns: Shape of the underlying data array.

array: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Get the raw data of the spectrum as a numpy array.

@classmethod
def load(cls, path: str | os.PathLike) -> bioshift.spectra.spectrum.Spectrum:

Create a spectrum from a path to a spectrum file. Automatically determines the file format and dispatches the correct spectrum reader. Currently spectra stored in .ucsf and .spc(.par) files are supported.

  • path: Path to the spectrum file.

Spectrum object

def intensity(
self,
shift: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]] | tuple[numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]], ...]
) -> numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]:

Evaluate the interpolated intensity at given chemical shift coords.

This method wraps scipy.interpolate.RegularGridInterpolator to interpolate values from a regularly spaced ND grid (self.array) defined over axes stored in self.transform.axes.

  • shift: Coordinates at which to evaluate
  • the intensity.
  • Accepts: - A NumPy array of shape (n_points, ndim), where each row is a point.
    • A tuple of arrays (e.g., from np.meshgrid) of equal shape, which will be stacked into coordinate points.

Interpolated intensity values at the specified coordinates. If shift is a tuple of meshgrid arrays, the output shape matches the grid shape.

def slice(self, axis: int, z: float) -> bioshift.spectra.spectrum.Spectrum:

Take a slice from the spectrum along the specified axis.

  • axis: The index of the axis perpendicular to the slice plane.
  • z: The chemical shift of the plane along the specified axis.

A new spectrum, with one fewer dimension, corresponding to the slice plane.

def project(self, axis: int) -> bioshift.spectra.spectrum.Spectrum:

Project the spectrum along one of its coordinate axes by taking the integral.

  • axis: The axis along which to project the spectrum.

A new spectrum with one fewer dimension.

def blur(self, sigma: tuple[float]) -> bioshift.spectra.spectrum.Spectrum:

Apply a gaussian blur to the spectrum.

  • sigma: The standard deviation of the gaussian kernel used for the blur.

A new spectrum which has been blurred.

def threshold(
self,
level: float,
soft: bool = True
) -> bioshift.spectra.spectrum.Spectrum:

Apply a threshold to the spectrum.

  • level: The threshold level. All points with intensities below this value are set to zero.
  • soft: If set to True, values above the threshold are shifted down by the threshold level,
  • in order to keep the spectrum continuous.

A new spectrum with all points below the threshold value set to zero.

def normalize(self) -> bioshift.spectra.spectrum.Spectrum:

Normalize the spectrum to have maximum 1.0.

A new spectrum with all values divided by the maximum of the original.

def transpose(
self,
axes: tuple[int, ...] | None = None
) -> bioshift.spectra.spectrum.Spectrum:

Transpose the spectrum, swapping the ordering of the axes.

  • Axes: Tuple of integers defining the new ordering of the axes.
  • If None are provided, then the default behaviour is to invert
  • the ordering of the axes (the same as np.transpose)

A new spectrum with axes reordered.

class SpectrumTransform:

Represents a diagonal affine transformation used to map between coordinates in the raw spectrum array and chemical shift values.

ndim: int

Number of dimensions in the spectrum.

shape: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Shape of the underlying data array.

scaling: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Vector of diagonal components of the affine transformation matrix. Components are scaling values along each axis.

offset: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Constant offset vector for the affine transformation.

bounds: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Bounds of the spectrum (in array grid coordinates).

inverse_scaling: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Vector containing the diagonal entries of the affine transformation matrix for the inverse transform. Used to convert from chemical shifts to grid coords.

inverse_offset: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]

Vector offset for the inverse transform. Used to convert from chemical shifts to grid coords.

def grid_to_shift(
self,
x: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]
) -> numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]:

Convert a grid coord to a chemical shift.

  • x: Array containing grid coordinates of points in the spectrum. Must be broadcastable to (ndim,).

Array of corresponding chemical shift values

def shift_to_grid(self, x) -> numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]:

Convert a grid coord to a chemical shift.

  • x: Array containing chemical shift coordinates. Must be broadcastable to (ndim,).

Array of corresponding grid coordinates.

@classmethod
def from_reference(
cls,
shape: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]],
spectral_width: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]],
spectrometer_frequency: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]],
ref_coord: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]],
ref_shift: numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]]
) -> bioshift.spectra.spectrumtransform.SpectrumTransform:

Create a SpectrumTransform from spectrum referencing information. All arguments must be NDArrays with shape (ndim,)

  • shape : Number of data points along each axis of the spectrum.
  • spectral_width: Difference in chemical shift across the width of the spectrum along each axis, measured in Hz.
  • spectrometer_frequency: Frequency of spectrometer along each axis, measured in MHz. Required to convert spectral width into ppm.
  • ref_coord: Grid coordinates of a reference point in the spectrum.
  • ref_shift: Chemical shift vector of the point located at the reference coordinate.

SpectrumTransform object with scaling and offset vectors calculated from the reference data.

axes: tuple[numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]], ...]
grid: tuple[numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]], ...]
def slice(self, axis: int) -> bioshift.spectra.spectrumtransform.SpectrumTransform:
def transpose(self, axes=None):
class NMRExperiment(enum.Enum):
NHSQC = <NMRExperiment.NHSQC: 'NHSQC'>
HNCO = <NMRExperiment.HNCO: 'HNCO'>
HNCACB = <NMRExperiment.HNCACB: 'HNCACB'>
HNCA = <NMRExperiment.HNCA: 'HNCA'>
HNCOCACB = <NMRExperiment.HNCOCACB: 'HN(CO)CACB'>
def load_spectrum(path: str | os.PathLike) -> bioshift.spectra.spectrum.Spectrum: