-
Notifications
You must be signed in to change notification settings - Fork 0
/
envelope.py
259 lines (211 loc) · 9.54 KB
/
envelope.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
# coding: utf-8
"""Envelopes for basic replacement methods."""
import concurrent
import pickle
import logging
import os
from glob import glob
from functools import lru_cache
import pandas as pd
import manualsarima as ms
import numpy as np
import transport as ts
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class BaseEnvelope(object):
def __init__(self, folder, index, timeline, *args, **kwargs):
self.folder = folder
self.timeline = timeline
self.index = index
model_basename = 'model_{}_{}.model'.format(self.index, self.timeline)
truth_basename = 'origin_{}_{}.raw'.format(self.index, self.timeline)
self._modelpath = os.path.join(os.path.dirname(__file__), 'model', model_basename)
self._truthpath = os.path.join(os.path.dirname(__file__),'data','original',truth_basename)
@property
def truth(self):
return pd.Series(np.fromfile(self._truthpath, np.float32))
@property
def model(self):
return pickle.load(open(self._modelpath, 'rb'))
@property
def parameters(self):
return ms.getparams(self.model)
@property
def _core(self):
df = pd.DataFrame()
files = [os.path.join(self.folder,
'*_{}_{}.{:02d}.raw'.format(self.index,
self.timeline, bits))
for bits in range(1, 33)]
files = [glob(x)[0] for x in files]
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_np = {executor.submit(np.fromfile,
fname, np.float32):
fname for fname in files}
for ftre in concurrent.futures.as_completed(future_to_np):
fname = future_to_np[ftre]
bits = int(fname[-6:-4])
try:
data = ftre.result()
df[self.index, bits] = data
except Exception:
raise
else:
logger.info('Loaded: %s', fname)
df.columns = pd.MultiIndex.from_tuples(df.columns,
names=['index', 'bits'])
return df
def uncompressed_bits(self, bits):
return self._core.loc[:, (self.index, bits)]
class TimeSeriesEnvelope(BaseEnvelope):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def signal(self):
return self._core
def signal_bits(self, bits):
return self.uncompressed_bits(bits)
class ARIMAEnvelope(BaseEnvelope):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.rev, _ = ms.differentiation(self.model.specification.get('k_diff',
0))
@property
@lru_cache(maxsize=32)
def signal(self):
df = pd.DataFrame()
r, _ = ms.differentiation(self.model.specification.get('k_diff', 0))
for column in self._core:
resid = self._core.loc[:, column]
df[column] = self._reverse(resid)
df.columns = pd.MultiIndex.from_tuples(df.columns,
names=['index', 'bits'])
df.sort_index(axis=1, level=1, inplace=True)
return df
def signal_bits(self, bits):
return self._reverse(bits)
def _reverse(self, resid):
if isinstance(resid, (int, float)):
resid = self.uncompressed_bits(resid)
if not isinstance(resid, pd.Series):
raise ValueError(resid, 'must be a pd.Series')
fitted = ms.calc_fitted_seasonal(resid=resid, **self.parameters)
return pd.Series(self.rev(fitted, self.truth))
class Modified(ARIMAEnvelope):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@lru_cache(maxsize=32)
def best_compressed(self, atol=1e-05):
for i in range(1, 33):
if not ts.indices_need_correction(self.signal_bits(i), self.truth,
atol=atol).any():
return self.uncompressed_bits(i)
return self.truth
@property
def blocksize(self):
arparams = self.parameters['arparams'].size
maparams = self.parameters['maparams'].size
return max(arparams, maparams)
def replace_first(self, percentage, from_bits, to_bits):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
num = int(to_series.size*percentage)
resid = from_series[:num].append(to_series[num:])
return self._reverse(resid)
def replace_evenly(self, percentage, from_bits, to_bits):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
resid = ts.replace(percentage, self.blocksize,
from_series, to_series, mode='percentage')
return self._reverse(resid)
def replace_special(self, percentage, from_bits, to_bits, naked=False):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
if not naked:
cumseries = pd.Series(np.diff(ts.cumcorr(self._reverse(to_series),
self.truth)))
else:
cumseries = pd.Series(np.diff(ts.cumcorr(to_series, self.truth)))
num = int(to_series.size*percentage)
listing = sorted([x for x in cumseries.sort_values()[:num].index])
resid = to_series.copy()
print('Special replaces:', len(listing))
for x in listing:
addpos = self.blocksize
try:
resid.set_value(x+addpos, from_series.iloc[x+addpos])
except IndexError:
resid.set_value(x, from_series.iloc[x])
return self._reverse(resid)
def raw_replace_special(self, percentage, from_bits, to_bits):
return self.replace_special(percentage, from_bits, to_bits, naked=True)
def replace_singles(self, listing, from_bits, to_bits):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
resid = to_series.copy()
for x in listing:
msg = 'Replace: Pos {} from {} to {}'.format(x, to_series.iloc[x],
from_series.iloc[x])
logging.info(msg)
resid.set_value(x, from_series.iloc[x])
return self._reverse(resid)
def replace_rolling(self, percentage, from_bits, to_bits, naked=False):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
num = int(to_series.size*percentage)
if naked:
roll_coor = to_series.rolling(self.blocksize).corr(self.truth)
else:
roll_coor = self._reverse(to_series).rolling(self.blocksize).corr(self.truth)
blockstarts = roll_coor[roll_coor.diff() < -1e-5].index.values
logger.info(blockstarts)
listing = set(sorted([x-y
for x in blockstarts
for y in range(self.blocksize)]))
listing = list(listing)[:num]
logging.info(listing)
resid = to_series.copy()
for x in listing[:num]:
msg = 'Replace: Pos {} from {} to {}'.format(x, to_series.iloc[x],
from_series.iloc[x])
logging.info(msg)
resid.set_value(x, from_series.iloc[x])
return self._reverse(resid)
def raw_replace_rolling(self, percentage, from_bits, to_bits):
return self.replace_rolling(percentage, from_bits, to_bits,
naked=True)
def replace_cumcorr(self, percentage, from_bits, to_bits, naked=False):
from_series = self.uncompressed_bits(from_bits)
to_series = self.uncompressed_bits(to_bits)
num = int(to_series.size*percentage)
injected = list()
resid = to_series.copy()
while len(injected) < num:
signal = self._reverse(resid=resid) if not naked else resid
ccorr = pd.Series(ts.cumcorr(timeseries=signal,
other=self.truth))
grouped = ccorr.diff().groupby((ccorr > ccorr.shift()).cumsum())
downfall = [(d[1:].index.values[0], d[1:].sum())
for _, d in grouped if len(d[1:].index.values) > 0]
sorted_downfall = sorted(downfall, key=lambda tup: tup[1])
for idx, _ in sorted_downfall:
minimum = idx
while minimum - 1 in injected:
minimum -= 1
if minimum < self.blocksize:
continue
listing = sorted([minimum-y for y in range(1, self.blocksize+1)])
if not all([x in injected for x in listing]):
break
for x in listing:
msg = 'Replace: Pos {} from {} to {}'.format(x, to_series.iloc[x],
from_series.iloc[x])
logging.warning(msg)
resid.set_value(x, from_series.iloc[x])
injected += listing
logger.info("Changed {} values.".format(injected))
return self._reverse(resid)
def raw_replace_cumcorr(self, percentage, from_bits, to_bits):
return self.replace_cumcorr(percentage, from_bits, to_bits,
naked=True)