Skip to content
Snippets Groups Projects
Commit 720d9c35 authored by Köllőd Csaba's avatar Köllőd Csaba
Browse files

New Feature: creating concatenated files

- creating concatenated files with respect to task and experimental session
- file format must be concluded
parent b057712b
No related branches found
No related tags found
No related merge requests found
from dataclasses import dataclass, field
from datetime import datetime
from datetime import datetime, timedelta
from pathlib import Path
from struct import unpack
......@@ -83,9 +83,10 @@ class FlowPacket:
def plot(self):
self._plot(self.eeg, 'EEG')
self._plot(self.eeg_giro, 'EEG Gyro')
self._plot(self.emg, 'EMG')
self._plot(self.spo2, 'SPO2')
self._plot(self.eeg_giro, 'EEG Gyroscope')
self._plot(self.eeg_acc, 'EEG Accelerometer')
# self._plot(self.emg, 'EMG')
# self._plot(self.spo2, 'SPO2')
plt.show()
def to_edf(self, filename):
......@@ -99,7 +100,7 @@ class FlowPacket:
sig = 0
for pkt in to_save:
for i, ch_dat in enumerate(pkt.data):
print(sig)
# print(sig)
edf.setPhysicalMaximum(sig, 3000 * EEG_EMG_SCALE)
edf.setPhysicalMinimum(sig, -3000 * EEG_EMG_SCALE)
# edf.setDigitalMaximum(sig, 2**7)
......@@ -114,7 +115,7 @@ class FlowPacket:
def _read_block_data(file, shape, format_, format_size):
sh = np.prod(shape)
data = unpack(format_ * sh, file.read(format_size * sh))
data = unpack('>' + format_ * sh, file.read(format_size * sh))
data = np.array(data)
data = data.reshape(shape)
return data
......@@ -124,7 +125,7 @@ def read_packet(file):
# header
date_, time_, flow, hr, spo2, vbat_eeg, vbat_arm = unpack('>II10sBHHH', file.read(25))
date_ = str(date_)
time_ = str(time_)
time_ = f'{time_:06}'
flow = int.from_bytes(flow, 'big')
date_ = datetime.fromisoformat(f'{date_[:4]}-{date_[4:6]}-{date_[6:]}T{time_[:2]}:{time_[2:4]}:{time_[4:]}')
spo2 /= 10.
......@@ -170,6 +171,7 @@ def read_packet(file):
def read_dat_file(file):
no_packets = unpack('>I', file.read(4))[0]
# print(no_packets)
packet_holder = read_packet(file)
for _ in range(no_packets - 1):
packet_holder += read_packet(file)
......@@ -180,13 +182,29 @@ def read_dat_file(file):
def read_data(path):
path = Path(path)
files = path.rglob('*.dat')
for file in files:
with open(file, 'rb') as f:
files = list(path.rglob('*.dat'))
with open(files[0], 'rb') as f:
data = read_dat_file(f)
exp = 1
rec = 1
for i in range(1, len(files)):
with open(files[i], 'rb') as f:
dat = read_dat_file(f)
if dat.date - data.date < timedelta(seconds=data.duration + 10):
data += dat
else:
data.plot()
# data.to_edf(f'exp{exp:03}-rec{rec:03}.edf')
if dat.date - data.date < timedelta(minutes=10):
rec += 1
else:
rec = 1
exp += 1
data = dat
data.plot()
# data.to_edf('test.edf')
exit(12)
# data.to_edf(f'exp{exp:03}-rec{rec:03}.edf')
if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment