Skip to content

Commit 5e96711

Browse files
committed
Auto stash before rebase of "origin/develop"
1 parent a436d27 commit 5e96711

File tree

4 files changed

+158
-14
lines changed

4 files changed

+158
-14
lines changed

viewspikes/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def get_ks2(raw, dsets, one):
4343

4444

4545
def get_spikes(dsets, one):
46-
dtypes_spikes = ['spikes.clusters', 'spikes.amps', 'spikes.times', 'clusters.channels', 'spikes.samples']
46+
dtypes_spikes = ['spikes.clusters', 'spikes.amps', 'spikes.times', 'clusters.channels',
47+
'spikes.samples', 'spikes.depths']
4748
dsets_spikes = [dset for dset in dsets if dset['dataset_type'] in dtypes_spikes]
4849
malf_path = next(iter(one.download_datasets(dsets_spikes))).parent
4950
channels = alf.io.load_object(malf_path, 'channels')

viewspikes/examples_local.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from pathlib import Path
2+
import numpy as np
3+
import scipy
4+
import matplotlib.pyplot as plt
5+
from easyqc.gui import viewseis
6+
7+
import alf.io
8+
from oneibl.one import ONE
9+
from ibllib.io import spikeglx
10+
from ibllib.ephys import neuropixel
11+
from ibllib.dsp import voltage
12+
from brainbox.plot import driftmap
13+
14+
from needles2 import run_needles2
15+
from viewspikes.data import stream, get_ks2, get_spikes
16+
from viewspikes.plots import plot_insertion, show_psd, overlay_spikes
17+
18+
RAW_PATH = Path("/datadisk/Data/spike_sorting/benchmark/raw")
19+
SORT_PATH = Path("/datadisk/team_drives/WG-Neural-Analysis/Spike-Sorting-Analysis/benchmarks")
20+
SORTERS = ['ks2','ks3', 'pyks2.5']
21+
22+
"8413c5c6-b42b-4ec6-b751-881a54413628",
23+
"8ca1a850-26ef-42be-8b28-c2e2d12f06d6",
24+
"ce24bbe9-ae70-4659-9e9c-564d1a865de8",
25+
"ce397420-3cd2-4a55-8fd1-5e28321981f4",
26+
27+
# Example 1
28+
pid, t0 = ("8413c5c6-b42b-4ec6-b751-881a54413628", 810)
29+
bin_file = next(RAW_PATH.joinpath(pid).rglob("*.ap.bin"))
30+
sr = spikeglx.Reader(bin_file)
31+
sel = slice(int(t0 * sr.fs), int((t0 + 4) * sr.fs))
32+
raw = sr[sel, :-1].T
33+
34+
35+
# Example 2: Plot Insertion for a given PID
36+
av = run_needles2.view(lazy=True)
37+
av.add_insertion_by_id(pid)
38+
39+
# Example 3: Show the PSD
40+
fig, ax = plt.subplots()
41+
fig.set_size_inches(14, 7)
42+
show_psd(raw, sr.fs, ax=ax)
43+
44+
# Example 4: Display the raw / pre-proc
45+
h = neuropixel.trace_header()
46+
sos = scipy.signal.butter(3, 300 / sr.fs / 2, btype='highpass', output='sos')
47+
butt = scipy.signal.sosfiltfilt(sos, raw)
48+
fk_kwargs ={'dx': 1, 'vbounds': [0, 1e6], 'ntr_pad': 160, 'ntr_tap': 0, 'lagc': .01, 'btype': 'lowpass'}
49+
destripe = voltage.destripe(raw, fs=sr.fs, fk_kwargs=fk_kwargs, tr_sel=np.arange(raw.shape[0]))
50+
eqc_butt = viewseis(butt.T, si=1 / sr.fs, h=h, t0=t0, title='butt', taxis=0)
51+
eqc_dest = viewseis(destripe.T, si=1 / sr.fs, h=h, t0=t0, title='destr', taxis=0)
52+
eqc_dest_ = viewseis(destripe.T, si=1 / sr.fs, h=h, t0=t0, title='destr_', taxis=0)
53+
# Example 5: overlay the spikes on the existing easyqc instances
54+
from ibllib.plots import color_cycle
55+
ss = {}
56+
symbols = 'x+o'
57+
eqcsort = {}
58+
for i, sorter in enumerate(SORTERS):
59+
alf_path = SORT_PATH.joinpath(sorter, pid,'alf')
60+
ss[sorter] = {}
61+
for k in ['spikes', 'clusters', 'channels']:
62+
ss[sorter][k] = alf.io.load_object(alf_path, k)
63+
col = (np.array(color_cycle(i)) * 255).astype(np.uint8)
64+
eqcsort[sorter] = viewseis(destripe.T, si=1 / sr.fs, h=h, t0=t0, title=sorter, taxis=0)
65+
_, _, _ = overlay_spikes(
66+
eqcsort[sorter], ss[sorter]['spikes'], ss[sorter]['clusters'], ss[sorter]['channels'],
67+
label=sorter, symbol=symbols[i])
68+
_, _, _ = overlay_spikes(
69+
eqc_dest_, ss[sorter]['spikes'], ss[sorter]['clusters'], ss[sorter]['channels'],
70+
rgb=tuple(col), label=sorter, symbol=symbols[i])
71+
# overlay_spikes(eqc_dest, ss[sorter]['spikes'], ss[sorter]['clusters'], ss[sorter]['channels'])
72+
# sc.setPen(pg.mkPen((0, 255, 0, 155), width=1))
73+
74+
##
75+
from ibllib.dsp.fourier import fshift
76+
from ibllib.dsp.voltage import destripe
77+
eqc_butt = viewseis(butt.T, si=1 / sr.fs, h=h, t0=t0, title='butt', taxis=0)
78+
bshift = fshift(butt, h['sample_shift'], axis=1)
79+
eqc_buts = viewseis(bshift.T, si=1 / sr.fs, h=h, t0=t0, title='shift', taxis=0)
80+
81+
82+
83+
##
84+
from oneibl.one import ONE
85+
import alf.io
86+
pid = "8413c5c6-b42b-4ec6-b751-881a54413628"
87+
one = ONE()
88+
89+
dtypes = ['spikes.amps', 'spikes.clusters', 'spikes.times',
90+
'clusters.channels',
91+
'clusters.mlapdv']
92+
93+
from ibllib.atlas import atlas
94+
from ibllib.pipes import histology
95+
from ibllib.ephys import neuropixel
96+
97+
import numpy as np
98+
neuropixel.TIP_SIZE_UM
99+
neuropixel.SITES_COORDINATES
100+
101+
len(one.alyx.rest('datasets', 'list', insertion=pid))
102+
103+
# if we don't have the data on the flatiron
104+
pi = one.alyx.rest('insertions', 'read', id=pid)
105+
traj = one.alyx.rest('trajectories', 'list', probe_insertion=pid)[-1]
106+
ins = atlas.Insertion.from_dict(traj)
107+
xyz_channels = histology.interpolate_along_track(
108+
ins.xyz, (neuropixel.SITES_COORDINATES[:, 1] + neuropixel.TIP_SIZE_UM) / 1e6)
109+
110+
111+
112+
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515

