Spaces:
Running
Running
louismichel
commited on
Commit
•
b48bae5
1
Parent(s):
057b9cf
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
import pickle
|
4 |
+
from sklearn.preprocessing import normalize
|
5 |
+
from werkzeug.utils import secure_filename
|
6 |
+
import scipy.signal as signal
|
7 |
+
import pandas as pd
|
8 |
+
import os
|
9 |
+
|
10 |
+
##########
|
11 |
+
|
12 |
+
with open('model_a1.bin', 'rb') as f_in:
|
13 |
+
model_n = pickle.load(f_in)
|
14 |
+
|
15 |
+
"""
|
16 |
+
def intensityNormalisationFeatureScaling(da, dtype):
|
17 |
+
max = da.max()
|
18 |
+
min = da.min()
|
19 |
+
|
20 |
+
return ((da - min) / (max - min)).astype(dtype)
|
21 |
+
"""
|
22 |
+
|
23 |
+
def filter_data(da, fs):
|
24 |
+
|
25 |
+
"""Filters the ECG data with a highpass at 0.1Hz and a bandstop around 50Hz (+/-2 Hz)"""
|
26 |
+
|
27 |
+
b_dc, a_dc = signal.butter(4, (0.1/fs*2), btype='highpass')
|
28 |
+
b_50, a_50 = signal.butter(4, [(48/fs*2),(52/fs*2)], btype='stop')
|
29 |
+
|
30 |
+
da = signal.lfilter(b_dc, a_dc, da)
|
31 |
+
da = signal.lfilter(b_50, a_50, da)
|
32 |
+
|
33 |
+
return da
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
# Return difference array
|
38 |
+
def return_diff_array_table(array, dur):
|
39 |
+
for idx in range(array.shape[1]-dur):
|
40 |
+
before_col = array[:,idx]
|
41 |
+
after_col = array[:,idx+dur]
|
42 |
+
new_col = ((after_col - before_col)+1)/2
|
43 |
+
new_col = new_col.reshape(-1,1)
|
44 |
+
if idx == 0:
|
45 |
+
new_table = new_col
|
46 |
+
else :
|
47 |
+
new_table = np.concatenate((new_table, new_col), axis=1)
|
48 |
+
#For concat add zero padding
|
49 |
+
padding_array = np.zeros(shape=(array.shape[0],dur))
|
50 |
+
new_table = np.concatenate((padding_array, new_table), axis=1)
|
51 |
+
return new_table
|
52 |
+
|
53 |
+
|
54 |
+
#Concat
|
55 |
+
def return_merge_diff_table(df, diff_dur):
|
56 |
+
fin_table = df.reshape(-1,187,1,1)
|
57 |
+
for dur in diff_dur:
|
58 |
+
temp_table = return_diff_array_table(df, dur)
|
59 |
+
fin_table = np.concatenate((fin_table, temp_table.reshape(-1,187,1,1)), axis=2)
|
60 |
+
return fin_table
|
61 |
+
|
62 |
+
|
63 |
+
def predict_endpoint(features):
|
64 |
+
#features = intensityNormalisationFeatureScaling(features, float)
|
65 |
+
features = filter_data(features, 300)
|
66 |
+
X = return_merge_diff_table(features, diff_dur=[1])
|
67 |
+
preds = model_n.predict(X)
|
68 |
+
return preds
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
app = Flask('__name__')
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
@app.route('/')
|
78 |
+
def home():
|
79 |
+
return render_template('index.html')
|
80 |
+
|
81 |
+
@app.route('/predict',methods=['POST'])
|
82 |
+
def predict():
|
83 |
+
|
84 |
+
Age=float(request.form['Age'])
|
85 |
+
|
86 |
+
Gender=float(request.form['Gender'])
|
87 |
+
|
88 |
+
f = request.files['signal']
|
89 |
+
f.save(secure_filename(f.filename))
|
90 |
+
|
91 |
+
df = pd.read_csv(f.filename, header=None)
|
92 |
+
|
93 |
+
df_2 = df.iloc[3:4,0:187]
|
94 |
+
|
95 |
+
features_t = np.array(df_2)
|
96 |
+
|
97 |
+
pred = predict_endpoint(features_t)
|
98 |
+
|
99 |
+
print(pred[0])
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
if float(pred[0][0]) > float(pred[0][1]):
|
105 |
+
os.remove(f.filename)
|
106 |
+
return render_template('Good.html', prediction=pred[0][0])
|
107 |
+
else:
|
108 |
+
return render_template('Bad.html', prediction2=pred[0][1])
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
if __name__ == "__main__":
|
113 |
+
#app.run(debug=True, host='0.0.0.0', port=9696)
|
114 |
+
app.run(debug=True)
|