Python routines dealing with Neuropixels data.
Project description
Npyx: loading, processing and plotting Neuropixels data
Npyx is a pure python library built for electrophysiologists using Neuropixels. It features a suite of core utility functions for loading, processing and plotting Neuropixels data.
This package results from the needs of an experimentalist who could not stand MATLAB, hence wrote himself a suite of functions to emotionally bear doing neuroscience. There isn't any dedicated preprint available yet, so if you enjoy this package and use it for your research, please cite this paper. Cheers!
Documentation:
Npyx works in harmony with the data formatting employed by SpikeGLX used in combination with Kilosort and Phy.
Most npyx functions take at least one input: dp
, which is the path to your Kilosort/phy dataset. You can find a full description of the structure of such datasets on phy documentation.
More precisely, every function requires the files myrecording.ap.meta
, spike_times.npy
and spike_clusters.npy
. Then particular functions will require particular files: loading waveforms with npyx.spk_wvf.wvf
or extracting your sync channel with npyx.io.get_npix_sync
require the raw data myrecording.ap.bin
, extracting the spike sorted group of your units cluster_groups.tsv
and so on.
Example use cases are:
Load synchronization channel
from npyx.io import get_npix_sync
dp = 'path/to/dataset'
onsets, offsets = get_npix_sync(dp)
# onsets/offsets are dictionnaries
# whose keys are ids of sync channel where signal was detected,
# and values the times of up (onsets) or down (offsets) threshold crosses in seconds.
Get good units from dataset
from npyx.gl import get_units
dp = 'path/to/dataset'
units = get_units(dp, quality='good')
Load spikes from unit u
from npyx.spk_t import trn
dp = 'path/to/dataset'
u=234
t = trn(dp, u) # gets all spikes from unit 234, in samples
Load waveforms from unit u
from npyx.io import read_spikeglx_meta
from npyx.spk_t import ids, trn
from npyx.spk_wvf import get_peak_chan, wvf, templates
dp = 'path/to/dataset'
u=234
# returns a random sample of 100 waveforms from unit 234, in uV, across 384 channels
waves = wvf(dp, u, n_waveforms=100, t_waveforms=82) # return array of shape (100, 82, 384) by default
# Get the unit peak channel (channel with the biggest amplitude)
peak_chan = get_peak_chan(dp,u)
# extract the waveforms located on peak channel
w=waves[:,:,peak_chan]
# Extract waveforms of spikes occurring between
# 900 and 1000s in the recording, because that's when your mouse scratched its butt
fs=read_spikeglx_meta['sRateHz']
t=trn(dp,u)/fs # convert in s
ids=ids(dp,u)[(t>900)&(t<1000)]
waves = wvf(dp, u, spike_ids=ids)
# If you want to load the templates instead (lighter)
temp = templates(dp,u) # return array of shape (n_templates, 82, n_channels)
Compute auto/crosscorrelogram between 2 units
from npyx.corr import ccg
dp = 'path/to/dataset'
# returns ccg between 234 and 92 with a binsize of 0.2 and a window of 80
c = ccg(dp, [234,92], cbin=0.2, cwin=80)
Plot correlograms and waveforms from unit u
# all plotting functions return matplotlib figures
from npyx.plot import plot_wvf
dp = 'path/to/dataset'
u=234
# plot waveform, 2.8ms around center, on 8 channels around peak channel,
# with no single waveforms in the background (sample_lines)
fig = plot_wvf(dp, u, Nchannels=8, t_waveforms=2.8, sample_lines=0)
# plot ccg between 234 and 92
fig = plot_ccg(dp, [u,92], cbin=0.2, cwin=80)
Merge datasets acquired on two probes simultaneously
# The three recordings need to include the same sync channel.
from npyx.Prophyler import Prophyler
dps = ['same_folder/lateralprobe_dataset',
'same_folder/medialprobe_dataset',
'same_folder/anteriorprobe_dataset']
probenames = ['lateral','medial','anterior']
dp_dict = {p:dp for p, dp in zip(dps, probenames)}
multipro = Prophyler(dp_dic)
dp=multipro.dp_pro
# This will merge the 3 datasets (only relevant information, not the raw data) in a new folder at
# same_folder/prophyler_lateralprobe_dataset_medialprobe_dataset_anteriorprobe_dataset
# which can then be used as if it were a single dataset by all npyx functions.
# The only difference is that units now need to be called as floats, of format unit_id.dataset_id.
# lateralprobe, medial probe and anteriorprobe dataset_ids will be 0,1 and 2.
t = trn(dp, 92.1) # get spikes of unit 92 in dataset 1 i.e. medialprobe
fig=plot_ccg(dp,[10,0,92.1,cbin=0.2,cwin=80]) # compute CCG between 2 units across datasets
There isn't any better doc atm - email Maxime Beau (PhD Hausser lab, UCL at time of writing) if you have any questions!
Installation:
Using a conda environment is very much advised. Instructions here: manage conda environments
Npyx supports Python 3.7+.
- as a user
- from pip (normally up to date)
conda create -n my_env python=3.7 conda activate my_env pip install npyx python -c 'import npyx' # should not return any error # If it does, install any missing dependencies with pip (hopefully none!)
- from the remote repository (always up to date - still private at time of writing, pip is a prerelease)
conda activate env_name pip install git+https://github.com/Npix-routines/NeuroPyxels@master
- as a superuser (recommended if plans to work on it/regularly pull upgrades)
Tip: in an ipython/jupyter session, use
%load_ext autoreload
then%autoreload
to make your local edits active in your session without having to restart your kernel. Amazing for development.conda activate my_env cd path/to/save_dir # any directory where your code will be accessible by your editor and safe. NOT downloads folder. git clone https://github.com/Npix-routines/NeuroPyxels cd NeuroPyxels python setup.py develop # this will create an egg link to save_dir, which means that you do not need to reinstall the package each time you pull an udpate from github.
and pull every now and then:conda activate env_name cd path/to/save_dir/NeuroPyxels git pull # And that's it, thanks to the egg link no need to reinstall the package!
Developer cheatsheet
Useful link to create a python package from a git repository
Push local updates to github:
# DO NOT DO THAT WITHOUT CHECKING WITH MAXIME FIRST
# ONLY ON DEDICATED BRANCH CREATED WITH THE GITHUB BROWSER INTERFACE
cd path/to/save_dir/NeuroPyxels
git checkout DEDICATED_BRANCH_NAME # ++++++ IMPORTANT
git add.
git commit -m "commit details - try to be specific"
git push origin DEDICATED_BRANCH_NAME # ++++++ IMPORTANT
# Then pull request to master branch using the online github green button! Do not forget this last step, to allow the others repo to sync.
Push local updates to PyPI (only Maxime)
First change the version in ./setup.py in a text editor
setup(name='npyx',
version='1.0',... # change to 1.1, 1.1.1...
Then delete the old distribution files before re-generating them for the new version using twine:
rm -r ./dist
rm -r ./build
rm -r ./npyx.egg-info
python setup.py sdist bdist_wheel # this will generate version 1.1 wheel without overwriting version 1.0 wheel in ./dist
Before pushing them to PyPI (older versions are saved online!)
twine upload dist/*
Uploading distributions to https://upload.pypi.org/legacy/
Enter your username: your-username
Enter your password: ****
Uploading npyx-1.1-py3-none-any.whl
100%|████████████████████████████████████████████████████████| 156k/156k [00:01<00:00, 96.8kB/s]
Uploading npyx-1.1.tar.gz
100%|█████████████████████████████████████████████████████████| 150k/150k [00:01<00:00, 142kB/s]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file npyx-1.6.5.tar.gz
.
File metadata
- Download URL: npyx-1.6.5.tar.gz
- Upload date:
- Size: 192.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fa7f60bafffd65f7bf6ceb42609b0c4251c7830efd6df8b9d091f6363e62970 |
|
MD5 | 5b4776cd3096d39c9b7a4b749599af11 |
|
BLAKE2b-256 | 41abcfb6ef836a133fcc3eb278829718a040807074d716fd1c37f18ac39208f8 |
File details
Details for the file npyx-1.6.5-py3-none-any.whl
.
File metadata
- Download URL: npyx-1.6.5-py3-none-any.whl
- Upload date:
- Size: 194.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6d5bb39bf8e561902421a21965b8d534acf462153301d6053ddc7738f37ea7c |
|
MD5 | 77a8be31f1eb10d0b6f07824bd22657e |
|
BLAKE2b-256 | b11422f40659715b0733455947f337693bb7fc5c4f276376b780b328ecb97fe1 |