DARTsort 0.3.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dartsort/__init__.py +68 -0
- dartsort/cli.py +105 -0
- dartsort/clustering/__init__.py +3 -0
- dartsort/clustering/cluster_util.py +392 -0
- dartsort/clustering/clustering.py +765 -0
- dartsort/clustering/clustering_features.py +202 -0
- dartsort/clustering/density.py +1299 -0
- dartsort/clustering/forward_backward.py +219 -0
- dartsort/clustering/gmm/mixture.py +5790 -0
- dartsort/clustering/gmm/stable_features.py +993 -0
- dartsort/clustering/kmeans.py +448 -0
- dartsort/clustering/merge.py +501 -0
- dartsort/clustering/modes.py +214 -0
- dartsort/clustering/refine_util.py +191 -0
- dartsort/clustering/relocate.py +109 -0
- dartsort/clustering/split.py +1285 -0
- dartsort/config.py +288 -0
- dartsort/detect/__init__.py +2 -0
- dartsort/detect/detect.py +449 -0
- dartsort/detect/detect_filters.py +37 -0
- dartsort/evaluate/__init__.py +1 -0
- dartsort/evaluate/analysis.py +594 -0
- dartsort/evaluate/comparison.py +534 -0
- dartsort/evaluate/hybrid_util.py +599 -0
- dartsort/evaluate/noise_recording_tools.py +184 -0
- dartsort/evaluate/sim_template_tools.py +842 -0
- dartsort/evaluate/simkit.py +840 -0
- dartsort/evaluate/simlib.py +270 -0
- dartsort/localize/__init__.py +0 -0
- dartsort/localize/localize_torch.py +291 -0
- dartsort/localize/localize_util.py +313 -0
- dartsort/main.py +657 -0
- dartsort/peel/__init__.py +6 -0
- dartsort/peel/grab.py +208 -0
- dartsort/peel/matching.py +628 -0
- dartsort/peel/matching_util/__init__.py +3 -0
- dartsort/peel/matching_util/compressed_upsampled.py +718 -0
- dartsort/peel/matching_util/drifty.py +791 -0
- dartsort/peel/matching_util/matching_base.py +616 -0
- dartsort/peel/matching_util/pairwise.py +359 -0
- dartsort/peel/matching_util/pairwise_util.py +1317 -0
- dartsort/peel/peel_base.py +1000 -0
- dartsort/peel/running_template.py +1498 -0
- dartsort/peel/subtract.py +1123 -0
- dartsort/peel/threshold.py +471 -0
- dartsort/peel/universal.py +191 -0
- dartsort/pretrained/default_temporal_kernel.npy +0 -0
- dartsort/pretrained/detect_np1.pt +0 -0
- dartsort/pretrained/detect_np2.pt +0 -0
- dartsort/pretrained/glasbey1024.npz +0 -0
- dartsort/pretrained/offset_trained_denoiser.pt +0 -0
- dartsort/pretrained/offset_trained_denoiser_added_temp.pt +0 -0
- dartsort/pretrained/offset_trained_denoiser_added_temp_boost.pt +0 -0
- dartsort/pretrained/original_single_chan_denoiser.pt +0 -0
- dartsort/pretrained/single_chan_denoiser.pt +0 -0
- dartsort/templates/__init__.py +11 -0
- dartsort/templates/get_templates.py +771 -0
- dartsort/templates/postprocess_util.py +555 -0
- dartsort/templates/realignment.py +466 -0
- dartsort/templates/superres_util.py +204 -0
- dartsort/templates/template_util.py +518 -0
- dartsort/templates/templates.py +528 -0
- dartsort/transform/__init__.py +2 -0
- dartsort/transform/_multichan_denoiser_kit.py +650 -0
- dartsort/transform/all_transformers.py +58 -0
- dartsort/transform/amortized_localization.py +503 -0
- dartsort/transform/amplitudes.py +197 -0
- dartsort/transform/decollider.py +635 -0
- dartsort/transform/enforce_decrease.py +158 -0
- dartsort/transform/localize.py +86 -0
- dartsort/transform/matching_denoiser.py +44 -0
- dartsort/transform/pipeline.py +391 -0
- dartsort/transform/single_channel_denoiser.py +153 -0
- dartsort/transform/single_channel_templates.py +98 -0
- dartsort/transform/supervised_denoiser.py +486 -0
- dartsort/transform/temporal_pca.py +413 -0
- dartsort/transform/transform_base.py +255 -0
- dartsort/util/__init__.py +0 -0
- dartsort/util/cli_util.py +179 -0
- dartsort/util/data_util.py +1128 -0
- dartsort/util/drift_util.py +1054 -0
- dartsort/util/internal_config.py +1074 -0
- dartsort/util/interpolation_util.py +1012 -0
- dartsort/util/job_util.py +19 -0
- dartsort/util/logging_util.py +91 -0
- dartsort/util/main_util.py +235 -0
- dartsort/util/more_operators.py +269 -0
- dartsort/util/multiprocessing_util.py +211 -0
- dartsort/util/nn_util.py +461 -0
- dartsort/util/noise_util.py +1461 -0
- dartsort/util/peel_util.py +223 -0
- dartsort/util/py_util.py +213 -0
- dartsort/util/registration_util.py +128 -0
- dartsort/util/residual_util.py +30 -0
- dartsort/util/sparse_util.py +81 -0
- dartsort/util/spikeio.py +227 -0
- dartsort/util/spiketorch.py +1198 -0
- dartsort/util/testing_util/__init__.py +0 -0
- dartsort/util/testing_util/matching_debug_util.py +517 -0
- dartsort/util/testing_util/mixture_testing_util.py +291 -0
- dartsort/util/torch_optimization_util.py +156 -0
- dartsort/util/torch_util.py +75 -0
- dartsort/util/universal_util.py +269 -0
- dartsort/util/waveform_util.py +809 -0
- dartsort/vis/__init__.py +19 -0
- dartsort/vis/analysis_plots.py +392 -0
- dartsort/vis/colors.py +36 -0
- dartsort/vis/gt.py +392 -0
- dartsort/vis/layout.py +149 -0
- dartsort/vis/metrics_util.py +23 -0
- dartsort/vis/mixture.py +1489 -0
- dartsort/vis/over_time.py +514 -0
- dartsort/vis/recanim.py +429 -0
- dartsort/vis/scatterplots.py +779 -0
- dartsort/vis/sorting.py +163 -0
- dartsort/vis/unit.py +917 -0
- dartsort/vis/unit_comparison.py +1017 -0
- dartsort/vis/versus.py +300 -0
- dartsort/vis/vismain.py +537 -0
- dartsort/vis/waveforms.py +357 -0
- dartsort-0.3.5.dist-info/METADATA +93 -0
- dartsort-0.3.5.dist-info/RECORD +125 -0
- dartsort-0.3.5.dist-info/WHEEL +5 -0
- dartsort-0.3.5.dist-info/entry_points.txt +2 -0
- dartsort-0.3.5.dist-info/top_level.txt +1 -0
dartsort/vis/recanim.py
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import matplotlib.pyplot as plt
|
|
2
|
+
from matplotlib.patches import Rectangle, RegularPolygon
|
|
3
|
+
from matplotlib.collections import PatchCollection
|
|
4
|
+
from matplotlib.animation import FuncAnimation
|
|
5
|
+
from matplotlib.backend_bases import ResizeEvent
|
|
6
|
+
import numpy as np
|
|
7
|
+
from tqdm.auto import trange
|
|
8
|
+
import seaborn as sns
|
|
9
|
+
|
|
10
|
+
from .colors import glasbey1024
|
|
11
|
+
from ..localize.localize_util import localize_waveforms
|
|
12
|
+
|
|
13
|
+
glasbey1024a = np.c_[glasbey1024, np.ones(len(glasbey1024))]
|
|
14
|
+
_t = np.array([1.0, 1, 1, 0])
|
|
15
|
+
_LW = 1
|
|
16
|
+
_MAX_SC = 512
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RecordingAnimation:
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
recording,
|
|
23
|
+
sorting,
|
|
24
|
+
template_data,
|
|
25
|
+
offset_frames=100,
|
|
26
|
+
show_frames=500,
|
|
27
|
+
zlim_start=None,
|
|
28
|
+
zlim_target=None,
|
|
29
|
+
zlim_hold_s=0.01,
|
|
30
|
+
zlim_target_s=0.01,
|
|
31
|
+
contact_wh=(15.0, 15.0),
|
|
32
|
+
soma_size=10.0,
|
|
33
|
+
soma_far_dist=15.0,
|
|
34
|
+
vmin=-5,
|
|
35
|
+
vmax=5,
|
|
36
|
+
cmap="seismic",
|
|
37
|
+
recording_frames_per_second=1000,
|
|
38
|
+
start_frame=0,
|
|
39
|
+
end_frame=None,
|
|
40
|
+
):
|
|
41
|
+
self.geom = recording.get_channel_locations()
|
|
42
|
+
self.recording = recording
|
|
43
|
+
self.sorting = sorting
|
|
44
|
+
self.template_data = template_data
|
|
45
|
+
self.trough_offset_samples = self.template_data.trough_offset_samples
|
|
46
|
+
self.spike_length_samples = self.template_data.spike_length_samples
|
|
47
|
+
|
|
48
|
+
self.contact_wh = contact_wh
|
|
49
|
+
self.vmin = vmin
|
|
50
|
+
self.vmax = vmax
|
|
51
|
+
self.cmap = plt.get_cmap(cmap)
|
|
52
|
+
|
|
53
|
+
self.soma_size = soma_size
|
|
54
|
+
self.soma_far_dist = soma_far_dist
|
|
55
|
+
|
|
56
|
+
self.probe_zmin = self.geom[:, 1].min()
|
|
57
|
+
self.probe_zmax = self.geom[:, 1].max()
|
|
58
|
+
self.zlim_start = zlim_start
|
|
59
|
+
self.zlim_target = zlim_target
|
|
60
|
+
self.zlim_target_s = zlim_target_s
|
|
61
|
+
self.zlim_hold_s = zlim_hold_s
|
|
62
|
+
self.zmargin = 3 * contact_wh[1]
|
|
63
|
+
self.show_frames = show_frames
|
|
64
|
+
self.offset_frames = offset_frames
|
|
65
|
+
|
|
66
|
+
self.recording_frames_per_second = recording_frames_per_second
|
|
67
|
+
self.start_frame = start_frame
|
|
68
|
+
if end_frame is None:
|
|
69
|
+
end_frame = recording.get_num_frames() - self.spike_length_samples
|
|
70
|
+
self.end_frame = end_frame
|
|
71
|
+
|
|
72
|
+
self.unit_mainchan_signal = unit_signal_traces(
|
|
73
|
+
recording, sorting, template_data
|
|
74
|
+
)
|
|
75
|
+
self.unit_maxchans = np.abs(template_data.templates).max(1).argmax(1)
|
|
76
|
+
self.signal = convolve(recording, sorting, template_data)
|
|
77
|
+
locs = localize_waveforms(template_data.templates, self.geom)
|
|
78
|
+
self.unit_xyz = np.c_[locs["x"], locs["y"], locs["z_abs"]]
|
|
79
|
+
|
|
80
|
+
# initialized later
|
|
81
|
+
self._has_drawn = False
|
|
82
|
+
self.figure = None
|
|
83
|
+
self.soma_patches = None
|
|
84
|
+
self.contact_patches = None
|
|
85
|
+
self.recording_ax = None
|
|
86
|
+
self.signal_ax = None
|
|
87
|
+
self.probe_ax = None
|
|
88
|
+
self.recording_im = None
|
|
89
|
+
self.signal_im = None
|
|
90
|
+
self.signal_sc = None
|
|
91
|
+
|
|
92
|
+
def clear(self):
|
|
93
|
+
self.set_drawables()
|
|
94
|
+
self.vlines = None
|
|
95
|
+
self.signal_im = self.recording_im = self.signal_sc = None
|
|
96
|
+
self.artists = None
|
|
97
|
+
self._has_drawn = False
|
|
98
|
+
|
|
99
|
+
def set_drawables(
|
|
100
|
+
self,
|
|
101
|
+
figure=None,
|
|
102
|
+
soma_patches=None,
|
|
103
|
+
contact_patches=None,
|
|
104
|
+
recording_ax=None,
|
|
105
|
+
signal_ax=None,
|
|
106
|
+
probe_ax=None,
|
|
107
|
+
):
|
|
108
|
+
self.figure = figure
|
|
109
|
+
self.soma_patches = soma_patches
|
|
110
|
+
self.contact_patches = contact_patches
|
|
111
|
+
self.recording_ax = recording_ax
|
|
112
|
+
self.signal_ax = signal_ax
|
|
113
|
+
self.probe_ax = probe_ax
|
|
114
|
+
|
|
115
|
+
def initialize_figure(self, figsize=(5, 5)):
|
|
116
|
+
figure = plt.Figure(figsize=figsize, constrained_layout=True)
|
|
117
|
+
axes = figure.subplot_mosaic("ab\nac", gridspec_kw=dict(width_ratios=[1, 3]))
|
|
118
|
+
left_ax = axes["a"]
|
|
119
|
+
right_axes = [axes["b"], axes["c"]]
|
|
120
|
+
# left_column, right_column = figure.subfigures(ncols=2, width_ratios=[2, 6])
|
|
121
|
+
# left_ax = left_column.subplots()
|
|
122
|
+
|
|
123
|
+
left_ax.set_aspect(1.0)
|
|
124
|
+
shank, contact_patches = draw_probe(left_ax, self.geom, *self.contact_wh)
|
|
125
|
+
soma_patches = [
|
|
126
|
+
draw_neuron(
|
|
127
|
+
left_ax,
|
|
128
|
+
*self.unit_xyz[j],
|
|
129
|
+
size=self.soma_size,
|
|
130
|
+
far_dist=self.soma_far_dist,
|
|
131
|
+
edgecolor=glasbey1024[j],
|
|
132
|
+
)
|
|
133
|
+
for j in range(len(self.template_data.templates))
|
|
134
|
+
]
|
|
135
|
+
soma_patches = PatchCollection(soma_patches, match_original=True)
|
|
136
|
+
left_ax.add_collection(soma_patches)
|
|
137
|
+
sns.despine(ax=left_ax, trim=True)
|
|
138
|
+
left_ax.set_xlim(
|
|
139
|
+
[
|
|
140
|
+
self.geom[:, 0].min() - 2 * self.contact_wh[0],
|
|
141
|
+
self.geom[:, 0].max() + 3 * self.contact_wh[0],
|
|
142
|
+
]
|
|
143
|
+
)
|
|
144
|
+
left_ax.set_xticks([self.geom[:, 0].min(), self.geom[:, 0].max()])
|
|
145
|
+
left_ax.set_xlabel("x (um)")
|
|
146
|
+
left_ax.set_ylabel("z (um)")
|
|
147
|
+
|
|
148
|
+
vlines = []
|
|
149
|
+
for ax in right_axes:
|
|
150
|
+
ax.set_ylabel("z (um)")
|
|
151
|
+
ax.set_xlabel("time (s)")
|
|
152
|
+
vlines.append(ax.axvline(self.offset_frames, color="k", lw=0.8, zorder=11))
|
|
153
|
+
self.vlines = vlines
|
|
154
|
+
|
|
155
|
+
self.set_drawables(
|
|
156
|
+
figure=figure,
|
|
157
|
+
soma_patches=soma_patches,
|
|
158
|
+
contact_patches=contact_patches,
|
|
159
|
+
recording_ax=right_axes[0],
|
|
160
|
+
signal_ax=right_axes[1],
|
|
161
|
+
probe_ax=left_ax,
|
|
162
|
+
)
|
|
163
|
+
self.artists = [shank, contact_patches, soma_patches, *vlines]
|
|
164
|
+
|
|
165
|
+
def draw_frame(self, frame_ix):
|
|
166
|
+
self.probe_ax.set_ylim(self.zlim(frame_ix))
|
|
167
|
+
sns.despine(ax=self.probe_ax, trim=True)
|
|
168
|
+
self.update_contacts(frame_ix)
|
|
169
|
+
self.update_somas(frame_ix)
|
|
170
|
+
imr = self.update_recording(frame_ix)
|
|
171
|
+
ims = self.update_signal(frame_ix)
|
|
172
|
+
for ax in (self.recording_ax, self.signal_ax):
|
|
173
|
+
ax.set_xlim(
|
|
174
|
+
[
|
|
175
|
+
frame_ix / self.recording.sampling_frequency,
|
|
176
|
+
(frame_ix + self.show_frames) / self.recording.sampling_frequency,
|
|
177
|
+
]
|
|
178
|
+
)
|
|
179
|
+
dt = 0.004
|
|
180
|
+
ft0 = frame_ix / self.recording.sampling_frequency
|
|
181
|
+
ft1 = (frame_ix + self.show_frames) / self.recording.sampling_frequency
|
|
182
|
+
t0 = dt * np.ceil(ft0 / dt)
|
|
183
|
+
t1 = dt * np.floor((ft1 - dt) / dt)
|
|
184
|
+
t1 = max(t0, t1)
|
|
185
|
+
ax.set_xticks(np.arange(t0, t1 + 1e-6, dt))
|
|
186
|
+
|
|
187
|
+
z0, z1 = self.zlim(frame_ix)
|
|
188
|
+
z0 = self.geom[:, 1][self.geom[:, 1] > z0].min()
|
|
189
|
+
z1 = self.geom[:, 1][self.geom[:, 1] < z1].max()
|
|
190
|
+
self.signal_ax.set_ylim([z0, z1])
|
|
191
|
+
|
|
192
|
+
self._has_drawn = True
|
|
193
|
+
for vl in self.vlines:
|
|
194
|
+
vl.set_xdata(
|
|
195
|
+
2
|
|
196
|
+
* [(frame_ix + self.offset_frames) / self.recording.sampling_frequency]
|
|
197
|
+
)
|
|
198
|
+
return self.artists + [imr, ims]
|
|
199
|
+
|
|
200
|
+
def animate(self, start_frame=None, end_frame=None):
|
|
201
|
+
if self.figure is None:
|
|
202
|
+
self.initialize_figure()
|
|
203
|
+
# if not self._has_drawn:
|
|
204
|
+
# self.draw_frame(0)
|
|
205
|
+
|
|
206
|
+
if start_frame is None:
|
|
207
|
+
start_frame = self.start_frame
|
|
208
|
+
if end_frame is None:
|
|
209
|
+
end_frame = self.end_frame
|
|
210
|
+
|
|
211
|
+
# artists = []
|
|
212
|
+
# for frame in :
|
|
213
|
+
# frame_artists = self.draw_frame(frame)
|
|
214
|
+
# artists.append(frame_artists)
|
|
215
|
+
with trange(start_frame, end_frame, desc="Frames") as pbar:
|
|
216
|
+
anim = FuncAnimation(
|
|
217
|
+
self.figure,
|
|
218
|
+
self.draw_frame,
|
|
219
|
+
frames=pbar,
|
|
220
|
+
cache_frame_data=False,
|
|
221
|
+
blit=False,
|
|
222
|
+
interval=0,
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
# anim = ArtistAnimation(self.figure, artists, interval=50, blit=False)
|
|
226
|
+
|
|
227
|
+
return anim
|
|
228
|
+
|
|
229
|
+
def update_somas(self, frame_ix):
|
|
230
|
+
signals = self.unit_mainchan_signal[frame_ix + self.offset_frames]
|
|
231
|
+
facecolors = self.to_color(signals)
|
|
232
|
+
self.soma_patches.set_facecolor(facecolors)
|
|
233
|
+
|
|
234
|
+
def update_contacts(self, frame_ix):
|
|
235
|
+
signals = self.recording.get_traces(
|
|
236
|
+
start_frame=frame_ix + self.offset_frames,
|
|
237
|
+
end_frame=frame_ix + self.offset_frames + 1,
|
|
238
|
+
)
|
|
239
|
+
facecolors = self.to_color(signals.ravel())
|
|
240
|
+
self.contact_patches.set_facecolor(facecolors)
|
|
241
|
+
|
|
242
|
+
def update_recording(self, frame_ix):
|
|
243
|
+
ac = self.active_channels(frame_ix)
|
|
244
|
+
chunk = self.recording.get_traces(
|
|
245
|
+
start_frame=frame_ix, end_frame=frame_ix + self.show_frames, channel_ids=ac
|
|
246
|
+
)
|
|
247
|
+
if self.recording_im is not None:
|
|
248
|
+
self.recording_im.set_data(chunk[:, ::-1].T)
|
|
249
|
+
self.recording_im.set_extent(self.extent(frame_ix))
|
|
250
|
+
self.recording_im = self.recording_ax.imshow(
|
|
251
|
+
chunk[:, ::-1].T,
|
|
252
|
+
extent=self.extent(frame_ix),
|
|
253
|
+
vmin=self.vmin,
|
|
254
|
+
vmax=self.vmax,
|
|
255
|
+
cmap=self.cmap,
|
|
256
|
+
interpolation="none",
|
|
257
|
+
aspect="auto",
|
|
258
|
+
# animated=self._has_drawn,
|
|
259
|
+
)
|
|
260
|
+
return self.recording_im
|
|
261
|
+
|
|
262
|
+
def update_signal(self, frame_ix):
|
|
263
|
+
ac = self.active_channels(frame_ix)
|
|
264
|
+
chunk = self.signal[frame_ix : frame_ix + self.show_frames, ac]
|
|
265
|
+
|
|
266
|
+
if self.signal_im is not None:
|
|
267
|
+
self.signal_im.set_data(chunk.T)
|
|
268
|
+
self.signal_im.set_extent(self.extent(frame_ix))
|
|
269
|
+
return self.signal_im
|
|
270
|
+
self.signal_im = self.signal_ax.imshow(
|
|
271
|
+
chunk.T,
|
|
272
|
+
extent=self.extent(frame_ix),
|
|
273
|
+
vmin=self.vmin,
|
|
274
|
+
vmax=self.vmax,
|
|
275
|
+
cmap=self.cmap,
|
|
276
|
+
interpolation="none",
|
|
277
|
+
aspect="auto",
|
|
278
|
+
origin="lower",
|
|
279
|
+
# animated=self._has_drawn,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
self.signal_sc = self.signal_ax.scatter(
|
|
283
|
+
self.sorting.times_samples / self.recording.sampling_frequency,
|
|
284
|
+
self.geom[:, 1][self.unit_maxchans[self.sorting.labels]],
|
|
285
|
+
edgecolor=glasbey1024a[self.sorting.labels],
|
|
286
|
+
marker="o",
|
|
287
|
+
s=10,
|
|
288
|
+
zorder=10,
|
|
289
|
+
facecolor=_t,
|
|
290
|
+
linewidth=_LW,
|
|
291
|
+
)
|
|
292
|
+
return self.signal_im
|
|
293
|
+
|
|
294
|
+
def active_channels(self, frame_ix):
|
|
295
|
+
zlim = self.zlim(frame_ix)
|
|
296
|
+
z = self.geom[:, 1]
|
|
297
|
+
return np.flatnonzero(z.clip(*zlim) == z)
|
|
298
|
+
|
|
299
|
+
def zlim(self, frame_ix):
|
|
300
|
+
zmin = self.probe_zmin - self.zmargin
|
|
301
|
+
zmax = self.probe_zmax + self.zmargin
|
|
302
|
+
if self.zlim_start is None and self.zlim_target is None:
|
|
303
|
+
return [zmin, zmax]
|
|
304
|
+
|
|
305
|
+
frame_s = frame_ix / self.recording.sampling_frequency
|
|
306
|
+
if frame_s < self.zlim_hold_s:
|
|
307
|
+
return [zmin, zmax]
|
|
308
|
+
frame_s = frame_s - self.zlim_hold_s
|
|
309
|
+
|
|
310
|
+
if self.zlim_start is None:
|
|
311
|
+
zlim_start = zmin, zmax
|
|
312
|
+
|
|
313
|
+
zmin0, zmax0 = zlim_start
|
|
314
|
+
zmin1, zmax1 = self.zlim_target
|
|
315
|
+
if frame_s > self.zlim_target_s:
|
|
316
|
+
return [zmin1, zmax1]
|
|
317
|
+
lerp1 = frame_s / self.zlim_target_s
|
|
318
|
+
lerp0 = 1 - lerp1
|
|
319
|
+
return [lerp0 * zmin0 + lerp1 * zmin1, lerp0 * zmax0 + lerp1 * zmax1]
|
|
320
|
+
|
|
321
|
+
def extent(self, frame_ix):
|
|
322
|
+
t0 = frame_ix / self.recording.sampling_frequency
|
|
323
|
+
t1 = (frame_ix + self.show_frames - 1) / self.recording.sampling_frequency
|
|
324
|
+
return [t0, t1, *self.zlim(frame_ix)]
|
|
325
|
+
|
|
326
|
+
def to_color(self, voltages):
|
|
327
|
+
v = voltages - self.vmin
|
|
328
|
+
v /= self.vmax - self.vmin
|
|
329
|
+
return self.cmap(v)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def draw_neuron(ax, x, y, z, size=10.0, far_dist=100.0, edgecolor="k"):
|
|
333
|
+
radius = size * np.sqrt(far_dist / y)
|
|
334
|
+
soma = RegularPolygon(
|
|
335
|
+
(x, z),
|
|
336
|
+
numVertices=3,
|
|
337
|
+
radius=radius,
|
|
338
|
+
facecolor=_t,
|
|
339
|
+
edgecolor=edgecolor,
|
|
340
|
+
linewidth=_LW,
|
|
341
|
+
zorder=1 + np.around(2 + 100 * y),
|
|
342
|
+
)
|
|
343
|
+
for theta in np.arange(np.pi / 2, 2 * np.pi, 2 * np.pi / 3):
|
|
344
|
+
vx = x + radius * np.cos(theta)
|
|
345
|
+
vz = z + radius * np.sin(theta)
|
|
346
|
+
vz1 = vz + radius * (2 * (vz > z) - 1)
|
|
347
|
+
ax.plot(
|
|
348
|
+
[vx, vx], [vz, vz1], lw=_LW, zorder=np.around(2 + 100 * y), color=edgecolor
|
|
349
|
+
)
|
|
350
|
+
return soma
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def draw_probe(ax, geom, contact_w=10.0, contact_h=10.0, shank_color="lightgray"):
|
|
354
|
+
xmin, zmin = geom.min(axis=0)
|
|
355
|
+
xmax, zmax = geom.max(axis=0)
|
|
356
|
+
(shank,) = ax.fill(
|
|
357
|
+
[
|
|
358
|
+
xmin - contact_w,
|
|
359
|
+
xmin - contact_w,
|
|
360
|
+
(xmin + xmax) / 2,
|
|
361
|
+
xmax + 2 * contact_w,
|
|
362
|
+
xmax + 2 * contact_w,
|
|
363
|
+
],
|
|
364
|
+
[zmax + 2 * contact_h, zmin, zmin - 4 * contact_h, zmin, zmax + 2 * contact_h],
|
|
365
|
+
facecolor=shank_color,
|
|
366
|
+
edgecolor="k",
|
|
367
|
+
linewidth=_LW,
|
|
368
|
+
zorder=0,
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
# return contact patches
|
|
372
|
+
contact_patches = [
|
|
373
|
+
Rectangle(
|
|
374
|
+
xy,
|
|
375
|
+
contact_w,
|
|
376
|
+
contact_h,
|
|
377
|
+
edgecolor="k",
|
|
378
|
+
linewidth=_LW,
|
|
379
|
+
facecolor=_t,
|
|
380
|
+
zorder=1,
|
|
381
|
+
joinstyle="bevel",
|
|
382
|
+
)
|
|
383
|
+
for xy in geom
|
|
384
|
+
]
|
|
385
|
+
contact_patches = PatchCollection(contact_patches, match_original=True)
|
|
386
|
+
ax.add_collection(contact_patches)
|
|
387
|
+
return shank, contact_patches
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def unit_signal_traces(recording, sorting, template_data):
|
|
391
|
+
trough_offset_samples = template_data.trough_offset_samples
|
|
392
|
+
temps = template_data.templates
|
|
393
|
+
|
|
394
|
+
mcs = np.argmax(np.abs(temps).max(axis=1), axis=1)
|
|
395
|
+
tmcs = temps[np.arange(len(mcs)), :, mcs]
|
|
396
|
+
tmcs = tmcs.astype(np.float16)
|
|
397
|
+
|
|
398
|
+
time_ix = np.arange(temps.shape[1]) - trough_offset_samples
|
|
399
|
+
time_ix = sorting.times_samples[:, None] + time_ix
|
|
400
|
+
|
|
401
|
+
traces = np.zeros((recording.get_num_frames(), len(temps)), dtype=np.float16)
|
|
402
|
+
np.add.at(
|
|
403
|
+
traces,
|
|
404
|
+
(time_ix, sorting.labels[:, None]),
|
|
405
|
+
tmcs[sorting.labels],
|
|
406
|
+
)
|
|
407
|
+
return traces
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def convolve(recording, sorting, template_data, chunklen=2**12):
|
|
411
|
+
traces = np.zeros(
|
|
412
|
+
(recording.get_num_frames(), recording.get_num_channels()), dtype=np.float16
|
|
413
|
+
)
|
|
414
|
+
temps = template_data.templates.astype(np.float16)
|
|
415
|
+
|
|
416
|
+
t_rel_ix = np.arange(template_data.spike_length_samples)
|
|
417
|
+
t_rel_ix -= template_data.trough_offset_samples
|
|
418
|
+
chan_ix = np.arange(traces.shape[1])
|
|
419
|
+
|
|
420
|
+
t = sorting.times_samples
|
|
421
|
+
for chunk_start in trange(0, traces.shape[0], chunklen, desc="Convolve"):
|
|
422
|
+
in_chunk = t == t.clip(chunk_start, chunk_start + chunklen - 1)
|
|
423
|
+
bt = t[in_chunk]
|
|
424
|
+
bl = sorting.labels[in_chunk]
|
|
425
|
+
btemps = temps[bl]
|
|
426
|
+
tix = bt[:, None, None] + t_rel_ix[None, :, None]
|
|
427
|
+
np.add.at(traces, (tix, chan_ix[None, None]), btemps)
|
|
428
|
+
|
|
429
|
+
return traces
|