Skip to content

Commit a01530c

Browse files
author
Thinh Nguyen
authored
Merge pull request #78 from kabilar/main
Fix for case where `pc_features.npy` does not exist
2 parents a20ab9b + 6b6f448 commit a01530c

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and
44
[Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.
55

6+
## [0.1.2] - 2022-06-09
7+
8+
+ Bugfix - Handle case where `pc_features.npy` does not exist.
9+
610
## [0.1.1] - 2022-06-01
711

812
+ Add - Secondary attributes to `PreClusterParamSteps` table

element_array_ephys/ephys_acute.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class Unit(dj.Part):
503503
spike_count: int # how many spikes in this recording for this unit
504504
spike_times: longblob # (s) spike times of this unit, relative to the start of the EphysRecording
505505
spike_sites : longblob # array of electrode associated with each spike
506-
spike_depths : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
506+
spike_depths=null : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
507507
"""
508508

509509
def make(self, key):
@@ -551,7 +551,7 @@ def make(self, key):
551551
'spike_times': unit_spike_times,
552552
'spike_count': spike_count,
553553
'spike_sites': spike_sites[kilosort_dataset.data['spike_clusters'] == unit],
554-
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit]})
554+
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit] if spike_depths else None})
555555

556556
self.insert1(key)
557557
self.Unit.insert([{**key, **u} for u in units])

element_array_ephys/ephys_chronic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ class Unit(dj.Part):
502502
spike_count: int # how many spikes in this recording for this unit
503503
spike_times: longblob # (s) spike times of this unit, relative to the start of the EphysRecording
504504
spike_sites : longblob # array of electrode associated with each spike
505-
spike_depths : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
505+
spike_depths=null : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
506506
"""
507507

508508
def make(self, key):
@@ -550,7 +550,7 @@ def make(self, key):
550550
'spike_times': unit_spike_times,
551551
'spike_count': spike_count,
552552
'spike_sites': spike_sites[kilosort_dataset.data['spike_clusters'] == unit],
553-
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit]})
553+
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit] if spike_depths else None})
554554

555555
self.insert1(key)
556556
self.Unit.insert([{**key, **u} for u in units])

element_array_ephys/ephys_precluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ class Unit(dj.Part):
637637
spike_count: int # how many spikes in this recording for this unit
638638
spike_times: longblob # (s) spike times of this unit, relative to the start of the EphysRecording
639639
spike_sites : longblob # array of electrode associated with each spike
640-
spike_depths : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
640+
spike_depths=null : longblob # (um) array of depths associated with each spike, relative to the (0, 0) of the probe
641641
"""
642642

643643
def make(self, key):
@@ -685,7 +685,7 @@ def make(self, key):
685685
'spike_times': unit_spike_times,
686686
'spike_count': spike_count,
687687
'spike_sites': spike_sites[kilosort_dataset.data['spike_clusters'] == unit],
688-
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit]})
688+
'spike_depths': spike_depths[kilosort_dataset.data['spike_clusters'] == unit] if spike_depths else None})
689689

690690
self.insert1(key)
691691
self.Unit.insert([{**key, **u} for u in units])

element_array_ephys/readers/kilosort.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def _stat(self):
9191
self._data[base] = (np.reshape(d, d.shape[0])
9292
if d.ndim == 2 and d.shape[1] == 1 else d)
9393

94+
self._data['channel_map'] = self._data['channel_map'].flatten()
95+
9496
# Read the Cluster Groups
9597
for cluster_pattern, cluster_col_name in zip(['cluster_groups.*', 'cluster_KSLabel.*'],
9698
['group', 'KSLabel']):
@@ -127,19 +129,23 @@ def get_best_channel(self, unit):
127129

128130
def extract_spike_depths(self):
129131
""" Reimplemented from https://github.com/cortex-lab/spikes/blob/master/analysis/ksDriftmap.m """
130-
ycoords = self.data['channel_positions'][:, 1]
131-
pc_features = self.data['pc_features'][:, 0, :] # 1st PC only
132-
pc_features = np.where(pc_features < 0, 0, pc_features)
133-
134-
# ---- compute center of mass of these features (spike depths) ----
135-
136-
# which channels for each spike?
137-
spk_feature_ind = self.data['pc_feature_ind'][self.data['spike_templates'], :]
138-
# ycoords of those channels?
139-
spk_feature_ycoord = ycoords[spk_feature_ind]
140-
# center of mass is sum(coords.*features)/sum(features)
141-
self._data['spike_depths'] = (np.sum(spk_feature_ycoord * pc_features**2, axis=1)
142-
/ np.sum(pc_features**2, axis=1))
132+
133+
if 'pc_features' in self.data:
134+
ycoords = self.data['channel_positions'][:, 1]
135+
pc_features = self.data['pc_features'][:, 0, :] # 1st PC only
136+
pc_features = np.where(pc_features < 0, 0, pc_features)
137+
138+
# ---- compute center of mass of these features (spike depths) ----
139+
140+
# which channels for each spike?
141+
spk_feature_ind = self.data['pc_feature_ind'][self.data['spike_templates'], :]
142+
# ycoords of those channels?
143+
spk_feature_ycoord = ycoords[spk_feature_ind]
144+
# center of mass is sum(coords.*features)/sum(features)
145+
self._data['spike_depths'] = (np.sum(spk_feature_ycoord * pc_features**2, axis=1)
146+
/ np.sum(pc_features**2, axis=1))
147+
else:
148+
self._data['spike_depths'] = None
143149

144150
# ---- extract spike sites ----
145151
max_site_ind = np.argmax(np.abs(self.data['templates']).max(axis=1), axis=1)

element_array_ephys/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""Package metadata."""
2-
__version__ = '0.1.1'
2+
__version__ = '0.1.2'

0 commit comments

Comments
 (0)