sortingview 0.15.0__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.
Files changed (119) hide show
  1. examples/example_autocorrelograms.py +33 -0
  2. examples/example_average_waveforms.py +82 -0
  3. examples/example_box_layout.py +70 -0
  4. examples/example_confusion_matrix.py +97 -0
  5. examples/example_cross_correlograms.py +36 -0
  6. examples/example_image.py +19 -0
  7. examples/example_markdown.py +41 -0
  8. examples/example_mountain_layout.py +54 -0
  9. examples/example_plotly.py +21 -0
  10. examples/example_position_plot.py +29 -0
  11. examples/example_raster_plot.py +34 -0
  12. examples/example_sorting_curation.py +33 -0
  13. examples/example_spike_amplitudes.py +47 -0
  14. examples/example_spike_locations.py +77 -0
  15. examples/example_splitter_layout.py +64 -0
  16. examples/example_tab_layout.py +44 -0
  17. examples/example_tiled_image.py +70 -0
  18. examples/example_timeseries_graph.py +49 -0
  19. examples/example_track_position_animation.py +76 -0
  20. examples/example_unit_locations.py +53 -0
  21. examples/example_unit_metrics_graph.py +44 -0
  22. examples/example_unit_similarity_matrix.py +35 -0
  23. examples/example_units_table.py +49 -0
  24. examples/helpers/compute_correlogram_data.py +66 -0
  25. examples/helpers/create_units_table.py +12 -0
  26. sortingview/SpikeSortingView/Figure.py +46 -0
  27. sortingview/SpikeSortingView/MultiTimeseries.py +29 -0
  28. sortingview/SpikeSortingView/SpikeSortingView.py +172 -0
  29. sortingview/SpikeSortingView/__init__.py +10 -0
  30. sortingview/SpikeSortingView/_autocorrelograms_view.py +22 -0
  31. sortingview/SpikeSortingView/_average_waveforms_view.py +32 -0
  32. sortingview/SpikeSortingView/_create_autocorrelograms.py +23 -0
  33. sortingview/SpikeSortingView/_create_average_waveforms.py +40 -0
  34. sortingview/SpikeSortingView/_create_electrode_geometry.py +19 -0
  35. sortingview/SpikeSortingView/_create_live_cross_correlograms.py +15 -0
  36. sortingview/SpikeSortingView/_create_mountain_layout.py +26 -0
  37. sortingview/SpikeSortingView/_create_raster_plot.py +32 -0
  38. sortingview/SpikeSortingView/_create_spike_amplitudes.py +35 -0
  39. sortingview/SpikeSortingView/_create_summary.py +34 -0
  40. sortingview/SpikeSortingView/_create_units_table.py +46 -0
  41. sortingview/SpikeSortingView/_cross_correlograms_view.py +24 -0
  42. sortingview/SpikeSortingView/_electrode_geometry_view.py +14 -0
  43. sortingview/SpikeSortingView/_raster_plot_view.py +31 -0
  44. sortingview/SpikeSortingView/_sorting_summary_view.py +32 -0
  45. sortingview/SpikeSortingView/_spike_amplitudes_view.py +31 -0
  46. sortingview/SpikeSortingView/_units_table_view.py +45 -0
  47. sortingview/SpikeSortingView/create_console_view.py +8 -0
  48. sortingview/SpikeSortingView/create_epochs.py +8 -0
  49. sortingview/SpikeSortingView/create_position_pdf_plot.py +54 -0
  50. sortingview/SpikeSortingView/create_position_plot.py +38 -0
  51. sortingview/SpikeSortingView/create_raw_traces_plot.py +8 -0
  52. sortingview/SpikeSortingView/create_spike_raster_plot.py +15 -0
  53. sortingview/SpikeSortingView/helpers/__init__.py +0 -0
  54. sortingview/SpikeSortingView/helpers/compute_correlogram_data.py +63 -0
  55. sortingview/SpikeSortingView/prepare_spikesortingview_data.py +258 -0
  56. sortingview/__init__.py +1 -0
  57. sortingview/figpack_backend.py +730 -0
  58. sortingview/sortingview_view_url.py +3 -0
  59. sortingview/version.py +2 -0
  60. sortingview/views/Autocorrelograms.py +34 -0
  61. sortingview/views/AverageWaveforms.py +77 -0
  62. sortingview/views/Box.py +32 -0
  63. sortingview/views/ConfusionMatrix.py +55 -0
  64. sortingview/views/CrossCorrelograms.py +36 -0
  65. sortingview/views/ElectrodeGeometry.py +19 -0
  66. sortingview/views/EphysTraces.py +23 -0
  67. sortingview/views/Image.py +39 -0
  68. sortingview/views/LayoutItem.py +39 -0
  69. sortingview/views/Markdown.py +19 -0
  70. sortingview/views/MountainLayout.py +37 -0
  71. sortingview/views/PlotlyFigure.py +20 -0
  72. sortingview/views/PositionPlot.py +58 -0
  73. sortingview/views/RasterPlot.py +36 -0
  74. sortingview/views/SortingCuration.py +18 -0
  75. sortingview/views/SortingCuration2.py +24 -0
  76. sortingview/views/SortingSelection.py +21 -0
  77. sortingview/views/SortingSummary.py +52 -0
  78. sortingview/views/SpikeAmplitudes.py +44 -0
  79. sortingview/views/SpikeLocations.py +59 -0
  80. sortingview/views/Splitter.py +26 -0
  81. sortingview/views/TabLayout.py +32 -0
  82. sortingview/views/TiledImage.py +116 -0
  83. sortingview/views/TimeseriesGraph.py +158 -0
  84. sortingview/views/UnitImages.py +41 -0
  85. sortingview/views/UnitLocations.py +36 -0
  86. sortingview/views/UnitMetricsGraph.py +47 -0
  87. sortingview/views/UnitSimilarityMatrix.py +34 -0
  88. sortingview/views/UnitsTable.py +51 -0
  89. sortingview/views/View.py +176 -0
  90. sortingview/views/__init__.py +31 -0
  91. sortingview/views/franklab/DecodedLinearPositionV1.py +43 -0
  92. sortingview/views/franklab/TrackPositionAnimationV1.py +187 -0
  93. sortingview/views/franklab/__init__.py +2 -0
  94. sortingview-0.15.0.dist-info/METADATA +25 -0
  95. sortingview-0.15.0.dist-info/RECORD +119 -0
  96. sortingview-0.15.0.dist-info/WHEEL +5 -0
  97. sortingview-0.15.0.dist-info/licenses/LICENSE +201 -0
  98. sortingview-0.15.0.dist-info/top_level.txt +3 -0
  99. tests/helpers/compute_correlogram_data.py +66 -0
  100. tests/test_autocorrelograms.py +32 -0
  101. tests/test_average_waveforms.py +71 -0
  102. tests/test_confusion_matrix.py +95 -0
  103. tests/test_cross_correlograms.py +35 -0
  104. tests/test_large_timeseries_graph.py +46 -0
  105. tests/test_layout.py +65 -0
  106. tests/test_markdown.py +38 -0
  107. tests/test_position_plot.py +27 -0
  108. tests/test_raster_plot.py +32 -0
  109. tests/test_sorting_curation_2.py +37 -0
  110. tests/test_sorting_selection.py +68 -0
  111. tests/test_spike_amplitudes.py +37 -0
  112. tests/test_spike_locations.py +74 -0
  113. tests/test_tab_layout.py +45 -0
  114. tests/test_tiled_image.py +68 -0
  115. tests/test_track_position_animation.py +73 -0
  116. tests/test_unit_locations.py +59 -0
  117. tests/test_unit_metrics_graph.py +41 -0
  118. tests/test_unit_similarity_matrix.py +33 -0
  119. tests/test_units_table.py +40 -0
