-
Notifications
You must be signed in to change notification settings - Fork 0
/
real_time_prediction.py
executable file
·113 lines (93 loc) · 3.4 KB
/
real_time_prediction.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 8 15:12:03 2020
@author: marley
"""
import numpy as np
import pandas as pd
from scipy import signal
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
import seaborn as sns
from features import *
import pickle
def get_name(channel_name, feature_name):
return "{}_{}".format(channel_name, feature_name)
def compute_feature(data, channel_names, feature_name, to_df=True):
"""
Get features from window, non-mutating
Parameters
----------q_40_60_abs(si
df : pd.DataFrame
dataframe with windows
channel_names : list of strings
feature_name : string
string name of the feature function
to_df : bool
if output should be converfeatures(ted to a dataframe
Returns
-------
df_result : pd.DataFrame (to_df = True) or dictionary
new dataframe with feature columns for each channel
"""
fn = globals()[feature_name]
# computed_features = []
new_channel_names = []
computed_features = [[fn(ch) for ch in data]]
computed_features = np.array(computed_features).T
result = {}
if computed_features.ndim > 2:
print(computed_features)
for i in range(computed_features.shape[0]):
feat = computed_features[i]
for channel_name, actual_feat in zip(channel_names, feat):
new_name = get_name(channel_name, feature_name) + "_" + str(i)
result[new_name] = actual_feat
new_channel_names.append(new_name)
else:
print('compted_features')
print(computed_features)
new_channel_names = [get_name(channel_name,feature_name) for channel_name in channel_names]
result = {get_name(channel_name, feature_name): feature
for channel_name, feature in zip(channel_names, computed_features)}
if to_df:
result = pd.DataFrame(result)
return result,new_channel_names
def compute_features(data, channel_names, feature_names, mutate=False):
"""
Get features from window, non-mutating
Parameters
----------
df : pd.DataFrame
dataframe with windows
channel_names : list of strings
feature_names : list of strings
Returns
-------
df_result : pd.DataFrame
new dataframe with feature columns for each channel
"""
all_results = {}
all_ch_names = []
for feature_name in feature_names:
result,new_channel_names = compute_feature(data, channel_names, feature_name, to_df=False)
all_results.update(result)
all_ch_names = all_ch_names + new_channel_names
return all_results,all_ch_names
def predict_function(arr):
res, _ = compute_features(arr, channel_names, features)
input_arr = np.array(list(res.values()))
return clf.predict_proba(np.squeeze(input_arr).reshape(1, -1))
# model_file = "model_windows-2020-02-23-03_08_2020_15:22:15.pkl"
# model_file = 'model_windows-2020-02-23-03_08_2020_15:41:37.pkl'
model_file = 'model_windows-2020-02-23-03_08_2020_15:46:13.pkl'
model_file = 'model_windows-2020-02-23-03_08_2020_15:48:56.pkl'
with open(model_file, 'rb') as f:
data = pickle.load(f)
clf = data['classifier']
features = data['features']
channels = [1,2,3,4,5,6,7,8]
channel_names = ['channel {}'.format(i) for i in channels]
sample = np.zeros([len(channels), 250])
predict_function(sample)