cibrrig.sorting.spikeinterface_ks4

Run Kilosort 4 locally on the NPX computer. Data must be reorganized using the preprocess.ephys_data_to_alf.py script first.

Attributes

N_JOBS

MOTION_PRESET

SCRATCH_NAME

job_kwargs

do_correction

do_correction

USE_MOTION_SI

sorter_params

COMPUTE_MOTION_SI

OPTO_OBJECTS

EXTENSIONS

_log

SORTER

Functions

log_elapsed_time(start_time)

move_motion_info(src, destination)

Rename the motion data computed by Spikeinterface into a alf-like format

remove_opto_artifacts(recording, session_path, probe_path)

Use the Spikeinterface "remove_artifacts" to zero out around the onsets and offsets of the laser

concatenate_recording(recording[, t0, tf])

Concatenate a multi-segment recording into a single continuous recording.

si_motion(recording, MOTION_PATH)

Compute motion using SpikeInterface (SI) and save the motion information.

plot_motion(motion_path, rec)

Plot the motion information and save the figure.

split_shanks_and_spatial_filter(rec)

Split a multishank recording into multiple groups and perform spatial filtering.

remove_and_interpolate(recording, probe_dir[, t0, tf, ...])

Remove channels outside the brain and interpolate bad channels

apply_preprocessing(recording, session_path, ...[, ...])

Apply the IBL preprocessing pipeline to the recording.

extract_breath_events(session_path, dest)

Create an 'events.csv' that has the times of each breath for the alf folder.

postprocess_sorting(analyzer_path, recording, sort_rez)

Postprocess the sorting result. Saves raw and automerged versions of the sorting analyzer to disk as .zarr files.

move_sorted_to_alf(sorted_dir, probe_local)

Move the sorted data from the local scratch directory to the alf directory.

run_probe(probe_src, probe_local[, testing, ...])

Run spikesorting on a single probe

cli(session_path, dest, testing, no_move_final, ...)

run(session_path[, dest, testing, no_move_final, ...])

Spike sort a session. A session is multiple simultanesouly recorded probes. Any instances of multiple

Module Contents

cibrrig.sorting.spikeinterface_ks4.N_JOBS = 1[source]
cibrrig.sorting.spikeinterface_ks4.MOTION_PRESET = 'dredge'[source]
cibrrig.sorting.spikeinterface_ks4.SCRATCH_NAME = 'SCRATCH_dredge'[source]
cibrrig.sorting.spikeinterface_ks4.job_kwargs[source]
cibrrig.sorting.spikeinterface_ks4.do_correction = False[source]
cibrrig.sorting.spikeinterface_ks4.do_correction = True[source]
cibrrig.sorting.spikeinterface_ks4.USE_MOTION_SI = True[source]
cibrrig.sorting.spikeinterface_ks4.sorter_params[source]
cibrrig.sorting.spikeinterface_ks4.COMPUTE_MOTION_SI = True[source]
cibrrig.sorting.spikeinterface_ks4.OPTO_OBJECTS = ['laser', 'laser2'][source]
cibrrig.sorting.spikeinterface_ks4.EXTENSIONS[source]
cibrrig.sorting.spikeinterface_ks4._log[source]
cibrrig.sorting.spikeinterface_ks4.SORTER = 'kilosort4'[source]
cibrrig.sorting.spikeinterface_ks4.log_elapsed_time(start_time)[source]
cibrrig.sorting.spikeinterface_ks4.move_motion_info(src, destination)[source]

Rename the motion data computed by Spikeinterface into a alf-like format If it doesn’t exist, do nothing

Parameters:
  • motion_path (Path) – Path to where the motion data live

  • destination (Path) – Path to where the motion data land

cibrrig.sorting.spikeinterface_ks4.remove_opto_artifacts(recording, session_path, probe_path, opto_objects=None, ms_before=0.125, ms_after=0.25)[source]

Use the Spikeinterface “remove_artifacts” to zero out around the onsets and offsets of the laser Assumes an ALF format and existence of laser tables and sync objects

