Esempio n. 1
0
    def test_weight(self):
        """test_weight: test weight handling"""
        nchan = 2
        nrow = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)
        # gridder
        gridder = self._generate_gridder(imparam=imparam)

        # working set
        ws = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)

        # expected data
        rdata = numpy.asarray([1.0, 2.0, 10.0, 20.0, 10.0, 20.0])
        idata = numpy.asarray([0.1, 0.2, 0.01, 0.02, -0.01, -0.02])
        weight = numpy.asarray([1.0, 0.0, 0.0, 1.0, 0.0, 1.0])

        # configure ws
        self._configure_uv(gridder, ws, 0, loc='center')
        self._configure_uv(gridder, ws, 1, loc='top_right')
        self._configure_data(ws, rdata[:4], idata[:4], weight[:4])

        # perform gridding and verification
        loc_real = ([3, 6, 0], [2, 4, 0], [0, 0, 0], [0, 1, 1])
        loc_imag = ([6, 0], [4, 0], [0, 0], [1, 1])
        nz = weight.nonzero()
        ref_real = rdata[nz] * weight[nz] / weight[nz]
        ref_imag = (idata[nz] * weight[nz] / weight[nz])[1:]
        self._grid_and_verify(gridder, ws, loc_real, loc_imag, ref_real,
                              ref_imag)
Esempio n. 2
0
    def test_row_flag(self):
        """test_row_flag: test row flag handling"""
        nchan = 2
        nrow = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)
        # gridder
        gridder = self._generate_gridder(imparam=imparam)

        # working set
        ws = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)

        # expected data
        rdata = numpy.asarray([1.0, 2.0, 10.0, 20.0, 10.0, 20.0])
        idata = numpy.asarray([0.1, 0.2, 0.01, 0.02, -0.01, -0.02])
        weight = numpy.asarray([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

        # configure ws
        self._configure_uv(gridder, ws, 0, loc='center')
        self._configure_uv(gridder, ws, 1, loc='top_right')
        self._configure_data(ws,
                             rdata[:4],
                             idata[:4],
                             weight[:4],
                             row_flag=[True, False])

        # perform gridding and verification
        loc_real = ([3, 3], [2, 2], [0, 0], [0, 1])
        loc_imag = ([], [], [], [])
        ref_real = (rdata * weight / weight)[:2]
        ref_imag = []
        self._grid_and_verify(gridder, ws, loc_real, loc_imag, ref_real,
                              ref_imag)
Esempio n. 3
0
    def test_position_channel(self):
        """test_position_channel: grid position test (spectral axis)"""
        nchan = 2
        nrow = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)
        # gridder
        gridder = self._generate_gridder(imparam=imparam)

        # working set
        ws = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)

        # expected data
        rdata = numpy.asarray([1.0, 2.0, 10.0, 20.0, 10.0, 20.0])
        idata = numpy.asarray([0.1, 0.2, 0.01, 0.02, -0.01, -0.02])
        weight = numpy.asarray([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])

        # configure ws
        self._configure_uv(gridder, ws, 0, loc='center')
        self._configure_uv(gridder, ws, 1, loc='top_right')
        self._configure_data(ws, rdata, idata, weight)

        # perform gridding and verification
        loc_real = ([3, 3, 6, 6, 0, 0], [2, 2, 4, 4, 0,
                                         0], [0, 0, 0, 0, 0,
                                              0], [0, 1, 0, 1, 0, 1])
        loc_imag = ([6, 6, 0, 0], [4, 4, 0, 0], [0, 0, 0, 0], [0, 1, 0, 1])
        ref_real = rdata * weight / weight
        ref_imag = (idata * weight / weight)[2:]
        self._grid_and_verify(gridder, ws, loc_real, loc_imag, ref_real,
                              ref_imag)
