-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_add_feat.py
82 lines (75 loc) · 2.62 KB
/
gen_add_feat.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
import numpy as np
import pandas as pd
import sys
import preproc
## discard columns that don't have enough samples before the current gap
def discard_add_feat(chl,rng):
if np.isnan(chl[(rng[0]-10000-1):(rng[0]-1)]).any():
return 1
else:return 0
## generate data from available channel for training and prediction
def max_add_feat(active_chl,rng,pf):
rng = rng
min_per=1
addfeat_data = 0
header = preproc.get_header(pf)
header_non_act = np.copy(header).tolist()
header_non_act.remove(active_chl)
header_non_act.remove('Elapsed time')
for chl in header_non_act:
sig_add = preproc.get_column(chl,pf)[2:]
sig_add = preproc.check(sig_add,return_series=1)[0]
chl_add = sig_add[rng[0]:rng[-1]]
if not discard_add_feat(sig_add,rng):
idx_isnan = np.isnan(chl_add)
idx = np.arange(len(chl_add))
idx_nan = idx[idx_isnan]
percent = len(idx_nan)/float(len(idx))
if min_per ==1:
min_per=percent
min_chl = sig_add
elif percent<=min_per:
min_per = percent
min_chl = sig_add
if min_per==0:
break
addfeat_data = min_chl[rng[0]-50000:rng[0]]
if addfeat_data!=0:
if np.isnan(addfeat_data).any():
idx = np.arange(len(addfeat_data))
idx_isnan = np.isnan(addfeat_data)
idx = idx[idx_isnan].tolist()
idx_start = idx[-1]
addfeat_data = addfeat_data[idx_start:]
return chl_add, addfeat_data
## generate training data of the active channel
def gen_tr_data(rng,active_chl_array):
tr = active_chl_array[rng[0]-50000-1:rng[0]-1]
if np.isnan(tr).any():
idx_isnan = np.isnan(tr)
idx = np.arange(len(tr))
idx = idx[idx_isnan].tolist()
idx_start = idx_isnan[-1]
tr = tr[idx_start:]
if len(tr)>1e4:
return tr
else:
return 0
## generate data to pass to neuralnet interpolator
def add_feat(rng, active_chl,pf): ## name of active channel
active_chl= active_chl
rng = rng
pf = pf
tr=0
add_feat_pred,add_feat_tr = max_add_feat(active_chl,rng,pf)
print add_feat_pred,add_feat_tr
active_chl_array = preproc.get_column(active_chl,pf)[2:]
active_chl_array = preproc.check(active_chl_array,return_series=1)[0]
tr = gen_tr_data(rng,active_chl_array)
if tr!=0 and add_feat_tr!=0:
tr_len = min(len(tr),len(add_feat_tr))
tr = tr[-tr_len:]
add_feat_tr = add_feat_tr[-tr_len:]
return tr, add_feat_tr, add_feat_pred
else:
return 0