cibrrig.preprocess.nidq_utils

I/O functions with NIDAQ data specifically for some of our physiology needs.

This module is primarily focused on loading and manipulating data from the NIDAQ files recorded by spikeglx, and passes most computation to the physiology module, which is more general in scope.

Attributes

_log

Functions

get_triggers(session_path)

Looks through all the NIDQ files to extract the trigger strings.

get_trig_string(in_str)

Extract the trigger string from a given input string using regex.

_extract_ds_chan(SR, chan_id[, ds_factor])

Extract and downsample a specific analog channel from the SpikeGLX reader.

load_mmap(fn)

Load a memory-mapped Nidaq file.

binary_onsets(x, thresh)

Binarize a signal at the level "thresh" and return the onset and offset indices.

get_tvec(dat, sr)

Generate a time vector for a given data array and sampling rate.

get_tvec_from_fn(fn)

Generate a time vector from a Nidaq file.

get_tvec_from_SR(SR)

Generate a time vector from a SpikeGLX reader object.

load_ds_pdiff(SR, chan_id[, ds_factor, inhale_dir])

Load and downsample the pdiff (differential pressure sensor) data.

load_dia_emg(SR, chan_id)

Read the raw diaphragm EMG data. Does not downsample the data

filt_int_ds_dia(x, sr[, ds_factor, rel_height, heartbeats])

Filter, integrate, and downsample the diaphragm EMG signal. Detect and summarize the diaphragm bursts.

extract_hr_channel(SR[, ekg_chan])

Extract heart rate from a dedicated EKG channel.

extract_temp(SR[, temp_chan, ds_factor])

Extract the temperature from the FHC DC temp controller. Assumes the manufacturer's calibration.

filt_int_ds_arbitrary(x, sr[, ds_factor])

Filter, integrate, and downsample an arbitrary signal.

Module Contents

cibrrig.preprocess.nidq_utils._log[source]
cibrrig.preprocess.nidq_utils.get_triggers(session_path)[source]

Looks through all the NIDQ files to extract the trigger strings.

Parameters:

session_path (Path) – Path to the session directory.

Returns:

Sorted list of trigger strings found in the NIDQ files.

Return type:

list

cibrrig.preprocess.nidq_utils.get_trig_string(in_str)[source]

Extract the trigger string from a given input string using regex.

Parameters:

in_str (str) – Input string containing the trigger information.

Returns:

Extracted trigger string.

Return type:

str

cibrrig.preprocess.nidq_utils._extract_ds_chan(SR, chan_id, ds_factor=10)[source]

Extract and downsample a specific analog channel from the SpikeGLX reader.

Parameters:
  • SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

  • chan_id (int) – Channel ID to extract.

  • ds_factor (int, optional) – Downsampling factor. Defaults to 10.

Returns:

A tuple containing:
  • np.ndarray: Downsampled data from the specified channel.

  • float: Downsampled sampling rate.

Return type:

tuple

cibrrig.preprocess.nidq_utils.load_mmap(fn)[source]

Load a memory-mapped Nidaq file.

Parameters:

fn (Path) – Path to the Nidaq.bin file.

Returns:

A tuple containing:
  • np.ndarray: Memory-mapped data array.

  • dict: Metadata dictionary.

Return type:

tuple

cibrrig.preprocess.nidq_utils.binary_onsets(x, thresh)[source]

Binarize a signal at the level “thresh” and return the onset and offset indices.

Parameters:
  • x (np.ndarray) – Input signal.

  • thresh (float) – Threshold value to determine binary state of HIGH (1) or LOW (0).

Returns:

A tuple containing:
  • np.ndarray: Indices of onset samples.

  • np.ndarray: Indices of offset samples.

Return type:

tuple

Raises:

ValueError – If the number of onsets does not match the number of offsets.

cibrrig.preprocess.nidq_utils.get_tvec(dat, sr)[source]

Generate a time vector for a given data array and sampling rate.

Parameters:
  • dat (np.ndarray) – Data array.

  • sr (float) – Sampling rate.

Returns:

Time vector.

Return type:

np.ndarray

cibrrig.preprocess.nidq_utils.get_tvec_from_fn(fn)[source]

Generate a time vector from a Nidaq file.

Parameters:

fn (Path) – Path to the Nidaq file.