Esempio n. 4
0
    def _test_lsr_freq(self, mfr='TOPO'):
        chunk = {}
        ndds = len(self.dd_spw_map)
        with casa.OpenTableForRead(os.path.join(self.vis, 'FIELD')) as tb:
            nfields = tb.nrows()
        with casa.OpenTableForReadWrite(
                os.path.join(self.vis, 'SPECTRAL_WINDOW')) as tb:
            mfrs = tb.getcol('MEAS_FREQ_REF')
            self.assertTrue(
                mfr in visconverter.VisibilityConverter.frequency_reference)
            mfr_id = visconverter.VisibilityConverter.frequency_reference.index(
                mfr)
            if not numpy.all(mfrs == mfr_id):
                mfrs[:] = mfr_id
                tb.putcol('MEAS_FREQ_REF', mfrs)

        visparam = paramcontainer.VisParamContainer(vis=self.vis)
        imageparam = paramcontainer.ImageParamContainer(
            imagename=self.imagename, phasecenter=str(self.field_id))
        converter = visconverter.VisibilityConverter(visparam, imageparam)

        nchunk = 1
        chunk['time'] = numpy.empty(nchunk, dtype=numpy.float64)
        chunk['data_desc_id'] = numpy.empty(nchunk, dtype=numpy.int32)
        chunk['field_id'] = numpy.empty_like(chunk['data_desc_id'])
        with casa.OpenTableForRead(self.vis) as tb:
            chunk['time'][:] = tb.getcell('TIME', 0)
        for field_id in range(nfields):
            chunk['field_id'][0] = field_id
            for dd_id in range(ndds):
                chunk['data_desc_id'][0] = dd_id
                lsr_freq = converter.get_lsr_frequency(chunk)
                #print lsr_freq
                spw_id = self.dd_spw_map[dd_id]
                nchan = self.nchans[spw_id]
                self.assertEqual(nchan + 1, len(lsr_freq))
                self.assertEqual(mfrs[spw_id], mfr_id)

                # special case for LSRK
                with casa.OpenTableForRead(
                        os.path.join(self.vis, 'SPECTRAL_WINDOW')) as tb:
                    chan_freq = tb.getcell('CHAN_FREQ', spw_id)
                    chan_width = tb.getcell('CHAN_WIDTH', spw_id)
                expected = numpy.empty(nchan + 1, dtype=numpy.float64)
                expected[:-1] = chan_freq - chan_width / 2.0
                expected[-1] = chan_freq[-1] + chan_width[-1] / 2.0
                if mfr == 'LSRK':
                    self.assertTrue(numpy.all(lsr_freq == expected))
                else:
                    expected = self._convert(chunk['time'][0], field_id, mfr,
                                             expected)
                    self.assertMaxDiffLess(expected, lsr_freq, 1e-15)
Esempio n. 5
0
 def get_converter(self, start, width, nchan):
     self.imageparam = paramcontainer.ImageParamContainer(
         imagename=self.imagename,
         start=start,
         width=width,
         nchan=nchan,
         phasecenter=str(self.field_id))
     print('LOG: s,w,n {0} {1} {2}'.format(self.imageparam.start,
                                           self.imageparam.width,
                                           self.imageparam.nchan))
     converter = visconverter.VisibilityConverter(self.visparam,
                                                  self.imageparam)
     return converter
Esempio n. 6
0
    def setUp(self):
        super(GridderTest, self).setUp()

        self.standard_imparam = paramcontainer.ImageParamContainer(
            nchan=1, imsize=[5, 7])
        convsupport = 1
        convsampling = 10
        gridfunction = almagridder.GridFunctionUtil.box(
            convsupport, convsampling)
        print('support {0} sampling {1} len(gridfunction) {2}'.format(
            convsupport, convsampling, len(gridfunction)))
        self.standard_gridparam = paramcontainer.GridParamContainer(
            convsupport=convsupport,
            convsampling=convsampling,
            gridfunction=gridfunction)
