Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spikeinterface view #4

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added src/viewephys/.DS_Store
Binary file not shown.
130 changes: 121 additions & 9 deletions src/viewephys/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ def on_horizontalSliderReleased(self):
if not self.cbs[k].isChecked():
continue
if k == 'destripe':
data = fcn_destripe(x=data, fs=self.sr.fs, channel_labels=True, h=self.sr.geometry, neuropixel_version=self.sr.major_version)
self.viewers[k] = viewephys(data, self.sr.fs, channels=self.sr.geometry, title=k, t0=t0 * T_SCALAR, t_scalar=T_SCALAR, a_scalar=A_SCALAR)
data = fcn_destripe(x=data, fs=self.sr.fs, channel_labels=True, h=self.sr.geometry,
neuropixel_version=self.sr.major_version)
self.viewers[k] = viewephys(data, self.sr.fs, channels=self.sr.geometry, title=k,
t0=t0 * T_SCALAR, t_scalar=T_SCALAR, a_scalar=A_SCALAR)

def closeEvent(self, event):
for k in self.viewers:
Expand All @@ -121,6 +123,96 @@ def closeEvent(self, event):
ev.close()
self.close()

# =------

class SpikeInterfaceViewer(QtWidgets.QMainWindow):
def __init__(self, recording, save_path_picks=None, *args, **kwargs):
"""
:param parent:
:param sr: ibllib.io.spikeglx.Reader instance
"""
super(SpikeInterfaceViewer, self).__init__(*args, *kwargs)
self.settings = QtCore.QSettings('int-brain-lab', 'SpikeInterfaceViewer')
uic.loadUi(Path(__file__).parent.joinpath('nav_file.ui'), self)
self.setWindowIcon(QtGui.QIcon(str(Path(__file__).parent.joinpath('viewephys.svg'))))
self.horizontalSlider.setMinimum(0)
self.horizontalSlider.setSingleStep(1)
self.horizontalSlider.setTickInterval(10)
self.horizontalSlider.sliderReleased.connect(self.on_horizontalSliderReleased)
self.horizontalSlider.valueChanged.connect(self.on_horizontalSliderValueChanged)
self.label_smin.setText('0')
self.show()
self.viewers = {'recording': None}
# self.cbs = {'butterworth': self.cb_butterworth_ap, 'destripe': self.cb_destripe_ap}
self.recording = recording
# Set save path for picks
if save_path_picks is None:
save_path_picks = Path(__file__).parent.joinpath('picks.csv') # This will save into the viewephys folder
elif save_path_picks.suffix != '.csv':
raise ValueError('Extension of save file must be .csv')
self.save_path_picks = save_path_picks
self.set_recording()

def set_recording(self, *args, **kwargs):
# enable and set slider
num_samples = self.recording.get_num_samples()
fs = self.recording.sampling_frequency
total_duration = self.recording.get_total_duration()
num_channel = self.recording.get_num_channels()
self.horizontalSlider.setMaximum(int(np.floor(num_samples / NSAMP_CHUNK)))
tmax = np.floor(num_samples / NSAMP_CHUNK) * NSAMP_CHUNK / fs

self.label_smax.setText(f"{tmax:0.2f}s")
tlabel = f'{total_duration} seconds long \n' \
f'{fs} Hz Sampling Frequency \n' \
f'{num_channel} Channels'
self.label.setText(tlabel)
first = 0 # first sample
self.horizontalSlider.setValue(first)
self.horizontalSlider.setEnabled(True)
self.on_horizontalSliderReleased()

for k in self.viewers:
# Propagate save path to each view
self.viewers[k].save_path_picks = self.save_path_picks
self.viewers[k].current_sample0 = first
# TODO make sure picks df remain across views
# Load if exists
if self.save_path_picks.exists():
df = pd.read_csv(self.save_path_picks)
self.viewers[k].ctrl.model.pickspikes.load_df(df)
self.viewers[k].update_pick_scatter()


def on_horizontalSliderValueChanged(self):
tcur = self.horizontalSlider.value() * NSAMP_CHUNK / self.recording.sampling_frequency
self.label_sval.setText(f"{tcur:0.2f}s")

