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.
- examples/example_autocorrelograms.py +33 -0
- examples/example_average_waveforms.py +82 -0
- examples/example_box_layout.py +70 -0
- examples/example_confusion_matrix.py +97 -0
- examples/example_cross_correlograms.py +36 -0
- examples/example_image.py +19 -0
- examples/example_markdown.py +41 -0
- examples/example_mountain_layout.py +54 -0
- examples/example_plotly.py +21 -0
- examples/example_position_plot.py +29 -0
- examples/example_raster_plot.py +34 -0
- examples/example_sorting_curation.py +33 -0
- examples/example_spike_amplitudes.py +47 -0
- examples/example_spike_locations.py +77 -0
- examples/example_splitter_layout.py +64 -0
- examples/example_tab_layout.py +44 -0
- examples/example_tiled_image.py +70 -0
- examples/example_timeseries_graph.py +49 -0
- examples/example_track_position_animation.py +76 -0
- examples/example_unit_locations.py +53 -0
- examples/example_unit_metrics_graph.py +44 -0
- examples/example_unit_similarity_matrix.py +35 -0
- examples/example_units_table.py +49 -0
- examples/helpers/compute_correlogram_data.py +66 -0
- examples/helpers/create_units_table.py +12 -0
- sortingview/SpikeSortingView/Figure.py +46 -0
- sortingview/SpikeSortingView/MultiTimeseries.py +29 -0
- sortingview/SpikeSortingView/SpikeSortingView.py +172 -0
- sortingview/SpikeSortingView/__init__.py +10 -0
- sortingview/SpikeSortingView/_autocorrelograms_view.py +22 -0
- sortingview/SpikeSortingView/_average_waveforms_view.py +32 -0
- sortingview/SpikeSortingView/_create_autocorrelograms.py +23 -0
- sortingview/SpikeSortingView/_create_average_waveforms.py +40 -0
- sortingview/SpikeSortingView/_create_electrode_geometry.py +19 -0
- sortingview/SpikeSortingView/_create_live_cross_correlograms.py +15 -0
- sortingview/SpikeSortingView/_create_mountain_layout.py +26 -0
- sortingview/SpikeSortingView/_create_raster_plot.py +32 -0
- sortingview/SpikeSortingView/_create_spike_amplitudes.py +35 -0
- sortingview/SpikeSortingView/_create_summary.py +34 -0
- sortingview/SpikeSortingView/_create_units_table.py +46 -0
- sortingview/SpikeSortingView/_cross_correlograms_view.py +24 -0
- sortingview/SpikeSortingView/_electrode_geometry_view.py +14 -0
- sortingview/SpikeSortingView/_raster_plot_view.py +31 -0
- sortingview/SpikeSortingView/_sorting_summary_view.py +32 -0
- sortingview/SpikeSortingView/_spike_amplitudes_view.py +31 -0
- sortingview/SpikeSortingView/_units_table_view.py +45 -0
- sortingview/SpikeSortingView/create_console_view.py +8 -0
- sortingview/SpikeSortingView/create_epochs.py +8 -0
- sortingview/SpikeSortingView/create_position_pdf_plot.py +54 -0
- sortingview/SpikeSortingView/create_position_plot.py +38 -0
- sortingview/SpikeSortingView/create_raw_traces_plot.py +8 -0
- sortingview/SpikeSortingView/create_spike_raster_plot.py +15 -0
- sortingview/SpikeSortingView/helpers/__init__.py +0 -0
- sortingview/SpikeSortingView/helpers/compute_correlogram_data.py +63 -0
- sortingview/SpikeSortingView/prepare_spikesortingview_data.py +258 -0
- sortingview/__init__.py +1 -0
- sortingview/figpack_backend.py +730 -0
- sortingview/sortingview_view_url.py +3 -0
- sortingview/version.py +2 -0
- sortingview/views/Autocorrelograms.py +34 -0
- sortingview/views/AverageWaveforms.py +77 -0
- sortingview/views/Box.py +32 -0
- sortingview/views/ConfusionMatrix.py +55 -0
- sortingview/views/CrossCorrelograms.py +36 -0
- sortingview/views/ElectrodeGeometry.py +19 -0
- sortingview/views/EphysTraces.py +23 -0
- sortingview/views/Image.py +39 -0
- sortingview/views/LayoutItem.py +39 -0
- sortingview/views/Markdown.py +19 -0
- sortingview/views/MountainLayout.py +37 -0
- sortingview/views/PlotlyFigure.py +20 -0
- sortingview/views/PositionPlot.py +58 -0
- sortingview/views/RasterPlot.py +36 -0
- sortingview/views/SortingCuration.py +18 -0
- sortingview/views/SortingCuration2.py +24 -0
- sortingview/views/SortingSelection.py +21 -0
- sortingview/views/SortingSummary.py +52 -0
- sortingview/views/SpikeAmplitudes.py +44 -0
- sortingview/views/SpikeLocations.py +59 -0
- sortingview/views/Splitter.py +26 -0
- sortingview/views/TabLayout.py +32 -0
- sortingview/views/TiledImage.py +116 -0
- sortingview/views/TimeseriesGraph.py +158 -0
- sortingview/views/UnitImages.py +41 -0
- sortingview/views/UnitLocations.py +36 -0
- sortingview/views/UnitMetricsGraph.py +47 -0
- sortingview/views/UnitSimilarityMatrix.py +34 -0
- sortingview/views/UnitsTable.py +51 -0
- sortingview/views/View.py +176 -0
- sortingview/views/__init__.py +31 -0
- sortingview/views/franklab/DecodedLinearPositionV1.py +43 -0
- sortingview/views/franklab/TrackPositionAnimationV1.py +187 -0
- sortingview/views/franklab/__init__.py +2 -0
- sortingview-0.15.0.dist-info/METADATA +25 -0
- sortingview-0.15.0.dist-info/RECORD +119 -0
- sortingview-0.15.0.dist-info/WHEEL +5 -0
- sortingview-0.15.0.dist-info/licenses/LICENSE +201 -0
- sortingview-0.15.0.dist-info/top_level.txt +3 -0
- tests/helpers/compute_correlogram_data.py +66 -0
- tests/test_autocorrelograms.py +32 -0
- tests/test_average_waveforms.py +71 -0
- tests/test_confusion_matrix.py +95 -0
- tests/test_cross_correlograms.py +35 -0
- tests/test_large_timeseries_graph.py +46 -0
- tests/test_layout.py +65 -0
- tests/test_markdown.py +38 -0
- tests/test_position_plot.py +27 -0
- tests/test_raster_plot.py +32 -0
- tests/test_sorting_curation_2.py +37 -0
- tests/test_sorting_selection.py +68 -0
- tests/test_spike_amplitudes.py +37 -0
- tests/test_spike_locations.py +74 -0
- tests/test_tab_layout.py +45 -0
- tests/test_tiled_image.py +68 -0
- tests/test_track_position_animation.py +73 -0
- tests/test_unit_locations.py +59 -0
- tests/test_unit_metrics_graph.py +41 -0
- tests/test_unit_similarity_matrix.py +33 -0
- tests/test_units_table.py +40 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
import sortingview.views as vv
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def units_table_view(self, *, unit_ids: List[int], unit_metrics: Union[List[dict], None] = None, label: Union[str, None] = None):
|
|
7
|
+
from .SpikeSortingView import SpikeSortingView
|
|
8
|
+
|
|
9
|
+
assert isinstance(self, SpikeSortingView)
|
|
10
|
+
|
|
11
|
+
if label is None:
|
|
12
|
+
label = "Units table"
|
|
13
|
+
|
|
14
|
+
traces_sample = self.get_traces_sample(segment=0)
|
|
15
|
+
noise_level = estimate_noise_level(traces_sample)
|
|
16
|
+
|
|
17
|
+
columns: List[vv.UnitsTableColumn] = [
|
|
18
|
+
vv.UnitsTableColumn(key="unitId", label="Unit", dtype="int"),
|
|
19
|
+
vv.UnitsTableColumn(key="numEvents", label="Num. events", dtype="int"),
|
|
20
|
+
vv.UnitsTableColumn(key="firingRateHz", label="Firing rate (Hz)", dtype="float"),
|
|
21
|
+
vv.UnitsTableColumn(key="snr", label="SNR", dtype="float"),
|
|
22
|
+
]
|
|
23
|
+
if unit_metrics is not None:
|
|
24
|
+
for u in unit_metrics:
|
|
25
|
+
columns.append(vv.UnitsTableColumn(key=f'metric:{u["name"]}', label=u["label"], dtype="float"))
|
|
26
|
+
rows: List[vv.UnitsTableRow] = []
|
|
27
|
+
for unit_id in unit_ids:
|
|
28
|
+
spike_train = self.get_unit_spike_train(unit_id=unit_id)
|
|
29
|
+
spike_amplitudes = self.get_unit_spike_amplitudes(unit_id=unit_id)
|
|
30
|
+
avg_spike_amplitude = np.mean(spike_amplitudes)
|
|
31
|
+
snr = np.abs(avg_spike_amplitude) / noise_level
|
|
32
|
+
values = {"unitId": unit_id, "numEvents": len(spike_train), "firingRateHz": len(spike_train) / (self.num_frames / self.sampling_frequency), "snr": snr}
|
|
33
|
+
if unit_metrics is not None:
|
|
34
|
+
for u in unit_metrics:
|
|
35
|
+
d = u["data"].get(str(unit_id), None)
|
|
36
|
+
if d is not None:
|
|
37
|
+
values[f'metric:{u["name"]}'] = d
|
|
38
|
+
rows.append(vv.UnitsTableRow(unit_id=unit_id, values=values))
|
|
39
|
+
view = vv.UnitsTable(columns=columns, rows=rows)
|
|
40
|
+
return view
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def estimate_noise_level(traces: np.ndarray):
|
|
44
|
+
est_noise_level = np.median(np.abs(traces - np.mean(traces, axis=0))) / 0.6745 # median absolute deviation (MAD) estimate of stdev
|
|
45
|
+
return est_noise_level
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
from .Figure import Figure
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_position_pdf_plot(*, start_time_sec: np.float32, sampling_frequency: np.float32, pdf: np.ndarray, label: str):
|
|
7
|
+
# Nt = pdf.shape[0]
|
|
8
|
+
# Np = pdf.shape[1]
|
|
9
|
+
|
|
10
|
+
A = pdf
|
|
11
|
+
B = A / np.reshape(np.repeat(np.max(A, axis=1), A.shape[1]), A.shape)
|
|
12
|
+
B = (B * 100).astype(np.uint8)
|
|
13
|
+
|
|
14
|
+
data = {"type": "PositionPdfPlot", "pdf": B, "samplingFrequency": sampling_frequency, "startTimeSec": start_time_sec}
|
|
15
|
+
return Figure(data=data, label=label)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def create_live_position_pdf_plot(
|
|
19
|
+
*,
|
|
20
|
+
start_time_sec: np.float32,
|
|
21
|
+
end_time_sec: np.float32,
|
|
22
|
+
sampling_frequency: np.float32,
|
|
23
|
+
num_positions: int,
|
|
24
|
+
pdf_object: dict,
|
|
25
|
+
segment_size: int,
|
|
26
|
+
multiscale_factor: int,
|
|
27
|
+
label: str,
|
|
28
|
+
linear_positions: Union[np.ndarray, None] = None
|
|
29
|
+
):
|
|
30
|
+
data = {
|
|
31
|
+
"type": "LivePositionPdfPlot",
|
|
32
|
+
"pdfObject": pdf_object,
|
|
33
|
+
"startTimeSec": start_time_sec,
|
|
34
|
+
"endTimeSec": end_time_sec,
|
|
35
|
+
"numPositions": num_positions,
|
|
36
|
+
"samplingFrequency": sampling_frequency,
|
|
37
|
+
"segmentSize": segment_size,
|
|
38
|
+
"multiscaleFactor": multiscale_factor,
|
|
39
|
+
}
|
|
40
|
+
if linear_positions is not None:
|
|
41
|
+
data["linearPositions"] = linear_positions
|
|
42
|
+
return Figure(data=data, label=label)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# def _get_subsample_inds(timestamps: np.ndarray, sampling_frequency: float):
|
|
46
|
+
# dt = 1 / sampling_frequency
|
|
47
|
+
# ret = []
|
|
48
|
+
# last_t = timestamps[0] - dt * 2
|
|
49
|
+
# for i in range(len(timestamps)):
|
|
50
|
+
# delta = timestamps[i] - last_t
|
|
51
|
+
# if delta >= dt * 0.95:
|
|
52
|
+
# ret.append(i)
|
|
53
|
+
# last_t = timestamps[i]
|
|
54
|
+
# return ret
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
from .Figure import Figure
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_position_plot(
|
|
7
|
+
*, timestamps: np.ndarray, positions: np.ndarray, dimension_labels: List[str], label: str, subsampling_frequency: Union[float, None] = None, discontinuous: bool = False
|
|
8
|
+
):
|
|
9
|
+
print("WARNING: create_position_plot is deprecated. Instead use vv.PositionPlot(...). See tests/test_position_plot.py")
|
|
10
|
+
if positions.ndim == 1:
|
|
11
|
+
positions = np.reshape(positions, (len(positions), 1))
|
|
12
|
+
assert positions.shape[0] == len(timestamps)
|
|
13
|
+
assert len(dimension_labels) == positions.shape[1]
|
|
14
|
+
if subsampling_frequency is not None:
|
|
15
|
+
inds = _get_subsample_inds(timestamps, subsampling_frequency)
|
|
16
|
+
timestamps = timestamps[inds]
|
|
17
|
+
positions = positions[inds, :]
|
|
18
|
+
data = {
|
|
19
|
+
"type": "PositionPlot",
|
|
20
|
+
"timeOffset": timestamps[0],
|
|
21
|
+
"timestamps": (timestamps - timestamps[0]).astype(np.float32),
|
|
22
|
+
"positions": positions,
|
|
23
|
+
"dimensionLabels": dimension_labels,
|
|
24
|
+
"discontinuous": discontinuous,
|
|
25
|
+
}
|
|
26
|
+
return Figure(data=data, label=label)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _get_subsample_inds(timestamps: np.ndarray, sampling_frequency: float):
|
|
30
|
+
dt = 1 / sampling_frequency
|
|
31
|
+
ret = []
|
|
32
|
+
last_t = timestamps[0] - dt * 2
|
|
33
|
+
for i in range(len(timestamps)):
|
|
34
|
+
delta = timestamps[i] - last_t
|
|
35
|
+
if delta >= dt * 0.95:
|
|
36
|
+
ret.append(i)
|
|
37
|
+
last_t = timestamps[i]
|
|
38
|
+
return ret
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
from .Figure import Figure
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_raw_traces_plot(*, start_time_sec: float, sampling_frequency: float, traces: np.ndarray, label: str):
|
|
7
|
+
data = {"type": "RawTracesPlot", "startTimeSec": start_time_sec, "samplingFrequency": sampling_frequency, "traces": traces}
|
|
8
|
+
return Figure(data=data, label=label)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from typing import List, Union
|
|
2
|
+
import numpy as np
|
|
3
|
+
from .Figure import Figure
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_spike_raster_plot(*, times: np.ndarray, labels: np.ndarray, label: str):
|
|
7
|
+
unit_ids = np.sort(np.unique(labels))
|
|
8
|
+
plots = []
|
|
9
|
+
for unit_id in unit_ids:
|
|
10
|
+
inds = np.where(labels == unit_id)[0]
|
|
11
|
+
spike_times_sec = times[inds]
|
|
12
|
+
plots.append({"unitId": unit_id, "spikeTimesSec": spike_times_sec.astype(np.float32)})
|
|
13
|
+
|
|
14
|
+
data = {"type": "RasterPlot", "startTimeSec": np.min(times), "endTimeSec": np.max(times), "plots": plots}
|
|
15
|
+
return Figure(data=data, label=label)
|
|
File without changes
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def compute_correlogram_data(*, times1: np.ndarray, times2: Optional[np.ndarray], sampling_frequency: float, window_size_msec: float, bin_size_msec: float):
|
|
6
|
+
num_bins = int(window_size_msec / bin_size_msec)
|
|
7
|
+
if num_bins % 2 == 0:
|
|
8
|
+
num_bins = num_bins - 1 # odd number of bins
|
|
9
|
+
num_bins_half = int((num_bins + 1) / 2)
|
|
10
|
+
bin_edges_msec = np.array((np.arange(num_bins + 1) - num_bins / 2) * bin_size_msec, dtype=np.float32)
|
|
11
|
+
bin_counts = np.zeros((num_bins,), dtype=np.int32)
|
|
12
|
+
if times2 is None:
|
|
13
|
+
# autocorrelogram
|
|
14
|
+
offset = 1
|
|
15
|
+
while True:
|
|
16
|
+
if offset >= len(times1):
|
|
17
|
+
break
|
|
18
|
+
deltas_msec = (times1[offset:] - times1[:-offset]) / sampling_frequency * 1000
|
|
19
|
+
deltas_msec = deltas_msec[deltas_msec <= bin_edges_msec[-1]]
|
|
20
|
+
if len(deltas_msec) == 0:
|
|
21
|
+
break
|
|
22
|
+
for i in range(num_bins_half):
|
|
23
|
+
start_msec = bin_edges_msec[num_bins_half - 1 + i]
|
|
24
|
+
end_msec = bin_edges_msec[num_bins_half + i]
|
|
25
|
+
ct = len(deltas_msec[(start_msec <= deltas_msec) & (deltas_msec < end_msec)])
|
|
26
|
+
bin_counts[num_bins_half - 1 + i] += ct
|
|
27
|
+
bin_counts[num_bins_half - 1 - i] += ct
|
|
28
|
+
offset = offset + 1
|
|
29
|
+
else:
|
|
30
|
+
# cross-correlogram
|
|
31
|
+
all_times = np.concatenate((times1, times2))
|
|
32
|
+
all_labels = np.concatenate((1 * np.ones(times1.shape), 2 * np.ones(times2.shape)))
|
|
33
|
+
sort_inds = np.argsort(all_times)
|
|
34
|
+
all_times = all_times[sort_inds]
|
|
35
|
+
all_labels = all_labels[sort_inds]
|
|
36
|
+
offset = 1
|
|
37
|
+
while True:
|
|
38
|
+
if offset >= len(all_times):
|
|
39
|
+
break
|
|
40
|
+
deltas_msec = (all_times[offset:] - all_times[:-offset]) / sampling_frequency * 1000
|
|
41
|
+
|
|
42
|
+
deltas12_msec = deltas_msec[(all_labels[offset:] == 2) & (all_labels[:-offset] == 1)]
|
|
43
|
+
deltas21_msec = deltas_msec[(all_labels[offset:] == 1) & (all_labels[:-offset] == 2)]
|
|
44
|
+
deltas11_msec = deltas_msec[(all_labels[offset:] == 1) & (all_labels[:-offset] == 1)]
|
|
45
|
+
deltas22_msec = deltas_msec[(all_labels[offset:] == 2) & (all_labels[:-offset] == 2)]
|
|
46
|
+
|
|
47
|
+
deltas12_msec = deltas12_msec[deltas12_msec <= bin_edges_msec[-1]]
|
|
48
|
+
deltas21_msec = deltas21_msec[deltas21_msec <= bin_edges_msec[-1]]
|
|
49
|
+
deltas11_msec = deltas11_msec[deltas11_msec <= bin_edges_msec[-1]]
|
|
50
|
+
deltas22_msec = deltas22_msec[deltas22_msec <= bin_edges_msec[-1]]
|
|
51
|
+
|
|
52
|
+
if (len(deltas12_msec) + len(deltas21_msec) + len(deltas11_msec) + len(deltas22_msec)) == 0:
|
|
53
|
+
break
|
|
54
|
+
|
|
55
|
+
for i in range(num_bins_half):
|
|
56
|
+
start_msec = bin_edges_msec[num_bins_half - 1 + i]
|
|
57
|
+
end_msec = bin_edges_msec[num_bins_half + i]
|
|
58
|
+
ct12 = len(deltas12_msec[(start_msec <= deltas12_msec) & (deltas12_msec < end_msec)])
|
|
59
|
+
ct21 = len(deltas21_msec[(start_msec <= deltas21_msec) & (deltas21_msec < end_msec)])
|
|
60
|
+
bin_counts[num_bins_half - 1 + i] += ct12
|
|
61
|
+
bin_counts[num_bins_half - 1 - i] += ct21
|
|
62
|
+
offset = offset + 1
|
|
63
|
+
return {"bin_edges_sec": (bin_edges_msec / 1000).astype(np.float32), "bin_counts": bin_counts.astype(np.int32)}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
from typing import Tuple, Union
|
|
2
|
+
from tempfile import TemporaryDirectory
|
|
3
|
+
import json
|
|
4
|
+
import math
|
|
5
|
+
import numpy as np
|
|
6
|
+
import h5py
|
|
7
|
+
import hashlib
|
|
8
|
+
import spikeinterface as si
|
|
9
|
+
import kachery as ka
|
|
10
|
+
import spikeinterface.preprocessing as spre
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def prepare_spikesortingview_data(
|
|
14
|
+
*,
|
|
15
|
+
recording: si.BaseRecording,
|
|
16
|
+
sorting: si.BaseSorting,
|
|
17
|
+
segment_duration_sec: float,
|
|
18
|
+
snippet_len: Tuple[int, int],
|
|
19
|
+
max_num_snippets_per_segment: Union[int, None],
|
|
20
|
+
channel_neighborhood_size: int,
|
|
21
|
+
bandpass_filter: bool = False,
|
|
22
|
+
) -> str:
|
|
23
|
+
# NOTE(DS): for data longer than 25hours with fs = 20000; num_frame is too large for int32
|
|
24
|
+
if recording.get_num_frames() > (2**31 - 1):
|
|
25
|
+
int_type = np.int64
|
|
26
|
+
else:
|
|
27
|
+
int_type = np.int32
|
|
28
|
+
# int_type = np.int64 #NOTE(DS): to test in short recording
|
|
29
|
+
|
|
30
|
+
print(f"int_type: {int_type}")
|
|
31
|
+
|
|
32
|
+
if bandpass_filter:
|
|
33
|
+
recording = spre.bandpass_filter(recording)
|
|
34
|
+
unit_ids = np.array(sorting.get_unit_ids()).astype(np.int32)
|
|
35
|
+
channel_ids = np.array(recording.get_channel_ids()).astype(np.int32)
|
|
36
|
+
sampling_frequency = recording.get_sampling_frequency()
|
|
37
|
+
num_frames = recording.get_num_frames()
|
|
38
|
+
num_frames_per_segment = math.ceil(segment_duration_sec * sampling_frequency)
|
|
39
|
+
num_segments = math.ceil(num_frames / num_frames_per_segment)
|
|
40
|
+
if hasattr(recording, "has_scaleable_traces") and callable(getattr(recording, "has_scaleable_traces")):
|
|
41
|
+
scalable = recording.has_scaleable_traces()
|
|
42
|
+
elif hasattr(recording, "has_scaled") and callable(getattr(recording, "has_scaled")):
|
|
43
|
+
scalable = recording.has_scaled()
|
|
44
|
+
else:
|
|
45
|
+
scalable = False
|
|
46
|
+
|
|
47
|
+
with TemporaryDirectory() as tmpdir:
|
|
48
|
+
output_file_name = tmpdir + "/spikesortingview.h5"
|
|
49
|
+
with h5py.File(output_file_name, "w") as f:
|
|
50
|
+
f.create_dataset("unit_ids", data=unit_ids)
|
|
51
|
+
f.create_dataset(
|
|
52
|
+
"sampling_frequency",
|
|
53
|
+
data=np.array([sampling_frequency]).astype(np.float32),
|
|
54
|
+
)
|
|
55
|
+
f.create_dataset("channel_ids", data=channel_ids)
|
|
56
|
+
f.create_dataset("num_frames", data=np.array([num_frames]).astype(int_type))
|
|
57
|
+
channel_locations = recording.get_channel_locations()
|
|
58
|
+
f.create_dataset("channel_locations", data=np.array(channel_locations))
|
|
59
|
+
f.create_dataset("num_segments", data=np.array([num_segments]).astype(np.int32))
|
|
60
|
+
f.create_dataset(
|
|
61
|
+
"num_frames_per_segment",
|
|
62
|
+
data=np.array([num_frames_per_segment]).astype(np.int32),
|
|
63
|
+
)
|
|
64
|
+
f.create_dataset(
|
|
65
|
+
"snippet_len",
|
|
66
|
+
data=np.array([snippet_len[0], snippet_len[1]]).astype(np.int32),
|
|
67
|
+
)
|
|
68
|
+
f.create_dataset(
|
|
69
|
+
"max_num_snippets_per_segment",
|
|
70
|
+
data=np.array([max_num_snippets_per_segment]).astype(np.int32),
|
|
71
|
+
)
|
|
72
|
+
f.create_dataset(
|
|
73
|
+
"channel_neighborhood_size",
|
|
74
|
+
data=np.array([channel_neighborhood_size]).astype(np.int32),
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# first get peak channels and channel neighborhoods
|
|
78
|
+
unit_peak_channel_ids = {}
|
|
79
|
+
fallback_unit_peak_channel_ids = {}
|
|
80
|
+
unit_channel_neighborhoods = {}
|
|
81
|
+
for iseg in range(num_segments):
|
|
82
|
+
something_missing = False
|
|
83
|
+
for unit_id in unit_ids:
|
|
84
|
+
if str(unit_id) not in unit_peak_channel_ids:
|
|
85
|
+
something_missing = True
|
|
86
|
+
if not something_missing:
|
|
87
|
+
break
|
|
88
|
+
print(f"Initial pass: segment {iseg}")
|
|
89
|
+
start_frame = iseg * num_frames_per_segment
|
|
90
|
+
end_frame = min(start_frame + num_frames_per_segment, num_frames)
|
|
91
|
+
start_frame_with_padding = max(start_frame - snippet_len[0], 0)
|
|
92
|
+
end_frame_with_padding = min(end_frame + snippet_len[1], num_frames)
|
|
93
|
+
traces_with_padding = recording.get_traces(
|
|
94
|
+
start_frame=start_frame_with_padding,
|
|
95
|
+
end_frame=end_frame_with_padding,
|
|
96
|
+
return_scaled=scalable,
|
|
97
|
+
)
|
|
98
|
+
assert isinstance(traces_with_padding, np.ndarray)
|
|
99
|
+
for unit_id in unit_ids:
|
|
100
|
+
if str(unit_id) not in unit_peak_channel_ids:
|
|
101
|
+
spike_train = sorting.get_unit_spike_train(
|
|
102
|
+
unit_id=unit_id,
|
|
103
|
+
start_frame=start_frame,
|
|
104
|
+
end_frame=end_frame,
|
|
105
|
+
)
|
|
106
|
+
assert isinstance(spike_train, np.ndarray)
|
|
107
|
+
if len(spike_train) > 0:
|
|
108
|
+
values = traces_with_padding[spike_train - start_frame_with_padding, :].astype(np.int32)
|
|
109
|
+
avg_value = np.mean(values, axis=0)
|
|
110
|
+
peak_channel_ind = np.argmax(np.abs(avg_value))
|
|
111
|
+
peak_channel_id = channel_ids[peak_channel_ind]
|
|
112
|
+
channel_neighborhood = get_channel_neighborhood(
|
|
113
|
+
channel_ids=channel_ids,
|
|
114
|
+
channel_locations=channel_locations,
|
|
115
|
+
peak_channel_id=peak_channel_id,
|
|
116
|
+
channel_neighborhood_size=channel_neighborhood_size,
|
|
117
|
+
)
|
|
118
|
+
if len(spike_train) >= 10:
|
|
119
|
+
unit_peak_channel_ids[str(unit_id)] = peak_channel_id
|
|
120
|
+
else:
|
|
121
|
+
fallback_unit_peak_channel_ids[str(unit_id)] = peak_channel_id
|
|
122
|
+
unit_channel_neighborhoods[str(unit_id)] = channel_neighborhood
|
|
123
|
+
for unit_id in unit_ids:
|
|
124
|
+
peak_channel_id = unit_peak_channel_ids.get(str(unit_id), None)
|
|
125
|
+
if peak_channel_id is None:
|
|
126
|
+
peak_channel_id = fallback_unit_peak_channel_ids.get(str(unit_id), None)
|
|
127
|
+
if peak_channel_id is None:
|
|
128
|
+
raise Exception(f"Peak channel not found for unit {unit_id}. This is probably because no spikes were found in any segment for this unit.")
|
|
129
|
+
channel_neighborhood = unit_channel_neighborhoods[str(unit_id)]
|
|
130
|
+
f.create_dataset(
|
|
131
|
+
f"unit/{unit_id}/peak_channel_id",
|
|
132
|
+
data=np.array([peak_channel_id]).astype(np.int32),
|
|
133
|
+
)
|
|
134
|
+
f.create_dataset(
|
|
135
|
+
f"unit/{unit_id}/channel_neighborhood",
|
|
136
|
+
data=np.array(channel_neighborhood).astype(np.int32),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
for iseg in range(num_segments):
|
|
140
|
+
print(f"Segment {iseg} of {num_segments}")
|
|
141
|
+
start_frame = iseg * num_frames_per_segment
|
|
142
|
+
end_frame = min(start_frame + num_frames_per_segment, num_frames)
|
|
143
|
+
start_frame_with_padding = max(start_frame - snippet_len[0], 0)
|
|
144
|
+
end_frame_with_padding = min(end_frame + snippet_len[1], num_frames)
|
|
145
|
+
traces_with_padding = recording.get_traces(
|
|
146
|
+
start_frame=start_frame_with_padding,
|
|
147
|
+
end_frame=end_frame_with_padding,
|
|
148
|
+
return_scaled=scalable,
|
|
149
|
+
)
|
|
150
|
+
traces_sample = traces_with_padding[
|
|
151
|
+
start_frame - start_frame_with_padding : start_frame - start_frame_with_padding + int(sampling_frequency * 1),
|
|
152
|
+
:,
|
|
153
|
+
]
|
|
154
|
+
f.create_dataset(f"segment/{iseg}/traces_sample", data=traces_sample)
|
|
155
|
+
all_subsampled_spike_trains = []
|
|
156
|
+
for unit_id in unit_ids:
|
|
157
|
+
peak_channel_id = unit_peak_channel_ids.get(str(unit_id), None)
|
|
158
|
+
if peak_channel_id is None:
|
|
159
|
+
peak_channel_id = fallback_unit_peak_channel_ids.get(str(unit_id), None)
|
|
160
|
+
if peak_channel_id is None:
|
|
161
|
+
raise Exception(f"Peak channel not found for unit {unit_id}. This is probably because no spikes were found in any segment for this unit.")
|
|
162
|
+
spike_train = sorting.get_unit_spike_train(unit_id=unit_id, start_frame=start_frame, end_frame=end_frame).astype(int_type)
|
|
163
|
+
f.create_dataset(f"segment/{iseg}/unit/{unit_id}/spike_train", data=spike_train)
|
|
164
|
+
channel_neighborhood = unit_channel_neighborhoods[str(unit_id)]
|
|
165
|
+
peak_channel_ind = channel_ids.tolist().index(peak_channel_id)
|
|
166
|
+
if len(spike_train) > 0:
|
|
167
|
+
spike_amplitudes = traces_with_padding[spike_train - start_frame_with_padding, peak_channel_ind]
|
|
168
|
+
f.create_dataset(
|
|
169
|
+
f"segment/{iseg}/unit/{unit_id}/spike_amplitudes",
|
|
170
|
+
data=spike_amplitudes,
|
|
171
|
+
)
|
|
172
|
+
else:
|
|
173
|
+
spike_amplitudes = np.array([], dtype=np.int32)
|
|
174
|
+
if max_num_snippets_per_segment is not None and len(spike_train) > max_num_snippets_per_segment:
|
|
175
|
+
subsampled_spike_train = subsample(spike_train, max_num_snippets_per_segment)
|
|
176
|
+
else:
|
|
177
|
+
subsampled_spike_train = spike_train
|
|
178
|
+
f.create_dataset(
|
|
179
|
+
f"segment/{iseg}/unit/{unit_id}/subsampled_spike_train",
|
|
180
|
+
data=subsampled_spike_train,
|
|
181
|
+
)
|
|
182
|
+
all_subsampled_spike_trains.append(subsampled_spike_train)
|
|
183
|
+
subsampled_spike_trains_concat = np.concatenate(all_subsampled_spike_trains)
|
|
184
|
+
# print('Extracting spike snippets')
|
|
185
|
+
spike_snippets_concat = extract_spike_snippets(
|
|
186
|
+
traces=traces_with_padding,
|
|
187
|
+
times=subsampled_spike_trains_concat - start_frame_with_padding,
|
|
188
|
+
snippet_len=snippet_len,
|
|
189
|
+
)
|
|
190
|
+
# print('Collecting spike snippets')
|
|
191
|
+
index = 0
|
|
192
|
+
for ii, unit_id in enumerate(unit_ids):
|
|
193
|
+
channel_neighborhood = unit_channel_neighborhoods[str(unit_id)]
|
|
194
|
+
channel_neighborhood_indices = [channel_ids.tolist().index(ch_id) for ch_id in channel_neighborhood]
|
|
195
|
+
num = len(all_subsampled_spike_trains[ii])
|
|
196
|
+
spike_snippets = spike_snippets_concat[index : index + num, :, channel_neighborhood_indices]
|
|
197
|
+
index = index + num
|
|
198
|
+
f.create_dataset(
|
|
199
|
+
f"segment/{iseg}/unit/{unit_id}/subsampled_spike_snippets",
|
|
200
|
+
data=spike_snippets,
|
|
201
|
+
)
|
|
202
|
+
uri = ka.store_file_local(output_file_name)
|
|
203
|
+
return uri
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def get_channel_neighborhood(
|
|
207
|
+
*,
|
|
208
|
+
channel_ids: np.ndarray,
|
|
209
|
+
channel_locations: np.ndarray,
|
|
210
|
+
peak_channel_id: int,
|
|
211
|
+
channel_neighborhood_size: int,
|
|
212
|
+
):
|
|
213
|
+
channel_locations_by_id = {}
|
|
214
|
+
for ii, channel_id in enumerate(channel_ids):
|
|
215
|
+
channel_locations_by_id[channel_id] = channel_locations[ii]
|
|
216
|
+
peak_location = channel_locations_by_id[int(peak_channel_id)]
|
|
217
|
+
distances = []
|
|
218
|
+
for channel_id in channel_ids:
|
|
219
|
+
loc = channel_locations_by_id[int(channel_id)]
|
|
220
|
+
dist = np.linalg.norm(np.array(loc) - np.array(peak_location))
|
|
221
|
+
distances.append(dist)
|
|
222
|
+
sorted_indices = np.argsort(distances)
|
|
223
|
+
neighborhood_channel_ids = []
|
|
224
|
+
for ii in range(min(channel_neighborhood_size, len(channel_ids))):
|
|
225
|
+
neighborhood_channel_ids.append(int(channel_ids[sorted_indices[ii]]))
|
|
226
|
+
return neighborhood_channel_ids
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def subsample(x: np.ndarray, num: int):
|
|
230
|
+
if num >= len(x):
|
|
231
|
+
return x
|
|
232
|
+
stride = math.floor(len(x) / num)
|
|
233
|
+
return x[0 : stride * num : stride]
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def extract_spike_snippets(*, traces: np.ndarray, times: np.ndarray, snippet_len: Tuple[int, int]):
|
|
237
|
+
a = snippet_len[0]
|
|
238
|
+
b = snippet_len[1]
|
|
239
|
+
T = a + b
|
|
240
|
+
M = traces.shape[1]
|
|
241
|
+
L = len(times)
|
|
242
|
+
ret = np.zeros((L, T, M), dtype=traces.dtype)
|
|
243
|
+
if L > 0:
|
|
244
|
+
for t in range(T):
|
|
245
|
+
valid_inds = np.where(times < traces.shape[0] - b)
|
|
246
|
+
ret[valid_inds, t, :] = traces[times[valid_inds] - a + t, :]
|
|
247
|
+
return ret
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _sha1_of_string(txt: str) -> str:
|
|
251
|
+
hh = hashlib.sha1(txt.encode("utf-8"))
|
|
252
|
+
ret = hh.hexdigest()
|
|
253
|
+
return ret
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _sha1_of_object(obj: object) -> str:
|
|
257
|
+
txt = json.dumps(obj, sort_keys=True, separators=(",", ":"))
|
|
258
|
+
return _sha1_of_string(txt)
|
sortingview/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .version import __version__ # noqa
|