@@ -0,0 +1,172 @@
1
+ from typing import Tuple, Union
2
+ import spikeinterface as si
3
+ import kachery as ka
4
+ import h5py
5
+ import numpy as np
6
+ from sortingview.SpikeSortingView.prepare_spikesortingview_data import prepare_spikesortingview_data
7
+
8
+
9
+ class SpikeSortingView:
10
+ def __init__(self, data_uri: str) -> None:
11
+ self._data_uri = data_uri
12
+ self._data_file_name = ka.load_file(data_uri)
13
+ if self._data_file_name is None:
14
+ raise Exception(f"Unable to load spikesortingview data file: {data_uri}")
15
+ with h5py.File(self._data_file_name, "r") as f:
16
+ # self._recording_object = json.loads(f.attrs["recording_object"])
17
+ # self._sorting_object = json.loads(f.attrs["sorting_object"])
18
+ self._unit_ids = np.array(f.get("unit_ids"))
19
+ self._sampling_frequency = np.array(f.get("sampling_frequency"))[0].item()
20
+ self._channel_ids = np.array(f.get("channel_ids"))
21
+ self._num_frames = np.array(f.get("num_frames"))[0].item()
22
+ self._channel_locations = np.array(f.get("channel_locations"))
23
+ self._num_segments = np.array(f.get("num_segments"))[0].item()
24
+ self._num_frames_per_segment = np.array(f.get("num_frames_per_segment"))[0].item()
25
+ a = np.array(f.get("snippet_len"))
26
+ self._snippet_len = (a[0].item(), a[1].item())
27
+ self._max_num_snippets_per_segment = np.array(f.get("max_num_snippets_per_segment"))[0].item()
28
+ self._channel_neighborhood_size = np.array(f.get("channel_neighborhood_size"))[0].item()
29
+
30
+ @staticmethod
31
+ def create(
32
+ *,
33
+ recording: si.BaseRecording,
34
+ sorting: si.BaseSorting,
35
+ segment_duration_sec: float,
36
+ snippet_len: Tuple[int, int],
37
+ max_num_snippets_per_segment: Union[int, None],
38
+ channel_neighborhood_size: int,
39
+ bandpass_filter: bool = False,
40
+ ):
41
+ data_uri = prepare_spikesortingview_data(
42
+ recording=recording,
43
+ sorting=sorting,
44
+ segment_duration_sec=segment_duration_sec,
45
+ snippet_len=snippet_len,
46
+ max_num_snippets_per_segment=max_num_snippets_per_segment,
47
+ channel_neighborhood_size=channel_neighborhood_size,
48
+ bandpass_filter=bandpass_filter,
49
+ )
50
+ return SpikeSortingView(data_uri)
51
+
52
+ @property
53
+ def data_uri(self):
54
+ return self._data_uri
55
+
56
+ # @property
57
+ # def recording_object(self):
58
+ # return self._recording_object
59
+
60
+ # @property
61
+ # def sorting_object(self):
62
+ # return self._sorting_object
63
+
64
+ @property
65
+ def unit_ids(self):
66
+ return self._unit_ids
67
+
68
+ @property
69
+ def sampling_frequency(self):
70
+ return self._sampling_frequency
71
+
72
+ @property
73
+ def channel_ids(self):
74
+ return self._channel_ids
75
+
76
+ @property
77
+ def num_frames(self):
78
+ return self._num_frames
79
+
80
+ @property
81
+ def channel_locations(self):
82
+ return self._channel_locations
83
+
84
+ @property
85
+ def num_segments(self):
86
+ return self._num_segments
87
+
88
+ @property
89
+ def num_frames_per_segment(self):
90
+ return self._num_frames_per_segment
91
+
92
+ @property
93
+ def snippet_len(self):
94
+ return self.snippet_len
95
+
96
+ @property
97
+ def max_num_snippets_per_segment(self):
98
+ return self._max_num_snippets_per_segment
99
+
100
+ @property
101
+ def channel_neighborhood_size(self):
102
+ return self._channel_neighborhood_size
103
+
104
+ def get_unit_spike_train(self, *, unit_id: int):
105
+ with h5py.File(self._data_file_name, "r") as f:
106
+ all = []
107
+ for iseg in range(self.num_segments):
108
+ st = np.array(f.get(f"segment/{iseg}/unit/{unit_id}/spike_train"))
109
+ if st.ndim == 1:
110
+ all.append(st)
111
+ return np.concatenate(all)
112
+
113
+ def get_unit_subsampled_spike_train(self, *, unit_id: int):
114
+ with h5py.File(self._data_file_name, "r") as f:
115
+ all = []
116
+ for iseg in range(self.num_segments):
117
+ st = np.array(f.get(f"segment/{iseg}/unit/{unit_id}/subsampled_spike_train"))
118
+ if st.ndim == 1:
119
+ all.append(st)
120
+ return np.concatenate(all)
121
+
122
+ def get_unit_spike_amplitudes(self, *, unit_id: int):
123
+ with h5py.File(self._data_file_name, "r") as f:
124
+ all = []
125
+ for iseg in range(self.num_segments):
126
+ amps = np.array(f.get(f"segment/{iseg}/unit/{unit_id}/spike_amplitudes"))
127
+ if amps.ndim == 1:
128
+ all.append(amps)
129
+ return np.concatenate(all)
130
+
131
+ def get_unit_subsampled_spike_snippets(self, *, unit_id: int) -> np.ndarray:
132
+ with h5py.File(self._data_file_name, "r") as f:
133
+ all = []
134
+ for iseg in range(self.num_segments):
135
+ snippets = np.array(f.get(f"segment/{iseg}/unit/{unit_id}/subsampled_spike_snippets"))
136
+ all.append(snippets)
137
+ return np.concatenate(all, axis=0)
138
+
139
+ def get_unit_channel_neighborhood(self, *, unit_id: int):
140
+ with h5py.File(self._data_file_name, "r") as f:
141
+ return np.array(f.get(f"unit/{unit_id}/channel_neighborhood"))
142
+
143
+ def get_unit_peak_channel_id(self, *, unit_id: int):
144
+ with h5py.File(self._data_file_name, "r") as f:
145
+ return np.array(f.get(f"unit/{unit_id}/peak_channel_id"))[0].item()
146
+
147
+ def get_traces_sample(self, *, segment: int) -> np.ndarray:
148
+ with h5py.File(self._data_file_name, "r") as f:
149
+ return np.array(f.get(f"segment/{segment}/traces_sample"))
150
+
151
+ # The following member functions are implemented in separate files
152
+
153
+ # older method
154
+ from ._create_autocorrelograms import create_autocorrelograms
155
+ from ._create_raster_plot import create_raster_plot
156
+ from ._create_average_waveforms import create_average_waveforms
157
+ from ._create_units_table import create_units_table
158
+ from ._create_summary import create_summary
159
+ from ._create_mountain_layout import create_mountain_layout
160
+ from ._create_spike_amplitudes import create_spike_amplitudes
161
+ from ._create_electrode_geometry import create_electrode_geometry
162
+ from ._create_live_cross_correlograms import create_live_cross_correlograms
163
+
164
+ # newest method
165
+ from ._sorting_summary_view import sorting_summary_view
166
+ from ._units_table_view import units_table_view
167
+ from ._raster_plot_view import raster_plot_view
168
+ from ._autocorrelograms_view import autocorrelograms_view
169
+ from ._cross_correlograms_view import cross_correlograms_view
170
+ from ._average_waveforms_view import average_waveforms_view
171
+ from ._electrode_geometry_view import electrode_geometry_view
172
+ from ._spike_amplitudes_view import spike_amplitudes_view
@@ -0,0 +1,10 @@
1
+ from .SpikeSortingView import SpikeSortingView
2
+ from .prepare_spikesortingview_data import prepare_spikesortingview_data
3
+ from .create_position_plot import create_position_plot
4
+ from .create_position_pdf_plot import create_position_pdf_plot, create_live_position_pdf_plot
5
+ from .MultiTimeseries import MultiTimeseries
6
+ from .create_epochs import create_epochs
7
+ from .create_spike_raster_plot import create_spike_raster_plot
8
+ from .create_console_view import create_console_view
9
+ from .create_raw_traces_plot import create_raw_traces_plot
10
+ from ._spike_amplitudes_view import spike_amplitudes_view
@@ -0,0 +1,22 @@
1
+ from typing import List, Union
2
+ from .helpers.compute_correlogram_data import compute_correlogram_data
3
+ import sortingview.views as vv
4
+
5
+
6
+ def autocorrelograms_view(self, *, unit_ids: List[int], label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Autocorrelograms"
13
+
14
+ autocorrelograms: List[vv.AutocorrelogramItem] = []
15
+ for unit_id in unit_ids:
16
+ times = self.get_unit_spike_train(unit_id=unit_id)
17
+ a = compute_correlogram_data(times1=times, times2=None, sampling_frequency=self.sampling_frequency, window_size_msec=50, bin_size_msec=1)
18
+ bin_edges_sec = a["bin_edges_sec"]
19
+ bin_counts = a["bin_counts"]
20
+ autocorrelograms.append(vv.AutocorrelogramItem(unit_id=unit_id, bin_edges_sec=bin_edges_sec, bin_counts=bin_counts))
21
+
22
+ return vv.Autocorrelograms(autocorrelograms)
@@ -0,0 +1,32 @@
1
+ from typing import List, Union
2
+ import numpy as np
3
+ import sortingview.views as vv
4
+
5
+
6
+ def average_waveforms_view(self, *, unit_ids: List[int], label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Average waveforms"
13
+
14
+ # traces_sample = self.get_traces_sample(segment=0)
15
+ # noise_level = estimate_noise_level(traces_sample)
16
+ plots: List[vv.AverageWaveformItem] = []
17
+ for unit_id in unit_ids:
18
+ snippets = self.get_unit_subsampled_spike_snippets(unit_id=unit_id)
19
+ waveform = np.mean(snippets, axis=0).astype(np.float32)
20
+ waveform_std_dev = np.sqrt(np.var(snippets, axis=0)).astype(np.float32)
21
+ channel_ids = self.get_unit_channel_neighborhood(unit_id=unit_id).tolist()
22
+ plots.append(vv.AverageWaveformItem(unit_id=unit_id, channel_ids=channel_ids, waveform=waveform.T, waveform_std_dev=waveform_std_dev.T))
23
+
24
+ channel_locations = {}
25
+ for ii, channel_id in enumerate(self.channel_ids):
26
+ channel_locations[str(channel_id)] = self.channel_locations[ii, :].astype(np.float32)
27
+ return vv.AverageWaveforms(average_waveforms=plots, channel_locations=channel_locations)
28
+
29
+
30
+ # def estimate_noise_level(traces: np.ndarray):
31
+ # est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
32
+ # return est_noise_level
@@ -0,0 +1,23 @@
1
+ from typing import List, Union
2
+ from .helpers.compute_correlogram_data import compute_correlogram_data
3
+ from .Figure import Figure
4
+
5
+
6
+ def create_autocorrelograms(self, *, unit_ids: List[int], label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Autocorrelograms"
13
+
14
+ autocorrelograms = []
15
+ for unit_id in unit_ids:
16
+ times = self.get_unit_spike_train(unit_id=unit_id)
17
+ a = compute_correlogram_data(times1=times, times2=None, sampling_frequency=self.sampling_frequency, window_size_msec=50, bin_size_msec=1)
18
+ bin_edges_sec = a["bin_edges_sec"]
19
+ bin_counts = a["bin_counts"]
20
+ autocorrelograms.append({"unitId": unit_id, "binEdgesSec": bin_edges_sec, "binCounts": bin_counts})
21
+
22
+ data = {"type": "Autocorrelograms", "autocorrelograms": autocorrelograms}
23
+ return Figure(data=data, label=label)
@@ -0,0 +1,40 @@
1
+ from typing import List, Tuple, Union
2
+ import numpy as np
3
+ from .helpers.compute_correlogram_data import compute_correlogram_data
4
+ from .Figure import Figure
5
+
6
+
7
+ def create_average_waveforms(self, *, unit_ids: List[int], label: Union[str, None] = None):
8
+ from .SpikeSortingView import SpikeSortingView
9
+
10
+ assert isinstance(self, SpikeSortingView)
11
+
12
+ if label is None:
13
+ label = "Average waveforms"
14
+
15
+ traces_sample = self.get_traces_sample(segment=0)
16
+ noise_level = estimate_noise_level(traces_sample)
17
+ plots = []
18
+ for unit_id in unit_ids:
19
+ snippets = self.get_unit_subsampled_spike_snippets(unit_id=unit_id)
20
+ waveform = np.mean(snippets, axis=0).astype(np.float32)
21
+ waveform_std_dev = np.sqrt(np.var(snippets, axis=0)).astype(np.float32)
22
+ channel_ids = self.get_unit_channel_neighborhood(unit_id=unit_id)
23
+ plots.append({"unitId": unit_id, "channelIds": channel_ids, "waveform": waveform.T, "waveformStdDev": waveform_std_dev.T})
24
+
25
+ channel_locations = {}
26
+ for ii, channel_id in enumerate(self.channel_ids):
27
+ channel_locations[str(channel_id)] = self.channel_locations[ii, :].astype(np.float32)
28
+ data = {
29
+ "type": "AverageWaveforms",
30
+ "averageWaveforms": plots,
31
+ "samplingFrequency": self.sampling_frequency,
32
+ "noiseLevel": float(noise_level),
33
+ "channelLocations": channel_locations,
34
+ }
35
+ return Figure(data=data, label=label)
36
+
37
+
38
+ def estimate_noise_level(traces: np.ndarray):
39
+ est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
40
+ return est_noise_level
@@ -0,0 +1,19 @@
1
+ from typing import List, Tuple, Union
2
+ import numpy as np
3
+ from .helpers.compute_correlogram_data import compute_correlogram_data
4
+ from .Figure import Figure
5
+
6
+
7
+ def create_electrode_geometry(self, *, label: Union[str, None] = None):
8
+ from .SpikeSortingView import SpikeSortingView
9
+
10
+ assert isinstance(self, SpikeSortingView)
11
+
12
+ if label is None:
13
+ label = "Electrode geometry"
14
+
15
+ channel_locations = {}
16
+ for ii, channel_id in enumerate(self.channel_ids):
17
+ channel_locations[str(channel_id)] = self.channel_locations[ii, :].astype(np.float32)
18
+ data = {"type": "ElectrodeGeometry", "channelLocations": channel_locations}
19
+ return Figure(data=data, label=label)
@@ -0,0 +1,15 @@
1
+ from typing import List, Union
2
+ from .helpers.compute_correlogram_data import compute_correlogram_data
3
+ from .Figure import Figure
4
+
5
+
6
+ def create_live_cross_correlograms(self, *, label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Cross correlograms"
13
+
14
+ data = {"type": "LiveCrossCorrelograms", "unitIds": self.unit_ids, "dataUri": self.data_uri}
15
+ return Figure(data=data, label=label)
@@ -0,0 +1,26 @@
1
+ import os
2
+ from typing import List, Union
3
+ import kachery as ka
4
+ from .Figure import Figure
5
+
6
+
7
+ def create_mountain_layout(self, *, figures: List[Figure], label: Union[str, None] = None, sorting_curation_uri: Union[str, None] = None):
8
+ from .SpikeSortingView import SpikeSortingView
9
+
10
+ assert isinstance(self, SpikeSortingView)
11
+
12
+ if label is None:
13
+ label = "SpikeSortingView"
14
+
15
+ data = {
16
+ "type": "MountainLayout",
17
+ "views": [{"type": fig0.data["type"], "label": fig0.label, "figureDataUri": _upload_data_and_return_uri(fig0.get_serialized_figure_data())} for fig0 in figures],
18
+ }
19
+ if sorting_curation_uri is not None:
20
+ data["sortingCurationUri"] = sorting_curation_uri
21
+ return Figure(data=data, label=label)
22
+
23
+
24
+ def _upload_data_and_return_uri(data):
25
+ data_uri = ka.store_json(data)
26
+ return data_uri
@@ -0,0 +1,32 @@
1
+ from typing import List, Union
2
+ import numpy as np
3
+ from .Figure import Figure
4
+
5
+
6
+ def create_raster_plot(self, *, unit_ids: List[int], label: Union[str, None] = None, _subsample_max_firing_rate: Union[float, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Raster plot"
13
+
14
+ plots = []
15
+ for unit_id in unit_ids:
16
+ spike_times_sec = np.array(self.get_unit_spike_train(unit_id=unit_id)) / self.sampling_frequency
17
+ if _subsample_max_firing_rate is not None:
18
+ max_num = int(self.num_frames / self.sampling_frequency * _subsample_max_firing_rate)
19
+ if len(spike_times_sec) > max_num:
20
+ spike_times_sec = _subsample(spike_times_sec, max_num)
21
+ plots.append({"unitId": unit_id, "spikeTimesSec": spike_times_sec.astype(np.float32)})
22
+
23
+ data = {"type": "RasterPlot", "startTimeSec": 0, "endTimeSec": self.num_frames / self.sampling_frequency, "plots": plots}
24
+ return Figure(data=data, label=label)
25
+
26
+
27
+ def _subsample(x: np.ndarray, num: int):
28
+ if num >= len(x):
29
+ return x
30
+ incr = len(x) / num
31
+ inds = np.floor(np.arange(num) * incr).astype(np.int32)
32
+ return x[inds]
@@ -0,0 +1,35 @@
1
+ from typing import List, Union
2
+ import numpy as np
3
+ from .Figure import Figure
4
+
5
+
6
+ def create_spike_amplitudes(self, *, unit_ids: List[int], label: Union[str, None] = None, _subsample_max_firing_rate: Union[float, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Spike amplitudes"
13
+
14
+ units = []
15
+ for unit_id in unit_ids:
16
+ spike_times_sec = np.array(self.get_unit_spike_train(unit_id=unit_id)) / self.sampling_frequency
17
+ spike_amplitudes = self.get_unit_spike_amplitudes(unit_id=unit_id)
18
+ if _subsample_max_firing_rate is not None:
19
+ max_num = int(self.num_frames / self.sampling_frequency * _subsample_max_firing_rate)
20
+ if len(spike_times_sec) > max_num:
21
+ spike_times_sec, spike_amplitudes = _subsample2(spike_times_sec, spike_amplitudes, max_num)
22
+ units.append({"unitId": unit_id, "spikeTimesSec": spike_times_sec.astype(np.float32), "spikeAmplitudes": spike_amplitudes.astype(np.float32)})
23
+
24
+ # traces_sample = self.get_traces_sample(segment=0)
25
+
26
+ data = {"type": "SpikeAmplitudes", "startTimeSec": 0, "endTimeSec": self.num_frames / self.sampling_frequency, "units": units}
27
+ return Figure(data=data, label=label)
28
+
29
+
30
+ def _subsample2(x: np.ndarray, y: np.ndarray, num: int):
31
+ if num >= len(x):
32
+ return x, y
33
+ incr = len(x) / num
34
+ inds = np.floor(np.arange(num) * incr).astype(np.int32)
35
+ return x[inds], y[inds]
@@ -0,0 +1,34 @@
1
+ from typing import Union
2
+ import numpy as np
3
+ from .Figure import Figure
4
+
5
+
6
+ def create_summary(self, *, label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Summary"
13
+
14
+ traces_sample = self.get_traces_sample(segment=0)
15
+ noise_level = estimate_noise_level(traces_sample)
16
+
17
+ data = {
18
+ "type": "Summary",
19
+ "recordingDescription": "",
20
+ "sortingDescription": "",
21
+ "unitIds": self.unit_ids,
22
+ "channelIds": self.channel_ids,
23
+ "samplingFrequency": float(self.sampling_frequency),
24
+ "numFrames": self.num_frames,
25
+ "numSegments": self.num_segments,
26
+ "channelLocations": self.channel_locations.astype(np.float32),
27
+ "noiseLevel": float(noise_level),
28
+ }
29
+ return Figure(data=data, label=label)
30
+
31
+
32
+ def estimate_noise_level(traces: np.ndarray):
33
+ est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
34
+ return est_noise_level
@@ -0,0 +1,46 @@
1
+ from typing import List, Tuple, Union
2
+ import numpy as np
3
+ from .helpers.compute_correlogram_data import compute_correlogram_data
4
+ from .Figure import Figure
5
+
6
+
7
+ def create_units_table(self, *, unit_ids: List[int], unit_metrics: Union[List[dict], None] = None, label: Union[str, None] = None):
8
+ from .SpikeSortingView import SpikeSortingView
9
+
10
+ assert isinstance(self, SpikeSortingView)
11
+
12
+ if label is None:
13
+ label = "Units table"
14
+
15
+ traces_sample = self.get_traces_sample(segment=0)
16
+ noise_level = estimate_noise_level(traces_sample)
17
+
18
+ columns = [
19
+ {"key": "unitId", "label": "Unit", "dtype": "int"},
20
+ {"key": "numEvents", "label": "Num. events", "dtype": "int"},
21
+ {"key": "firingRateHz", "label": "Firing rate (Hz)", "dtype": "float"},
22
+ {"key": "snr", "label": "SNR", "dtype": "float"},
23
+ ]
24
+ if unit_metrics is not None:
25
+ for u in unit_metrics:
26
+ columns.append({"key": f'metric:{u["name"]}', "label": u["label"], "dtype": "float"})
27
+ rows = []
28
+ for unit_id in unit_ids:
29
+ spike_train = self.get_unit_spike_train(unit_id=unit_id)
30
+ spike_amplitudes = self.get_unit_spike_amplitudes(unit_id=unit_id)
31
+ avg_spike_amplitude = np.mean(spike_amplitudes)
32
+ snr = np.abs(avg_spike_amplitude) / noise_level
33
+ values = {"unitId": unit_id, "numEvents": len(spike_train), "firingRateHz": len(spike_train) / (self.num_frames / self.sampling_frequency), "snr": snr}
34
+ if unit_metrics is not None:
35
+ for u in unit_metrics:
36
+ d = u["data"].get(str(unit_id), None)
37
+ if d is not None:
38
+ values[f'metric:{u["name"]}'] = d
39
+ rows.append({"unitId": unit_id, "values": values})
40
+ data = {"type": "UnitsTable", "columns": columns, "rows": rows}
41
+ return Figure(data=data, label=label)
42
+
43
+
44
+ def estimate_noise_level(traces: np.ndarray):
45
+ est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
46
+ return est_noise_level
@@ -0,0 +1,24 @@
1
+ from typing import List
2
+ from .helpers.compute_correlogram_data import compute_correlogram_data
3
+ import sortingview.views as vv
4
+
5
+
6
+ def cross_correlograms_view(self, *, unit_ids: List[int], hide_unit_selector: bool = False):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ cross_correlograms: List[vv.CrossCorrelogramItem] = []
12
+ for unit_id1 in unit_ids:
13
+ times1 = self.get_unit_spike_train(unit_id=unit_id1)
14
+ for unit_id2 in unit_ids:
15
+ times2 = self.get_unit_spike_train(unit_id=unit_id2)
16
+ if unit_id1 == unit_id2:
17
+ a = compute_correlogram_data(times1=times1, times2=None, sampling_frequency=self.sampling_frequency, window_size_msec=50, bin_size_msec=1)
18
+ else:
19
+ a = compute_correlogram_data(times1=times1, times2=times2, sampling_frequency=self.sampling_frequency, window_size_msec=50, bin_size_msec=1)
20
+ bin_edges_sec = a["bin_edges_sec"]
21
+ bin_counts = a["bin_counts"]
22
+ cross_correlograms.append(vv.CrossCorrelogramItem(unit_id1=unit_id1, unit_id2=unit_id2, bin_edges_sec=bin_edges_sec, bin_counts=bin_counts))
23
+
24
+ return vv.CrossCorrelograms(cross_correlograms=cross_correlograms, hide_unit_selector=hide_unit_selector)
@@ -0,0 +1,14 @@
1
+ from typing import List, Tuple, Union
2
+ import numpy as np
3
+ import sortingview.views as vv
4
+
5
+
6
+ def electrode_geometry_view(self):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ channel_locations = {}
12
+ for ii, channel_id in enumerate(self.channel_ids):
13
+ channel_locations[str(channel_id)] = self.channel_locations[ii, :].astype(np.float32)
14
+ return vv.ElectrodeGeometry(channel_locations=channel_locations)
@@ -0,0 +1,31 @@
1
+ from typing import List, Union
2
+ import numpy as np
3
+ import sortingview.views as vv
4
+
5
+
6
+ def raster_plot_view(self, *, unit_ids: List[int], label: Union[str, None] = None, _subsample_max_firing_rate: Union[float, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Raster plot"
13
+
14
+ plots: List[vv.RasterPlotItem] = []
15
+ for unit_id in unit_ids:
16
+ spike_times_sec = np.array(self.get_unit_spike_train(unit_id=unit_id)) / self.sampling_frequency
17
+ if _subsample_max_firing_rate is not None:
18
+ max_num = int(self.num_frames / self.sampling_frequency * _subsample_max_firing_rate)
19
+ if len(spike_times_sec) > max_num:
20
+ spike_times_sec = _subsample(spike_times_sec, max_num)
21
+ plots.append(vv.RasterPlotItem(unit_id=unit_id, spike_times_sec=spike_times_sec.astype(np.float32)))
22
+
23
+ return vv.RasterPlot(start_time_sec=0, end_time_sec=self.num_frames / self.sampling_frequency, plots=plots)
24
+
25
+
26
+ def _subsample(x: np.ndarray, num: int):
27
+ if num >= len(x):
28
+ return x
29
+ incr = len(x) / num
30
+ inds = np.floor(np.arange(num) * incr).astype(np.int32)
31
+ return x[inds]
@@ -0,0 +1,32 @@
1
+ from typing import Union
2
+ import numpy as np
3
+ import sortingview.views as vv
4
+
5
+
6
+ def sorting_summary_view(self, *, label: Union[str, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ if label is None:
12
+ label = "Summary"
13
+
14
+ traces_sample = self.get_traces_sample(segment=0)
15
+ noise_level = estimate_noise_level(traces_sample)
16
+
17
+ return vv.SortingSummary(
18
+ recording_description="",
19
+ sorting_description="",
20
+ unit_ids=self.unit_ids,
21
+ channel_ids=self.channel_ids,
22
+ sampling_frequency=float(self.sampling_frequency),
23
+ num_frames=self.num_frames,
24
+ num_segments=self.num_segments,
25
+ channel_locations=self.channel_locations.astype(np.float32),
26
+ noise_level=float(noise_level),
27
+ )
28
+
29
+
30
+ def estimate_noise_level(traces: np.ndarray):
31
+ est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
32
+ return est_noise_level
@@ -0,0 +1,31 @@
1
+ from typing import List, Union
2
+ import numpy as np
3
+ import sortingview.views as vv
4
+
5
+
6
+ def spike_amplitudes_view(self, *, unit_ids: List[int], hide_unit_selector: bool = False, _subsample_max_firing_rate: Union[float, None] = None):
7
+ from .SpikeSortingView import SpikeSortingView
8
+
9
+ assert isinstance(self, SpikeSortingView)
10
+
11
+ plots: List[vv.SpikeAmplitudesItem] = []
12
+ for unit_id in unit_ids:
13
+ spike_times_sec = np.array(self.get_unit_spike_train(unit_id=unit_id)) / self.sampling_frequency
14
+ spike_amplitudes = self.get_unit_spike_amplitudes(unit_id=unit_id)
15
+ if _subsample_max_firing_rate is not None:
16
+ max_num = int(self.num_frames / self.sampling_frequency * _subsample_max_firing_rate)
17
+ if len(spike_times_sec) > max_num:
18
+ spike_times_sec, spike_amplitudes = _subsample2(spike_times_sec, spike_amplitudes, max_num)
19
+ plots.append(vv.SpikeAmplitudesItem(unit_id=unit_id, spike_times_sec=spike_times_sec.astype(np.float32), spike_amplitudes=spike_amplitudes.astype(np.float32)))
20
+
21
+ # traces_sample = self.get_traces_sample(segment=0)
22
+
23
+ return vv.SpikeAmplitudes(start_time_sec=0, end_time_sec=self.num_frames / self.sampling_frequency, plots=plots, hide_unit_selector=hide_unit_selector)
24
+
25
+
26
+ def _subsample2(x: np.ndarray, y: np.ndarray, num: int):
27
+ if num >= len(x):
28
+ return x, y
29
+ incr = len(x) / num
30
+ inds = np.floor(np.arange(num) * incr).astype(np.int32)
31
+ return x[inds], y[inds]