def on_horizontalSliderReleased(self):
first = int(float(self.horizontalSlider.value()) * NSAMP_CHUNK)
last = first + int(NSAMP_CHUNK)
data = self.recording.get_traces(start_frame=first, end_frame=last).T

t0 = first / self.recording.sampling_frequency * 0
# TODO if t0 is not zero the sliders bugs and does not lead to display change (empty)

for k in self.viewers:
self.viewers[k] = viewephys(data, self.recording.sampling_frequency,
channels=None, title=k,
t0=t0 * T_SCALAR, t_scalar=T_SCALAR, a_scalar=A_SCALAR)
self.viewers[k].current_sample0 = first
self.viewers[k].update_pick_scatter()


def closeEvent(self, event):
for k in self.viewers:
ev = self.viewers[k]
if ev is not None:
ev.close()
self.close()

#-----


class PickSpikes():

Expand All @@ -134,6 +226,7 @@ def init_df(self, nrow=0):
'trace': np.zeros(nrow, dtype=np.int32) * -1,
'amp': np.zeros(nrow, dtype=np.int32),
'group': np.zeros(nrow, dtype=np.int32),
'sample0': np.zeros(nrow, dtype=np.int32)
})
return init_df

Expand All @@ -151,19 +244,20 @@ def load_df(self, df):

if isinstance(df, pd.DataFrame):
# check all keys are in
indxmissing = np.where(~df.columns.isin(default_df.columns))[0]
indxmissing = np.where(~default_df.columns.isin(df.columns))[0]
if len(indxmissing) > 0:
raise ValueError(f'df does not contain column {default_df.columns[indxmissing]}')
self.update_pick(df)
else:
raise ValueError('df input is not pd.DataFrame')

def new_row_frompick(self, sample=None, trace=None, amp=None, group=None):
def new_row_frompick(self, sample=None, trace=None, amp=None, group=None, sample0=None):
new_row = self.init_df(nrow=1)
new_row['sample'] = sample
new_row['trace'] = trace
new_row['amp'] = amp
new_row['group'] = group
new_row['sample0'] = sample0
return new_row

def add_spike(self, new_row):
Expand All @@ -184,14 +278,18 @@ def remove_spike(self, indx_remove):
df_updated = df_updated.reset_index(drop=True)
self.update_pick(df_updated)


def indx_select(self, sample, trace, s_range=0.5 * 30000, tr_range=3):
iclose = np.where(np.logical_and(
np.abs(self.picks['sample'] - sample) <= (s_range + 1),
np.abs(self.picks['trace'] - trace) <= (tr_range + 1)
))[0]
return iclose

def save_picks(self, save_path):
self.picks.to_csv(save_path, index=False)
# chose format CSV output


class EphysViewer(EasyQC):
keyPressed = QtCore.pyqtSignal(int)

Expand All @@ -202,6 +300,8 @@ def __init__(self, *args, **kwargs):
self.menufile.setEnabled(True)
self.settings = QtCore.QSettings('int-brain-lab', 'EphysViewer')
self.header_curves = {}
self.current_sample0 = 0
self.save_path_picks = None
# menus handling
# menu pick
self.menupick = self.menuBar().addMenu('&Pick')
Expand Down Expand Up @@ -278,6 +378,18 @@ def on_key_picking_mode(self, key):
match key:
case QtCore.Qt.Key.Key_Space:
self.ctrl.model.pick_group += 1
case QtCore.Qt.Key.Key_S:
print(f"Saved picks to: {self.save_path_picks}")
self.ctrl.model.pickspikes.save_picks(self.save_path_picks)

def update_pick_scatter(self):
# updates scatter plot with only picks from T0
df = self.ctrl.model.pickspikes.picks
df_local_picks = df.loc[df["sample0"] == self.current_sample0]

self.ctrl.add_scatter(df_local_picks['sample'] * self.ctrl.model.si,
df_local_picks['trace'],
label='_picks', rgb=PICK_COLOR)