Esempio n. 7
0
    def test_gridder_polarization_map(self):
        """test_gridder_polarization_map: test polarization mapping"""
        nchan = 1
        nrow = 2
        npol = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)
        # gridder
        gridder = self._generate_gridder(imparam=imparam)

        # working set
        ws = self._generate_ws_template(nrow=nrow, npol=npol, nchan=nchan)

        # expected data
        rdata = numpy.asarray([1.0, 2.0, 10.0, 20.0, 10.0, 20.0])
        idata = numpy.asarray([0.1, 0.2, 0.01, 0.02, -0.01, -0.02])
        weight = numpy.asarray([1.0, 1.0, 1.0])

        # configure ws
        self._configure_uv(gridder, ws, 0, loc='center')
        self._configure_uv(gridder, ws, 1, loc='top_right')
        self._configure_data(ws, rdata[:4], idata[:4], weight[:2])

        # customize polarization map
        ws.pol_map[:] = 0

        # perform gridding and verification
        loc_real = ([3, 6, 0], [2, 4, 0], [0, 0, 0], [0, 0, 0])
        loc_imag = ([6, 0], [4, 0], [0, 0], [0, 0])
        ref_real = numpy.zeros(len(loc_real[0]), dtype=numpy.float32)
        ref_imag = numpy.zeros_like(ref_real)
        ones = numpy.ones(npol, dtype=numpy.float32)
        ref_real[0] = numpy.sum(ws.rdata[0] * ws.weight[0, 0]) / numpy.sum(
            ones * ws.weight[0, 0])
        ref_real[1] = numpy.sum(ws.rdata[1] * ws.weight[1, 0]) / numpy.sum(
            ones * ws.weight[1, 0])
        ref_real[2] = ref_real[1]
        ref_imag[0] = numpy.sum(ws.idata[0] * ws.weight[0, 0]) / numpy.sum(
            ones * ws.weight[0, 0])
        ref_imag[1] = numpy.sum(ws.idata[1] * ws.weight[1, 0]) / numpy.sum(
            ones * ws.weight[1, 0])
        ref_imag[2] = -ref_imag[1]
        self._grid_and_verify(gridder, ws, loc_real, loc_imag, ref_real,
                              ref_imag[1:])
