Skip to content

Commit 05c2d1c

Browse files
committed
Update DriftRasterMapWidget
1 parent 753cf23 commit 05c2d1c

3 files changed

Lines changed: 35 additions & 59 deletions

File tree

src/spikeinterface/widgets/amplitudes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class AmplitudesWidget(BaseRasterWidget):
2929
The segment index (or None if mono-segment)
3030
max_spikes_per_unit : int or None, default: None
3131
Number of max spikes per unit to display. Use None for all spikes
32+
y_lim : tuple or None, default: None
33+
The min and max depth to display, if None (min and max of the amplitudes).
34+
scatter_decimate : int, default: 1
35+
If > 1, the scatter points are decimated.
3236
hide_unit_selector : bool, default: False
3337
If True the unit selector is not displayed
3438
(sortingview backend)
@@ -49,6 +53,8 @@ def __init__(
4953
unit_colors=None,
5054
segment_index=None,
5155
max_spikes_per_unit=None,
56+
y_lim=None,
57+
scatter_decimate=1,
5258
hide_unit_selector=False,
5359
plot_histograms=False,
5460
bins=None,
@@ -108,6 +114,8 @@ def __init__(
108114
hide_unit_selector=hide_unit_selector,
109115
plot_legend=plot_legend,
110116
y_label="Amplitude",
117+
y_lim=y_lim,
118+
scatter_decimate=scatter_decimate,
111119
)
112120

113121
BaseRasterWidget.__init__(self, **plot_data, backend=backend, **backend_kwargs)

src/spikeinterface/widgets/motion.py

Lines changed: 25 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from spikeinterface.core import BaseRecording, SortingAnalyzer
88
from spikeinterface.sortingcomponents.motion import Motion
9+
from .rasters import BaseRasterWidget
910

1011

1112
class MotionWidget(BaseWidget):
@@ -97,7 +98,7 @@ def plot_matplotlib(self, data_plot, **backend_kwargs):
9798
ax.set_ylabel("Depth [um]")
9899

99100

100-
class DriftRasterMapWidget(BaseWidget):
101+
class DriftRasterMapWidget(BaseRasterWidget):
101102
"""
102103
Plot the drift raster map from peaks or a SortingAnalyzer.
103104
The drift raster map is a scatter plot of the estimated peak depth vs time and it is
@@ -200,56 +201,14 @@ def __init__(
200201
if peak_amplitudes is not None:
201202
peak_amplitudes = peak_amplitudes[peak_mask]
202203

203-
plot_data = dict(
204-
peaks=peaks,
205-
peak_locations=peak_locations,
206-
peak_amplitudes=peak_amplitudes,
207-
direction=direction,
208-
sampling_frequency=sampling_frequency,
209-
segment_index=segment_index,
210-
depth_lim=depth_lim,
211-
color_amplitude=color_amplitude,
212-
color=color,
213-
scatter_decimate=scatter_decimate,
214-
cmap=cmap,
215-
clim=clim,
216-
alpha=alpha,
217-
recording=recording,
218-
)
219-
BaseWidget.__init__(self, plot_data, backend=backend, **backend_kwargs)
220-
221-
def plot_matplotlib(self, data_plot, **backend_kwargs):
222-
import matplotlib.pyplot as plt
223-
from matplotlib.colors import Normalize
224-
from .utils_matplotlib import make_mpl_figure
204+
from matplotlib.pyplot import colormaps
225205

226-
from spikeinterface.sortingcomponents.motion import correct_motion_on_peaks
227-
228-
dp = to_attr(data_plot)
229-
230-
assert backend_kwargs["axes"] is None, "axes argument is not allowed in DriftRasterMapWidget. Use ax instead."
231-
232-
self.figure, self.axes, self.ax = make_mpl_figure(**backend_kwargs)
233-
234-
if dp.recording is None:
235-
peak_times = dp.peaks["sample_index"] / dp.sampling_frequency
236-
else:
237-
peak_times = dp.recording.sample_index_to_time(dp.peaks["sample_index"], segment_index=dp.segment_index)
238-
239-
peak_locs = dp.peak_locations[dp.direction]
240-
if dp.scatter_decimate is not None:
241-
peak_times = peak_times[:: dp.scatter_decimate]
242-
peak_locs = peak_locs[:: dp.scatter_decimate]
243-
244-
if dp.color_amplitude:
245-
amps = dp.peak_amplitudes
206+
if color_amplitude:
207+
amps = peak_amplitudes
246208
amps_abs = np.abs(amps)
247209
q_95 = np.quantile(amps_abs, 0.95)
248-
if dp.scatter_decimate is not None:
249-
amps = amps[:: dp.scatter_decimate]
250-
amps_abs = amps_abs[:: dp.scatter_decimate]
251-
cmap = plt.colormaps[dp.cmap]
252-
if dp.clim is None:
210+
cmap = colormaps[cmap]
211+
if clim is None:
253212
amps = amps_abs
254213
amps /= q_95
255214
c = cmap(amps)
@@ -259,17 +218,26 @@ def plot_matplotlib(self, data_plot, **backend_kwargs):
259218
color_kwargs = dict(
260219
color=None,
261220
c=c,
262-
alpha=dp.alpha,
221+
alpha=alpha,
263222
)
264223
else:
265-
color_kwargs = dict(color=dp.color, c=None, alpha=dp.alpha)
266-
267-
self.ax.scatter(peak_times, peak_locs, s=1, **color_kwargs)
268-
if dp.depth_lim is not None:
269-
self.ax.set_ylim(*dp.depth_lim)
270-
self.ax.set_title("Peak depth")
271-
self.ax.set_xlabel("Times [s]")
272-
self.ax.set_ylabel("Depth [$\\mu$m]")
224+
color_kwargs = dict(color=color, c=None, alpha=alpha)
225+
226+
# convert data into format that `BaseRasterWidget` can take it in
227+
spike_train_data = {0: peaks["sample_index"] / sampling_frequency}
228+
y_axis_data = {0: peak_locations[direction]}
229+
230+
plot_data = dict(
231+
spike_train_data=spike_train_data,
232+
y_axis_data=y_axis_data,
233+
y_lim=depth_lim,
234+
color_kwargs=color_kwargs,
235+
scatter_decimate=scatter_decimate,
236+
title="Peak depth",
237+
y_label="Depth [um]",
238+
)
239+
240+
BaseRasterWidget.__init__(self, **plot_data, backend=backend, **backend_kwargs)
273241

274242

275243
class MotionInfoWidget(BaseWidget):

src/spikeinterface/widgets/rasters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from warnings import warn
55

66
from .base import BaseWidget, to_attr, default_backend_kwargs
7-
from . import get_some_colors
7+
from .utils import get_some_colors
88

99

1010
class BaseRasterWidget(BaseWidget):
@@ -188,7 +188,7 @@ def plot_ipywidgets(self, data_plot, **backend_kwargs):
188188

189189
import ipywidgets.widgets as W
190190
from IPython.display import display
191-
from spikeinterface.widgets.utils_ipywidgets import check_ipywidget_backend, UnitSelector
191+
from .utils_ipywidgets import check_ipywidget_backend, UnitSelector
192192

193193
check_ipywidget_backend()
194194

0 commit comments

Comments
 (0)