def mouseClickPickingEvent(self, event):
"""
Expand All @@ -294,6 +406,8 @@ def mouseClickPickingEvent(self, event):
return
TR_RANGE = 3
S_RANGE = int(0.5 / self.ctrl.model.si)
# TODO modify s_range so it is scaled according to zoom,
# otherwise can be hard to delete when zoomed out
qxy = self.imageItem_seismic.mapFromScene(event.scenePos())
s, tr = (qxy.x(), qxy.y())
# if event.buttons() == QtCore.Qt.MiddleButton:
Expand Down Expand Up @@ -331,13 +445,11 @@ def mouseClickPickingEvent(self, event):
group = 0 # TODO group
# Create new row
new_row = self.ctrl.model.pickspikes.new_row_frompick(
sample=tmax, trace=xmax, amp=amp, group=group)
sample=tmax, trace=xmax, amp=amp, group=group, sample0=self.current_sample0)
self.ctrl.model.pickspikes.add_spike(new_row=new_row)

# updates scatter plot
self.ctrl.add_scatter(self.ctrl.model.pickspikes.picks['sample'] * self.ctrl.model.si,
self.ctrl.model.pickspikes.picks['trace'],
label='_picks', rgb=PICK_COLOR)
self.update_pick_scatter()

def save_current_plot(self, filename):
"""
Expand Down
4 changes: 4 additions & 0 deletions src/viewephys/picks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sample,trace,amp,group,sample0
2717,297,26411326.0,0,330000
3612,296,-75356264.0,0,330000
3991,297,-64742784.0,0,330000
9 changes: 5 additions & 4 deletions src/viewephys/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import one.alf.io as alfio
from one.alf.files import get_session_path
import spikeglx
from ibldsp import voltage, utils
from ibldsp import voltage
from iblatlas.atlas import BrainRegions

from viewephys.gui import viewephys, SNS_PALETTE
Expand Down Expand Up @@ -181,9 +181,10 @@ def show_ephys(self, t0, tlen=.4):
sos = scipy.signal.butter(**butter_kwargs, output='sos')
butt = scipy.signal.sosfiltfilt(sos, raw)
destripe = voltage.destripe(raw, fs=self.data.sr.fs, channel_labels=True)
self.eqc_raw = viewephys(butt, self.data.sr.fs, channels=self.data.channels, br=self.data.br, title='butt', t0=t0, t_scalar=1)
self.eqc_des = viewephys(destripe, self.data.sr.fs, channels=self.data.channels, br=self.data.br, title='destripe', t0=t0, t_scalar=1)
stripes_noise = 20 * np.log10(np.median(utils.rms(butt - destripe)))
self.eqc_raw = viewephys(butt, self.data.sr.fs, channels=self.data.channels,
br=self.data.br, title='butt', t0=t0, t_scalar=1)
self.eqc_des = viewephys(destripe, self.data.sr.fs, channels=self.data.channels,
br=self.data.br, title='destripe', t0=t0, t_scalar=1)
eqc_xrange = [t0 + tlen / 2 - 0.01, t0 + tlen / 2 + 0.01]
self.eqc_des.viewBox_seismic.setXRange(*eqc_xrange)
self.eqc_raw.viewBox_seismic.setXRange(*eqc_xrange)
Expand Down
153 changes: 153 additions & 0 deletions src/viewephys/spike_interface.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>785</width>
<height>295</height>
</rect>
</property>
<property name="windowTitle">
<string>Spike Interface Viewer</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<widget class="QSlider" name="horizontalSlider">
<property name="enabled">
<bool>false</bool>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>Dataset Info</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QComboBox" name="uiComboProcessing">
<property name="minimumSize">
<size>
<width>160</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="5" column="0">
<widget class="QFrame" name="frame_2">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_smin">
<property name="text">
<string>smin</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_sval">
<property name="text">
<string>sval</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_smax">
<property name="text">
<string>smax</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>785</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionopen">
<property name="text">
<string>open</string>
</property>
</action>
<action name="actionopen_live_recording">
<property name="text">
<string>open live recording</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
Loading