Parameters:
  • recording (spikeinterface recording extractor)

  • session_path (Path)

  • probe_dir (Path)

  • object (str, optional) – ALF object. Defaults to ‘laser’.

  • ms_before (float, optional) – Time before laser to blank. Defaults to 0.125.

  • ms_after (float, optional) – Time after laser to blank. Defaults to 0.25.

Returns:

Recording extractor with artifacts removed.

Return type:

spikeinterface.RecordingExtractor

cibrrig.sorting.spikeinterface_ks4.concatenate_recording(recording, t0=0, tf=None)[source]

Concatenate a multi-segment recording into a single continuous recording.

This function takes a recording that may have multiple segments and concatenates them into a single continuous recording that Kilosort can handle. Optionally, it can clip the dataset in time.

Parameters:
  • recording (spikeinterface.RecordingExtractor) – Recording extractor object that may have multiple segments.

  • t0 (int, optional) – Start time in seconds. Defaults to 0.

  • tf (int, optional) – End time in seconds. Defaults to None, which means the entire recording.

Returns:

Concatenated recording extractor object.

Return type:

spikeinterface.RecordingExtractor

cibrrig.sorting.spikeinterface_ks4.si_motion(recording, MOTION_PATH)[source]

Compute motion using SpikeInterface (SI) and save the motion information.

This function estimates the motion of the recording using SpikeInterface. If motion information already exists at the specified path, it loads the existing motion information and interpolates the motion. Otherwise, it performs motion correction and saves the motion information.

Parameters:
  • recording (spikeinterface.RecordingExtractor) – Recording extractor object.

  • MOTION_PATH (Path) – Path to save or load motion information.

Returns:

A tuple containing:
  • spikeinterface.RecordingExtractor: Motion-corrected recording extractor object.

  • dict: Motion information dictionary.

Return type:

tuple

cibrrig.sorting.spikeinterface_ks4.plot_motion(motion_path, rec)[source]

Plot the motion information and save the figure.

This function loads the motion information from the specified path, plots the motion, and saves the figure as ‘driftmap.png’ and ‘driftmap_zoom.png’.

Parameters:
  • MOTION_PATH (Path) – Directory where the motion info lives

  • rec (SI recording) – Recording to plot the motion on

cibrrig.sorting.spikeinterface_ks4.split_shanks_and_spatial_filter(rec)[source]

Split a multishank recording into multiple groups and perform spatial filtering.

This function splits a multishank recording into separate channel groups based on the ‘group’ property. It then applies a highpass spatial filter to each channel group and combines the preprocessed recordings into a single recording.

Parameters:

rec (spikeinterface.RecordingExtractor) – Recording extractor object containing the multishank recording.

Returns:

Combined recording extractor object with spatially filtered data.

Return type:

spikeinterface.RecordingExtractor

cibrrig.sorting.spikeinterface_ks4.remove_and_interpolate(recording, probe_dir, t0=0, tf=120, remove=True, plot=True, save=True)[source]

Remove channels outside the brain and interpolate bad channels

Parameters:
  • recording (spikeinterface.RecordingExtractor) – Recording extractor object.

  • t0 (float, optional) – Start time in seconds. Defaults to 0.

  • tf (float, optional) – End time in seconds. Defaults to 120.

  • remove (bool, optional) – If True, remove channels outside the brain. Defaults to True.

  • plot (bool, optional) – If True, plot the traces before and after removing bad channels. Defaults to True.

  • save (bool, optional) – If True, save the channel labels. Defaults to True.

Returns:

Recording extractor object with bad channels removed and interpolated. np.array: Array of channel indices that were removed.

Return type:

spikeinterface.RecordingExtractor

cibrrig.sorting.spikeinterface_ks4.apply_preprocessing(recording, session_path, probe_dir, testing, skip_remove_opto=False)[source]

Apply the IBL preprocessing pipeline to the recording.

This function applies a series of preprocessing steps to the recording 1. Highpass filtering 2. Phase shifting 3. Bad channel detection and interpolation 4. Spatial filtering (destriping)

