-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget_traces.py
82 lines (69 loc) · 2.77 KB
/
get_traces.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
import numpy as np
import os
import re
from scipy.io import loadmat
directory = "/path/to/files/clips/Volumes/Seagate/seizure_detection/competition_data/clips/"
def get_num_traces(patient):
patient_directory = os.path.join(directory, patient)
regex = r'^([^_]+_\d+)_(\w+?)_segment_(\d+)\.mat$'
files = [i for i in os.listdir(patient_directory) if \
re.match(regex, i).groups()[0] == patient]
print(len(files))
data = loadmat(os.path.join(patient_directory, files[0]))['data']
return data.shape[0]
def get_traces(patient, trace_type, channel=None, latency=False):
patient_directory = os.path.join(directory, patient)
regex = r'^([^_]+_\d+)_(\w+?)_segment_(\d+)\.mat$'
files = [i for i in os.listdir(patient_directory) if \
re.match(regex, i).groups()[1] == trace_type]
files.sort()
output = []
for f in files:
data = loadmat(os.path.join(patient_directory, f))
if type(channel) is int:
output.append(data['data'][channel,:])
else:
for i in range(data['data'].shape[0]):
output.append(data['data'][i,:])
return np.array(output)
def get_early_traces(patient, channel=None):
patient_directory = os.path.join(directory, patient)
regex = r'^([^_]+_\d+)_(\w+?)_segment_(\d+)\.mat$'
files = [i for i in os.listdir(patient_directory) if \
re.match(regex, i).groups()[1] in ['ictal', 'interictal']]
files.sort()
output = []
early = []
for f in files:
data = loadmat(os.path.join(patient_directory, f))
output.append(data['data'][channel,:])
if 'latency' not in data:
early.append(0)
elif int(data['latency']) > 15:
early.append(0)
else:
early.append(1)
return (np.array(output), np.array(early))
def get_training_traces(patient, channel=None):
"""Get training traces"""
ictal = get_traces(patient, 'ictal', channel)
interictal = get_traces(patient, 'interictal', channel)
combined = np.concatenate([ictal, interictal])
label = np.concatenate([[1]*ictal.shape[0], [0]*interictal.shape[0]])
return (combined, label)
def get_testing_traces(patient, channel=None):
"""Get testing traces"""
patient_directory = os.path.join(directory, patient)
regex = r'^([^_]+_\d+)_(\w+?)_segment_(\d+)\.mat$'
files = [i for i in os.listdir(patient_directory) if \
re.match(regex, i).groups()[1] == 'test']
files.sort()
output = []
for f in files:
data = loadmat(os.path.join(patient_directory, f))['data']
if type(channel) is int:
output.append(data[channel,:])
else:
for i in range(data.shape[0]):
output.append(data[i,:])
return (np.array(output), files)