-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcgutil.py
126 lines (94 loc) · 4 KB
/
pcgutil.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
"""Script to preprocess PCGs
Usage:
pcgutil.py [-p PCGS] [-r RECORDS] (--hpf | --env) [-o OUTFILE] filter
pcgutil.py [-p PCGS] [-s STATES] [-r RECORDS] [-o OUTFILE] medoids
pcgutil.py dtwmatrix MEDOIDS [SPLITS]...
pcgutil.py (-h | --help)
Arguments:
filter Filter and store PCG file
medoids Compute and store DTW medoids
dtwmatrix Computes and store the DTW distance matrix for the given records medoids
MEDOIDS Medoids to use for the matix computation
SPLITS Record files to use for the matrix computation
Options:
-h, --help Show this screen.
-p PCGS Specify .mat file containing the PCG recordings
Defaults to data/PCG_training.mat
-s STATES Specify .mat file containing the associated states
Defaults to data/states_training.mat
-r RECORDS Text file with the record ids
Defaults to data/RECORDS
-o OUTFILE File to save the features to
--hpf Apply high pass filter
--env Use homomorphic envelope
"""
import os
import time
import numpy as np
from scipy.io import savemat
from tqdm import tqdm
from docopt import docopt
from _defaults import *
from features import ALENGTH, dtw_preprocess, interDTW_down
from utils.misc import custom_loadmat, extract
from utils.dtwpy import dtw_medoid, dtw_distances
from utils.segmentation import get_intervals, get_transitions
dtw_params = {
'n_jobs': -1,
'constraint': 'sakoe_chiba',
'k': 0.1,
'path_norm': False,
'normalize': True
}
if __name__ == '__main__':
arguments = docopt(__doc__, version='1.0')
if arguments['-p']:
PCG_FILE = arguments['-p']
pcgs = custom_loadmat(PCG_FILE)
if arguments['-s']:
STATE_FILE = arguments['-s']
states = custom_loadmat(STATE_FILE)
if arguments['-r']:
RECORD_FILE = arguments['-r']
records = extract(RECORD_FILE)
filename = PCG_FILE
if arguments['filter']:
if arguments['--hpf']:
filename = filename.replace('PCG', 'fPCG')
pre = 'hpf'
if arguments['--env']:
filename = filename.replace('PCG', 'ePCG')
pre = 'env'
fpcgs = {r: dtw_preprocess(pcgs[r], pre=pre) for r in tqdm(records)}
if arguments['-o']:
filename = arguments['-o']
savemat(filename, fpcgs)
print('Saved to {0}'.format(filename))
elif arguments['medoids']:
filename = filename.replace('PCG', 'medoids')
inters = {r: get_intervals(pcgs[r], get_transitions(states[r]), interval='RR',
resize=ALENGTH['RR'] // interDTW_down) for r in tqdm(records)}
medoid_indexes = [dtw_medoid(inters[r], **dtw_params) for r in tqdm(records)]
medoids = {r: inters[r][m] for r, m in zip(records, medoid_indexes)}
if arguments['-o']:
filename = arguments['-o']
savemat(filename, medoids)
print('Saved to {0}'.format(filename))
directory = filename.replace('.mat', '/')
os.makedirs(directory, exist_ok=True)
with open(directory + 'index.csv', 'w') as fp:
for r, m in zip(records, medoid_indexes):
print("{0},{1}".format(r, m), file=fp)
elif arguments['dtwmatrix']:
medoids = custom_loadmat(arguments['MEDOIDS'])
directory = arguments['MEDOIDS'].replace('.mat', '/')
os.makedirs(directory, exist_ok=True)
for split in arguments['SPLITS']:
start = time.time()
records = extract(SPLITS_FOLDER + split)
n = len(records)
print("{0} : Processing {1} records ({2})".format(split, n, (n * (n - 1)) // 2))
medoids_i = [medoids[r] for r in records]
dist_matrix = dtw_distances(medoids_i, **dtw_params)
np.save(directory + split, dist_matrix)
print("{0} : Saved to {1}{0} - T {2:.3f}s".format(split, directory, time.time() - start))