-
Notifications
You must be signed in to change notification settings - Fork 6
/
data.py
179 lines (128 loc) · 4.33 KB
/
data.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
from window import winbatch
import syn
import os
import sys
def get_series(id):
"""returns 2d shape (time,ndim)"""
def txtrdr(*args,**kwargs):
kwargs.setdefault('dtype','float32')
return np.loadtxt(os.path.join(os.path.split(os.path.abspath(
sys.modules[__name__].__file__))[0]
,'data'
,id)
,**kwargs)
if 'ecg' in id:
ecg=txtrdr()[::20,None]
if id=='ecg':
return ecg
elif id=='ecg-anom':
ecg=get_series('ecg')
sn1=.25*ecg.shape[0]
sn2= .3*ecg.shape[0]
sn=(.2*np.sin(np.linspace(0,3*3.14,num=(sn2-sn1))))
#put anomaly in input
ecg[sn1:sn2]=sn[:,None]
return ecg
elif 'sleep'==id:
#from https://physionet.org/atm/ucddb/ucddb002_lifecard.edf/180/60/rdsamp/csv/pS/samples.csv
#St. Vincent's University Hospital / University College Dublin
#Sleep Apnea Database
return txtrdr(id,skiprows=2,delimiter=',')[::3,2,None]
elif 'sin'==id:
#import syn why not work here???
return syn.pulsegen()[:,None]
elif 'spike'==id:
return syn.cyclespike()[:,None]
elif 'spikereg'==id:
return syn.cyclespikereg()[:,None]
elif 'spikelv'==id:
return syn.cyclespikelv()[:,None]
elif 'twitter'==id:
return txtrdr(id,skiprows=1,usecols=(3,))[:,None]
elif 'test'==id:
return get_series('sin')
elif 'power'==id:# http://www.cs.ucr.edu/~eamonn/discords/
pd=txtrdr(id,skiprows=0)[::5,None]
return pd/np.median(pd)
elif 'lintrend'==id:# yahoo anom det A4Benchmark-TS2
pd=txtrdr(id,skiprows=1,delimiter=',',usecols=(1,))[:,None]
return pd/np.median(pd)
elif 'sleeptest'==id:#the anom region is just avg of data
d=get_series('sleep')
d[1500:1740,0]=np.mean(d[:,0])
return d
#should not be here
raise KeyError('series not found')
def get_kwargs(id,**kwargs):
# nice to check the number of batches
# winbatch(ts,**getKwargs(ts)).length should be 'reasonable'
kwargs2=kwargs.copy()
ts=get_series(id)
tnth=int(.1*len(ts))
kwargs.setdefault('min_winsize', int( tnth))
kwargs.setdefault('slide_jump' , int(.10*tnth))
kwargs.setdefault('winsize_jump', int(.10*tnth))
kwargs.setdefault('batch_size', 10 )
if 'ecg'==id:
kwargs['min_winsize']= 300
kwargs['slide_jump']= 20
kwargs['winsize_jump']= 20
kwargs['batch_size']= 30
elif 'sleep'==id:
kwargs['min_winsize']= 300
kwargs['slide_jump']= 20
kwargs['winsize_jump']= 20
kwargs['batch_size']= 30
elif 'sin'==id:
kwargs['batch_size']= 30
elif 'twitter'==id:
kwargs['batch_size']= 30
kwargs['min_winsize']= 4000
elif 'power'==id:
kwargs['min_winsize']= 100
kwargs['slide_jump']= 100
kwargs['winsize_jump']= 20
kwargs['batch_size']= 30
elif 'lintrend'==id:
kwargs['winsize_jump']=30
kwargs['slide_jump']=30
elif 'spike' in id:
kwargs['winsize_jump']=20
kwargs['slide_jump']=20
kwargs['min_winsize']=100
#else:
# raise KeyError
# but the kwargs in the func arg overrides
for ak in kwargs2: kwargs[ak]=kwargs2[ak]
return kwargs
def get(id,**kwargs):
"""for consumption by rnn training"""
ts=get_series(id)
kwargs=get_kwargs(id,**kwargs)
batches=[]
bg=winbatch(ts,**kwargs)
for i in xrange(bg.length):
batches.append(bg())
return batches
def dim(id):
return get_series(id).shape[1]
from window import slidingwindow as win
def window(id,**kwargs):
a=[]
ts=get_series(id)
for awin in win(ts,**kwargs):
a.append(awin)
return np.array(a,dtype='float32')
from itertools import cycle
class list_call(object):
def __init__(self,tsbatch_list,**kwargs):
self.kwargs=kwargs
self.tsbatch_list=tsbatch_list
self.iter=cycle(self.__iter__())
def __iter__(self):
return iter(self.tsbatch_list)
def __call__(self):
return self.iter.next()
def __len__(self):
return len(self.tsbatch_list)