diff --git a/Controller.py b/Controller.py index 7bd91b4..488ab3d 100644 --- a/Controller.py +++ b/Controller.py @@ -4,6 +4,7 @@ from DatParser import DatParser import numpy as np from warnings import warn import os +from redetector import detect_spiketimes import matplotlib.pyplot as plt @@ -39,7 +40,6 @@ class Controller: def save_parameters(self, folder_path): header = "trial_index,threshold,min_window,step_size\n" for repro in self.thresholds.keys(): - # TODO change '.' to self.data_path after testing: with open(folder_path + "/{}_thresholds.csv".format(repro), "w") as threshold_file: threshold_file.write(header) for i in range(len(self.sorting[repro])): @@ -56,11 +56,32 @@ class Controller: def save_redetected_spikes(self, folder_path): # TODO save redetected spikes: - header = "trial_index,threshold,min_window,step_size\n" + redetection_folder = folder_path + "/redetected_spikes/" + if not os.path.exists(redetection_folder): + os.mkdir(redetection_folder) + for repro in self.thresholds.keys(): - # TODO change '.' to self.data_path after testing: - with open(folder_path + "/{}_thresholds.csv".format(repro), "w") as threshold_file: - threshold_file.write(header) + for i in range(len(self.sorting[repro])): + if len(self.thresholds[repro][i]) == 0: + continue # If no params were saved, saving a redetection file makes no sense + else: + # format and write given thresholding parameters + # i is the trial idx + thresh = self.thresholds[repro][i][0] + min_window = self.thresholds[repro][i][1] + step = self.thresholds[repro][i][2] + + traces, spiketimes, recording_times = self.get_traces_with_spiketimes(repro) + trace = traces[i] + spiketimes = spiketimes[i] + recording_times = recording_times + sampling_interval = self.sampling_interval + + time = np.arange(len(trace)) * sampling_interval - recording_times[0] + redetect = detect_spiketimes(time, trace, thresh, min_window, step) + + np.save(redetection_folder + "spikes_repro_{}_trial_{}.npy".format(repro, i), np.array(redetect)) + print("Redetected spikes saved!") def get_repros(self): return self.parser.get_measured_repros() @@ -193,4 +214,4 @@ def average_spike_height(spike_train: np.ndarray, v1: np.ndarray, sampling_rate, # spike_values = [v1[i] for i in indices if 0 <= i < len(v1)] # average_height = np.mean(spike_values) # -# return average_height \ No newline at end of file +# return average_height diff --git a/ReadMe.md b/ReadMe.md index cdc758d..26b9182 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,15 +1,15 @@ -# Spike redetector +# Spike Redetector -###redetection parameters: +### Redetection Parameters: -- **Threshold**: number of standard deviations between a maximum and a minimum to count as a spike. -- **Min window size**: The minimum size of a window (as number of indices) in which spikes are detected. The voltage trace is split into parts +- ***Threshold***: number of standard deviations between a maximum and a minimum to count as a spike. +- ***Min window size***: The minimum size of a window (as number of indices) in which spikes are detected. The voltage trace is split into parts if the maximum or minimum of the next part are too different from the current part. This makes the estimation of the STD a lot more stable between trials where the absolute voltage might wander. -- **step size**: The number of indices that are used in each step while trying to increase the size of the window. +- ***step size***: The number of indices that are used in each step while trying to increase the size of the window. -### plot controls: +### Plot Controls: - Drag and drop with LMB -- zoom control with RMB -> press and hold; up down is zoom on y-axis while left right is zoom on x-axis \ No newline at end of file +- zoom control with RMB -> press and hold; up down is zoom on y-axis while left right is zoom on x-axis diff --git a/SpikeRedetectGui.py b/SpikeRedetectGui.py index 7555702..1ef78c2 100644 --- a/SpikeRedetectGui.py +++ b/SpikeRedetectGui.py @@ -219,7 +219,6 @@ class SpikeRedetectGui(QWidget): @pyqtSlot() def save_threshold_parameters(self): - # TODO add question for save_path data_path = QFileDialog.getExistingDirectory(self, directory=self.controller.data_path, caption='Select a folder to save the parameters:') if data_path is None: return diff --git a/main.py b/main.py index 223d393..eb5eda2 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,11 @@ import sys - from PyQt5.QtWidgets import QApplication from SpikeRedetectGui import SpikeRedetectGui -test_file = "../neuronModel/data/final_sam/2010-11-08-al-invivo-1" +test_file = "../neuronModel/data/final/2010-11-08-al-invivo-1" sam_file = "../neuronModel/data/final_sam/2011-10-25-ad-invivo-1" # TODO CLI diff --git a/testing.py b/testing.py index 6b74a5d..cae918a 100644 --- a/testing.py +++ b/testing.py @@ -14,6 +14,11 @@ failure_to_read_sam = ["2012-06-27-an-invivo-1", "2012-12-13-ag-invivo-1"] def main(): + + test = np.load("redetected_spikes/spikes_repro_BaselineActivity_trial_0.npy") + print() + + for cell in sorted(os.listdir(DATA_FOLDER)): if cell in failure_to_read_sam: continue @@ -77,4 +82,4 @@ def test_getting_repros(data_provider: Controller): if __name__ == '__main__': - main() \ No newline at end of file + main()