ibllib.ephys.spikes

Functions

create_insertion

Create or update a probe insertion in Alyx and return description and the alyx rest record.

detection

Detects and de-duplicates negative voltage spikes based on voltage thresholding.

driftmap_spike_sorting_memmap

Build a spike-sorting driftmap 2D histogram from npy files without loading all spikes into RAM.

ks2_to_alf

Convert Kilosort 2 output to ALF dataset for single probe data

ks2_to_tar

Compress output from kilosort 2 into tar file in order to register to flatiron and move to spikesorters/ks2_matlab/probexx path.

probes_description

Aggregate probes information into ALF files Register alyx probe insertions and Micro-manipulator trajectories Input: raw_ephys_data/probeXX/ Output: alf/probes.description.npy

sync_spike_sorting

Synchronizes the spike.times using the previously computed sync files

create_insertion(alyx: AlyxClient, md: dict, label: str, eid: str) Tuple[dict, dict][source]

Create or update a probe insertion in Alyx and return description and the alyx rest record.

This function checks if a probe insertion with the given label already exists for the specified session. If it doesn’t exist, it creates a new one. If it does, it updates the existing record. It also prepares a dictionary with essential probe details.

Parameters:
  • alyx (one.webclient.AlyxClient) – An instance of the Alyx rest client.

  • md (dict) – A Bunch object containing metadata from a spikeglx meta file, including ‘neuropixelVersion’, ‘serial’, and ‘fileName’.

  • label (str) – The label for the probe insertion (e.g., ‘probe00’).

  • eid (str) – The unique experiment ID (UUID) for the session.

Returns:

A tuple containing: - description (dict): A dictionary with probe details for metadata file,

containing keys ‘label’, ‘model’, ‘serial’, ‘raw_file_name’.

  • insertion (dict): The Alyx record for the created or updated probe insertion.

Return type:

tuple

probes_description(ses_path, one)[source]

Aggregate probes information into ALF files Register alyx probe insertions and Micro-manipulator trajectories Input:

raw_ephys_data/probeXX/

Output:

alf/probes.description.npy

sync_spike_sorting(ap_file, out_path)[source]

Synchronizes the spike.times using the previously computed sync files

Parameters:
  • ap_file – raw binary data file for the probe insertion

  • out_path – probe output path (usually {session_path}/alf/{probe_label})

ks2_to_alf(ks_path, bin_path, out_path, bin_file=None, ampfactor=1, label=None, force=True)[source]

Convert Kilosort 2 output to ALF dataset for single probe data

Parameters:
  • ks_path

  • bin_path – path of raw data

  • out_path

Returns:

ks2_to_tar(ks_path, out_path, force=False)[source]

Compress output from kilosort 2 into tar file in order to register to flatiron and move to spikesorters/ks2_matlab/probexx path. Output file to register

Parameters:
  • ks_path – path to kilosort output

  • out_path – path to keep the

:return path to tar ks output

To extract files from the tar file can use this code .. rubric:: Example

save_path = Path(‘folder you want to extract to’) with tarfile.open(‘_kilosort_output.tar’, ‘r’) as tar_dir:

tar_dir.extractall(path=save_path)

driftmap_spike_sorting_memmap(times_file, depths_file, t_bin=0.007, d_bin=10, chunk_size=2000000, tlim=None, dlim=None)[source]

Build a spike-sorting driftmap 2D histogram from npy files without loading all spikes into RAM.

Produces the same result as calling brainbox.plot.driftmap with plot_style='bincount', but processes spikes in temporal chunks of chunk_size so peak RAM usage stays proportional to the chunk size rather than to the total number of spikes (~32 MB per chunk for the default of 2 M spikes, vs. ~800 MB for a typical 50 M-spike recording).

Parameters:
  • times_file (pathlib.Path) – Path to spikes.times.npy. Must be sorted ascending.

  • depths_file (pathlib.Path) – Path to spikes.depths.npy, same length as times_file.

  • t_bin (float) – Time bin width in seconds.

  • d_bin (float) – Depth bin width in micrometres.

  • chunk_size (int) – Number of spikes processed per iteration (default: 2 000 000 ≈ 32 MB working set for two float64 arrays).

  • tlim (list of float, optional) – [t_start, t_end] in seconds. Defaults to [times[0], times[-1]] (requires only two element reads via mmap).

  • dlim (list of float, optional) – [d_min, d_max] in micrometres. When omitted the full depth range is scanned in chunks; pass it explicitly (e.g. from channels.localCoordinates) to avoid that extra pass.

Returns:

  • R (numpy.ndarray) – 2-D float32 array of shape (n_depth_bins, n_time_bins) with spike counts per bin.

  • t_scale (numpy.ndarray) – Left edge of each time bin (seconds).

  • d_scale (numpy.ndarray) – Left edge of each depth bin (micrometres).

  • n_spikes (int) – Total number of spikes (length of times_file).

detection(data, fs, h, detect_threshold=-4, time_tol=0.002, distance_threshold_um=70)[source]

Detects and de-duplicates negative voltage spikes based on voltage thresholding. The de-duplication step locks in maximum amplitude events. To account for collisions the amplitude is assumed to be decaying from the peak. If this is a multipeak event, each is labeled as a spike.

Parameters:
  • data – 2D numpy array nsamples x nchannels

  • fs – sampling frequency (Hz)

  • h – dictionary with neuropixel geometry header: see. neuropixel.trace_header

  • detect_threshold – negative value below which the voltage is considered to be a spike

  • time_tol – time in seconds for which samples before and after are assumed to be part of the spike

  • distance_threshold_um – distance for which exceeding threshold values are assumed to part of the same spike

Returns:

spikes dictionary of vectors with keys “time”, “trace”, “amp” and “ispike”