Giter VIP home page Giter VIP logo

pykilosort's Introduction

[WIP] Python port of KiloSort2

CircleCI

This is a work-in-progress literal Python port of the original MATLAB version of Kilosort 2, written by Marius Pachitariu. The code is still being debugged and is not ready for use.

Installation

System Requirements

The code makes extensive use of the GPU via the CUDA framework. A high-end NVIDIA GPU with at least 8GB of memory is required.

A good CPU and a large amount of RAM (minimum 32GB or 64GB) is also required.

See the Wiki on the Matlab version for more information.

You will need NVIDIA drivers and cuda-toolkit installed on your computer too. This can be the hardest part of the installation. To test if your is working OK you should be able to run the following:

nvidia-smi # Should show how much your GPU is being used right now
nvcc # This is the CUDA compiler

Doing the install

We don't currently provide a packaged version of pykilosort on pypy or conda-forge. Thus you will need to clone the repo first git clone https://github.com/rossant/pykilosort.git && cd pykilosort.

Conda (recomended)

If you don't already have conda installed you can follow the guide here.

To create a conda environment with these dependencies, run the command: conda env create -f pyks2.yml inside your pykilosort directory.

Pip

You can also install the requirements via pip: pip install -r requirements.txt. This is not the tested path and so if you run into issues you may get less support.

Usage

Example

The programming interface is subject to change. The following code example should be saved in a directory, along with the following files:

  • imec_385_100s.bin
  • chanMap.npy (be careful with 0- and 1- indexing discrepancy between MATLAB and Python: don't forget to subtract 1 if this file was used in Kilosort)
  • xc.npy
  • yc.npy
  • kcoords.npy
from pathlib import Path
import numpy as np

from pykilosort import add_default_handler, run, Bunch

add_default_handler(level='DEBUG')

dat_path = Path('imec_385_100s.bin')
dir_path = dat_path.parent
probe = Bunch()
probe.NchanTOT = 385
# WARNING: indexing mismatch with MATLAB hence the -1
probe.chanMap = np.load(dir_path / 'chanMap.npy').squeeze().astype(np.int64) - 1
probe.xc = np.load(dir_path / 'xc.npy').squeeze()
probe.yc = np.load(dir_path / 'yc.npy').squeeze()
probe.kcoords = np.load(dir_path / 'kcoords.npy').squeeze()

run(dat_path, probe=probe, dir_path=dir_path, n_channels=385, dtype=np.int16, sample_rate=3e4)

Disk cache (serialized results & parameter objects)

The MATLAB version used a big rez structured object containing the input data, the parameters, intermediate and final results.

The Python version makes the distinction between:

  • raw_data: a NumPy-like object of shape (n_channels_total, n_samples)
  • probe: a Bunch instance (dictionary) with the channel coordinates, the indices of the "good channels"
  • params: a Bunch instance (dictionary) with optional user-defined parameters. It can be empty. Any missing parameter is transparently replaced by the default as found in default_params.py file in the repository.
  • intermediate: a Bunch instance (dictionary) with intermediate arrays.

These objects are accessible via the context (ctx) which replaces the MATLAB rez object: ctx.raw_data, etc.

This context also stores a special object called ctx.intermediate which stores intermediate arrays. This object derives from Bunch and implements special methods to save and load arrays in a temporary folder. By default, an intermediate result called ctx.intermediate.myarray is stored in ./.kilosort/context/myarray.npy.

The main run() function checks the existence of some of these intermediate arrays to skip some steps that might have run already, for a given dataset.

The suffixes _m (merge), _s (split), _c (cutoff) are used to disambiguate between multiple processing steps for the same arrays (they would be overwritten otherwise).

Technical notes about the port

The following differences between MATLAB and Python required special care during the port:

  • Discrepancy between 0-based and 1-based indexing.
  • MATLAB uses Fortran ordering for arrays, whereas NumPy uses C ordering by default. The Python code therefore uses Fortran ordering exclusively so that the custom CUDA kernels can be used with no modification.
  • In MATLAB, arrays can be extended transparently with indexing, whereas NumPy/CuPy requires explicit concatenation.
  • The MATLAB code used mex C files to launch CUDA kernels, whereas the Python code uses CuPy directly.
  • A few workarounds around limitations of CuPy compared to MATLAB: no cp.median(), no GPU version of the lfilter() LTI filter in CuPy (a custom CUDA kernel had to be written), etc.

pykilosort's People

Contributors

shashwatsridhar avatar rossant avatar alexmorley avatar kushbanga avatar oliche avatar m-beau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.