Skip to content

Commit

Permalink
set title
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Jan 10, 2025
1 parent 4ba1d2b commit 3eca246
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions iblrig/gui/online_plots.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ctypes
import datetime
import json
import os
from collections.abc import Iterable
Expand Down Expand Up @@ -218,6 +219,8 @@ def _onSelectionChange(self, selected: QItemSelection, _deselected: QItemSelecti

class OnlinePlotsModel(QObject):
currentTrialChanged = Signal(int)
titleChanged = Signal(str)
subtitleChanged = Signal(str)
_trial_data = pd.DataFrame()
_bpod_data = pd.DataFrame()
trials_table = pd.DataFrame()
Expand Down Expand Up @@ -301,6 +304,7 @@ def readJsonable(self, _: str) -> None:
self.ntrials_correct += row.trial_correct

self.setCurrentTrial(self.nTrials() - 1)
self.titleChanged.emit(self.getTitle())

@Slot(int)
def setCurrentTrial(self, value: int) -> None:
Expand All @@ -314,12 +318,22 @@ def currentTrial(self) -> int:
def nTrials(self) -> int:
return len(self._trial_data)

def timeElapsed(self) -> datetime.timedelta:
seconds = 0 if self.nTrials() == 0 else (self._bpod_data.index[-1] - self._bpod_data.index[0]).seconds
return datetime.timedelta(seconds=seconds)

def percentCorrect(self) -> float:
return self.ntrials_correct / (self.nTrials() if self.nTrials() > 0 else np.nan) * 100

def bpod_data(self, trial: int) -> pd.DataFrame:
return self._bpod_data[self._bpod_data.Trial == trial]

def getTitle(self) -> str:
protocol = self.task_settings.get('PYBPOD_PROTOCOL', 'unknown task protocol')
trials = f'{self.nTrials()} trial{"s" if self.nTrials != 1 else ""}'
spacer = ' · '
return f'{protocol}{spacer}{trials}{spacer}time: {self.timeElapsed()}'


class StimulusDelegate(QStyledItemDelegate):
pen = QColor(0, 0, 0, 128)
Expand Down Expand Up @@ -556,7 +570,7 @@ def __init__(self, raw_data_folder: DirectoryPath, parent: QObject | None = None
layout.setColumnStretch(1, 2)

# main title
self.title = QLabel('This is the main title', self)
self.title = QLabel(self.model.getTitle(), self)
self.title.setAlignment(Qt.AlignHCenter)
font = self.title.font()
font.setPointSize(15)
Expand All @@ -566,10 +580,10 @@ def __init__(self, raw_data_folder: DirectoryPath, parent: QObject | None = None
layout.addWidget(self.title, 0, 0, 1, 3)

# sub title
subtitle = QLabel('This is the sub-title', self)
subtitle.setAlignment(Qt.AlignHCenter)
subtitle.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
layout.addWidget(subtitle, 1, 0, 1, 3)
self.subtitle = QLabel('This is the sub-title', self)
self.subtitle.setAlignment(Qt.AlignHCenter)
self.subtitle.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum)
layout.addWidget(self.subtitle, 1, 0, 1, 3)

# trial history
self.trials = TrialsWidget(self, self.model.table_model)
Expand Down Expand Up @@ -616,8 +630,17 @@ def __init__(self, raw_data_folder: DirectoryPath, parent: QObject | None = None
layout.addWidget(self.bpodWidget, 4, 0, 1, 3)

self.model.currentTrialChanged.connect(self.updatePlots)
self.model.titleChanged.connect(self.setTitle)
self.updatePlots(self.model.nTrials() - 1)

@Slot(str)
def setTitle(self, str):
self.title.setText(str)

@Slot(str)
def setTitleBackground(self, color: str):
self.title.setStyleSheet(f'QLabel {{ background-color: {color}; }}')

def mouseOverBarChart(self, event):
statusbar = self.window().statusBar()
if event.exit:
Expand All @@ -630,7 +653,6 @@ def mouseOverBarChart(self, event):

@Slot(int)
def updatePlots(self, trial: int):
self.title.setText(f'Trial {trial}')
self.bpodWidget.setData(self.model.bpod_data(trial))
self.trials.table_view.setCurrentIndex(self.model.table_model.index(trial, 0))
self.trials.table_view.scrollTo(self.model.table_model.index(trial, 0))
Expand Down

0 comments on commit 3eca246

Please sign in to comment.