-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
178 lines (163 loc) · 5.79 KB
/
Copy pathutils.py
File metadata and controls
178 lines (163 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
import serial
import sys
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import os
import json
import sys
import time
def save_data(data, part:int, filename):
"""
Save the collected EEG data to a CSV file and plot the data.
"""
df = pd.DataFrame(data, columns=[
'timestamp', 'signal_strength', 'attention', 'meditation',
'delta', 'theta', 'low_alpha', 'high_alpha', 'low_beta', 'high_beta',
'low_gamma', 'high_gamma'
])
file_index = check_file(part, filename)
df.to_csv(f'{filename}/{part}/mindflex/{part}_{file_index}.csv', index=False)
def check_file(part, filename):
file_index = 1
while os.path.exists(f'{filename}/{part}/mindflex/{part}_{file_index}.csv'):
file_index += 1
return file_index
def check_patient(name, surname, age, pat_list):
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
return True
return False
def add_patient(name, surname, age):
if not os.path.exists("patients.json"):
with open("patients.json", "w") as file:
json.dump([],file)
with open("patients.json", "r") as file:
pat_list = json.load(file)
if check_patient(name,surname,age,pat_list):
return False
else:
# create patient data card in json
path = os.path.join("data", f"{name}_{surname}_{age}")
os.makedirs(path)
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
person = {
"Name": name,
"Surname": surname,
"Age": age,
"created_ago": f"{date}",
"created_at" : path,
"part_no": 0,
"part_1": [],
"part_2": [],
"part_3": []
}
pat_list.append(person)
with open("patients.json", "w") as file:
json.dump(pat_list, file)
return True
def load_patient(name,surname,age):
with open("patients.json", "r") as file:
pat_list = json.load(file)
if check_patient(name,surname,age,pat_list):
return []
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
return pat
return []
def update_patient(name,surname,age,part_no):
with open("patients.json", "r") as file:
pat_list = json.load(file)
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
pat['part_no'] = part_no
break
with open("patients.json", "w") as file:
json.dump(pat_list, file)
def reset_patient(name,surname,age):
with open("patients.json", "r") as file:
pat_list = json.load(file)
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
pat['part_no'] = 0
pat['part_1'] = []
pat['part_2'] = []
pat['part_3'] = []
path = os.path.join("data", f"{name}_{surname}_{age}")
os.remove(path) #potentially dangerous
os.makedirs(path)
break
with open("patients.json", "w") as file:
json.dump(pat_list, file)
def remove_patient(name,surname,age):
with open("patients.json", "r") as file:
pat_list = json.load(file)
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
pat_list.remove(pat)
path = os.path.join("data", f"{name}_{surname}_{age}")
os.remove(path)
break
with open("patients.json", "w") as file:
json.dump(pat_list, file)
def remove_part(name,surname,age, part):
with open("patients.json", "r") as file:
pat_list = json.load(file)
for pat in pat_list:
if pat['Name'] == name and pat['Surname'] == surname and pat['Age'] == age:
path = os.path.join("data", f"{name}_{surname}_{age}", f"{part}")
os.remove(path)
break
with open("patients.json", "w") as file:
json.dump(pat_list, file)
def connect2headset(port="COM5"):
if sys.platform == 'linux':
serial_connection = serial.Serial(
# assume port is assigned to headset
port="/dev/ttyUSB0",
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
else:
# assume COM port is assigned to headset
serial_connection = serial.Serial(
port=port,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
return serial_connection
def add_run_path(name, surname, age, part_no, run_no, file_path):
"""
Appends the run number and file path to the specific part list in patients.json.
"""
json_path = "patients.json"
if not os.path.exists(json_path):
return False
with open(json_path, "r") as file:
pat_list = json.load(file)
found = False
for pat in pat_list:
if pat['Name'] == str(name) and pat['Surname'] == str(surname) and pat['Age'] == str(age):
part_key = f"part_{part_no}"
if part_key not in pat:
pat[part_key] = []
new_record = {
"run": run_no,
"path": file_path,
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
pat[part_key].append(new_record)
found = True
break
if found:
with open(json_path, "w") as file:
json.dump(pat_list, file, indent=4)
return True
else:
print(f"[JSON] Error: Patient {name} {surname} not found.")
return False