neurokin.utils.neural package#

Submodules#

neurokin.utils.neural.exporting module#

neurokin.utils.neural.exporting.export_neural_data_to_bin(data, filename)[source]#

Export the raw data to a binary file in C major order.

Parameters:
  • data – data to be stored, should have shape n_ch*n_sample

  • filename – name of the file

Returns:

neurokin.utils.neural.importing module#

neurokin.utils.neural.importing.import_binary_to_float32(filename, channel_number, sample_number)[source]#

Imports binary data stored in C major to a float32 array

Parameters:
  • filename – file to import from

  • sample_number – number of sample in each channel

  • channel_number – number of channels

Returns:

the array with shape channel_number*sample_number

neurokin.utils.neural.importing.import_open_ephys_channel_data(folderpath: str, experiment: str, recording: str, channels: list | int | None = None, sync_present: bool = False, sync_ch: int | None = None, source_processor: str | None = None, start_sample_index: int = 0, end_sample_index: int = -1, convert_to_volts: bool = True) -> (<class 'float'>, numpy.ndarray)[source]#

Imports open ephys data from binary files.

Parameters:
  • folderpath – Folderpath where the experiment is, including the Node

  • experiment – experiment folder

  • recording – recording folder

  • channels – indicate which channels to return

Returns:

Sampling frequency is returned as a float and raw data are returned in arbitrary units

neurokin.utils.neural.importing.import_tdt_channel_data(folderpath, ch=[], t1=0, t2=-1, stream_name='Wav1', stim_name='Wav1', sync_present=False) -> (<class 'float'>, numpy.typing.ArrayLike)[source]#

Wrapper for the import function of tdt, to be more user friendly.

Parameters:
  • folderpath – folderpath of the subject experiment

  • ch

  • stream_name

  • stim_name

  • sync_present

  • t1 – initial time to index in seconds

  • t2 – last time to index in seconds

Returns:

frequency sample and raw sample

neurokin.utils.neural.importing.time_to_sample(timestamp: float, fs: float, is_t1: bool = False, is_t2: bool = False) int[source]#

Function adapted from time2sample in TDTbin2py.py Returns the sample index given a time in seconds and the sampling frequency. It has to be specified if the timestamp refers to t1 or t2. :param timestamp: time in seconds :param fs: sampling frequency :param is_t1: specify if the timestamp is t1 :param is_t2: specify if the timestamp is t2 :return:

neurokin.utils.neural.neural_plot module#

neurokin.utils.neural.neural_plot.plot_spectrogram(ax: matplotlib.axes, fs: float, raw: numpy.typing.ArrayLike, ylim: List[int], title: str = '', tick_spacing: int = 10, nfft: int | None = None, noverlap: int | None = None, vmin=None, vmax=None, **kwargs) matplotlib.axes[source]#

Wrapper for matplotlib spectrogram to add some style elements.

Parameters:
  • ax – axes where to plot

  • fs – sampling frequency

  • raw – raw data

  • title – title of the figure

  • ylim – limit of frequencies

  • tick_spacing – spacing of the ticks on the axis

  • nfft – nfft to comput the fft

  • noverlap – number of overlap points

Returns:

neurokin.utils.neural.neural_plot.plot_welch(ax: matplotlib.axes, freqs: numpy.typing.ArrayLike, pxx_den: numpy.typing.ArrayLike, title: str = 'PSD', xlim=None, ylim=None, tick_spacing: int = 10) matplotlib.axes[source]#

Returns a plot of psd with some style elements

Parameters:
  • ax – axes where to plot

  • freqs – array like of frequencies corresponding to the x axis

  • pxx_den – array like of powers corresponding to the y axis

  • title – str

  • xlim – limit of frequencies for plotting

  • ylim – limit of power for plotting

  • tick_spacing – spacing of the ticks on the axis

Returns:

neurokin.utils.neural.processing module#

neurokin.utils.neural.processing.average_block(array: numpy.typing.ArrayLike, start: int, stop: int) numpy.ndarray[source]#

Averages a subset of elements

Parameters:
  • array – input array

  • start – start idx for parsing

  • stop – stop idx for parsing

Returns:

average of the subset

neurokin.utils.neural.processing.calculate_power_spectral_density(data: numpy.typing.ArrayLike, fs: float, **kwargs) tuple[source]#

Calculate the frequencies and power spectral densities from the raw recording time series data.

Parameters:
  • data – raw recording data

  • fs – sampling frequency

Returns:

frequencies, power spectral density

neurokin.utils.neural.processing.find_closest_index(data: numpy.typing.ArrayLike, datapoint: float) int[source]#

Given an array of data and a datapoint it returns the index of the element that has the minimum difference to the datapoint

Parameters:
  • data – data array-like

  • datapoint – datapoint to find a close value to

Returns:

index of the closest element

neurokin.utils.neural.processing.find_closest_smaller_index(data: numpy.typing.ArrayLike, datapoint: float) int | Tuple[int, int][source]#

Given an array of data and a datapoint, returns the index of the element that is the closest but lower than the datapoint.

For a 1D array, returns an integer index. For a 2D array, returns a tuple (row, column) indicating the location of the closest element.

Parameters:
  • data – data array-like (1D or 2D)

  • datapoint – datapoint to find a close value to

Returns:

index of the closest element below datapoint (int for 1D or tuple for 2D)

neurokin.utils.neural.processing.get_average_amplitudes(parsed_raw, tested_amplitudes, pulses_number=None)[source]#

Assumes an experiment where a sequence of stimulation amplitudes are applied, every stimulation with a fixed number of pulses

Parameters:
  • parsed_raw

  • tested_amplitudes

  • pulses_number

Returns:

neurokin.utils.neural.processing.get_median_distance(a: numpy.typing.ArrayLike) float[source]#

Gets median distance between points

Parameters:

a – array-like data

Returns:

median

neurokin.utils.neural.processing.get_spectrogram_data(fs: float, raw: numpy.typing.ArrayLike, nfft: int | None = None, noverlap: int | None = None) Tuple[numpy.typing.ArrayLike][source]#

Gets the data used to plot a spectrogram

Parameters:
  • fs – sampling frequency

  • raw – raw data

  • nfft – nfft to compute the fft

  • noverlap – number of overlap points

Returns:

neurokin.utils.neural.processing.get_stim_timestamps(sync_ch: numpy.ndarray, expected_pulses: int | None = None) numpy.ndarray[source]#

Get indexes of only threshold crossing up from 0, i.e. edge detection. Sometimes there are spurious signals on the stimulation channel, when the stimulator turns off; if expected_pulses is given this function trims to the expected number of pulses.

Parameters:
  • sync_ch – the stimulation sync channel data

  • expected_pulses – number of expected pulses

Returns:

trimmed indexes

neurokin.utils.neural.processing.get_timestamps_stim_blocks(neudata, n_amp_tested, pulses, time_stim)[source]#

Given a DBS recording with multiple stimulation amplitudes tested it gives the time stamps of the onset and end of each block of stimulation.

Parameters:
  • neudata – NeuralData object, containing sync_data

  • n_amp_tested

  • pulses

  • time_stim

Returns:

neurokin.utils.neural.processing.parse_raw(raw: numpy.ndarray, stimulation_idxs: numpy.ndarray, samples_before_stim: int, skip_one: bool = False, min_len_chunk: int = 1) numpy.ndarray[source]#

Parses the signal given the timestamps of the stimulation.

Parameters:
  • raw – raw data of one channel

  • stimulation_idxs – indexes of the stimulation onset

  • samples_before_stim – how much before the stimulation onset to parse

  • skip_one – if True parses every second stimulation

  • min_len_chunk – filters the chunks to have a minimal len between pulses

Returns:

parsed raw signal into an array of equally sized chunks

neurokin.utils.neural.processing.running_mean(x: numpy.typing.ArrayLike, n: int) numpy.typing.ArrayLike[source]#

Returns the running mean with window n

Parameters:
  • x – data

  • n – window size

Returns:

smoothed data

neurokin.utils.neural.processing.running_mean_2D(x: numpy.typing.ArrayLike, n: int) numpy.typing.ArrayLike[source]#

Returns the running mean with window n, on 2D data

Parameters:
  • x – data

  • n – window size

Returns:

smoothed data

neurokin.utils.neural.processing.simply_mean_data_binarize(sync_ch: numpy.ndarray)[source]#

Return a simply binarized array by setting anything below the mean to 0 and everything above to 1. Use only in very clear-cut cases

Parameters:

sync_ch – input sync channel

Returns:

binarized array

neurokin.utils.neural.processing.trim_equal_len(raw: List[numpy.typing.ArrayLike]) List[float][source]#

Trims a list of arrays to all have the same length.

Parameters:

raw – raw data

Returns:

list with trimmed data

Module contents#