-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoh3toEDF.py
199 lines (170 loc) · 5.54 KB
/
coh3toEDF.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
""" This script is intended to batch convert Deltamed .eeg files to .edf files.
"""
# comtypes==1.1.7 pywinauto pywin32==228 and Python 3.8
import json
import os
import sys
from utils import (
convert_coh3_to_edf,
display_arguments,
ensure_path,
handle_yes_no,
list_files,
)
def exe_path():
""" Return the path of the executable or of the script. """
if hasattr(sys, 'frozen'):
return os.path.dirname(os.path.abspath(sys.executable))
return os.path.dirname(os.path.abspath(__file__))
SCRIPT_PATH = exe_path()
CONFIG_FILE = 'coh3toEDF.config'
CONFIG_FILE = os.path.join(SCRIPT_PATH, CONFIG_FILE)
try:
EXECUTABLE_PATH = json.load(open(CONFIG_FILE))['path_to_executable']
except FileNotFoundError:
EXECUTABLE_PATH = os.path.join(SCRIPT_PATH, 'coh3toEDF.exe')
print(
'Generating "coh3toEDF.config" file, please edit it '
'to set the correct path to the executable "coh3toEDF.exe".\n'
'You can find "coh3toEDF.config" here: {0}'.format(SCRIPT_PATH)
)
config = {'path_to_executable': EXECUTABLE_PATH}
json.dump(config, open(CONFIG_FILE, 'w'))
def main(
original_path: str,
executable_path: str = EXECUTABLE_PATH,
destination_path: str = None,
overwrite: bool = False,
):
""" Run batch conversion process.
Args:
original_path: path to the dataset to convert.
executable_path: path to the converter executable.
destination_path: destination path.
overwrite: if True, any previous .edf
with the same name will be overwrited.
"""
print('1 - List the files to convert...')
files = [
os.path.abspath(file_) for file_ in list_files(original_path)
if os.path.basename(file_).lower().endswith('.eeg')
]
n_files = len(files)
print('{0} file(s) will be converted.'.format(n_files))
print('2 - Convert files')
for index, file_ in enumerate(sorted(files), start=1):
# Destination file path
if destination_path is None:
file_destination_path = file_[:-4] + '.EDF'
else:
file_destination_path = os.path.join(
destination_path,
os.path.relpath(file_, original_path)
)
print(
'({0}/{1}) Convert "{2}" to "{3}"'.format(
index,
n_files,
file_,
file_destination_path,
),
)
ensure_path(path=os.path.dirname(file_destination_path))
if os.path.isfile(file_destination_path) and not overwrite:
print('File has already been converted.')
else:
if os.path.isfile(file_destination_path):
print('File has already been converted (will be overwrited).')
convert_coh3_to_edf(
executable_path=executable_path,
eeg_path=file_,
edf_path=file_destination_path,
)
if n_files:
print('3 - Kill the converter process(es).')
os.system(
'taskkill /f /im {0}'.format(
os.path.basename(executable_path),
),
)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(allow_abbrev=True)
parser.add_argument(
'path',
type=str,
help='path to the dataset to convert from coh3 (.eeg) to EDF',
)
parser.add_argument(
'-dp',
'--destination_path',
type=str,
help=(
'destination of the converted (.edf) files'
),
default=None,
)
parser.add_argument(
'-ep',
'--executable_path',
type=str,
help=(
'path to the converter executable'
),
default=None,
)
parser.add_argument(
'-o',
'--overwrite',
help='if set, the program will overwrite the existing .edf files',
action='store_true',
default=False,
)
# By default ask to the user if a want to proceed.
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-y',
'--yes',
help='if set, the program will start directly',
action='store_true',
default=False,
)
group.add_argument(
'-n',
'--no',
help='if set, the program will exit directly',
action='store_true',
default=False,
)
args = parser.parse_args()
display_arguments(args)
handle_yes_no(args)
if args.executable_path is None:
# Load config information
configs = json.load(open(CONFIG_FILE))
path_to_executable = configs['path_to_executable']
if not os.path.isfile(path_to_executable):
print(
'"{0}" does not exist. '
'The path to the executable "coh3toEDF.exe" is not valid. '
'Please check that the path provided in the "coh3toEDG.config"'
' file is correct.'.format(path_to_executable)
)
sys.exit(1)
else:
path_to_executable = args.executable_path
if not os.path.isfile(path_to_executable):
print(
'"{0}" does not exist. '
'The path to the executable "coh3toEDF.exe" is not valid. '
'Please check that the path provided as argument.'.format(
path_to_executable,
)
)
sys.exit(1)
main(
original_path=args.path,
executable_path=path_to_executable,
destination_path=args.destination_path,
overwrite=args.overwrite,
)