|
| 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 | + |
0 commit comments