wgridder_bench.py 6.55 KB
 Martin Reinecke committed Sep 10, 2020 1 2 3 4 5 6 7 8 9 10 11 12 13 ``````# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # `````` Philipp Arras committed Sep 11, 2020 14 ``````# Copyright(C) 2019-2020 Max-Planck-Society `````` Martin Reinecke committed Sep 10, 2020 15 16 17 18 `````` from time import time import ducc0.wgridder as wgridder `````` Philipp Arras committed Sep 11, 2020 19 ``````import matplotlib.pyplot as plt `````` Martin Reinecke committed Sep 10, 2020 20 21 22 ``````import numpy as np `````` Martin Reinecke committed Sep 12, 2020 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 ``````def get_indices(name): from os.path import join from casacore.tables import table with table(join(name, 'POLARIZATION'), readonly=True, ack=False) as t: pol = list(t.getcol('CORR_TYPE')[0]) if set(pol) <= set([5, 6, 7, 8]): ind = [pol.index(5), pol.index(8)] else: ind = [pol.index(9), pol.index(12)] return ind def determine_weighting(t): fullwgt = False weightcol = "WEIGHT" try: t.getcol("WEIGHT_SPECTRUM", startrow=0, nrow=1) weightcol = "WEIGHT_SPECTRUM" fullwgt = True except: pass return fullwgt, weightcol def extra_checks(t): if len(set(t.getcol('FIELD_ID'))) != 1: raise RuntimeError if len(set(t.getcol('DATA_DESC_ID'))) != 1: raise RuntimeError def read_ms_i(name): `````` Martin Reinecke committed Sep 10, 2020 55 56 57 58 59 60 61 62 63 64 `````` # Assumptions: # - Only one field # - Only one spectral window # - Flag both LL and RR if one is flagged from os.path import join from casacore.tables import table with table(join(name, 'SPECTRAL_WINDOW'), readonly=True, ack=False) as t: freq = t.getcol('CHAN_FREQ')[0] nchan = freq.shape[0] `````` Martin Reinecke committed Sep 12, 2020 65 `````` ind = get_indices(name) `````` Martin Reinecke committed Sep 10, 2020 66 `````` with table(name, readonly=True, ack=False) as t: `````` Martin Reinecke committed Sep 12, 2020 67 68 69 70 71 72 73 74 75 76 `````` fullwgt, weightcol = determine_weighting(t) extra_checks(t) nrow = t.nrows() active_rows = np.ones(nrow, dtype=np.bool) active_channels = np.zeros(nchan, dtype=np.bool) step = max(1, nrow//100) # how many rows to read in every step # determine which subset of rows/channels we need to input `````` Martin Reinecke committed Sep 10, 2020 77 78 79 `````` start = 0 while start < nrow: stop = min(nrow, start+step) `````` Martin Reinecke committed Sep 12, 2020 80 81 82 83 84 85 86 87 88 89 `````` tflags = t.getcol('FLAG', startrow=start, nrow=stop-start) ncorr = tflags.shape[2] tflags = tflags[..., ind] tflags = np.any(tflags.astype(np.bool), axis=-1) twgt = t.getcol(weightcol, startrow=start, nrow=stop-start)[..., ind] twgt = 1/np.sum(1/twgt, axis=-1) tflags[twgt==0] = True active_rows[start:stop] = np.invert(np.all(tflags, axis=-1)) active_channels = np.logical_or(active_channels, np.invert(np.all(tflags, axis=0))) `````` Martin Reinecke committed Sep 10, 2020 90 `````` start = stop `````` Martin Reinecke committed Sep 11, 2020 91 `````` `````` Martin Reinecke committed Sep 12, 2020 92 93 94 95 96 97 98 99 100 `````` nrealrows, nrealchan = np.sum(active_rows), np.sum(active_channels) start, realstart = 0, 0 vis = np.empty((nrealrows, nrealchan), dtype=np.complex64) wgtshp = (nrealrows, nrealchan) if fullwgt else (nrealrows,) wgt = np.empty(wgtshp, dtype=np.float32) flags = np.empty((nrealrows, nrealchan), dtype=np.bool) while start < nrow: stop = min(nrow, start+step) realstop = realstart+np.sum(active_rows[start:stop]) `````` Martin Reinecke committed Sep 13, 2020 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 `````` if realstop > realstart: allrows = stop-start == realstop-realstart tvis = t.getcol("DATA", startrow=start, nrow=stop-start)[..., ind] tvis = np.sum(tvis, axis=-1) if not allrows: tvis = tvis[active_rows[start:stop]] tvis = tvis[:, active_channels] tflags = t.getcol('FLAG', startrow=start, nrow=stop-start)[..., ind] tflags = np.any(tflags.astype(np.bool), axis=-1) if not allrows: tflags = tflags[active_rows[start:stop]] tflags = tflags[:, active_channels] twgt = t.getcol(weightcol, startrow=start, nrow=stop-start)[..., ind] twgt = 1/np.sum(1/twgt, axis=-1) if not allrows: twgt = twgt[active_rows[start:stop]] if fullwgt: twgt = twgt[:, active_channels] tflags[twgt==0] = True vis[realstart:realstop] = tvis wgt[realstart:realstop] = twgt flags[realstart:realstop] = tflags `````` Martin Reinecke committed Sep 12, 2020 124 125 126 `````` start, realstart = stop, realstop uvw = t.getcol("UVW")[active_rows] `````` Martin Reinecke committed Sep 10, 2020 127 `````` `````` Martin Reinecke committed Sep 12, 2020 128 129 `````` print('# Rows: {} ({} fully flagged)'.format(nrow, nrow-vis.shape[0])) print('# Channels: {} ({} fully flagged)'.format(nchan, nchan-vis.shape[1])) `````` Martin Reinecke committed Sep 10, 2020 130 `````` print('# Correlations: {}'.format(ncorr)) `````` Martin Reinecke committed Sep 11, 2020 131 `````` print('Full weights' if fullwgt else 'Row-only weights') `````` Martin Reinecke committed Sep 13, 2020 132 `````` nflagged = np.sum(flags) + (nrow-nrealrows)*nchan + (nchan-nrealchan)*nrow `````` Martin Reinecke committed Sep 12, 2020 133 134 `````` print("{} % flagged".format(nflagged/(nrow*nchan)*100)) freq = freq[active_channels] `````` Martin Reinecke committed Sep 11, 2020 135 136 137 138 139 `````` # blow up wgt to the right dimensions if necessary if not fullwgt: wgt = np.broadcast_to(wgt.reshape((-1,1)), vis.shape) `````` Martin Reinecke committed Sep 10, 2020 140 141 142 `````` return (np.ascontiguousarray(uvw), np.ascontiguousarray(freq), np.ascontiguousarray(vis), `````` Martin Reinecke committed Sep 11, 2020 143 `````` np.ascontiguousarray(wgt) if fullwgt else wgt, `````` Martin Reinecke committed Sep 10, 2020 144 145 146 147 `````` 1-flags.astype(np.uint8)) def main(): `````` Martin Reinecke committed Sep 12, 2020 148 149 ``````# ms, fov_deg = '/home/martin/ms/supernovashell.55.7+3.4.spw0.ms', 2. # ms, fov_deg = '/home/martin/ms/1052736496-averaged.ms', 45. `````` Martin Reinecke committed Sep 13, 2020 150 151 `````` ms, fov_deg = '/home/martin/ms/1052735056.ms', 45. # ms, fov_deg = '/home/martin/ms/cleaned_G330.89-0.36.ms', 2. `````` Martin Reinecke committed Sep 12, 2020 152 `````` uvw, freq, vis, wgt, flags = read_ms_i(ms) `````` Martin Reinecke committed Sep 10, 2020 153 154 155 `````` npixdirty = 1200 DEG2RAD = np.pi/180 `````` Martin Reinecke committed Sep 12, 2020 156 `````` pixsize = fov_deg/npixdirty*DEG2RAD `````` Martin Reinecke committed Sep 10, 2020 157 158 159 160 161 162 163 164 165 166 167 168 169 170 `````` nthreads = 2 epsilon = 1e-4 print('Start gridding...') do_wstacking = True t0 = time() dirty = wgridder.ms2dirty(uvw, freq, vis, wgt, npixdirty, npixdirty, pixsize, pixsize, 0, 0, epsilon, do_wstacking, nthreads, verbosity=1, mask=flags) print('Done') t = time() - t0 print("{} s".format(t)) t0 = time() _ = wgridder.dirty2ms(uvw, freq, dirty, wgt, pixsize, `````` Philipp Arras committed Sep 11, 2020 171 `````` pixsize, 0, 0, epsilon, do_wstacking, nthreads, verbosity=1, mask=flags) `````` Martin Reinecke committed Sep 10, 2020 172 173 174 175 176 177 178 179 180 181 `````` print('Done') t = time() - t0 print("{} s".format(t)) print("{} visibilities/thread/s".format(np.sum(wgt != 0)/nthreads/t)) plt.imshow(dirty.T, origin='lower') plt.show() if __name__ == "__main__": main()``````