spcal.io¶
Import and export of data.
spcal.io.export¶
spcal.io.nu¶
Loading data from Nu Instruments ICP-ToF.
- spcal.io.nu.apply_autoblanking(autob_events: ndarray, signals: ndarray, masses: ndarray, info: dict, blank_all_signals: bool = False) ndarray¶
Apply the auto-blanking to the integrated data. There must be one cycle / segment and no missing acquisitions / data!
- Parameters:
autob – list of events from read_nu_autob_binary
signals – 2d array of signals from get_signals_from_nu_data
masses – 1d array of masses, from get_masses_from_nu_data
info – dict of parameters, as returned by read_nu_directory
blank_all_signals – apply blanking to all data, not just specified regions
- Returns:
blanked data
- spcal.io.nu.blanking_regions_from_autob(autob_events: ndarray, num_acc: int, start_coef: tuple[float, float], end_coef: tuple[float, float]) tuple[list[tuple[int, int]], list[ndarray]]¶
Extract blanking regions from autoblank data.
- Parameters:
autob – list of events from read_nu_autob_binary
num_acc – number of accumulations per acquisition
start_coef – blanker open coefs ‘BlMassCalStartCoef’
end_coef – blanker close coefs ‘BlMassCalEndCoef’
- Returns:
list of (start, end) of each region, array of (start, end) masses
- spcal.io.nu.eventtime_from_info(info: dict) float¶
Reads the dwelltime (total acquistion time) from run.info. Rounds to the nearest ns.
- Parameters:
info – dict of parameters, as returned by read_nu_directory
- Returns:
dwelltime in s
- spcal.io.nu.indicies_from_integ(integ: ndarray, info: dict) ndarray¶
Get the acuisition index from integ files.
- Parameters:
integ – from read_integ_binary
info – dict of parameters, as returned by read_nu_directory
- Returns:
array of indicies
- spcal.io.nu.is_nu_directory(path: Path) bool¶
Checks path is directory containing a ‘run.info’ and ‘integrated.index’
- spcal.io.nu.is_nu_run_info_file(path: Path) bool¶
Checks file exists and is called ‘run.info’.
- spcal.io.nu.masses_from_integ(integ: ndarray, info: dict) ndarray¶
Converts Nu peak centers into masses.
- Parameters:
integ – from read_integ_binary
info – dict of parameters, as returned by read_nu_directory
- Returns:
2d array of masses
- spcal.io.nu.read_autob_binary(path: Path, first_cyc_number: int | None = None, first_seg_number: int | None = None, first_acq_number: int | None = None) ndarray¶
Read a Nu Instruments .autob autoblanking binary.
These files are (unfortunately) not a fixed size, so ‘num_edges’ should be read for each array value to determine the true size of ‘edges’.
- Parameters:
path – Path to the binary
first_cyc_number – optional check of first cycle number
first_seg_number – optional check of first segment number
first_acq_number – optional check of first acquisition number
- Returns:
structured array
- Raises:
ValueError if cyc, seg or acq number do not match –
- spcal.io.nu.read_binaries_in_index(root: Path, index: list[dict], binary_ext: str, binary_read_fn: Callable[[Path, int, int, int], ndarray], binary_read_kwargs: dict | None = None, cyc_number: int | None = None, seg_number: int | None = None) list[ndarray]¶
Generic function for reading all Nu binaries stored in an index file.
This can be used for .integ, .autob, .pulse, etc.
- Parameters:
root – directory containing files and index
index – list of indices from json.loads
binary_ext – extension of binary files, e.g. ‘.integ’
binary_read_fn – function to read binary file
binary_read_kwrags – keywords to forward to ‘binary_read_fn’
cyc_number – restrict to cycle, None for all
seg_number – restrict to segments, None for all
- Returns:
binary data as a list of arrays
- spcal.io.nu.read_directory(path: str | Path, first_integ_file: int = 0, last_integ_file: int | None = None, autoblank: str = 'regions', cycle: int | None = None, segment: int | None = None, raw: bool = False) tuple[ndarray, ndarray, ndarray, dict]¶
Read the Nu Instruments raw data directory, retuning data and run info.
Directory must contain ‘run.info’, ‘integrated.index’ and at least one ‘.integ’ file. Data is read from ‘.integ’ files listed in the ‘integrated.index’ and are checked for correct starting cycle, segment and acquisition numbers.
- Parameters:
path – path to data directory
first_integ_file – first integ to read
last_integ_file – last integ to read, can be used as a max
autoblank – apply autoblanking to overrange regions or to all masses, one of ‘off’, ‘regions’, ‘all’
cycle – limit import to cycle
segment – limit import to segment
raw – return raw ADC counts
- Returns:
masses from first acquisition signals in counts times in s dict of parameters from run.info
- Raises:
ValueError if invalid directory or no integ files are selected –
- spcal.io.nu.read_integ_binary(path: Path, first_cyc_number: int | None = None, first_seg_number: int | None = None, first_acq_number: int | None = None, memmap: bool = False) ndarray¶
Read a Nu Instruments .integ binary.
The data type of returned array will change depending on the value of ‘num_results’. From v2.0.11 the .integ may be compressed using gzip.
- Parameters:
path – Path to the binary
first_cyc_number – optional check of first cycle number
first_seg_number – optional check of first segment number
first_acq_number – optional check of first acquisition number
memmap – use memmap instead of reading in data, can’t be used when compressed
- Returns:
structured array
- Raises:
ValueError if cyc, seg or acq number do not match –
- spcal.io.nu.select_nu_signals(masses: ndarray, signals: ndarray, selected_masses: dict[str, float], max_mass_diff: float = 0.1) ndarray¶
Reduces signals to the isotopes in selected_masses. ‘masses’ must be sorted
- Parameters:
masses – from read_nu_directory
signals – from read_nu_directory
selected_masses – dict of isotope name: mass
max_mass_diff – maximum difference (Da) from mass to allow
- Returns:
structured array of signals
- Raises:
ValueError if the smallest mass difference from 'selected_masses' is – greater than ‘max_mass_diff’
- spcal.io.nu.signals_from_integs(integs: list[ndarray], info: dict) ndarray¶
Converts signals from integ data to counts.
Inserts nan values for discontinuities in integ index.
- Parameters:
integ – from read_integ_binary
info – dict of parameters, as returned by read_nu_directory
- Returns:
signals in counts
- spcal.io.nu.times_from_integs(integs: list[ndarray], run_info: dict) ndarray¶
Get the acquisition times from a list of .integ files.
Calculates the times using the acquistion, segment and cycle for each result.
- Parameters:
integs – list of parsed .integ files, from read_integ_binary
run_info – dict of the run.info file, from json.load
- Returns:
array of times for each result in all .integs
spcal.io.session¶
spcal.io.text¶
Reading single particle data from csv files.
- spcal.io.text.guess_text_parameters(lines: list[str]) tuple[str, int, int]¶
Guesses the delimiter, skip_rows and column count.
- Parameters:
lines – list of lines in file or header
- Returns:
delimiter, skip_rows, column_count
- spcal.io.text.is_text_file(path: Path) bool¶
Checks path exists and is a ‘.csv’, ‘.txt’ or ‘.text’.
- spcal.io.text.iso_time_to_float_seconds(text: str) float¶
Convert an ISO time string to a float.
- spcal.io.text.read_single_particle_file(path: Path | str, delimiter: str = ',', skip_rows: int = 1) ndarray¶
Imports data stored as text with elements in columns.
- Parameters:
path – path to file
delimiter – delimiting character between columns
skip_rows – the first data (not header) line else None
- Returns:
data, structred array
- spcal.io.text.replace_comma_decimal(fp, ncols: int, delimiter: str = ',')¶
Yields lines in a text file with the comma replaced with a period.
If a line has less delimiters than the expected number of columns - 1, then it is skipped. If the delimiter is a comma, no action is performed.
- Parameters:
fp – file pointer, e.g. from .open
ncols – expected number of columns
delimiter – column delimiter
- Yields:
line with comma replaced, when number of columns is correct
spcal.io.tofwerk¶
Loading data from TOFWERK ICP-ToF.
- spcal.io.tofwerk.calibrate_index_to_mass(indices: ndarray, mode: int, p: list[float]) ndarray¶
Calibrate sample indicies to mass / charge.
- Parameters:
indices – array of sample incidies
mode – mode from /FullSpectra/MassCalibMode
ps – coefficients from /FullSpectra/MassCalibration p_
- Returns:
calibrated masses
- spcal.io.tofwerk.calibrate_mass_to_index(masses: ndarray, mode: int, p: list[float]) ndarray¶
Calibrate mass / charge to sample indicies.
- Parameters:
indices – array of sample incidies
mode – mode from /FullSpectra/MassCalibMode
ps – coefficients from /FullSpectra/MassCalibration p_
- Returns:
sample indicies
- spcal.io.tofwerk.integrate_tof_data(h5: File, idx: ndarray | None = None) ndarray¶
Integrates TofData to recreate PeakData. Returned data is in ions/extraction for compatibility with PeakData, it can be converted to ions/acquisition by via *
factor_extraction_to_acquisition. Integration is summing from int(lower index limit) + 1 to int(upper index limit).- Parameters:
h5 – opened h5 file
idx – only integrate these peak idx
- Returns:
data equivilent to PeakData
- spcal.io.tofwerk.is_tofwerk_file(path: Path) bool¶
Checks if the file is a .h5 file.
- spcal.io.tofwerk.read_tofwerk_file(path: Path | str, idx: ndarray | None = None) tuple[ndarray, ndarray, float]¶
Reads a TOFWERK TofDaq .hdf and returns peak data and peak info.
- Parameters:
path – path to .hdf archive
idx – limit extraction to these idx
- Returns:
structured array of peak data in ions / acquisition information from the PeakTable dwelltime