jar_project/jar_functions.py
2020-06-25 14:37:52 +02:00

50 lines
2.5 KiB
Python

import os #compability with windows
def parse_dataset(dataset_name):
assert(os.path.exists(dataset_name)) #see if data exists
f = open(dataset_name, 'r') #open data we gave in
lines = f.readlines() #read data
f.close() #?
eodfs = [] #metadata lists for every loop
deltafs = []
stimulusfs = []
times = [] #data itself
frequencies = []
amplitudes = []
time = [] #temporary lists with data we put in the lists above
ampl = []
freq = []
for i in range(len(lines)):
l = lines[i].strip() #all lines of textdata, exclude all empty lines (empty () default for spacebar)
if "#" in l and "EODf" in l: #if line starts with # EODf:
eodfs.append(float(l.split(':')[-1].strip()[:-2])) #append: line splitted by ':' the 2nd part ([-1],
if "#" in l and "Delta f" in l: #which got striped so we sure there is no space at the end,
deltafs.append(float(l.split(':')[-1].strip()[:-2])) #from that all expect the last two signs (Hz unit)
if "#" in l and "StimulusFrequency" in l: #this for different metadata in different lists
stimulusfs.append(float(l.split(':')[-1].strip()[:-2]))
if '#Key' in l:
if len(time) != 0: #therefore empty in the first round
times.append(time) #2nd loop means time != 0, so we put the times/amplitudes/frequencies to
amplitudes.append(ampl) #the data of the first loop
frequencies.append(freq)
time = [] #temporary lists with the data of the 2nd loop which we put to the lists above
ampl = []
freq = []
if len(l) > 0 and l[0] is not '#': #line not empty and doesnt start with #
temporary = list(map(float, l.split())) #temporary list where we got 3 index splitted by spacebar, map to find them
time.append(temporary[0]) #temporary lists with the data at that place, respectively
freq.append(temporary[1])
ampl.append(temporary[2])
times.append(time) #append data from one list to another
amplitudes.append(ampl)
frequencies.append(freq)
return times, frequencies, amplitudes, eodfs, deltafs, stimulusfs #output of the function