-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
61 lines (49 loc) · 1.79 KB
/
predict.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
#!/usr/bin/env python3
# python3 predict.py <RECORD_NAME>
import sys
import json
import numpy as np
from sklearn.externals import joblib
from features import feature_dict
from utils.segmentation import get_transitions
from utils.misc import custom_loadmat
FEATURES = [
("mfcc", {'interval': 'S1'}),
("mfcc", {'interval': 'Sys'}),
("mfcc", {'interval': 'S2'}),
("mfcc", {'interval': 'Dia'}),
("dtw", {'interval': 'RR', 'constraint': 'sakoe_chiba', 'k': 0.1, 'norm': 'resample', 'pre': 'env'}),
]
def predict(clf, X, t=0.0, r=0.0):
try:
y_pred = clf.decision_function(X)
y_pred[y_pred > t + r] = 1
y_pred[y_pred < t - r] = -1
y_pred[np.abs(y_pred - t) <= r] = 0
except:
y_pred = clf.predict(X)
return y_pred
if __name__ == '__main__':
# Parse arguments
if len(sys.argv) < 2:
raise ValueError("Insufficient arguments")
record_name = sys.argv[1]
clf_folder = sys.argv[2]
# Load matlab output and get transitions of states
variables = custom_loadmat('segmentation/%s.mat' % record_name)
pcg = variables['PCG_resampled']
states = variables['assigned_states']
transitions = get_transitions(states)
# Load Classifier
clf = joblib.load(clf_folder+"/classifier.pkl")
with open(clf_folder + '/model.json', 'r') as fp:
model = json.load(fp)
t, r = model['thr__t'], model['thr__r']
# Compute features for the record and predict the label
X = np.concatenate([feature_dict[f](pcg, transitions, **kwargs) for f,kwargs in FEATURES])
X = X.reshape(1,-1)
# print(X)
y_pred = predict(clf, X, t, r)
# Append the result to the output file
with open('answers.txt','a') as f:
print('%s,%d' % (record_name, y_pred), file = f)