diff --git a/fishbook/reproclasses.py b/fishbook/reproclasses.py index 883da38..e35bb7a 100644 --- a/fishbook/reproclasses.py +++ b/fishbook/reproclasses.py @@ -63,7 +63,8 @@ class BaselineData: if not self.__dataset: return self.__repros = RePro.find("BaselineActivity", cell_id=self.__cell.id) - for r in self.__repros: + for i in tqdm(range(len(self.__repros)), desc="loading data"): + r = self.__repros[i] sd = self.__read_spike_data(r) if sd is not None and len(sd) > 1: self.__spike_data.append(sd) @@ -399,9 +400,11 @@ class FIData: sd, c, eods, time = self.__read_spike_data(r) if sd is not None and len(sd) > 1: self.__spike_data.extend(sd) - self.__eod_data.extend(eods) + if eods: + self.__eod_data.extend(eods) self.__contrasts.extend(c) - self.__eod_times.extend(time) + if time: + self.__eod_times.extend(time) else: continue @@ -414,9 +417,7 @@ class FIData: if self.__dataset.has_nix: return self.__read_spikes_from_nix(repro) else: - print("Sorry, so far only from nix!!!") - pass - return None, None, None, None + return self.__read_spikes_from_directory(repro) def __do_read_spike_data_from_nix(self, mtag: nix.pycore.MultiTag, stimulus: Stimulus, repro: RePro): r_settings = repro.settings.split("\n") @@ -458,10 +459,9 @@ class FIData: data_source = os.path.join(self.__dataset.data_source, self.__dataset.id + ".nix") if not os.path.exists(data_source): print("Data not found! Trying from directory") - return self.__read_spike_data_from_directory(repro) + return self.__read_spikes_from_directory(repro) f = nix.File.open(data_source, nix.FileMode.ReadOnly) b = f.blocks[0] - mt = None for i in tqdm(range(len(stimuli)), desc="Loading data"): s = stimuli[i] @@ -476,9 +476,40 @@ class FIData: f.close() return spikes, contrasts, eods, time - def __read_spikes_from_directory(self, repro = RePro): + def __do_read_data_block(self, f, l): + spikes = [] + while len(l.strip()) > 0 and "#" not in l: + spikes.append(float(l.strip())) + l = f.readline() + return spikes, l - pass + def __read_spikes_from_directory(self, repro: RePro): + spikes = [] + contrasts = [] + print("Warning! Exact reconstruction of stimulus order not possible for old relacs files!") + data_source = os.path.join(self.__dataset.data_source, "fispikes1.dat") + if os.path.exists(data_source): + with open(data_source, 'r') as f: + l = f.readline() + fish_intensity = None + stim_intensity = None + while l: + l = l.strip().lower() + if "index" in l: + fish_intensity = None + stim_intensity = None + if "intensity = " in l: + if "true intensity = " in l: + fish_intensity = float(l.split("=")[-1].strip()[:-2]) + elif "pre" not in l: + stim_intensity = float(l.split("=")[-1].strip()[:-2]) + if len(l) > 0 and "#" not in l: # data line + sp, l = self.__do_read_data_block(f, l) + spikes.append(sp) + contrasts.append((stim_intensity/fish_intensity-1)*100) + continue + l = f.readline() + return spikes, contrasts, None, None @property def size(self) -> int: @@ -508,6 +539,9 @@ class FIData: :param index: the index of the trial. Default of -1 indicates that all data should be returned. :return: Either two vectors representing time and the local eod or two lists of such vectors """ + if len(self.__eod_data) == 0: + print("EOD data not available for old-style relacs data.") + return None, None if 0 <= index < self.size: return self.__eod_times[index], self.__eod_data[index] else: @@ -516,7 +550,7 @@ class FIData: def contrast(self, index=-1): """ The stimulus contrast used in the respective trial(s). - \ + :param index: the index of the trial. Default of -1 indicates that all data should be returned. :return: Either a single scalar representing the contrast, or a list of such scalars, one entry for each trial. """ @@ -577,10 +611,9 @@ class FIData: return boltzmann_fit - if __name__ == "__main__": - #dataset = Dataset(dataset_id='2011-06-14-ag') - dataset = Dataset(dataset_id="2018-01-19-ac-invivo-1") - # dataset = Dataset(dataset_id='2018-11-09-aa-invivo-1') - baseline = BaselineData(dataset) + # dataset = Dataset(dataset_id='2011-06-14-ag') + # dataset = Dataset(dataset_id="2018-01-19-ac-invivo-1") + dataset = Dataset(dataset_id='2013-04-18-ac') + fi_curve = FIData(dataset) embed() \ No newline at end of file