Skip to content

Commit

Permalink
Tentative fix for KiloSort 2.5: use 0 instead of NaN with empty templ…
Browse files Browse the repository at this point in the history
…ates
  • Loading branch information
rossant committed Jan 7, 2021
1 parent 38ff8e2 commit 3e4dfb9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion phylib/io/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,13 @@ def _load_templates(self):
try:
path = self._find_path(
'templates.npy', 'templates.waveforms.npy', 'templates.waveforms.*.npy')
data = self._read_array(path, mmap_mode='r')
data = self._read_array(path, mmap_mode='r+')
data = np.atleast_3d(data)
assert data.ndim == 3
assert data.dtype in (np.float32, np.float64)
# WARNING: this will load the full array in memory, might cause memory problems
empty_templates = np.all(np.all(np.isnan(data), axis=1), axis=1)
data[empty_templates, ...] = 0
n_templates, n_samples, n_channels_loc = data.shape
except IOError:
return
Expand Down Expand Up @@ -818,6 +821,7 @@ def _find_best_channels(self, template, amplitude_threshold=None):
# Compute the template amplitude on each channel.
assert template.ndim == 2 # shape: (n_samples, n_channels)
amplitude = template.max(axis=0) - template.min(axis=0)
assert not np.all(np.isnan(amplitude)), "Template is all NaN!"
assert amplitude.ndim == 1 # shape: (n_channels,)
# Find the peak channel.
best_channel = np.argmax(amplitude)
Expand Down

1 comment on commit 3e4dfb9

@linusmartensson
Copy link

Choose a reason for hiding this comment

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

Alternative suggestion: Set mmap_mode=None
This worked for us locally, and has the same memory concerns as this solution, but reduces complexity in the change.

Please sign in to comment.