-
Notifications
You must be signed in to change notification settings - Fork 3
/
waterfall_plotter.cpp
469 lines (356 loc) · 11.7 KB
/
waterfall_plotter.cpp
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
465
466
467
468
469
// FIXME this code could be streamlined significantly by using the downsampled_intensity helper class
#include <cmath>
#include <cstring>
#include <algorithm>
#include <png.h>
#include "ch_vdif_assembler.hpp"
using namespace std;
namespace ch_vdif_assembler {
#if 0
}; // pacify emacs c-mode!
#endif
// -------------------------------------------------------------------------------------------------
//
// png_writer: a thin wrapper class around libpng for writing images (nothing in this class
// has anything to do with vdif data)
struct png_writer : noncopyable {
png_structp png_ptr;
png_infop info_ptr;
FILE *fp;
png_writer();
~png_writer();
//
// @rgb should be an array of shape (ny,nx,3) where the last index is "rgb".
// The first index (y) runs from top to bottom (not bottom to top) in the image.
//
void write(const string &filename, png_byte *rgb, int nx, int ny);
void deallocate();
};
png_writer::png_writer()
: png_ptr(NULL), info_ptr(NULL), fp(NULL)
{ }
png_writer::~png_writer()
{
this->deallocate();
}
void png_writer::deallocate()
{
// close file first
if (fp) {
fclose(fp);
fp = NULL;
}
//
// The libpng documentation doesn't really explain how to clean up its state.
//
// I dug into the source code and decided that one call to png_destroy_write_struct()
// destroys both the png_ptr and the info_ptr, and does the right thing if only the
// png_ptr is non-NULL.
//
if (png_ptr || info_ptr) {
png_destroy_write_struct(&png_ptr, &info_ptr);
png_ptr = NULL;
info_ptr = NULL;
}
}
void png_writer::write(const string &filename, png_byte *rgb, int nx, int ny)
{
// just in case any state remains from a previous call
this->deallocate();
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
xassert(png_ptr);
info_ptr = png_create_info_struct(png_ptr);
xassert(info_ptr);
fp = fopen(filename.c_str(), "wb");
if (!fp) {
cerr << "png_writer: couldn't open file " << filename << endl;
this->deallocate();
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
cerr << "png_writer: libpng internal failure\n";
this->deallocate();
return;
}
png_init_io(png_ptr, fp);
png_set_IHDR(png_ptr, info_ptr, nx, ny,
8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
for (int i = 0; i < ny; i++)
png_write_row(png_ptr, &rgb[3*i*nx]);
png_write_end(png_ptr, NULL);
this->deallocate();
cout << "wrote " << filename << endl;
}
// -------------------------------------------------------------------------------------------------
//
// This class really represents a pair of images, one for each polarization
struct waterfall_image {
int nfreq;
int nt_bins;
// These buffers have shape (nfreq, 2, nt_bins) where the second index is polarization
std::vector<double> acc_num;
std::vector<double> acc_den;
png_writer pw;
// Construct empty image
waterfall_image(int nfreq, int nt_bins);
// Construct image by downgrading existing image (e.g. to make html thumbnail)
waterfall_image(const waterfall_image &w, int new_nfreq, int new_nt_bins);
// This is the main computational routine.
void absorb_chunk(const assembled_chunk &chunk);
void clear();
void subtract_medians();
// The mask here has shape (nfreq, nt_bins) and applies to a single polarization
double compute_variance_outside_mask(int pol, const int *mask);
// Generates a mask internally using iterative clipping
double compute_clipped_variance(int pol);
void write_image(const string &filename, int pol, double dv);
void write_image(const string &filename, int pol);
};
// Construct empty image
waterfall_image::waterfall_image(int nfreq_, int nt_bins_)
: nfreq(nfreq_), nt_bins(nt_bins_),
acc_num(nfreq_ * 2 * nt_bins_, 0),
acc_den(nfreq_ * 2 * nt_bins_, 0)
{
xassert(nfreq > 0);
xassert(nt_bins > 0);
}
// Construct image by downgrading existing image (e.g. to make html thumbnail)
waterfall_image::waterfall_image(const waterfall_image &w, int new_nfreq, int new_nt_bins)
: nfreq(new_nfreq), nt_bins(new_nt_bins),
acc_num(new_nfreq * 2 * new_nt_bins, 0),
acc_den(new_nfreq * 2 * new_nt_bins, 0)
{
xassert(nfreq > 0);
xassert(nt_bins > 0);
xassert(w.nfreq % nfreq == 0);
xassert(w.nt_bins % nt_bins == 0);
int ff = w.nfreq / nfreq;
int bb = w.nt_bins / nt_bins;
for (int f = 0; f < w.nfreq; f++) {
for (int pol = 0; pol < 2; pol++) {
int isrc = (2*f + pol) * w.nt_bins;
int idst = (2*(f/ff) + pol) * nt_bins;
for (int b = 0; b < w.nt_bins; b++) {
acc_num[idst + (b/bb)] += w.acc_num[isrc + b];
acc_den[idst + (b/bb)] += w.acc_den[isrc + b];
}
}
}
}
//
// Since this routine is the rate-limiting step of the waterfall_assembler,
// it's written with a lot of optimization
//
void waterfall_image::absorb_chunk(const assembled_chunk &a)
{
// Number of timestamps per waterfall bin
int tt = constants::timestamps_per_frame / this->nt_bins;
// These assumptions simplify the code, but could be removed if necessary
xassert(this->nfreq == constants::chime_nfreq);
xassert(constants::timestamps_per_frame % this->nt_bins == 0);
xassert(a.t0 % tt == 0);
xassert(a.nt % tt == 0);
xassert(tt % 16 == 0);
int b0 = (a.t0 / tt) % nt_bins; // first waterfall bin in assembled chunk
int nb = (a.nt / tt); // number of waterfall bins in assembled chunk
for (int f = 0; f < nfreq; f++) {
for (int pol = 0; pol < 2; pol++) {
for (int b = 0; b < nb; b++) {
// Points to the data which will be accumulated into waterfall bin (f,p,b0+b)
const uint8_t *buf = &a.buf[(2*f+pol) * a.nt + b*tt];
int num = 0;
int den = 0;
int sum, count;
for (int j = 0; j < tt; j += 16) {
assembled_chunk::sum16_auto_correlations(sum, count, buf+j);
num += sum;
den += count;
}
acc_num[f*2*nt_bins + pol*nt_bins + b+b0] += (float)num;
acc_den[f*2*nt_bins + pol*nt_bins + b+b0] += (float)den;
}
}
}
}
void waterfall_image::subtract_medians()
{
vector<double> buf(nt_bins);
int nvals;
// loop over (freq,pol pairs)
for (int i = 0; i < 2*nfreq; i++) {
nvals = 0;
for (int j = 0; j < nt_bins; j++) {
if (acc_den[i*nt_bins+j] > 99.5)
buf[nvals++] = acc_num[i*nt_bins+j] / acc_den[i*nt_bins+j];
}
if (!nvals)
continue;
std::nth_element(buf.begin(), buf.begin() + (nvals/2), buf.end());
double median = buf[nvals/2];
for (int j = 0; j < nt_bins; j++)
acc_num[i*nt_bins+j] -= median * acc_den[i*nt_bins+j];
}
}
void waterfall_image::clear()
{
memset(&acc_num[0], 0, acc_num.size() * sizeof(acc_num[0]));
memset(&acc_den[0], 0, acc_den.size() * sizeof(acc_den[0]));
}
//
// @mask assumed 0 or 1
//
double waterfall_image::compute_variance_outside_mask(int pol, const int *mask)
{
double num2 = 0.0;
double den2 = 0.0;
for (int ifreq = 0; ifreq < nfreq; ifreq++) {
for (int b = 0; b < nt_bins; b++) {
if (mask[ifreq*nt_bins + b]) {
double num = acc_num[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
double den = acc_den[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
num2 += (num*num)/(den*den);
den2 += 1.0;
}
}
}
return (den2 > 0.0) ? (num2/den2) : 0.0;
}
double waterfall_image::compute_clipped_variance(int pol)
{
vector<int> mask(nfreq * nt_bins, 0);
for (int ifreq = 0; ifreq < nfreq; ifreq++) {
for (int b = 0; b < nt_bins; b++) {
double den = acc_den[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
mask[ifreq*nt_bins + b] = (den > 99.5);
}
}
double vvar = compute_variance_outside_mask(pol, &mask[0]);
if (vvar <= 0.0)
return 0.0;
for (int n = 0; n < 3; n++) {
// extend mask by clipping at 2 sigma
for (int ifreq = 0; ifreq < nfreq; ifreq++) {
for (int b = 0; b < nt_bins; b++) {
double num = acc_num[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
double den = acc_den[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
int ok = (num*num < 4*vvar*den*den);
mask[ifreq*nt_bins + b] &= ok;
}
}
// recompute variance with extended mask
double vvar2 = compute_variance_outside_mask(pol, &mask[0]);
if (vvar2 <= 0.0)
return vvar;
vvar = vvar2;
}
return vvar;
}
void waterfall_image::write_image(const string &filename, int pol, double dv)
{
vector<png_byte> rgb(3 * nfreq * nt_bins, 0);
if (dv <= 0.0) {
pw.write(filename, &rgb[0], nt_bins, nfreq);
return;
}
for (int ifreq = 0; ifreq < nfreq; ifreq++) {
for (int b = 0; b < nt_bins; b++) {
double num = acc_num[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
double den = acc_den[ifreq*(2*nt_bins) + (pol*nt_bins) + b];
if (den > 99.5) {
int val = 128 * (num/den/dv) + 128;
val = max(val, 0);
val = min(val, 255);
rgb[ifreq*(3*nt_bins) + 3*b] = (unsigned int)val;
rgb[ifreq*(3*nt_bins) + 3*b + 2] = (unsigned int)(255-val);
}
}
}
pw.write(filename, &rgb[0], nt_bins, nfreq);
}
void waterfall_image::write_image(const string &filename, int pol)
{
double vvar = this->compute_clipped_variance(pol);
double dv = 2.5 * sqrt(max(vvar,0.0));
this->write_image(filename, pol, dv);
}
// -------------------------------------------------------------------------------------------------
struct waterfall_plotter : public vdif_processor
{
std::string outdir;
std::string imgdir;
int64_t curr_frame;
bool curr_frame_initialized;
waterfall_image curr_img;
waterfall_plotter(const std::string &outdir, bool is_critical);
virtual ~waterfall_plotter() { }
// Devirtualize vdif_processor
virtual void process_chunk(const shared_ptr<assembled_chunk> &chunk);
virtual void initialize();
virtual void finalize();
void write_images();
};
waterfall_plotter::waterfall_plotter(const string &outdir_, bool is_critical)
: vdif_processor("waterfall plotter", is_critical),
outdir(outdir_),
imgdir(outdir_ + string("/img")),
curr_frame(0),
curr_frame_initialized(false),
curr_img(constants::chime_nfreq, 1024)
{
xmkdir(outdir);
xmkdir(imgdir);
}
//
// This routine defines the behavior of the waterfall_plotter class.
//
// It calls assembler.get_mask() and assembler.get_visibilities() to get
// the current chunk of data, and absorbs it into the waterfall plot.
//
void waterfall_plotter::process_chunk(const shared_ptr<assembled_chunk> &chunk)
{
int64_t frame = chunk->t0 / constants::timestamps_per_frame;
if (!curr_frame_initialized) {
curr_frame = frame;
curr_frame_initialized = true;
curr_img.clear();
}
else if (curr_frame != frame) {
// Frame complete, write and move on to the next
this->write_images();
curr_frame = frame;
curr_img.clear();
}
curr_img.absorb_chunk(*chunk);
}
void waterfall_plotter::initialize()
{
curr_frame_initialized = false;
curr_img.clear();
}
void waterfall_plotter::finalize()
{
this->write_images();
curr_img.clear();
}
void waterfall_plotter::write_images()
{
curr_img.subtract_medians();
waterfall_image thumbnail(curr_img, 256, 256);
thumbnail.subtract_medians();
for (int pol = 0; pol < 2; pol++) {
stringstream ss1;
ss1 << imgdir << "/full_pol" << pol << "_frame" << curr_frame << ".png";
curr_img.write_image(ss1.str(), pol);
stringstream ss2;
ss2 << imgdir << "/thumbnail_pol" << pol << "_frame" << curr_frame << ".png";
thumbnail.write_image(ss2.str(), pol);
}
}
shared_ptr<vdif_processor> make_waterfall_plotter(const string &outdir, bool is_critical)
{
return make_shared<waterfall_plotter> (outdir, is_critical);
}
} // namespace ch_vdif_assembler