1616
one = ONE()
1717

18+
19+
"da8dfec1-d265-44e8-84ce-6ae9c109b8bd", # SWC_043_2020-09-21_probe00 ok
20+
"b749446c-18e3-4987-820a-50649ab0f826", # KS023_2019-12-10_probe01 ok
21+
"f86e9571-63ff-4116-9c40-aa44d57d2da9", # CSHL049_2020-01-08_probe00 a bit stripy but fine
22+
"675952a4-e8b3-4e82-a179-cc970d5a8b01", # CSH_ZAD_029_2020-09-19_probe01 a bit stripy as well
23+
1824
## Example 1: Stream one second of ephys data
19-
pid, t0 = ('8413c5c6-b42b-4ec6-b751-881a54413628', 610)
25+
pid, t0 = ("e864fca7-40e3-4a80-b736-51d4662405e4", 2155)
2026
sr, dsets = stream(pid, t0=t0, one=one, cache=True)
2127

2228
## Example 2: Plot Insertion for a given PID (todo: use Needles 2 for interactive)
@@ -35,16 +41,37 @@
3541
butt = scipy.signal.sosfiltfilt(sos, raw)
3642
fk_kwargs ={'dx': 1, 'vbounds': [0, 1e6], 'ntr_pad': 160, 'ntr_tap': 0, 'lagc': .01, 'btype': 'lowpass'}
3743
destripe = voltage.destripe(raw, fs=sr.fs, fk_kwargs=fk_kwargs, tr_sel=np.arange(raw.shape[0]))
38-
ks2 = get_ks2(raw, dsets, one)
3944
eqc_butt = viewseis(butt.T, si=1 / sr.fs, h=h, t0=t0, title='butt', taxis=0)
4045
eqc_dest = viewseis(destripe.T, si=1 / sr.fs, h=h, t0=t0, title='destr', taxis=0)
41-
eqc_ks2 = viewseis(ks2.T, si=1 / sr.fs, h=h, t0=t0, title='ks2', taxis=0)
4246

