From df346b2a358ce7ed11d8353a52d613c3a4f04622 Mon Sep 17 00:00:00 2001 From: alexanderott Date: Sun, 7 Mar 2021 18:33:21 +0100 Subject: [PATCH] stim values extraction but without functionality --- Controller.py | 37 +++++++++++++++++++++++++++++++------ SpikeRedetectGui.py | 20 +++++++++++++------- main.py | 19 ++++++------------- redetector.py | 4 ++-- 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/Controller.py b/Controller.py index 9ac24d9..0a4d9ba 100644 --- a/Controller.py +++ b/Controller.py @@ -3,6 +3,7 @@ from DatParser import DatParser import numpy as np from warnings import warn +import os import matplotlib.pyplot as plt @@ -32,6 +33,7 @@ class Controller: self.sorting = {} self.thresholds = {} + self.stim_values = {} self.recording_times = {} def save_parameters(self): @@ -84,9 +86,10 @@ class Controller: [time_traces, v1_traces, eod_traces, local_eod_traces, stimulus_traces] = self.get_unsorted_traces(repro) (spiketimes, metadata) = self.get_unsorted_spiketimes(repro) + repro_stim_values = self.extract_stim_values(repro, metadata) + self.stim_values[repro] = repro_stim_values if len(v1_traces) != len(spiketimes): - warn("get_traces_with_spiketimes():Unequal number of traces and spiketimes for repro {}!" - "Returning only according to the number of traces!".format(repro)) + warn("get_traces_with_spiketimes():Unequal number of traces ({}) and spiketimes ({}) for repro {}! Returning only according to the number of traces!".format(len(v1_traces), len(spiketimes), repro)) ash = calculate_distance_matrix_traces_spikes(v1_traces, spiketimes, self.parser.get_sampling_interval(), before) @@ -102,13 +105,35 @@ class Controller: return v1_traces, sorted_spiketimes, (before, after) def get_stim_values(self, repro): + return sorted(np.unique(self.stim_values[repro])) + + def extract_stim_values(self, repro, metadata_list): + stim_values = [] if repro == "BaselineActivity": - return ["None"] + for i in range(len(metadata_list)): + stim_values.append("None") elif repro == "FICurve": - # TODO other function that just provides the contrasts - return sorted([str(x[0]) for x in self.parser.get_fi_curve_contrasts()]) + current_intensity = None + for i in range(len(metadata_list)): + if len(metadata_list[i]) != 0: + current_intensity = float(metadata_list[i][-1]["intensity"][:-2]) + + stim_values.append(current_intensity) + + elif repro == "FileStimulus": + for i in range(len(metadata_list)): + stim_values.append(os.path.basename(metadata_list[i][0]["file"])) + + elif repro == "SAM": + for i in range(len(metadata_list)): + deltaf = metadata_list[i][0]["----- Stimulus -------------------------------------------------------"]["deltaf"] + contrast = metadata_list[i][0]["----- Stimulus -------------------------------------------------------"]["contrast"] + stim_values.append("{} {}".format(deltaf, contrast)) + + if len(stim_values) == 0: + return ["Stimulus values of repro not supported"] - return ["repro not supported"] + return stim_values def get_trials(self, repro, stimulus_value): pass diff --git a/SpikeRedetectGui.py b/SpikeRedetectGui.py index 71766d2..4152943 100644 --- a/SpikeRedetectGui.py +++ b/SpikeRedetectGui.py @@ -1,7 +1,7 @@ from PyQt5.QtWidgets import QWidget, QPushButton, QSizePolicy, QLineEdit,\ QMessageBox, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QFrame,\ - QDoubleSpinBox, QComboBox, QSpinBox, QCheckBox + QDoubleSpinBox, QComboBox, QSpinBox, QCheckBox, QFileDialog from PyQt5.QtCore import pyqtSlot import numpy as np @@ -14,9 +14,13 @@ from redetector import detect_spiketimes class SpikeRedetectGui(QWidget): - def __init__(self, controller: Controller): + def __init__(self, data_path=None): super().__init__() - self.controller = controller + + if data_path is None: + data_path = QFileDialog.getExistingDirectory(self, caption='Select a cell directory:') + + self.controller = Controller(data_path) self.title = 'Spike Redetection' self.left = 10 self.top = 10 @@ -188,6 +192,9 @@ class SpikeRedetectGui(QWidget): def repro_change(self): repro = self.repro_box.currentText() + redetection = (self.threshold_spinbox.value(), self.window_spinbox.value(), self.step_spinbox.value()) + self.canvas.plot(self.trial_idx, self.repro_box.currentText(), self.controller, redetection) + # reset trials and stim values self.trial_change(0) self.stim_val_box.clear() @@ -195,8 +202,7 @@ class SpikeRedetectGui(QWidget): for val in self.controller.get_stim_values(repro): self.stim_val_box.addItem(str(val)) - redetection = (self.threshold_spinbox.value(), self.window_spinbox.value(), self.step_spinbox.value()) - self.canvas.plot(self.trial_idx, self.repro_box.currentText(), self.controller, redetection) + @pyqtSlot() def accept_redetection(self): @@ -320,9 +326,9 @@ class PlotCanvas(FigureCanvas): ax.clear() time = np.arange(len(trace)) * sampling_interval - recording_times[0] ax.plot(time, trace) - ax.eventplot(spiketimes, lineoffsets=max(trace) + 1, colors="black") + ax.eventplot(spiketimes, lineoffsets=max(trace) + 2, colors="black") redetect = detect_spiketimes(time, trace, redetection_vars[0], redetection_vars[1], redetection_vars[2]) - ax.eventplot(redetect, lineoffsets=max(trace) + 2, colors="red") + ax.eventplot(redetect, lineoffsets=max(trace) + 1, colors="red") ax.set_title('Trial XYZ') if self.current_repro == repro: diff --git a/main.py b/main.py index 9760979..164da56 100644 --- a/main.py +++ b/main.py @@ -1,28 +1,21 @@ import sys -import os -from PyQt5.QtWidgets import QApplication, QFileDialog +from PyQt5.QtWidgets import QApplication -from Controller import Controller from SpikeRedetectGui import SpikeRedetectGui + test_file = "../neuronModel/data/final_sam/2010-11-08-al-invivo-1" +sam_file = "../neuronModel/data/final_sam/2011-10-25-ad-invivo-1" +# TODO CLI def main(): app = QApplication(sys.argv) - data_path = QFileDialog.getExistingDirectory(caption='Select a directory') - if os.path.isdir(data_path): - data_provider = Controller(data_path) - else: - print("Cell loading didn't work!\n Cell folder: {}".format(data_path)) - return - #data_provider = Controller(test_file) - ex = SpikeRedetectGui(data_provider) - sys.exit(app.exec_()) - + ex = SpikeRedetectGui(sam_file) + sys.exit(app.exec_()) if __name__ == '__main__': diff --git a/redetector.py b/redetector.py index 0062949..63b4a53 100644 --- a/redetector.py +++ b/redetector.py @@ -16,14 +16,14 @@ def detect_spike_indices_automatic_split(v1, threshold, min_length=5000, split_s break_threshold = 0.25 splits = [] - if len(v1) < min_length: + if len(v1) <= min_length: splits = [(0, len(v1))] else: last_max = max(v1[0:min_length]) last_min = min(v1[0:min_length]) idx = min_length - while idx < len(v1): + while idx <= len(v1): if idx + step_size > len(v1): splits.append((split_start, len(v1))) break