Skip to content

io

SimpReader

SimpReader(filename, format, b0=None, nucleus=None)

A class to read and process NMR data from SIMPSON files.

Attributes: filename (str): The name of the file to read. format (str): The format of the file (spe, fid, xreim). b0 (str, optional): The magnetic field strength in MHz or T. nucleus (str, optional): The nucleus type.

Example: reader = SimpReader('spe_file', 'spe', b0='9.4T', nucleus='13C')

Source code in simpyson/io.py
def __init__(self, filename, format, b0=None, nucleus=None):
    self.filename = filename
    self.format = format
    self.b0 = b0
    self.nucleus = nucleus
    self._read_file()

to_fid

to_fid()

Converts spectrum (SPE) data to FID.

Raises: ValueError: If the format is not SPE.

Returns: SimpReader: A new SimpReader instance with FID format data.

Source code in simpyson/io.py
def to_fid(self):
    """
    Converts spectrum (SPE) data to FID.

    Raises:
        ValueError: If the format is not SPE.

    Returns:
        SimpReader: A new SimpReader instance with FID format data.
    """
    if self.format != 'spe':
        raise ValueError('Only SPE format can be converted to FID.')

    fid = copy.deepcopy(self)

    npoints = fid.data['np']
    sw = fid.data['sw']
    hz = fid.data['hz']
    signal = fid.data['real'] + 1j * fid.data['imag']
    signal = np.fft.ifft(np.fft.ifftshift(signal))
    real = np.real(signal)
    imag = np.imag(signal)
    dt = 1.0 / sw
    time = np.linspace(0, npoints*dt, int(npoints)) * 10e3  # Match _read_fid scaling
    fid.data = {'real': real, 'imag': imag, 'np': npoints, 'sw': sw, 'time': time}

    fid.format = 'fid'

    return fid

to_spe

to_spe()

Converts FID data to spectrum (SPE).

Raises: ValueError: If the format is not FID.

Returns: SimpReader: A new SimpReader instance with SPE format data.

Example: spectrum = reader.to_spe()

Source code in simpyson/io.py
def to_spe(self):
    """
    Converts FID data to spectrum (SPE).

    Raises:
        ValueError: If the format is not FID.

    Returns:
        SimpReader: A new SimpReader instance with SPE format data.

    Example:
        spectrum = reader.to_spe()
    """
    if self.format != 'fid':
        raise ValueError('Only FID format can be converted to SPE.')

    spectrum = copy.deepcopy(self)

    npoints = spectrum.data['np']
    sw = spectrum.data['sw']
    raw_signal = spectrum.data['real'] + 1j * spectrum.data['imag']
    signal = np.fft.fftshift(np.fft.fft(raw_signal))
    real = np.real(signal)
    imag = np.imag(signal)
    hz = np.linspace(-sw/2, sw/2, int(npoints))
    spectrum.data = {'real': real, 'imag': imag, 'np': npoints, 'sw': sw, 'hz': hz}

    if spectrum.b0 is not None and spectrum.nucleus is not None:
        try:
            spectrum.data['ppm'] = hz2ppm(hz, spectrum.b0, spectrum.nucleus)
        except ValueError as e:
            print(f"Error converting to ppm: {e}")

    spectrum.format = 'spe'

    return spectrum