Kilosort4 API

run_kilosort

kilosort.run_kilosort.compute_drift_correction(ops, device, tic0=nan, progress_bar=None, file_object=None)[source]

Compute drift correction parameters and save them to ops.

Parameters:
  • ops (dict) – Dictionary storing settings and results for all algorithmic steps.

  • device (torch.device) – Indicates whether pytorch operations should be run on cpu or gpu.

  • tic0 (float; default=np.nan.) – Start time of run_kilosort.

  • progress_bar (TODO; optional.) – Informs tqdm package how to report progress, type unclear.

  • file_object (array-like file object; optional.) – Must have ‘shape’ and ‘dtype’ attributes and support array-like indexing (e.g. [:100,:], [5, 7:10], etc). For example, a numpy array or memmap.

Returns:

  • ops (dict)

  • bfile (kilosort.io.BinaryFiltered) – Wrapped file object for handling data.

kilosort.run_kilosort.compute_preprocessing(ops, device, tic0=nan, file_object=None)[source]

Compute preprocessing parameters and save them to ops.

Parameters:
  • ops (dict) – Dictionary storing settings and results for all algorithmic steps.

  • device (torch.device) – Indicates whether pytorch operations should be run on cpu or gpu.

  • tic0 (float; default=np.nan) – Start time of run_kilosort.

  • file_object (array-like file object; optional.) – Must have ‘shape’ and ‘dtype’ attributes and support array-like indexing (e.g. [:100,:], [5, 7:10], etc). For example, a numpy array or memmap.

Returns:

ops

Return type:

dict

kilosort.run_kilosort.detect_spikes(ops, device, bfile, tic0=nan, progress_bar=None)[source]

Run spike sorting algorithm and save intermediate results to ops.

Parameters:
  • ops (dict) – Dictionary storing settings and results for all algorithmic steps.

  • device (torch.device) – Indicates whether pytorch operations should be run on cpu or gpu.

  • bfile (kilosort.io.BinaryFiltered) – Wrapped file object for handling data.

  • tic0 (float; default=np.nan.) – Start time of run_kilosort.

  • progress_bar (TODO; optional.) – Informs tqdm package how to report progress, type unclear.

Returns:

  • st (np.ndarray) – 1D vector of spike times for all clusters.

  • clu (np.ndarray) – 1D vector of cluster ids indicating which spike came from which cluster, same shape as st.

  • tF (np.ndarray) – TODO

  • Wall (np.ndarray) – TODO

kilosort.run_kilosort.get_run_parameters(ops) list[source]

Get ops dict values needed by run_kilosort subroutines.

kilosort.run_kilosort.initialize_ops(settings, probe, data_dtype, do_CAR, invert_sign, device) dict[source]

Package settings and probe information into a single ops dictionary.

kilosort.run_kilosort.run_kilosort(settings, probe=None, probe_name=None, filename=None, data_dir=None, file_object=None, results_dir=None, data_dtype=None, do_CAR=True, invert_sign=False, device=None, progress_bar=None, save_extra_vars=False)[source]

Run full spike sorting pipeline on specified data.

Parameters:
  • settings (dict) –

    Specifies a number of configurable parameters used throughout the spike sorting pipeline. See kilosort/parameters.py for a full list of available parameters. NOTE: n_chan_bin must be specified here, but all other settings are

    optional.

  • probe (dict; optional.) – A Kilosort4 probe dictionary, as returned by kilosort.io.load_probe.

  • probe_name (str; optional.) – Filename of probe to use, within the default PROBE_DIR. Only include the filename without any preceeding directories. Will ony be used if probe is None. Alternatively, the full filepath to a probe stored in any directory can be specified with settings = {‘probe_path’: …}. See kilosort.utils for default PROBE_DIR definition.

  • filename (str or Path; optional.) – Full path to binary data file. If specified, will also set data_dir = filename.parent.

  • data_dir (str or Path; optional.) – Specifies directory where binary data file is stored. Kilosort will attempt to find the binary file. This works best if there is exactly one file in the directory with a .bin, .bat, .dat, or .raw extension. Only used if filename is None. Also see kilosort.io.find_binary.

  • file_object (array-like file object; optional.) – Must have ‘shape’ and ‘dtype’ attributes and support array-like indexing (e.g. [:100,:], [5, 7:10], etc). For example, a numpy array or memmap. Must specify a valid filename as well, even though data will not be directly loaded from that file.

  • results_dir (str or Path; optional.) – Directory where results will be stored. By default, will be set to data_dir / ‘kilosort4’.

  • data_dtype (str or type; optional.) – dtype of data in binary file, like ‘int32’ or np.uint16. By default, dtype is assumed to be ‘int16’.

  • do_CAR (bool; default=True.) – If True, apply common average reference during preprocessing (recommended).

  • invert_sign (bool; default=False.) – If True, flip positive/negative values in data to conform to standard expected by Kilosort4.

  • device (torch.device; optional.) – CPU or GPU device to use for PyTorch calculations. By default, PyTorch will use the first detected GPU. If no GPUs are detected, CPU will be used. To set this manually, specify device = torch.device(<device_name>). See PyTorch documentation for full description.

  • progress_bar (tqdm.std.tqdm or QtWidgets.QProgressBar; optional.) – Used by sorting steps and GUI to track sorting progress. Users should not need to specify this.

  • save_extra_vars (bool; default=False.) – If True, save tF and Wall to disk after sorting.

Raises:

ValueError – If settings[n_chan_bin] is None (default). User must specify, for example: run_kilosort(settings={‘n_chan_bin’: 385}).

Returns:

Description TODO

Return type:

ops, st, clu, tF, Wall, similar_templates, is_ref, est_contam_rate

kilosort.run_kilosort.save_sorting(ops, results_dir, st, clu, tF, Wall, imin, tic0=nan, save_extra_vars=False)[source]

Save sorting results, and format them for use with Phy

Parameters:
  • ops (dict) – Dictionary storing settings and results for all algorithmic steps.

  • results_dir (pathlib.Path) – Directory where results should be saved.

  • st (np.ndarray) – 1D vector of spike times for all clusters.

  • clu (np.ndarray) – 1D vector of cluster ids indicating which spike came from which cluster, same shape as st.

  • tF (np.ndarray) – TODO

  • Wall (np.ndarray) – TODO

  • imin (int) – Minimum sample index used by BinaryRWFile, exported spike times will be shifted forward by this number.

  • tic0 (float; default=np.nan.) – Start time of run_kilosort.

Returns:

  • ops (dict)

  • similar_templates (np.ndarray)

  • is_ref (np.ndarray)

  • est_contam_rate (np.ndarray)

kilosort.run_kilosort.set_files(settings, filename, probe, probe_name, data_dir, results_dir)[source]

Parse file and directory information for data, probe, and results.