def test_phase_rotation_stokesiquv(self): self.vis = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) self.vismodel = dft_skycomponent_visibility(self.vis, self.comp) # Predict visibilities with new phase centre independently ha_diff = -(self.compabsdirection.ra - self.phasecentre.ra).to( u.rad).value vispred = create_visibility( self.lowcore, self.times + ha_diff, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.compabsdirection, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) vismodel2 = dft_skycomponent_visibility(vispred, self.comp) # Should yield the same results as rotation rotatedvis = phaserotate_visibility( self.vismodel, newphasecentre=self.compabsdirection, tangent=False) assert_allclose(rotatedvis.vis, vismodel2.vis, rtol=3e-6) assert_allclose(rotatedvis.uvw, vismodel2.uvw, rtol=3e-6)
def test_phase_rotation_stokesi(self): # Define the component and give it some spectral behaviour f = numpy.array([100.0]) self.flux = numpy.array([f, 0.8 * f, 0.6 * f]) # The phase centre is absolute and the component is specified relative (for now). # This means that the component should end up at the position phasecentre+compredirection self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') self.compabsdirection = SkyCoord(ra=+181.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') pcof = self.phasecentre.skyoffset_frame() self.compreldirection = self.compabsdirection.transform_to(pcof) self.comp = Skycomponent( direction=self.compreldirection, frequency=self.frequency, flux=self.flux, polarisation_frame=PolarisationFrame("stokesI")) self.vis = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesI")) self.vismodel = dft_skycomponent_visibility(self.vis, self.comp) # Predict visibilities with new phase centre independently ha_diff = -(self.compabsdirection.ra - self.phasecentre.ra).to( u.rad).value vispred = create_visibility( self.lowcore, self.times + ha_diff, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.compabsdirection, weight=1.0, polarisation_frame=PolarisationFrame("stokesI")) vismodel2 = dft_skycomponent_visibility(vispred, self.comp) # Should yield the same results as rotation rotatedvis = phaserotate_visibility( self.vismodel, newphasecentre=self.compabsdirection, tangent=False) assert_allclose(rotatedvis.vis, vismodel2.vis, rtol=3e-6) assert_allclose(rotatedvis.uvw, vismodel2.uvw, rtol=3e-6)
def predict_skycomponent_visibility( vis: Union[Visibility, BlockVisibility], sc: Union[Skycomponent, List[Skycomponent]] ) -> Union[Visibility, BlockVisibility]: """Predict the visibility from a Skycomponent, add to existing visibility, for Visibility or BlockVisibility Now replaced by dft_skycomponent_visibility :param vis: Visibility or BlockVisibility :param sc: Skycomponent or list of SkyComponents :return: Visibility or BlockVisibility """ log.warning( "predict_skycomponent_visibility: deprecated - please use dft_skycomponent_visibility" ) from rascil.processing_components.imaging.dft import dft_skycomponent_visibility return dft_skycomponent_visibility(vis, sc)
def test_dft_idft_stokesiquv_visibility(self): for vpol in [ PolarisationFrame("linear"), PolarisationFrame("circular") ]: self.vis = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) self.vismodel = dft_skycomponent_visibility(self.vis, self.comp) rcomp, weights = idft_visibility_skycomponent( self.vismodel, self.comp) assert_allclose(self.comp.flux, numpy.real(rcomp[0].flux), rtol=1e-11)
def test_apply_primary_beam_imageplane(self): self.createVis() telescope = 'MID' lflux = numpy.array([[100.0, 1.0, -10.0, +60.0]]) cflux = numpy.array([[100.0, 60.0, -10.0, +1.0]]) apply_pb = True for flux, vpol in ((lflux, PolarisationFrame("linear")), (cflux, PolarisationFrame("circular"))): # print("Testing {0}".format(vpol.type)) # print("Original flux = {}".format(flux)) bvis = create_blockvisibility( self.config, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=vpol, zerow=True) component_centre = SkyCoord(ra=+15.5 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') component = create_skycomponent( direction=component_centre, flux=flux, frequency=self.frequency, polarisation_frame=PolarisationFrame("stokesIQUV")) model = create_image_from_visibility( bvis, cellsize=self.cellsize, npixel=self.npixel, override_cellsize=False, polarisation_frame=PolarisationFrame("stokesIQUV")) pb = create_pb(model, telescope=telescope, use_local=False) if apply_pb: pbcomp = apply_beam_to_skycomponent(component, pb) # print("After application of primary beam {}".format(str(pbcomp.flux))) else: pbcomp = copy_skycomponent(component) bvis = dft_skycomponent_visibility(bvis, pbcomp) iquv_image = idft_visibility_skycomponent(bvis, component)[0]
def actualSetUp(self, freqwin=1, block=False, dospectral=True, image_pol=PolarisationFrame('stokesI'), zerow=False): self.npixel = 512 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = freqwin self.vis = list() self.ntimes = 5 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 if freqwin > 1: self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin) self.channelwidth = numpy.array(freqwin * [self.frequency[1] - self.frequency[0]]) else: self.frequency = numpy.array([1e8]) self.channelwidth = numpy.array([1e6]) if image_pol == PolarisationFrame('stokesIQUV'): self.vis_pol = PolarisationFrame('linear') self.image_pol = image_pol f = numpy.array([100.0, 20.0, -10.0, 1.0]) elif image_pol == PolarisationFrame('stokesIQ'): self.vis_pol = PolarisationFrame('linearnp') self.image_pol = image_pol f = numpy.array([100.0, 20.0]) elif image_pol == PolarisationFrame('stokesIV'): self.vis_pol = PolarisationFrame('circularnp') self.image_pol = image_pol f = numpy.array([100.0, 20.0]) else: self.vis_pol = PolarisationFrame('stokesI') self.image_pol = PolarisationFrame('stokesI') f = numpy.array([100.0]) if dospectral: flux = numpy.array([f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency]) else: flux = numpy.array([f]) self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-45.0 * u.deg, frame='icrs', equinox='J2000') self.vis = ingest_unittest_visibility(self.low, self.frequency, self.channelwidth, self.times, self.vis_pol, self.phasecentre, block=block, zerow=zerow) self.model = create_unittest_model(self.vis, self.image_pol, npixel=self.npixel, nchan=freqwin) self.components = create_unittest_components(self.model, flux) self.model = insert_skycomponent(self.model, self.components) self.vis = dft_skycomponent_visibility(self.vis, self.components) # Calculate the model convolved with a Gaussian. self.cmodel = smooth_image(self.model) if self.persist: export_image_to_fits(self.model, '%s/test_imaging_model.fits' % self.dir) if self.persist: export_image_to_fits(self.cmodel, '%s/test_imaging_cmodel.fits' % self.dir)
def test_apply_voltage_pattern_image_pointsource(self): self.createVis(rmax=1e3) telescope = 'MID_FEKO_B2' vpol = PolarisationFrame("linear") self.times = numpy.linspace(-4, +4, 8) * numpy.pi / 12.0 bvis = create_blockvisibility(self.config, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=vpol, zerow=True) cellsize = advise_wide_field(bvis)['cellsize'] pbmodel = create_image_from_visibility( bvis, cellsize=self.cellsize, npixel=self.npixel, override_cellsize=False, polarisation_frame=PolarisationFrame("stokesIQUV")) vpbeam = create_vp(pbmodel, telescope=telescope, use_local=False) vpbeam.wcs.wcs.ctype[0] = 'RA---SIN' vpbeam.wcs.wcs.ctype[1] = 'DEC--SIN' vpbeam.wcs.wcs.crval[0] = pbmodel.wcs.wcs.crval[0] vpbeam.wcs.wcs.crval[1] = pbmodel.wcs.wcs.crval[1] s3_components = create_test_skycomponents_from_s3( flux_limit=0.1, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=1.5 * numpy.pi / 180.0) for comp in s3_components: comp.polarisation_frame = PolarisationFrame('stokesIQUV') comp.flux = numpy.array([[comp.flux[0, 0], 0.0, 0.0, 0.0]]) s3_components = filter_skycomponents_by_flux(s3_components, 0.0, 10.0) from rascil.processing_components.image import show_image import matplotlib.pyplot as plt plt.clf() show_image(vpbeam, components=s3_components) plt.show(block=False) vpcomp = apply_voltage_pattern_to_skycomponent(s3_components, vpbeam) bvis.data['vis'][...] = 0.0 + 0.0j bvis = dft_skycomponent_visibility(bvis, vpcomp) rec_comp = idft_visibility_skycomponent(bvis, vpcomp)[0] stokes_comp = list() for comp in rec_comp: stokes_comp.append( convert_pol_frame(comp.flux[0], PolarisationFrame("linear"), PolarisationFrame("stokesIQUV"))) stokesI = numpy.abs( numpy.array([comp_flux[0] for comp_flux in stokes_comp]).real) stokesQ = numpy.abs( numpy.array([comp_flux[1] for comp_flux in stokes_comp]).real) stokesU = numpy.abs( numpy.array([comp_flux[2] for comp_flux in stokes_comp]).real) stokesV = numpy.abs( numpy.array([comp_flux[3] for comp_flux in stokes_comp]).real) plt.clf() plt.loglog(stokesI, stokesQ, '.', label='Q') plt.loglog(stokesI, stokesU, '.', label='U') plt.loglog(stokesI, stokesV, '.', label='V') plt.xlabel("Stokes Flux I (Jy)") plt.ylabel("Flux (Jy)") plt.legend() plt.savefig('%s/test_primary_beams_pol_rsexecute_stokes_errors.png' % self.dir) plt.show(block=False) split_times = False if split_times: bvis_list = list() for rows in vis_timeslice_iter(bvis, vis_slices=8): bvis_list.append(create_visibility_from_rows(bvis, rows)) else: bvis_list = [bvis] bvis_list = rsexecute.scatter(bvis_list) model_list = \ [rsexecute.execute(create_image_from_visibility, nout=1)(bv, cellsize=cellsize, npixel=4096, phasecentre=self.phasecentre, override_cellsize=False, polarisation_frame=PolarisationFrame("stokesIQUV")) for bv in bvis_list] model_list = rsexecute.persist(model_list) bvis_list = weight_list_rsexecute_workflow(bvis_list, model_list) continuum_imaging_list = \ continuum_imaging_list_rsexecute_workflow(bvis_list, model_list, context='2d', algorithm='hogbom', facets=1, niter=1000, fractional_threshold=0.1, threshold=1e-4, nmajor=5, gain=0.1, deconvolve_facets=4, deconvolve_overlap=32, deconvolve_taper='tukey', psf_support=64, restore_facets=4, psfwidth=1.0) clean, residual, restored = rsexecute.compute(continuum_imaging_list, sync=True) centre = 0 if self.persist: export_image_to_fits( clean[centre], '%s/test_primary_beams_pol_rsexecute_clean.fits' % self.dir) export_image_to_fits( residual[centre][0], '%s/test_primary_beams_pol_rsexecute_residual.fits' % self.dir) export_image_to_fits( restored[centre], '%s/test_primary_beams_pol_rsexecute_restored.fits' % self.dir) plt.clf() show_image(restored[centre]) plt.show(block=False) qa = qa_image(restored[centre]) assert numpy.abs(qa.data['max'] - 0.9953017707113947) < 1.0e-7, str(qa) assert numpy.abs(qa.data['min'] + 0.0036396480874570846) < 1.0e-7, str(qa)
def test_apply_voltage_pattern_skycomponent(self): self.createVis() telescope = 'MID_FEKO_B2' vpbeam = create_vp(telescope=telescope, use_local=False) vpol = PolarisationFrame("linear") bvis = create_blockvisibility(self.config, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=vpol) cellsize = advise_wide_field(bvis)['cellsize'] component_centre = SkyCoord(ra=+15.25 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') model = create_image_from_visibility( bvis, cellsize=cellsize, npixel=1024, phasecentre=component_centre, override_cellsize=False, polarisation_frame=PolarisationFrame("linear")) bvis = weight_blockvisibility(bvis, model) pbmodel = create_image_from_visibility( bvis, cellsize=self.cellsize, npixel=self.npixel, override_cellsize=False, polarisation_frame=PolarisationFrame("stokesIQUV")) vpbeam = create_vp(pbmodel, telescope=telescope, use_local=False) vpbeam.wcs.wcs.ctype[0] = 'RA---SIN' vpbeam.wcs.wcs.ctype[1] = 'DEC--SIN' vpbeam.wcs.wcs.crval[0] = pbmodel.wcs.wcs.crval[0] vpbeam.wcs.wcs.crval[1] = pbmodel.wcs.wcs.crval[1] for case, flux in enumerate( (numpy.array([[100.0, 0.0, 0.0, 0.0]]), numpy.array([[100.0, 100.0, 0.0, 0.0]]), numpy.array([[100.0, 0.0, 100.0, 0.0]]), numpy.array([[100.0, 0.0, 0.0, 100.0]]), numpy.array([[100.0, 1.0, -10.0, +60.0]]))): component = create_skycomponent( direction=component_centre, flux=flux, frequency=self.frequency, polarisation_frame=PolarisationFrame("stokesIQUV")) vpcomp = apply_voltage_pattern_to_skycomponent(component, vpbeam) bvis.data['vis'][...] = 0.0 + 0.0j bvis = dft_skycomponent_visibility(bvis, vpcomp) polimage, sumwt = invert_2d(bvis, model, dopsf=False) export_image_to_fits( polimage, "{0}/test_primary_beams_pol_case{1}.fits".format( self.dir, case)) # Check out the path via components found_components = find_skycomponents(polimage, threshold=20.0, npixels=5) assert len(found_components) == 1 inv_vpcomp = apply_voltage_pattern_to_skycomponent( found_components[0], vpbeam, inverse=True) assert_array_almost_equal(flux, numpy.real(inv_vpcomp.flux), 1) # Now check out the path via images vpbeam.wcs.wcs.ctype[0] = polimage.wcs.wcs.ctype[0] vpbeam.wcs.wcs.ctype[1] = polimage.wcs.wcs.ctype[1] vpbeam.wcs.wcs.crval[0] = polimage.wcs.wcs.crval[0] vpbeam.wcs.wcs.crval[1] = polimage.wcs.wcs.crval[1] vpbeam_regrid, footprint = reproject_image(vpbeam, polimage.wcs, polimage.shape) polimage_corrected = apply_voltage_pattern_to_image(polimage, vpbeam_regrid, inverse=True, min_det=0.3) export_image_to_fits( polimage_corrected, "{0}/test_primary_beams_pol_corrected_case{1}.fits".format( self.dir, case)) found_components = find_skycomponents(polimage_corrected, threshold=20.0, npixels=5) assert len(found_components) == 1
def test_apply_voltage_pattern_dft(self): self.createVis() nfailures = 0 telescope = 'MID_FEKO_B2' for flux in (numpy.array([[100.0, 0.0, 0.0, 0.0]]), numpy.array([[100.0, 100.0, 0.0, 0.0]]), numpy.array([[100.0, 0.0, 100.0, 0.0]]), numpy.array([[100.0, 0.0, 0.0, 100.0]]), numpy.array([[100.0, 1.0, -10.0, +60.0]])): vpol = PolarisationFrame("linear") try: bvis = create_blockvisibility( self.config, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=vpol) component_centre = SkyCoord(ra=+16.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') component = create_skycomponent( direction=component_centre, flux=flux, frequency=self.frequency, polarisation_frame=PolarisationFrame("stokesIQUV")) model = create_image_from_visibility( bvis, cellsize=self.cellsize, npixel=self.npixel, override_cellsize=False, polarisation_frame=PolarisationFrame("stokesIQUV")) vpbeam = create_vp(model, telescope=telescope, use_local=False) vpbeam.wcs.wcs.ctype[0] = 'RA---SIN' vpbeam.wcs.wcs.ctype[1] = 'DEC--SIN' vpbeam.wcs.wcs.crval[0] = model.wcs.wcs.crval[0] vpbeam.wcs.wcs.crval[1] = model.wcs.wcs.crval[1] assert component.polarisation_frame == PolarisationFrame( "stokesIQUV") vpcomp = apply_voltage_pattern_to_skycomponent( component, vpbeam) assert vpcomp.polarisation_frame == vpbeam.polarisation_frame bvis = dft_skycomponent_visibility(bvis, vpcomp) vpcomp = idft_visibility_skycomponent(bvis, vpcomp)[0][0] assert vpcomp.polarisation_frame == bvis.polarisation_frame inv_vpcomp = apply_voltage_pattern_to_skycomponent( vpcomp, vpbeam, inverse=True) assert vpcomp.polarisation_frame == PolarisationFrame( "stokesIQUV") # print("After application of primary beam {}".format(str(vpcomp.flux))) # print("After correction of primary beam {}".format(str(inv_vpcomp.flux))) # print("{0} {1} succeeded".format(vpol, str(flux))) assert_array_almost_equal(flux, numpy.real(inv_vpcomp.flux), 9) except AssertionError as e: print(e) print("{0} {1} failed".format(vpol, str(flux))) nfailures += 1 assert nfailures == 0, "{} tests failed".format(nfailures)