From ca7d64291d6853b19123addf2bac1a275f10a4a6 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Mon, 9 Sep 2019 00:23:02 +0200 Subject: [PATCH] parsing of stimuli.dat file --- util.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/util.py b/util.py index 6716409..5997edf 100644 --- a/util.py +++ b/util.py @@ -1,6 +1,7 @@ from functools import reduce import numpy as np import nixio as nix +import re import os import glob import datetime as dt @@ -31,6 +32,104 @@ def read_info_file(file_name): return root +def parse_metadata_line(line): + if not line.startswith("#"): + return None, None + + line = line.strip("#").strip() + parts = line.split(":") + if len(parts) == 0: + return None, None + if len(parts) == 1 or len(parts[-1].strip()) == 0: + return parts[0].strip(), None + else: + return parts[0].strip(), parts[-1].strip() + + +def has_signal(line, col_names): + """ + Checks whether a signal/stimulus was given in the line. + :param line: the current line of the data table + :param col_names: The names of the table header columns + :return: whether or not any of the signal entries is not empty ("-") + """ + values = line.split() + for i, n in enumerate(col_names): + if n.lower() == "signal" and i < len(values): + if len(values[i].strip()) > 0 and values[i].strip()[0] != "-": + return True + else: + print(i, values[i]) + return False + + +def parse_table(lines, start_index): + """ + + :param lines: + :param start_index: + :return: + """ + data_indices = {} + stim_count = 0 + names = re.split(r'\s{2,}', lines[start_index + 3][1:].strip()) + while start_index < len(lines): + l = lines[start_index].strip() + if l.startswith("#"): # ignore + start_index += 1 + elif len(l) > 0: + if stim_count == 0 and (has_signal(l, names)): + data_indices[stim_count] = l.split()[0] + stim_count += 1 + elif stim_count > 0: + data_indices[stim_count] = l.split()[0] + stim_count += 1 + start_index += 1 + else: + start_index += 1 + break + return data_indices, start_index + + +def read_stimuli_file(file_name): + repro_settings = [] + settings = {} + with open(file_name, 'r') as f: + lines = f.readlines() + index = 0 + current_section = None + current_section_name = "" + while index < len(lines): + l = lines[index].strip() + if len(l) == 0: + index += 1 + elif l.startswith("#") and "key" not in l.lower(): + name, value = parse_metadata_line(l) + if not name: + continue + if name and not value: + if current_section: + settings[current_section_name] = current_section.copy() + + current_section = {} + current_section_name = name + else: + current_section[name] = value + index += 1 + elif l.lower().startswith("#key"): # table data coming + data, index = parse_table(lines, index) + settings["stim_data"] = data + # we are done with this repro run + settings[current_section_name] = current_section.copy() + repro_settings.append(settings.copy()) + current_section = None + settings = {} + else: + # data lines, ignore them here + index += 1 + return repro_settings + + def find_key_recursive(dictionary, key, path=[]): assert(isinstance(dictionary, dict)) if key in dictionary.keys(): @@ -155,6 +254,7 @@ def mtag_settings_to_yaml(mtag, pos_index): if __name__ == "__main__": + """ nix_file = "../../science/high_freq_chirps/data/2018-11-09-aa-invivo-1/2018-11-09-aa-invivo-1.nix" f = nix.File.open(nix_file, nix.FileMode.ReadOnly) b = f.blocks[0] @@ -164,4 +264,7 @@ if __name__ == "__main__": print(nix_metadata_to_yaml(b.metadata)) embed() f.close() - + """ + dataset = "/Users/jan/zwischenlager/2012-03-23-ad" + settings = read_stimuli_file(os.path.join(dataset, "stimuli.dat")) + embed()