-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-almost-everything.py
executable file
·464 lines (339 loc) · 13.8 KB
/
test-almost-everything.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python
import sys
import json
import numpy as np
import numpy.random as rand
import rf_pipelines
########################################### helpers ##############################################
def xdiv(m, n):
assert n > 0
assert m >= 0
return m // n
def maxdiff(a1, a2):
assert a1.shape == a2.shape
return np.max(np.abs(a1-a2))
def fact2(n, kmin=1):
"""Factorizes n as (2^m k), where k is odd, and returns m."""
assert n >= kmin
m = 0
while ((n % 2) == 0) and (n >= 2*kmin):
(m, n) = (m+1, n//2)
return m
def wi_copy(intensity, weights, nt_chunk=None):
assert intensity.shape == weights.shape
assert intensity.ndim == 2
(nfreq, nt) = intensity.shape
assert nfreq > 0
assert nt > 0
if nt_chunk is None:
return (np.copy(intensity), np.copy(weights))
assert nt_chunk > 0
nt_new = ((nt + nt_chunk - 1) // nt_chunk) * nt_chunk
i_copy = np.empty((nfreq, nt_new), dtype=np.float32)
i_copy[:,:nt] = intensity[:,:]
i_copy[:,nt:] = rand.uniform(size=(nfreq,nt_new-nt))
w_copy = np.empty((nfreq, nt_new), dtype=np.float32)
w_copy[:,:nt] = weights[:,:]
w_copy[:,nt:] = 0.
return (i_copy, w_copy)
####################################################################################################
#
# make_random_*
#
# These functions return json-seralized random pipeline_objects (not the pipeline_objects themselves)
def make_random_polynomial_detrender(nfreq_ds, nds):
assert nfreq_ds > 0
assert nds > 0
if (rand.randint(0,2)) and (nfreq_ds >= 16):
axis = 'AXIS_FREQ'
maxdeg = (nfreq_ds // 8) - 2
maxdeg = min(maxdeg, 4)
polydeg = rand.randint(0, maxdeg+1)
nt_chunk = 8 * nds * max(rand.randint(-5,5), 0)
else:
axis = 'AXIS_TIME'
polydeg = rand.randint(0,5)
nt_chunk = 8 * nds * rand.randint(polydeg+2, 2*polydeg+4)
return {
'class_name': 'polynomial_detrender',
'nt_chunk': nt_chunk,
'axis': axis,
'polydeg': polydeg,
'epsilon': rand.uniform(1.0e-2, 2.0e-2)
}
def make_random_spline_detrender(nfreq_ds, nds):
assert nfreq_ds >= 32
max_nbins = nfreq_ds // 32
return {
'class_name': 'spline_detrender',
'nt_chunk': 8 * nds * max(rand.randint(-5,10),0),
'axis': 'AXIS_FREQ',
'nbins': rand.randint(1, min(max_nbins+1,5)),
'epsilon': rand.uniform(3.0e-4, 6.0e-4)
}
def make_random_intensity_clipper(nfreq_ds, nds):
assert nfreq_ds >= 32
a = rand.randint(0,3)
Dt = 2**max(rand.randint(-3,5),0)
if (a == 0) and (nfreq_ds >= 32):
axis = 'AXIS_FREQ'
m = fact2(nfreq_ds, kmin=32)
Df = 2**max(rand.randint(-3,m+1),0)
nt_chunk = 8 * nds * Dt * rand.randint(1, 8)
elif a == 1:
axis = 'AXIS_TIME'
m = fact2(nfreq_ds)
Df = 2**max(rand.randint(-3,m+1),0)
nt_chunk = 8 * nds * Dt * rand.randint(8, 16)
else:
axis = 'AXIS_NONE'
m = fact2(nfreq_ds)
Df = 2**max(rand.randint(-3,m+1),0)
p = (8 * Df) // nfreq_ds
nt_chunk = 8 * nds * Dt * rand.randint(p+1, 10)
return {
'class_name': 'intensity_clipper',
'axis': axis,
'Df': Df,
'Dt': Dt,
'nt_chunk': nt_chunk,
'sigma': rand.uniform(1.7, 2.0),
'iter_sigma': rand.uniform(1.7, 2.0),
'niter': max(rand.randint(-3,4), 1),
'two_pass': True if rand.randint(0,2) else False
}
def make_random_std_dev_clipper(nfreq_ds, nds):
assert nfreq_ds >= 32
assert nfreq_ds % 8 == 0
axis = 'AXIS_FREQ' if rand.randint(0,2) else 'AXIS_TIME'
m = fact2(nfreq_ds, kmin=4)
Df = 2**max(rand.randint(-3,m-2),0)
Dt = 2**max(rand.randint(-3,5),0)
nt_chunk = 8 * nds * Dt * rand.randint(8,16)
return {
'class_name': 'std_dev_clipper',
'axis': axis,
'Df': Df,
'Dt': Dt,
'nt_chunk': nt_chunk,
'sigma': rand.uniform(1.7, 2.0),
'two_pass': True if rand.randint(0,2) else False
}
def make_random_transform_list(nfreq_ds, nds, nelements):
assert nelements > 0
ret = [ ]
while (rand.uniform() < 0.5) and (nelements > 1):
n = rand.randint(1, nelements+1)
ret.append(make_random_pipeline(nfreq_ds, nds, n))
nelements -= (n-1)
while len(ret) < nelements:
# If more transforms are added to the list below,
# don't forget to increment the randint() upper limit!
r = rand.randint(0,5)
if (r == 0):
ret.append(make_random_polynomial_detrender(nfreq_ds, nds))
elif (r == 1) and (nfreq_ds >= 32):
ret.append(make_random_spline_detrender(nfreq_ds, nds))
elif (r == 2) and (nfreq_ds >= 32):
ret.append(make_random_intensity_clipper(nfreq_ds, nds))
elif (r == 3) and (nfreq_ds % 8 == 0) and (nfreq_ds >= 32):
ret.append(make_random_std_dev_clipper(nfreq_ds, nds))
return ret
def make_random_pipeline(nfreq_ds, nds, nelements, allow_downsampling=True):
m = fact2(nfreq_ds)
n = 5 - fact2(nds)
Df = 2**max(rand.randint(-5,m+1),0) if allow_downsampling else 1
Dt = 2**max(rand.randint(-5,n+1),0) if allow_downsampling else 1
tl = make_random_transform_list(nfreq_ds // Df, nds * Dt, nelements)
if (len(tl) > 1) or (rand.uniform() < 0.1):
ret = { 'class_name': 'pipeline', 'name': 'pipeline', 'elements': tl }
else:
ret = tl[0]
if (Df == 1) and (Dt == 1) and (rand.random() < 0.9):
return ret
ret = {
'class_name': 'wi_sub_pipeline',
'sub_pipeline': ret,
'w_cutoff': rand.uniform(0.0, 0.01),
'nfreq_out': nfreq_ds // Df,
'nds_out': nds * Dt,
'Df': Df,
'Dt': Dt
}
if rand.uniform() < 0.33:
ret['nfreq_out'] = 0
elif rand.uniform() < 0.5:
ret['Df'] = 0
if rand.uniform() < 0.33:
ret['nds_out'] = 0
elif rand.uniform() < 0.5:
ret['Dt'] = 0
return ret
####################################################################################################
#
# emulate_pipeline
def emulate_pipeline(pipeline_json, intensity, weights, nds=1):
"""Returns new (intensity, weights) pair."""
assert intensity.ndim == 2
assert intensity.shape == weights.shape
(nfreq, nt_ds) = intensity.shape
if pipeline_json['class_name'] == 'pipeline':
for q in pipeline_json['elements']:
(intensity, weights) = emulate_pipeline(q, intensity, weights, nds)
return (intensity, weights)
if pipeline_json['class_name'] == 'wi_sub_pipeline':
sub_pipeline = pipeline_json['sub_pipeline']
w_cutoff = pipeline_json['w_cutoff']
nfreq_out = pipeline_json['nfreq_out']
nds_out = pipeline_json['nds_out']
Df = pipeline_json['Df']
Dt = pipeline_json['Dt']
if (nfreq_out > 0) and (Df > 0):
assert nfreq == nfreq_out * Df
elif (nfreq_out == 0) and (Df > 0):
assert nfreq % Df == 0
nfreq_out = nfreq // Df
elif (nfreq_out > 0) and (Df == 0):
assert nfreq % nfreq_out == 0
Df = nfreq // nfreq_out
if (nds_out > 0) and (Dt > 0):
assert nds_out == nds * Dt
elif (nds_out == 0) and (Dt > 0):
nds_out = nds * Dt
elif (nds_out > 0) and (Dt == 0):
assert nds_out % nds == 0
Dt = nds_out // nds
(i_copy, w_copy) = wi_copy(intensity, weights, 8*Dt)
(i_ds, w_ds) = rf_pipelines.wi_downsample(i_copy, w_copy, Df, Dt)
n = i_ds.shape[1]
(i_ds, w_ds) = emulate_pipeline(sub_pipeline, i_ds, w_ds, nds_out)
rf_pipelines.weight_upsample(w_copy, w_ds[:,:n], w_cutoff)
return (i_copy, w_copy)
if pipeline_json['class_name'] == 'polynomial_detrender':
axis = pipeline_json['axis']
polydeg = pipeline_json['polydeg']
nt_chunk = pipeline_json['nt_chunk']
epsilon = pipeline_json['epsilon']
if axis == 'AXIS_FREQ':
(i_copy, w_copy) = wi_copy(intensity, weights, 8)
rf_pipelines.apply_polynomial_detrender(i_copy, w_copy, axis, polydeg, epsilon)
return (i_copy, w_copy)
if axis == 'AXIS_TIME':
n = xdiv(nt_chunk, nds)
(i_copy, w_copy) = wi_copy(intensity, weights, n)
for it in xrange(0, nt_ds, n):
rf_pipelines.apply_polynomial_detrender(i_copy[:,(it):(it+n)], w_copy[:,(it):(it+n)], axis, polydeg, epsilon)
return (i_copy, w_copy)
raise RuntimeError('emulate_pipeline: unsupported polynomial_detrender axis "%s"' % axis)
if pipeline_json['class_name'] == 'spline_detrender':
axis = pipeline_json['axis']
nbins = pipeline_json['nbins']
epsilon = pipeline_json['epsilon']
if axis == 'AXIS_FREQ':
(i_copy, w_copy) = wi_copy(intensity, weights, 8)
rf_pipelines.apply_spline_detrender(i_copy, w_copy, axis, nbins, epsilon)
return (i_copy, w_copy)
raise RuntimeError('emulate_pipeline: unsupported polynomial_detrender axis "%s"' % axis)
if pipeline_json['class_name'] == 'intensity_clipper':
axis = pipeline_json['axis']
Df = pipeline_json['Df']
Dt = pipeline_json['Dt']
niter = pipeline_json['niter']
sigma = pipeline_json['sigma']
iter_sigma = pipeline_json['iter_sigma']
nt_chunk = pipeline_json['nt_chunk']
two_pass = pipeline_json['two_pass']
if axis == 'AXIS_FREQ':
(i_copy, w_copy) = wi_copy(intensity, weights, 8*Dt)
rf_pipelines.apply_intensity_clipper(i_copy, w_copy, axis, sigma, niter, iter_sigma, Df, Dt, two_pass)
return (i_copy, w_copy)
n = xdiv(nt_chunk, nds)
(i_copy, w_copy) = wi_copy(intensity, weights, n)
for it in xrange(0, nt_ds, n):
rf_pipelines.apply_intensity_clipper(i_copy[:,(it):(it+n)], w_copy[:,(it):(it+n)], axis, sigma, niter, iter_sigma, Df, Dt, two_pass)
return (i_copy, w_copy)
if pipeline_json['class_name'] == 'std_dev_clipper':
axis = pipeline_json['axis']
Df = pipeline_json['Df']
Dt = pipeline_json['Dt']
sigma = pipeline_json['sigma']
nt_chunk = pipeline_json['nt_chunk']
two_pass = pipeline_json['two_pass']
n = xdiv(nt_chunk, nds)
(i_copy, w_copy) = wi_copy(intensity, weights, n)
for it in xrange(0, nt_ds, n):
rf_pipelines.apply_std_dev_clipper(i_copy[:,(it):(it+n)], w_copy[:,(it):(it+n)], axis, sigma, Df, Dt, two_pass)
return (i_copy, w_copy)
raise RuntimeError('emulate_pipeline: unsupported class_name "%s"' % pipeline_json['class_name'])
####################################################################################################
class initial_stream(rf_pipelines.wi_stream):
def __init__(self, nfreq=None):
rf_pipelines.wi_stream.__init__(self, 'initial_stream')
if nfreq is None:
nfreq = 2**rand.randint(0, 10)
nfreq *= rand.randint(128//nfreq + 1, 2048//nfreq + 1)
self.nfreq = nfreq
self.nt_tot = rand.randint(10000, 40000)
self.intensity = rand.standard_normal(size=(nfreq,self.nt_tot))
self.weights = rand.uniform(0.5, 1.0, size=(nfreq,self.nt_tot))
self.nt_chunk = rand.randint(20, 50)
def _fill_chunk(self, intensity, weights, pos):
intensity[:,:] = 0.
weights[:,:] = 0.
if pos >= self.nt_tot:
return False
n = min(self.nt_tot - pos, self.nt_chunk)
intensity[:,:n] = self.intensity[:,pos:(pos+n)]
weights[:,:n] = self.weights[:,pos:(pos+n)]
return True
class final_transform(rf_pipelines.wi_transform):
def __init__(self, nt_chunk=None):
if nt_chunk is None:
nt_chunk = rand.randint(10,20)
rf_pipelines.wi_transform.__init__(self, "final_transform")
self.nt_chunk = nt_chunk
self.intensity_chunks = [ ]
self.weight_chunks = [ ]
def _process_chunk(self, intensity, weights, pos):
self.intensity_chunks.append(np.copy(intensity))
self.weight_chunks.append(np.copy(weights))
def get_results(self):
intensity = np.concatenate(self.intensity_chunks, axis=1)
weights = np.concatenate(self.weight_chunks, axis=1)
return (intensity, weights)
def run_test():
s = initial_stream()
u = final_transform()
tj = make_random_transform_list(s.nfreq, 1, nelements=rand.randint(10,20))
t = [ rf_pipelines.pipeline_object.from_json(j) for j in tj ]
p = rf_pipelines.pipeline([s] + t + [u])
p.bind(outdir=None, verbosity=0, debug=True)
# Check jsonization (test is slightly stronger if bind() comes first)
tj2 = [ x.jsonize() for x in t ]
rf_pipelines.utils.json_assert_equal(tj, tj2, name1='reference_json', name2='pipeline_json')
# First run
p.run(outdir=None, verbosity=0, debug=True)
(i0,w0) = u.get_results()
nt0 = i0.shape[1]
assert nt0 >= s.nt_tot
assert i0.shape == w0.shape == (s.nfreq, nt0)
assert np.all(w0[:,s.nt_tot:] == 0.0)
# Second run
pj = { 'class_name': 'pipeline', 'elements': tj }
(i1,w1) = emulate_pipeline(pj, s.intensity, s.weights)
nt1 = i1.shape[1]
assert nt1 >= s.nt_tot
assert i1.shape == w1.shape == (s.nfreq, nt1)
assert np.all(w1[:,s.nt_tot:] == 0.0)
# Compare
eps_i = maxdiff((i0*w0)[:,:s.nt_tot], (i1*w1)[:,:s.nt_tot])
eps_w = maxdiff(w0[:,:s.nt_tot], w1[:,:s.nt_tot])
assert eps_i < 1.0e-5
assert eps_w < 1.0e-5
####################################################################################################
rand.seed(23)
niter = 100
for iter in xrange(niter):
print 'iteration %d/%d' % (iter, niter)
run_test()
print 'test-almost-everything: pass'