cibrrig.sorting.spikeinterface_ks4 ================================== .. py:module:: cibrrig.sorting.spikeinterface_ks4 .. autoapi-nested-parse:: Run Kilosort 4 locally on the NPX computer. Data must be reorganized using the preprocess.ephys_data_to_alf.py script first. Attributes ---------- .. autoapisummary:: cibrrig.sorting.spikeinterface_ks4.N_JOBS cibrrig.sorting.spikeinterface_ks4.MOTION_PRESET cibrrig.sorting.spikeinterface_ks4.SCRATCH_NAME cibrrig.sorting.spikeinterface_ks4.job_kwargs cibrrig.sorting.spikeinterface_ks4.do_correction cibrrig.sorting.spikeinterface_ks4.do_correction cibrrig.sorting.spikeinterface_ks4.USE_MOTION_SI cibrrig.sorting.spikeinterface_ks4.sorter_params cibrrig.sorting.spikeinterface_ks4.COMPUTE_MOTION_SI cibrrig.sorting.spikeinterface_ks4.OPTO_OBJECTS cibrrig.sorting.spikeinterface_ks4.EXTENSIONS cibrrig.sorting.spikeinterface_ks4._log cibrrig.sorting.spikeinterface_ks4.SORTER Functions --------- .. autoapisummary:: cibrrig.sorting.spikeinterface_ks4.log_elapsed_time cibrrig.sorting.spikeinterface_ks4.move_motion_info cibrrig.sorting.spikeinterface_ks4.remove_opto_artifacts cibrrig.sorting.spikeinterface_ks4.concatenate_recording cibrrig.sorting.spikeinterface_ks4.si_motion cibrrig.sorting.spikeinterface_ks4.plot_motion cibrrig.sorting.spikeinterface_ks4.split_shanks_and_spatial_filter cibrrig.sorting.spikeinterface_ks4.remove_and_interpolate cibrrig.sorting.spikeinterface_ks4.apply_preprocessing cibrrig.sorting.spikeinterface_ks4.extract_breath_events cibrrig.sorting.spikeinterface_ks4.postprocess_sorting cibrrig.sorting.spikeinterface_ks4.move_sorted_to_alf cibrrig.sorting.spikeinterface_ks4.run_probe cibrrig.sorting.spikeinterface_ks4.cli cibrrig.sorting.spikeinterface_ks4.run Module Contents --------------- .. py:data:: N_JOBS :value: 1 .. py:data:: MOTION_PRESET :value: 'dredge' .. py:data:: SCRATCH_NAME :value: 'SCRATCH_dredge' .. py:data:: job_kwargs .. py:data:: do_correction :value: False .. py:data:: do_correction :value: True .. py:data:: USE_MOTION_SI :value: True .. py:data:: sorter_params .. py:data:: COMPUTE_MOTION_SI :value: True .. py:data:: OPTO_OBJECTS :value: ['laser', 'laser2'] .. py:data:: EXTENSIONS .. py:data:: _log .. py:data:: SORTER :value: 'kilosort4' .. py:function:: log_elapsed_time(start_time) .. py:function:: move_motion_info(src, destination) Rename the motion data computed by Spikeinterface into a alf-like format If it doesn't exist, do nothing :param motion_path: Path to where the motion data live :type motion_path: Path :param destination: Path to where the motion data land :type destination: Path .. py:function:: remove_opto_artifacts(recording, session_path, probe_path, opto_objects=None, ms_before=0.125, ms_after=0.25) 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 :param recording: :type recording: spikeinterface recording extractor :param session_path: :type session_path: Path :param probe_dir: :type probe_dir: Path :param object: ALF object. Defaults to 'laser'. :type object: str, optional :param ms_before: Time before laser to blank. Defaults to 0.125. :type ms_before: float, optional :param ms_after: Time after laser to blank. Defaults to 0.25. :type ms_after: float, optional :returns: Recording extractor with artifacts removed. :rtype: spikeinterface.RecordingExtractor .. py:function:: concatenate_recording(recording, t0=0, tf=None) 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. :param recording: Recording extractor object that may have multiple segments. :type recording: spikeinterface.RecordingExtractor :param t0: Start time in seconds. Defaults to 0. :type t0: int, optional :param tf: End time in seconds. Defaults to None, which means the entire recording. :type tf: int, optional :returns: Concatenated recording extractor object. :rtype: spikeinterface.RecordingExtractor .. py:function:: si_motion(recording, MOTION_PATH) 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. :param recording: Recording extractor object. :type recording: spikeinterface.RecordingExtractor :param MOTION_PATH: Path to save or load motion information. :type MOTION_PATH: Path :returns: A tuple containing: - spikeinterface.RecordingExtractor: Motion-corrected recording extractor object. - dict: Motion information dictionary. :rtype: tuple .. py:function:: plot_motion(motion_path, rec) 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'. :param MOTION_PATH: Directory where the motion info lives :type MOTION_PATH: Path :param rec: Recording to plot the motion on :type rec: SI recording .. py:function:: split_shanks_and_spatial_filter(rec) 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. :param rec: Recording extractor object containing the multishank recording. :type rec: spikeinterface.RecordingExtractor :returns: Combined recording extractor object with spatially filtered data. :rtype: spikeinterface.RecordingExtractor .. py:function:: remove_and_interpolate(recording, probe_dir, t0=0, tf=120, remove=True, plot=True, save=True) Remove channels outside the brain and interpolate bad channels :param recording: Recording extractor object. :type recording: spikeinterface.RecordingExtractor :param t0: Start time in seconds. Defaults to 0. :type t0: float, optional :param tf: End time in seconds. Defaults to 120. :type tf: float, optional :param remove: If True, remove channels outside the brain. Defaults to True. :type remove: bool, optional :param plot: If True, plot the traces before and after removing bad channels. Defaults to True. :type plot: bool, optional :param save: If True, save the channel labels. Defaults to True. :type save: bool, optional :returns: Recording extractor object with bad channels removed and interpolated. np.array: Array of channel indices that were removed. :rtype: spikeinterface.RecordingExtractor .. py:function:: apply_preprocessing(recording, session_path, probe_dir, testing, skip_remove_opto=False) 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. :param recording: Recording extractor object. :type recording: spikeinterface.RecordingExtractor :param session_path: Path to the session directory. :type session_path: str or Path :param probe_dir: Path to the probe directory. :type probe_dir: str or Path :param testing: If True, run in testing mode with limited data. :type testing: bool :param skip_remove_opto: If True, skip the removal of optogenetic artifacts. Defaults to False. :type skip_remove_opto: bool, optional :returns: Preprocessed and concatenated recording extractor object. :rtype: spikeinterface.RecordingExtractor .. py:function:: extract_breath_events(session_path, dest) 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 .. py:function:: 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. 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) :param analyzer_path: Path to save the analyzer. :type analyzer_path: Path :param recording: Recording extractor object. :type recording: spikeinterface.RecordingExtractor :param sort_rez: Sorting extractor object. :type sort_rez: spikeinterface.SortingExtractor :returns: Postprocessed sorting analyzer object. :rtype: spikeinterface.SortingAnalyzer .. py:function:: move_sorted_to_alf(sorted_dir, probe_local) Move the sorted data from the local scratch directory to the alf directory. :param sorted_dir: Path to the sorted data directory. :type sorted_dir: Path :param probe_local: Path to the probe directory in the alf folder. :type probe_local: Path :returns: None .. py:function:: run_probe(probe_src, probe_local, testing=False, skip_remove_opto=False) Run spikesorting on a single probe :param probe_dir: Path to the probe directory. :type probe_dir: Path :param probe_local: Local path to save phy sorting to. :type probe_local: str :param testing: If True, run in testing mode (short data snippet). Defaults to False. :type testing: bool, optional :param skip_remove_opto: If True, skip the removal of opto artifacts. Defaults to False. :type skip_remove_opto: bool, optional :returns: Path to the sorted data. :rtype: Path .. py:function:: cli(session_path, dest, testing, no_move_final, skip_remove_opto, keep_scratch) .. py:function:: run(session_path, dest=None, testing=False, no_move_final=False, skip_remove_opto=False, rm_intermediate=True) 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/` 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