Returns:

Time vector corresponding to the data in the file.

Return type:

np.ndarray

cibrrig.preprocess.nidq_utils.get_tvec_from_SR(SR)[source]

Generate a time vector from a SpikeGLX reader object.

Parameters:

SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

Returns:

Time vector corresponding to the data in the reader object.

Return type:

np.ndarray

cibrrig.preprocess.nidq_utils.load_ds_pdiff(SR, chan_id, ds_factor=10, inhale_dir=-1)[source]

Load and downsample the pdiff (differential pressure sensor) data.

Parameters:
  • SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

  • chan_id (int) – Channel ID for the pdiff signal.

  • ds_factor (int, optional) – Downsampling factor. Defaults to 10.

  • inhale_dir (int, optional) – Direction of inhalation. Defaults to -1.

Returns:

A tuple containing:
  • np.ndarray: Downsampled pdiff data.

  • float: Downsampled sampling rate.

Return type:

tuple

cibrrig.preprocess.nidq_utils.load_dia_emg(SR, chan_id)[source]

Read the raw diaphragm EMG data. Does not downsample the data

Subtract the mean from the raw data.

Parameters:
  • SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

  • chan_id (int) – Channel ID for the diaphragm EMG signal.

Returns:

A tuple containing:
  • np.ndarray: Raw diaphragm EMG data.

  • float: Sampling rate of the diaphragm recording.

Return type:

tuple

cibrrig.preprocess.nidq_utils.filt_int_ds_dia(x, sr, ds_factor=10, rel_height=0.95, heartbeats=None)[source]

Filter, integrate, and downsample the diaphragm EMG signal. Detect and summarize the diaphragm bursts. Uses median filtering to smooth the signal, which can be slow but is effective.

Parameters:
  • x (np.ndarray) – Raw diaphragm EMG signal.

  • sr (float) – Sampling rate of the input signal.

  • ds_factor (int, optional) – Downsampling factor. Defaults to 10.

  • rel_height (float, optional) – Relative height for burst detection. Defaults to 0.95.

  • heartbeats (np.ndarray, optional) – Precomputed heartbeats. Defaults to None.

Returns:

A tuple containing:
  • pd.DataFrame: DataFrame with burst statistics.

  • np.ndarray: Downsampled and normalized diaphragm signal.

  • float: Downsampled sampling rate.

  • np.ndarray: Heart rate data.

  • np.ndarray: Filtered diaphragm signal.

  • np.ndarray: Detected heartbeats.

Return type:

tuple

cibrrig.preprocess.nidq_utils.extract_hr_channel(SR, ekg_chan=2)[source]

Extract heart rate from a dedicated EKG channel. First subtracts the mean from the EKG signal.

Passes the mean-subtracted EKG signal to the physiology.extract_hr_from_ekg function.

Parameters:
  • SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

  • ekg_chan (int, optional) – Channel ID for the EKG signal. Defaults to 2.

Returns:

Timestamps of detected heartbeats.

Return type:

np.ndarray

Raises:

ValueError – If the EKG signal cannot be processed.

cibrrig.preprocess.nidq_utils.extract_temp(SR, temp_chan=7, ds_factor=10)[source]

Extract the temperature from the FHC DC temp controller. Assumes the manufacturer’s calibration.

Parameters:
  • SR (spikeglx.Reader) – SpikeGLX reader object for the recording.

  • temp_chan (int, optional) – Channel ID for the temperature signal. Defaults to 7.

  • ds_factor (int, optional) – Downsampling factor. Defaults to 10.

Returns:

Downsampled temperature data.

Return type:

np.ndarray

cibrrig.preprocess.nidq_utils.filt_int_ds_arbitrary(x, sr, ds_factor=10)[source]

Filter, integrate, and downsample an arbitrary signal.

Applies a second order Butterworth bandpass filter between 300 and 5000 Hz.

Parameters:
  • x (np.ndarray) – Input signal.

  • sr (float) – Sampling rate of the input signal.

  • ds_factor (int, optional) – Downsampling factor. Defaults to 10.

Returns:

A tuple containing:
  • np.ndarray: Processed and downsampled signal.

  • float: Downsampled sampling rate.

  • np.ndarray: Filtered signal.

Return type:

tuple