Skip to content

Commit 1021699

Browse files
authored
Merge pull request #25 from Tauffer-Consulting/compiled-kilosort-all
Add Matlab Compiled Kilosort - All versions
2 parents dbf7033 + 04b0a5b commit 1021699

23 files changed

Lines changed: 648 additions & 0 deletions

File tree

kilosort_no_license/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Kilosort Compiled Docker Images
2+
3+
This documentation is intended to show how to create a Docker image with Matlab compiled implementation of Kilosort sorter. The main goal of this project is to avoid the requirement of Matlab Licenses and also abstract the installation and setup steps to run kilosort
4+
5+
## Requirements
6+
- Packaging a MATLAB Docker image is supported on Linux only
7+
- Docker
8+
- Matlab
9+
- NVIDIA GPU and CUDA capabilities
10+
11+
### Matlab Requirements
12+
- MATLAB Compiler
13+
- Parallel Computing Toolbox
14+
- Signal Processing Toolbox
15+
- Statistics and Machine Learning Toolbox
16+
17+
## Creating Docker Image
18+
19+
There are four main steps to generate a functional kilosort-compiled docker image:
20+
21+
1. Kilosort Setup
22+
2. Compile Kilosort as Standalone Application
23+
3. Create a (base) docker image with Matlab Runtime and the compiled application from step 2
24+
4. Extend the docker image from step 3 for improvements and fixes
25+
26+
Detailed descriptions for each step are documented in `README.md` for each supported kilosort version folder (`kilosort-compiled`, `kilosort2-compiled` and so on)
27+
28+
Licenses for Matlab and toolboxes are needed only for compiling kilosort as Standalone Application and to generate the base Docker image. After this process, no license will be required, either to extend the base image or to run the sorter.
29+
30+
## Running a container
31+
32+
After generating kilosort-compiled image, [nvidia-container-toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#setting-up-nvidia-container-toolkit) is required to run a docker with GPU capabilities
33+
34+
The base syntax to run dockerized kilosort is:
35+
36+
```bash
37+
docker run -v <host-data-folder>:<docker-data-folder> <image>:<tag> <ks_command> [ARGS]
38+
```
39+
- A volume has to be binded to a folder were all the data to run kilosort are stored
40+
- `<image>`: Image name given by `build.sh` script
41+
- `<tag>`: Image Tag given by `build.sh` script
42+
- `<ks_command>`: Depends on kilosort version, options are `ks_compiled`, `ks2_compiled`, `ks2_5_compiled` or `ks3_compiled`
43+
- `[ARGS]`: Currently the only ARG option is the folder where the data are stored (\<docker-data-folder\>)
44+
45+
This kilosort compiled version assumes that there are also two `.mat` files in this data folder:
46+
1. `ops.mat`: Configurations (options) data
47+
2. `chanMap.mat`: ChannelMap data
48+
49+
Both files are automatically generated by spikeinterface when running `run\_sorter`. If you want to run compiled kilosort independently you need to generate them manually (HOW TO still needs to be done)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM ks-matlab-base
2+
3+
RUN chmod 755 /usr/bin/mlrtapp/ks_compiled
4+
ENV PATH="/usr/bin/mlrtapp:${PATH}"
5+
6+
RUN apt-get update -y
7+
RUN apt-get install software-properties-common -y
8+
RUN add-apt-repository ppa:deadsnakes/ppa -y
9+
RUN apt-get install git python3.8 python3.8-dev -y
10+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
11+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
12+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10
13+
RUN apt-get install python3-pip -y
14+
RUN update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
15+
RUN pip install -U pip
16+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Generating Compiled Kilosort Docker Image
2+
3+
## 1. Kilosort Setup
4+
- Git clone or Download Kilosort [source code](https://github.com/cortex-lab/KiloSort)
5+
- Open Matlab
6+
- Compile Kilosort mexfiles:
7+
- Set Matlab's workspace folder to `<git-cloned-path>/KiloSort/CUDA`
8+
- In Matlab console run:
9+
```matlab
10+
>> mexGPUall
11+
```
12+
13+
## 2. Compiling Kilosort as Matlab's Standalone Application
14+
- Set Matlab's workspace folder to `/path/to/spikeinterface-dockerfiles/kilosort_no_license`
15+
- Run `mcc` command with `utils` folder and kilosort path:
16+
```matlab
17+
>> mcc -m kilosort-compiled/ks_compiled.m -a utils -a <git-cloned-path>/KiloSort
18+
```
19+
20+
## 3. Generating Base Docker Image
21+
- To generate the base docker image (called `ks-matlab-base`) with the compiled application, run the following command in Matlab console:
22+
```matlab
23+
>> compiler.package.docker('ks_compiled', 'requiredMCRProducts.txt', 'ImageName', 'ks-matlab-base')
24+
```
25+
26+
- [Optional] Files generated by Matlab Compiler can be deleted:
27+
- In your terminal, go to the `kilosort_no_license` folder in this project:
28+
```bash
29+
$ cd /path/to/spikeinterface-dockerfiles/kilosort_no_license
30+
```
31+
- Run `rm` command:
32+
```bash
33+
$ rm -r \
34+
includedSupportPackages.txt \
35+
ks-matlab-basedocker/ \
36+
ks_compiled \
37+
mccExcludedFiles.log \
38+
readme.txt \
39+
requiredMCRProducts.txt \
40+
run_ks_compiled.sh \
41+
unresolvedSymbols.txt
42+
```
43+
44+
## 4. Extending Base Image and creating final image
45+
46+
The Dockerfile in this folder applies some fixes and updates to the base image generated automatically by Matlab in order to properly run kilosort:
47+
48+
- In your terminal, go to the folder for this project:
49+
```bash
50+
$ cd /path/to/spikeinterface-dockerfiles/kilosort_no_license/kilosort-compiled
51+
```
52+
53+
- Run build script:
54+
```bash
55+
$ source build.sh
56+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
docker build -t spikeinterface/kilosort-compiled-base:0.1.0 .
4+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function ks_master(fpath)
2+
try
3+
set(groot,'defaultFigureVisible', 'off');
4+
5+
% Load channel map file
6+
load(fullfile(fpath, 'chanMap.mat'));
7+
8+
% Load the configuration file, it builds the structure of options (ops)
9+
load(fullfile(fpath, 'ops.mat'));
10+
11+
useGPU = ops.GPU;
12+
13+
% load predefined principal components (visualization only (Phy): used for features)
14+
dd = load('PCspikes2.mat'); % you might want to recompute this from your own data
15+
ops.wPCA = dd.Wi(:,1:7); % PCs
16+
17+
% This part runs the normal Kilosort processing on the simulated data
18+
[rez, DATA, uproj] = preprocessData(ops); % preprocess data and extract spikes for initialization
19+
rez = fitTemplates(rez, DATA, uproj); % fit templates iteratively
20+
rez = fullMPMU(rez, DATA);% extract final spike times (overlapping extraction)
21+
22+
% save python results file for Phy
23+
rezToPhy(rez, fullfile(fpath));
24+
catch
25+
fprintf('----------------------------------------');
26+
fprintf(lasterr());
27+
quit(1);
28+
end
29+
quit(0);
30+
end
31+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
docker push spikeinterface/kilosort-compiled-base:0.1.0
4+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM ks2-matlab-base
2+
3+
RUN chmod 755 /usr/bin/mlrtapp/ks2_compiled
4+
ENV PATH="/usr/bin/mlrtapp:${PATH}"
5+
6+
RUN apt-get update -y
7+
RUN apt-get install software-properties-common -y
8+
RUN add-apt-repository ppa:deadsnakes/ppa -y
9+
RUN apt-get install git python3.8 python3.8-dev -y
10+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
11+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2
12+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 10
13+
RUN apt-get install python3-pip -y
14+
RUN update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
15+
RUN pip install -U pip
16+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Generating Compiled Kilosort2 Docker Image
2+
3+
## 1. Kilosort Setup
4+
- Git clone or Download Kilosort [source code](https://github.com/MouseLand/Kilosort)
5+
- Git checkout to the correct version:
6+
```bash
7+
$ git checkout tags/v2.0
8+
```
9+
- Open Matlab
10+
- Compile Kilosort mexfiles:
11+
- Set Matlab's workspace folder to `<git-cloned-path>/Kilosort/CUDA`
12+
- In Matlab console run:
13+
```matlab
14+
>> mexGPUall
15+
```
16+
17+
## 2. Compiling Kilosort as Matlab's Standalone Application
18+
- Set Matlab's workspace folder to `/path/to/spikeinterface-dockerfiles/kilosort_no_license`
19+
- Run `mcc` command with `utils` folder and kilosort path:
20+
```matlab
21+
>> mcc -m kilosort2-compiled/ks2_compiled.m -a utils -a <git-cloned-path>/Kilosort
22+
```
23+
24+
## 3. Generating Base Docker Image
25+
- To generate the base docker image (called `ks2-matlab-base`) with the compiled application, run the following command in Matlab console:
26+
```matlab
27+
>> compiler.package.docker('ks2_compiled', 'requiredMCRProducts.txt', 'ImageName', 'ks2-matlab-base')
28+
```
29+
30+
- [Optional] Files generated by Matlab Compiler can be deleted:
31+
- In your terminal, go to the `kilosort_no_license` folder in this project:
32+
```bash
33+
$ cd /path/to/spikeinterface-dockerfiles/kilosort_no_license
34+
```
35+
- Run `rm` command:
36+
```bash
37+
$ rm -r \
38+
includedSupportPackages.txt \
39+
ks2-matlab-basedocker/ \
40+
ks2_compiled \
41+
mccExcludedFiles.log \
42+
readme.txt \
43+
requiredMCRProducts.txt \
44+
run_ks2_compiled.sh \
45+
unresolvedSymbols.txt
46+
```
47+
48+
## 4. Extending Base Image and creating final image
49+
50+
The Dockerfile in this folder applies some fixes and updates to the base image generated automatically by Matlab in order to properly run kilosort2:
51+
52+
- In your terminal, go to the folder for this project:
53+
```bash
54+
$ cd /path/to/spikeinterface-dockerfiles/kilosort_no_license/kilosort2-compiled
55+
```
56+
57+
- Run build script:
58+
```bash
59+
$ source build.sh
60+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
docker build -t spikeinterface/kilosort2-compiled-base:0.1.0 .
4+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function ks2_compiled(fpath)
2+
try
3+
set(groot,'defaultFigureVisible', 'off');
4+
5+
% Load channel map file
6+
load(fullfile(fpath, 'chanMap.mat'));
7+
8+
% Load the configuration file, it builds the structure of options (ops)
9+
load(fullfile(fpath, 'ops.mat'));
10+
11+
% preprocess data to create temp_wh.dat
12+
rez = preprocessDataSub(ops);
13+
14+
% time-reordering as a function of drift
15+
rez = clusterSingleBatches(rez);
16+
17+
% main tracking and template matching algorithm
18+
rez = learnAndSolve8b(rez);
19+
20+
% final merges
21+
rez = find_merges(rez, 1);
22+
23+
% final splits by SVD
24+
rez = splitAllClusters(rez, 1);
25+
26+
% final splits by amplitudes
27+
rez = splitAllClusters(rez, 0);
28+
29+
% decide on cutoff
30+
rez = set_cutoff(rez);
31+
32+
fprintf('found %d good units \n', sum(rez.good>0))
33+
34+
fprintf('Saving results to Phy \n')
35+
rezToPhy(rez, fullfile(fpath));
36+
catch
37+
fprintf('----------------------------------------');
38+
fprintf(lasterr());
39+
quit(1);
40+
end
41+
quit(0);
42+
end

0 commit comments

Comments
 (0)