Optionally, it can also remove optogenetic artifacts and concatenate recording segments.

Parameters:
  • recording (spikeinterface.RecordingExtractor) – Recording extractor object.

  • session_path (str or Path) – Path to the session directory.

  • probe_dir (str or Path) – Path to the probe directory.

  • testing (bool) – If True, run in testing mode with limited data.

  • skip_remove_opto (bool, optional) – If True, skip the removal of optogenetic artifacts. Defaults to False.

Returns:

Preprocessed and concatenated recording extractor object.

Return type:

spikeinterface.RecordingExtractor

cibrrig.sorting.spikeinterface_ks4.extract_breath_events(session_path, dest)[source]

Create an ‘events.csv’ that has the times of each breath for the alf folder. This is then used in phy if the PSTH plugin exists. :param session_path: Path to the session directory where the original data exists :type session_path: Path :param alf_path: Path to the ALF sorted data :type alf_path: Path

Returns:

None

cibrrig.sorting.spikeinterface_ks4.postprocess_sorting(analyzer_path, recording, sort_rez)[source]

Postprocess the sorting result. Saves raw and automerged versions of the sorting analyzer to disk as .zarr files.

Performs these steps: 1. Creates a sorting analyzer in memory 2. Compute all extensions requested in global EXTENSIONS 3. Remove redundant units 4. Compute PCs 5. Compute quality metrics 6. Saves the analyzer (as .raw.zarr) 7. Auto-merge units 8. Recompute metrics on merged data (required to prevent crashes downstream) 9. Save the automerged analyzer (as .zarr)

Parameters:
  • analyzer_path (Path) – Path to save the analyzer.

  • recording (spikeinterface.RecordingExtractor) – Recording extractor object.

  • sort_rez (spikeinterface.SortingExtractor) – Sorting extractor object.

Returns:

Postprocessed sorting analyzer object.

Return type:

spikeinterface.SortingAnalyzer

cibrrig.sorting.spikeinterface_ks4.move_sorted_to_alf(sorted_dir, probe_local)[source]

Move the sorted data from the local scratch directory to the alf directory.

Parameters:
  • sorted_dir (Path) – Path to the sorted data directory.

  • probe_local (Path) – Path to the probe directory in the alf folder.

Returns:

None

cibrrig.sorting.spikeinterface_ks4.run_probe(probe_src, probe_local, testing=False, skip_remove_opto=False)[source]

Run spikesorting on a single probe

Parameters:
  • probe_dir (Path) – Path to the probe directory.

  • probe_local (str) – Local path to save phy sorting to.

  • testing (bool, optional) – If True, run in testing mode (short data snippet). Defaults to False.

  • skip_remove_opto (bool, optional) – If True, skip the removal of opto artifacts. Defaults to False.

Returns:

Path to the sorted data.

Return type:

Path

cibrrig.sorting.spikeinterface_ks4.cli(session_path, dest, testing, no_move_final, skip_remove_opto, keep_scratch)[source]
cibrrig.sorting.spikeinterface_ks4.run(session_path, dest=None, testing=False, no_move_final=False, skip_remove_opto=False, rm_intermediate=True)[source]

Spike sort a session. A session is multiple simultanesouly recorded probes. Any instances of multiple recordings must occur in the same anatomical location

If a destination is not provided, the sorted data will be placed in the session/alf/<sorter> directory. :param session_path: Path to the session directory. :type session_path: str or Path :param dest: Destination directory for the sorted data. Defaults to None. :type dest: str or Path, optional :param testing: If True, run in testing mode. Defaults to False. :type testing: bool, optional :param no_move_final: If True, do not move the final sorted data. Defaults to False. :type no_move_final: bool, optional :param skip_remove_opto: If True, skip the removal of opto artifacts. Defaults to False. :type skip_remove_opto: bool, optional :param rm_intermediate: If True, remove intermediate files. Defaults to True. :type rm_intermediate: bool, optional