Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No curation ephys AND kilosort triggering #54

Closed
wants to merge 136 commits into from
Closed
Show file tree
Hide file tree
Changes from 126 commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
f740aef
added NWB export
Aug 18, 2021
042cc46
copied `ephys` to `ephys-no-curation`, added `recording_duration`, make
Sep 19, 2021
a39a9b1
remove `Curation`
Sep 19, 2021
590310e
make `clustering_output_dir` user-input optional, auto infer
Sep 19, 2021
adfad95
minor bug fix
Sep 19, 2021
b7c0845
improve kilosort loading routine - add `validate()` method
Sep 20, 2021
de84ce0
helper for `ProbeInsertion` - `auto_generate_entries(session_key)`
Sep 20, 2021
4397dd7
no-curation, store processed data in user-specified `processed_data_d…
Sep 21, 2021
5420ae0
using `find_full_path` for session dir as well - improve robustness
Sep 21, 2021
70e93b3
Update ephys_no_curation.py
Sep 21, 2021
a7e7554
bugfix
Sep 22, 2021
0d56456
automate ClusteringTask insertion
Sep 23, 2021
047bfa1
triggering Kilosort with ecephys_spike_sorting package
Sep 30, 2021
60105da
remove `_required_packages_paths`
Sep 30, 2021
a9326ea
minor stylistic improvements
Sep 30, 2021
149ef3f
Apply suggestions from code review
Sep 30, 2021
9f98195
typo fix
Oct 1, 2021
a24bd1a
Merge branch 'no-curation' of https://github.com/ttngu207/element-arr…
Oct 1, 2021
ba28637
handles a weird windows/unix path incompatibility (even with pathlib)
Oct 1, 2021
5cdc994
fix missing `clustering_method`
Oct 1, 2021
6c646bb
flag to create spike sorting output dir
Oct 1, 2021
dd01fd2
Update kilosort_trigger.py
Oct 1, 2021
f699ce7
minor tweak/improvements in kilosort triggering
Oct 1, 2021
6bf0eb1
bugfix in triggering ecephys_spike_sorting
Oct 1, 2021
6788180
handle cases where `fileTimeSecs` is not available
Oct 1, 2021
d63561f
bugfix
Oct 2, 2021
375e437
missing generate module json
Oct 2, 2021
c593baf
change default `noise_template_use_rf` to False
Oct 2, 2021
69c5e51
minor bugfix
Oct 4, 2021
a6cae12
add kilosort 2.5 as default content
Oct 4, 2021
a0ea9f7
make variable naming consistent
Oct 4, 2021
acdab12
include `clustering_method` into the calculation of `param_set_hash`
Oct 4, 2021
38c5be6
updating `kilosort_repository` depending on which KSVer to be used
Oct 4, 2021
28c4452
bugfix
Oct 4, 2021
94aade7
bugfix, convert path to string
Oct 4, 2021
ae966aa
log the folder creation
Oct 5, 2021
b9f4e92
add debugging lines
Oct 5, 2021
044c389
bugfix - no dir created
Oct 5, 2021
0f0c212
Update kilosort.py
Oct 6, 2021
67341d8
new version 0.1.0b1
Oct 6, 2021
c2ee64f
improve clusteringtask and waveform ingestion routine
Oct 7, 2021
dc7ddd9
minor cleanup
Oct 7, 2021
408532c
stage tracking and resumable kilosort run
Oct 7, 2021
55bec01
minor bugfix
Oct 7, 2021
f34e95d
bugfix logging for kilosort triggering
Oct 7, 2021
19b704b
duration for each module run
Oct 7, 2021
beaf765
check `ap.bin` file validity before triggering kilosort (based on fil…
Oct 8, 2021
74e3ead
Apply suggestions from code review
Oct 11, 2021
e8ffe17
stylistic improvements, addressing code review comments
Oct 11, 2021
d07f830
handle NWB export with multiple curated clusterings from one session
Nov 2, 2021
19616ef
Update nwb.py
Nov 2, 2021
09c1e60
minor bugfix
Nov 2, 2021
3ebdf23
Merge pull request #2 from ttngu207/nwb-export
Nov 2, 2021
b25f065
add nwb export to `no-curation` ephys
Nov 2, 2021
49ca0be
first prototype for running the ecephys_pipeline with OpenEphys
Nov 9, 2021
199a2ba
bugfix for running kilosort for Open Ephys data
Nov 9, 2021
58b5984
specify additional recording-info as part of the `params`
Nov 11, 2021
ddc3b94
add `neuropixels UHD` probe type
Nov 18, 2021
df599fb
triggering kilosort analysis for open-ephys
Nov 18, 2021
819ff19
first prototype for pykilosort
Nov 19, 2021
81d99c8
use_C_waves=False for OpenEphys
Nov 19, 2021
96931a4
using fork of pyopenephys
Nov 22, 2021
b6f8f99
minor bugfix in running pykilosort
Nov 22, 2021
4250220
update openephys loader
Nov 22, 2021
af2b18b
fix package requirement formatting error
Nov 22, 2021
e325a30
handles format differences between npx1 vs 3A
Nov 22, 2021
db3027b
supporting `neuropixels UHD` in `ephys.EphysRecording`
Nov 23, 2021
f55a6a7
bugfix in electrode sites design for Neuropixels UHD probe
Nov 23, 2021
d105419
search recording channels for Open Ephys based on channel names
Nov 23, 2021
0d16e7e
bugfix openephys loader
Nov 23, 2021
c213325
bugfix - extract recording datetime (instead of using experiment date…
Nov 23, 2021
adffe34
bugfix, timedelta as seconds
Nov 23, 2021
43d6614
handles extraction of `connected` channels for NP_PROBE format in Ope…
Nov 23, 2021
da10c66
clusters extraction - check `cluster_group.tsv` and `cluster_KSLabel.…
Nov 25, 2021
02069c9
bugfix in triggering pykilosort
Nov 25, 2021
142459d
handle missing `sample_rate` from pykilosort params.py
Nov 25, 2021
1c39568
for pykilosort's probe, provide both Nchan and NchanTOT
Nov 25, 2021
2022e91
openephys loader - handles signalchain and processor as single elemen…
Nov 26, 2021
02c4b67
handles multi-probe for older OE version
Nov 26, 2021
8dff08b
NWB export fix, specifying which ephys module
Nov 29, 2021
6107e8e
version bump
Nov 29, 2021
dae36d1
fix NWB export - null `waveforms` - back to version 0.1.0b1
Nov 29, 2021
8657d58
version bump
Nov 29, 2021
aa47c8a
include probe as part of the electrode_group name for uniqueness
Nov 29, 2021
31bba8c
bugfix in assigning unit electrode indices
ttngu207 Dec 7, 2021
f9a4754
Merge pull request #3 from ttngu207/nwb-export
Dec 7, 2021
f2881ca
all three ephys files up to date
A-Baji Jan 24, 2022
c56cd18
applied requested changes
A-Baji Jan 24, 2022
9dd6b42
Merge pull request #4 from A-Baji/no-curation
Jan 24, 2022
4951b39
DEPRECATING NWB EXPORT
Jan 26, 2022
b4832ea
Merge branch 'main' of https://github.com/datajoint/element-array-eph…
Jan 26, 2022
d66368c
minor bugfix
Jan 27, 2022
779b2fb
name tweak
Jan 27, 2022
f4052cc
Apply suggestions from code review
Jan 27, 2022
03cab02
rename `sess_dir` -> `session_dir`
Jan 31, 2022
cb1a041
Update requirements.txt
Jan 31, 2022
7320f9f
Update requirements.txt
Jan 31, 2022
f0b3d4a
Update requirements.txt
Jan 31, 2022
bdcfa46
handle older open ephys format for single probe
Feb 18, 2022
83649f5
remove unintended prototyping work
Feb 18, 2022
c2f8aea
delete nwb export - rename `ephys` module -> `ephys_acute`
Feb 25, 2022
e0a9a4f
Update __init__.py
Mar 14, 2022
769de13
bugfix
Mar 16, 2022
39c8579
configurable `paramset_idx` for auto ClusteringTask generation
Mar 25, 2022
85c7c8b
Update openephys.py
May 12, 2022
11a12ba
update openephys loader - handle new open ephys format
May 20, 2022
cd5fe70
handles new probe naming in latest Open Ephys format
May 20, 2022
af864d7
Apply suggestions from code review
May 23, 2022
47babf3
added assertion - safeguard against failed loading of continuous.dat
May 23, 2022
7fecff1
Merge branch 'no-curation' of https://github.com/ttngu207/element-arr…
May 23, 2022
ddd4095
Merge remote-tracking branch 'upstream/run_kilosort' into no-curation
May 23, 2022
a578d85
Update requirements.txt
May 23, 2022
f4b917d
`kilosort2` also as part of the `contents` for ClusteringMethod
May 23, 2022
f11e016
bugfix for LFP electrode mapping
May 24, 2022
07604e2
update open ephys loader to handle "STREAM" in latest format
May 26, 2022
4e367d7
added loading of electrode location for new openephys format
Jun 1, 2022
67039ac
more robust loading of openephys format
Jun 3, 2022
9a59e57
improve logic for running kilosort modules in a resumable fashion
Jun 7, 2022
b97566e
code cleanup, minor bugfix
Jun 7, 2022
3f1ee37
bugfix, match new implementation for openephys
Jun 9, 2022
0c77826
improve kilosort calls, handle spaces in paths
Jun 30, 2022
b71b459
remove space escaping character
Jun 30, 2022
a3c5c2f
improve error message
Jul 19, 2022
fd331bd
Merge branch 'no-curation' of https://github.com/ttngu207/element-arr…
Jul 19, 2022
b2bd0ee
implement data compression using `mtscomp` for openephys and spikeglx…
Jul 25, 2022
97f3d21
garbage collect openephys objects
Aug 12, 2022
d8aea04
garbage collect openephys objects
Aug 15, 2022
f9f18d0
Update kilosort_triggering.py
Aug 30, 2022
5905392
bugfix in running catgt
Sep 2, 2022
987231b
enable catgt run
Sep 2, 2022
aed42ca
bugfix
Sep 2, 2022
fbdbe24
bugfix
Sep 2, 2022
35da39b
`missing_ok` arg only available in python 3.8+
Sep 2, 2022
c33d1b0
bugfix - path search for catgt output
Sep 2, 2022
f60ba3d
improve error handling
Sep 2, 2022
71d87ae
Update kilosort_triggering.py
Sep 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.