4347
# Example 5: overlay the spikes on the existing easyqc instances
4448
spikes, clusters, channels = get_spikes(dsets, one)
45-
overlay_spikes(eqc_butt, spikes, clusters, channels)
49+
_, tspi, xspi = overlay_spikes(eqc_butt, spikes, clusters, channels)
4650
overlay_spikes(eqc_dest, spikes, clusters, channels)
4751
overlay_spikes(eqc_ks2, spikes, clusters, channels)
4852

4953
# Do the driftmap
5054
driftmap(spikes['times'], spikes['depths'], t_bin=0.1, d_bin=5, ax=axes[1])
55+
56+
57+
##
58+
import alf.io
59+
eid = dsets[0]['session'][-36:]
60+
tdsets = one.alyx.rest('datasets', 'list', session=eid, django='name__icontains,trials.')
61+
one.download_datasets(tdsets)
62+
trials = alf.io.load_object(one.path_from_eid(eid).joinpath('alf'), 'trials')
63+
64+
rewards = trials['feedback_times'][trials['feedbackType'] == 1]
65+
66+
67+
##
68+
69+
rewards = trials['feedback_times'][trials['feedbackType'] == 1]
70+
71+
72+
## do drift map
73+
fig, ax = plt.subplots()
74+
driftmap(spikes['times'], spikes['depths'], t_bin=0.1, d_bin=5, ax=ax)
75+
from ibllib.plots import vertical_lines
76+
vertical_lines(rewards, ymin=0, ymax=3800, ax=ax)
77+

viewspikes/plots.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def plot_alignment(insertion, traj, ind=None):
125125
plt.show()
126126

127127

128-
def overlay_spikes(self, spikes, clusters, channels):
128+
def overlay_spikes(self, spikes, clusters, channels, rgb=None, label='default', symbol='x'):
129129
first_sample = self.ctrl.model.t0 / self.ctrl.model.si
130130
last_sample = first_sample + self.ctrl.model.ns
131131

@@ -140,17 +140,21 @@ def overlay_spikes(self, spikes, clusters, channels):
140140
xspi = np.tile(xspi, n_side_by_side) + addx
141141
yspi = np.tile(tspi, n_side_by_side)
142142
if self.ctrl.model.taxis == 1:
143-
self.ctrl.add_scatter(xspi, tspi)
143+
self.ctrl.add_scatter(xspi, tspi, label=label)
144144
else:
145-
self.ctrl.add_scatter(tspi, xspi)
146-
sc = self.layers['default']['layer']
145+
self.ctrl.add_scatter(tspi, xspi, label=label)
146+
sc = self.layers[label]['layer']
147147
sc.setSize(8)
148-
sc.setSymbol('x')
148+
sc.setSymbol(symbol)
149149
# sc.setPen(pg.mkPen((0, 255, 0, 155), width=1))
150-
rgbs = [list((rgb * 255).astype(np.uint8)) for rgb in color_cycle(spikes['clusters'][ifirst:ilast])]
151-
sc.setBrush([pg.mkBrush(rgb) for rgb in rgbs])
152-
sc.setPen([pg.mkPen(rgb) for rgb in rgbs])
153-
return sc
150+
if rgb is None:
151+
rgbs = [list((rgb * 255).astype(np.uint8)) for rgb in color_cycle(spikes['clusters'][ifirst:ilast])]
152+
sc.setBrush([pg.mkBrush(rgb) for rgb in rgbs])
153+
sc.setPen([pg.mkPen(rgb) for rgb in rgbs])
154+
else:
155+
sc.setBrush(pg.mkBrush(rgb))
156+
sc.setPen(pg.mkPen(rgb))
157+
return sc, tspi, xspi
154158

155159
# sc.setData(x=xspi, y=tspi, brush=pg.mkBrush((255, 0, 0)))
156160
def callback(sc, points, evt):

0 commit comments

Comments
 (0)