Esempio n. 8
0
    def test_multi_ws(self):
        """test_multi_ws: test gridding multiple ws"""
        nchan = 2
        nrow = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)
        # gridder
        gridder = self._generate_gridder(imparam=imparam)

        # working set
        nws = 2
        ws1 = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)
        ws2 = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)

        # expected data
        rdata1 = numpy.asarray([1.0, 2.0, 10.0, 20.0, 10.0, 20.0])
        rdata2 = rdata1 * 0.25
        idata1 = numpy.asarray([0.1, 0.2, 0.01, 0.02, -0.01, -0.02])
        idata2 = idata1 * 5.0
        weight1 = numpy.asarray([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
        weight2 = weight1 * 0.5

        # configure ws1
        self._configure_uv(gridder, ws1, 0, loc='center')
        self._configure_uv(gridder, ws1, 1, loc='top_right')
        self._configure_data(ws1, rdata1[:4], idata1[:4], weight1[:4])

        # configure ws2
        self._configure_uv(gridder, ws2, 0, loc='center')
        self._configure_uv(gridder, ws2, 1, loc='top_right')
        self._configure_data(ws2, rdata2[:4], idata2[:4], weight2[:4])

        # perform gridding and verification
        loc_real = ([3, 3, 6, 6, 0, 0], [2, 2, 4, 4, 0,
                                         0], [0, 0, 0, 0, 0,
                                              0], [0, 1, 0, 1, 0, 1])
        loc_imag = ([6, 6, 0, 0], [4, 4, 0, 0], [0, 0, 0, 0], [0, 1, 0, 1])
        ref_real = ((rdata1 * weight1 + rdata2 * weight2) /
                    (weight1 + weight2))
        ref_imag = ((idata1 * weight1 + idata2 * weight2) /
                    (weight1 + weight2))[2:]
        self._grid_and_verify(gridder, [ws1, ws2], loc_real, loc_imag,
                              ref_real, ref_imag)
Esempio n. 9
0
    def get_default_imageparam(self):
        qa = casa.CreateCasaQuantity()
        phasecenter = '9:00:00 -60.00.00 J2000'
        #phasecenter_rec = imagewriter.parse_phasecenter(phasecenter_str)
        start = qa.quantity('101GHz')
        width = qa.quantity('1MHz')
        nchan = 3
        imsize = [10, 9]
        cell = '1arcsec'
        imageparam = paramcontainer.ImageParamContainer(
            imagename=self.imagename,
            phasecenter=phasecenter,
            imsize=imsize,
            cell=cell,
            start=start,
            width=width,
            nchan=nchan,
            outframe='LSRK',
            stokes='I')

        return imageparam
Esempio n. 10
0
    def _test_gridfunction(self, gtype, support, sampling=100, gparam=None):
        params = {'convsupport': support, 'convsampling': sampling}
        if isinstance(gparam, dict):
            params.update(gparam)
        gridfunction = getattr(almagridder.GridFunctionUtil, gtype)(**params)
        print('Custom Grid Fucntion:')
        print('support {0} sampling {1} len(gridfunction) {2}'.format(
            support, sampling, len(gridfunction)))
        gridparam = paramcontainer.GridParamContainer(
            convsupport=support,
            convsampling=sampling,
            gridfunction=gridfunction)

        nchan = 2
        nrow = 2
        imparam = paramcontainer.ImageParamContainer(
            nchan=nchan, imsize=self.standard_imparam.imsize)

        # gridder
        gridder = self._generate_gridder(imparam=imparam, gridparam=gridparam)

        # working set
        ws = self._generate_ws_template(nrow=nrow, npol=1, nchan=nchan)

        # expected data
        rdata = numpy.asarray([1.0, 2.0, 10.0, 20.0])
        idata = numpy.asarray([0.1, 0.2, 0.01, 0.02])
        weight = numpy.asarray([1.0, 1.0, 1.0, 1.0])

        # configure ws
        self._configure_uv(gridder, ws, 0, loc='center')
        # shift uv: distance from pixel center is 0.707 (=sqrt(2) * 0.5)
        ws.u[0] += 0.51
        ws.v[0] += 0.51
        self._configure_uv(gridder, ws, 1, loc='center')
        # shift uv: distance from pixel center is 0.5
        ws.u[1] += 0.51
        self._configure_data(ws, rdata, idata, weight)

        # perform gridding and verification
        w = len(gridfunction.nonzero()[0]) / sampling
        iw = int(w)
        print(w)
        #umin = int(round(ws.u[0] - iw))
        #umax = int(ws.u[0] + iw)
        cen = (float(gridder.nu - 1) / 2, float(gridder.nv - 1) / 2)
        print('cen', cen, 'nunv', gridder.nu, gridder.nv)
        print('u:', ws.u)
        print('v:', ws.v)
        umin = 0
        umax = gridder.nu
        vmin = 0
        vmax = gridder.nv
        lv = list(range(vmin, vmax)) * gridder.nu
        lv.sort()
        lv = lv + lv
        lu = list(range(umin, umax))
        lu = lu * gridder.nv
        lu = lu + lu
        lp = [0] * len(lv)
        lc = [0] * (len(lv) // 2) + [1] * (len(lv) // 2)
        #print 'lu, lv, lp, lc=', lu, lv, lp, lc
        loc_real = (lv, lu, lp, lc)
        ref_real = numpy.zeros(len(loc_real[0]), dtype=numpy.float32)
        ref_imag = numpy.zeros_like(ref_real)
        w_real = numpy.zeros_like(ref_real)
        w_imag = numpy.zeros_like(ref_real)

        def _get_kernel_index(du, dv, sampling):
            dist = numpy.sqrt(du * du + dv * dv) * numpy.float64(sampling)
            idist = int(dist)
            if dist - idist >= 1.0 - 1e-10:
                print('Increment idist since dist-int(dist) > 1')
                idist += 1
            return idist

        def _accumulate(greal,
                        gimag,
                        wreal,
                        wimag,
                        gindex,
                        dreal,
                        dimag,
                        dweight,
                        dindex,
                        gridfunction,
                        idist,
                        conj=False):
            gr = 0.0
            gi = 0.0
            wr = 0.0
            wi = 0.0
            if idist < len(gridfunction) - 1:
                sign = -1 if conj else 1
                k = gridfunction[idist]
                print('accumulate', k, dreal[dindex], dimag[dindex],
                      dweight[dindex])
                gr += dreal[dindex] * k * dweight[dindex]
                gi += sign * dimag[dindex] * k * dweight[dindex]
                wr += k * dweight[dindex]
                wi += k * dweight[dindex]
            return gr, gi, wr, wi

        for i in range(len(ref_real)):
            x = lu[i]
            y = lv[i]
            p = lp[i]
            c = lc[i]
            du1 = ws.u[0] - x
            dv1 = ws.v[0] - y
            du2 = ws.u[1] - x
            dv2 = ws.v[1] - y
            idist1 = _get_kernel_index(du1, dv1, sampling)
            idist2 = _get_kernel_index(du2, dv2, sampling)

            self.assertGreaterEqual(idist1, 0)
            self.assertGreaterEqual(idist2, 0)
            #print 'dist', dist1, dist2
            gr1, gi1, wr1, wi1 = _accumulate(ref_real, ref_imag, w_real,
                                             w_imag, i, rdata, idata, weight,
                                             c, gridfunction, idist1, False)
            gr2, gi2, wr2, wi2 = _accumulate(ref_real, ref_imag, w_real,
                                             w_imag, i, rdata, idata, weight,
                                             2 + c, gridfunction, idist2,
                                             False)
            ref_real[i] += gr1 + gr2
            ref_imag[i] += gi1 + gi2
            w_real[i] += wr1 + wr2
            w_imag[i] += wi1 + wi2

            # grid complex conjugate
            du1 = cen[0] + (cen[0] - ws.u[0]) - x
            dv1 = cen[1] + (cen[1] - ws.v[0]) - y
            du2 = cen[0] + (cen[0] - ws.u[1]) - x
            dv2 = cen[1] + (cen[1] - ws.v[1]) - y
            idist1 = _get_kernel_index(du1, dv1, sampling)
            idist2 = _get_kernel_index(du2, dv2, sampling)
            self.assertGreaterEqual(idist1, 0)
            self.assertGreaterEqual(idist2, 0)
            gr1, gi1, wr1, wi1 = _accumulate(ref_real, ref_imag, w_real,
                                             w_imag, i, rdata, idata, weight,
                                             c, gridfunction, idist1, True)
            gr2, gi2, wr2, wi2 = _accumulate(ref_real, ref_imag, w_real,
                                             w_imag, i, rdata, idata, weight,
                                             2 + c, gridfunction, idist2, True)
            ref_real[i] += gr1 + gr2
            ref_imag[i] += gi1 + gi2
            w_real[i] += wr1 + wr2
            w_imag[i] += wi1 + wi2

            if w_real[i] > 0.0:
                ref_real[i] /= w_real[i]
            if w_imag[i] > 0.0:
                ref_imag[i] /= w_imag[i]

        eps = 1.0e-7
        ndx_real = []
        for i in range(len(ref_real)):
            if abs(ref_real[i]) != 0.0:
                ndx_real.append(i)
        print('ndx_real', ndx_real)
        loc_real = (numpy.asarray(lv)[ndx_real], numpy.asarray(lu)[ndx_real],
                    numpy.asarray(lp)[ndx_real], numpy.asarray(lc)[ndx_real])
        ref_real = ref_real[ndx_real]
        ndx_imag = []
        for i in range(len(ref_imag)):
            if abs(ref_imag[i]) >= eps:
                ndx_imag.append(i)
        print('ndx_imag', ndx_imag)
        loc_imag = (numpy.asarray(lv)[ndx_imag], numpy.asarray(lu)[ndx_imag],
                    numpy.asarray(lp)[ndx_imag], numpy.asarray(lc)[ndx_imag])
        ref_imag = ref_imag[ndx_imag]
        self._grid_and_verify(gridder, ws, loc_real, loc_imag, ref_real,
                              ref_imag)