## [0.1.0b4] - 2021-11-29
### Added
+ Processing with Kilosort and pyKilosort for Open Ephys and SpikeGLX


## [0.1.0b0] - 2021-05-07
### Added
+ First beta release
Expand Down
16 changes: 14 additions & 2 deletions element_array_ephys/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# ephys_acute as default
import element_array_ephys.ephys_acute as ephys
import datajoint as dj
import logging
import os


dj.config['enable_python_native_blobs'] = True


def get_logger(name):
log = logging.getLogger(name)
log.setLevel(os.getenv('LOGLEVEL', 'INFO'))
return log

from . import ephys_acute as ephys
385 changes: 325 additions & 60 deletions element_array_ephys/ephys_acute.py

Large diffs are not rendered by default.

318 changes: 265 additions & 53 deletions element_array_ephys/ephys_chronic.py

Large diffs are not rendered by default.

988 changes: 988 additions & 0 deletions element_array_ephys/ephys_no_curation.py

Large diffs are not rendered by default.

95 changes: 40 additions & 55 deletions element_array_ephys/probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def activate(schema_name, *, create_schema=True, create_tables=True):
schema.activate(schema_name, create_schema=create_schema, create_tables=create_tables)

# Add neuropixels probes
for probe_type in ('neuropixels 1.0 - 3A', 'neuropixels 1.0 - 3B',
for probe_type in ('neuropixels 1.0 - 3A', 'neuropixels 1.0 - 3B', 'neuropixels UHD',
Copy link
Collaborator

@kabilar kabilar Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for probe_type in ('neuropixels 1.0 - 3A', 'neuropixels 1.0 - 3B', 'neuropixels UHD',
for probe_type in ('neuropixels Phase 3A', 'neuropixels 1.0 - Phase 3B', 'neuropixels UHD',

Copy link
Collaborator

@kabilar kabilar Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor fix. Phase 3A is different than Neuropixels 1.0 which is also called Phase 3B.

Copy link
Collaborator

@kabilar kabilar Mar 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'neuropixels 2.0 - SS', 'neuropixels 2.0 - MS'):
ProbeType.create_neuropixels_probe(probe_type)

Expand Down Expand Up @@ -46,15 +46,38 @@ class Electrode(dj.Part):
def create_neuropixels_probe(probe_type='neuropixels 1.0 - 3A'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def create_neuropixels_probe(probe_type='neuropixels 1.0 - 3A'):
def create_neuropixels_probe(probe_type='neuropixels Phase 3A'):

"""
Create `ProbeType` and `Electrode` for neuropixels probes:
1.0 (3A and 3B), 2.0 (SS and MS)
+ neuropixels 1.0 - 3A
+ neuropixels 1.0 - 3B
Comment on lines +49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ neuropixels 1.0 - 3A
+ neuropixels 1.0 - 3B
+ neuropixels Phase 3A
+ neuropixels 1.0 - Phase 3B

+ neuropixels UHD
+ neuropixels 2.0 - SS
+ neuropixels 2.0 - MS

For electrode location, the (0, 0) is the
bottom left corner of the probe (ignore the tip portion)
Electrode numbering is 1-indexing
"""

neuropixels_probes_config = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to double check these numbers. I have been going through the manuscripts. Do you have a source that compiles all of these numbers? Thanks.

'neuropixels 1.0 - 3A': dict(site_count=960, col_spacing=32, row_spacing=20,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'neuropixels 1.0 - 3A': dict(site_count=960, col_spacing=32, row_spacing=20,
'neuropixels Phase 3A': dict(site_count=960, col_spacing=32, row_spacing=20,

white_spacing=16, col_count=2,
shank_count=1, shank_spacing=0),
'neuropixels 1.0 - 3B': dict(site_count=960, col_spacing=32, row_spacing=20,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'neuropixels 1.0 - 3B': dict(site_count=960, col_spacing=32, row_spacing=20,
'neuropixels 1.0 - Phase 3B': dict(site_count=960, col_spacing=32, row_spacing=20,

white_spacing=16, col_count=2,
shank_count=1, shank_spacing=0),
'neuropixels UHD': dict(site_count=384, col_spacing=6, row_spacing=6,
white_spacing=0, col_count=8,
shank_count=1, shank_spacing=0),
'neuropixels 2.0 - SS': dict(site_count=1280, col_spacing=32, row_spacing=15,
white_spacing=0, col_count=2,
shank_count=1, shank_spacing=250),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
shank_count=1, shank_spacing=250),
shank_count=1, shank_spacing=0),

'neuropixels 2.0 - MS': dict(site_count=1280, col_spacing=32, row_spacing=15,
white_spacing=0, col_count=2,
shank_count=4, shank_spacing=250)
}

def build_electrodes(site_count, col_spacing, row_spacing,
white_spacing, col_count=2,
shank_count=1, shank_spacing=250):
white_spacing, col_count,
shank_count, shank_spacing):
"""
:param site_count: site count per shank
:param col_spacing: (um) horrizontal spacing between sites
Expand All @@ -66,14 +89,15 @@ def build_electrodes(site_count, col_spacing, row_spacing,
:return:
"""
row_count = int(site_count / col_count)
x_coords = np.tile([0, 0 + col_spacing], row_count)
x_white_spaces = np.tile([white_spacing, white_spacing, 0, 0], int(row_count / 2))
x_coords = np.tile(np.arange(0, col_spacing * col_count, col_spacing), row_count)
y_coords = np.repeat(np.arange(row_count) * row_spacing, col_count)

x_coords = x_coords + x_white_spaces
y_coords = np.repeat(np.arange(row_count) * row_spacing, 2)
if white_spacing:
x_white_spaces = np.tile([white_spacing, white_spacing, 0, 0], int(row_count / 2))
x_coords = x_coords + x_white_spaces

shank_cols = np.tile([0, 1], row_count)
shank_rows = np.repeat(range(row_count), 2)
shank_cols = np.tile(range(col_count), row_count)
shank_rows = np.repeat(range(row_count), col_count)

npx_electrodes = []
for shank_no in range(shank_count):
Expand All @@ -88,51 +112,12 @@ def build_electrodes(site_count, col_spacing, row_spacing,

return npx_electrodes

# ---- 1.0 3A ----
if probe_type == 'neuropixels 1.0 - 3A':
electrodes = build_electrodes(site_count=960, col_spacing=32, row_spacing=20,
white_spacing=16, col_count=2)

probe_type = {'probe_type': 'neuropixels 1.0 - 3A'}
with ProbeType.connection.transaction:
ProbeType.insert1(probe_type, skip_duplicates=True)
ProbeType.Electrode.insert([{**probe_type, **e} for e in electrodes],
skip_duplicates=True)

# ---- 1.0 3B ----
if probe_type == 'neuropixels 1.0 - 3B':
electrodes = build_electrodes(site_count=960, col_spacing=32, row_spacing=20,
white_spacing=16, col_count=2)

probe_type = {'probe_type': 'neuropixels 1.0 - 3B'}
with ProbeType.connection.transaction:
ProbeType.insert1(probe_type, skip_duplicates=True)
ProbeType.Electrode.insert([{**probe_type, **e} for e in electrodes],
skip_duplicates=True)

# ---- 2.0 Single shank ----
if probe_type == 'neuropixels 2.0 - SS':
electrodes = build_electrodes(site_count=1280, col_spacing=32, row_spacing=15,
white_spacing=0, col_count=2,
shank_count=1, shank_spacing=250)

probe_type = {'probe_type': 'neuropixels 2.0 - SS'}
with ProbeType.connection.transaction:
ProbeType.insert1(probe_type, skip_duplicates=True)
ProbeType.Electrode.insert([{**probe_type, **e} for e in electrodes],
skip_duplicates=True)

# ---- 2.0 Multi shank ----
if probe_type == 'neuropixels 2.0 - MS':
electrodes = build_electrodes(site_count=1280, col_spacing=32, row_spacing=15,
white_spacing=0, col_count=2,
shank_count=4, shank_spacing=250)

probe_type = {'probe_type': 'neuropixels 2.0 - MS'}
with ProbeType.connection.transaction:
ProbeType.insert1(probe_type, skip_duplicates=True)
ProbeType.Electrode.insert([{**probe_type, **e} for e in electrodes],
skip_duplicates=True)
electrodes = build_electrodes(**neuropixels_probes_config[probe_type])
probe_type = {'probe_type': probe_type}
with ProbeType.connection.transaction:
ProbeType.insert1(probe_type, skip_duplicates=True)
ProbeType.Electrode.insert([{**probe_type, **e} for e in electrodes],
skip_duplicates=True)


@schema
Expand Down
43 changes: 29 additions & 14 deletions element_array_ephys/readers/kilosort.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Kilosort:

kilosort_files = [
_kilosort_core_files = [
'params.py',
'amplitudes.npy',
'channel_map.npy',
Expand All @@ -22,47 +22,60 @@ class Kilosort:
'similar_templates.npy',
'spike_templates.npy',
'spike_times.npy',
'spike_times_sec.npy',
'spike_times_sec_adj.npy',
'template_features.npy',
'template_feature_ind.npy',
'templates.npy',
'templates_ind.npy',
'whitening_mat.npy',
'whitening_mat_inv.npy',
'spike_clusters.npy',
'spike_clusters.npy'
]

_kilosort_additional_files = [
'spike_times_sec.npy',
'spike_times_sec_adj.npy',
'cluster_groups.csv',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'cluster_groups.csv',
'cluster_group.tsv',

'cluster_KSLabel.tsv'
]

# keys to self.files, .data are file name e.g. self.data['params'], etc.
kilosort_keys = [path.splitext(kilosort_file)[0] for kilosort_file in kilosort_files]
kilosort_files = _kilosort_core_files + _kilosort_additional_files

def __init__(self, kilosort_dir):
self._kilosort_dir = pathlib.Path(kilosort_dir)
self._files = {}
self._data = None
self._clusters = None

params_filepath = kilosort_dir / 'params.py'

if not params_filepath.exists():
raise FileNotFoundError(f'No Kilosort output found in: {kilosort_dir}')
self.validate()

params_filepath = kilosort_dir / 'params.py'
self._info = {'time_created': datetime.fromtimestamp(params_filepath.stat().st_ctime),
'time_modified': datetime.fromtimestamp(params_filepath.stat().st_mtime)}

@property
def data(self):
if self._data is None:
self._stat()
self._load()
return self._data

@property
def info(self):
return self._info

def _stat(self):
def validate(self):
"""
Check if this is a valid set of kilosort outputs - i.e. all crucial files exist
"""
missing_files = []
for f in Kilosort._kilosort_core_files:
full_path = self._kilosort_dir / f
if not full_path.exists():
missing_files.append(f)
if missing_files:
raise FileNotFoundError(f'Kilosort files missing in ({self._kilosort_dir}):'
f' {missing_files}')

def _load(self):
self._data = {}
for kilosort_filename in Kilosort.kilosort_files:
kilosort_filepath = self._kilosort_dir / kilosort_filename
Expand Down Expand Up @@ -91,16 +104,18 @@ def _stat(self):
self._data[base] = (np.reshape(d, d.shape[0])
if d.ndim == 2 and d.shape[1] == 1 else d)

self._data['channel_map'] = self._data['channel_map'].flatten()

# Read the Cluster Groups
for cluster_pattern, cluster_col_name in zip(['cluster_groups.*', 'cluster_KSLabel.*'],
for cluster_pattern, cluster_col_name in zip(['cluster_group.*', 'cluster_KSLabel.*'],
['group', 'KSLabel']):
try:
cluster_file = next(self._kilosort_dir.glob(cluster_pattern))
except StopIteration:
pass
else:
cluster_file_suffix = cluster_file.suffix
assert cluster_file_suffix in ('.csv', '.tsv', '.xlsx')
assert cluster_file_suffix in ('.tsv', '.xlsx')
break
else:
raise FileNotFoundError(
Expand Down
Loading