def test_voronoi_decomposition(self): bright_components = create_low_test_skycomponents_from_gleam( flux_limit=1.0, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) model = create_image(npixel=512, cellsize=0.001, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI')) beam = create_low_test_beam(model, use_local=False) bright_components = apply_beam_to_skycomponent(bright_components, beam) bright_components = filter_skycomponents_by_flux(bright_components, flux_min=2.0) vor, vor_array = voronoi_decomposition(model, bright_components) assert len(bright_components) == (numpy.max(vor_array) + 1)
def test_find_nearest_component(self): self.components = create_low_test_skycomponents_from_gleam( flux_limit=0.1, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) match, sep = find_nearest_skycomponent(self.components[3].direction, self.components) assert match.name == 'GLEAM J211146-685527', match.name
def test_find_skycomponent_separation(self): self.components = create_low_test_skycomponents_from_gleam( flux_limit=0.1, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) separations = find_separation_skycomponents(self.components[0:99]) assert separations[0, 0] == 0.0 assert numpy.max(separations) > 0.0
def convert_visibility_to_stokesI(vis): """Convert the polarisation frame data into Stokes I dropping other polarisations, return new Visibility Args: vis (obj): RASCIL visibility data. Returns: vis: New, converted visibility data. """ polarisation_frame = PolarisationFrame('stokesI') poldef = vis.polarisation_frame if poldef == PolarisationFrame('linear'): vis_data = convert_linear_to_stokesI(vis.data['vis']) vis_weight = (vis.weight[..., 0] + vis.weight[..., 3])[..., numpy.newaxis] vis_imaging_weight = (vis.imaging_weight[..., 0] + vis.imaging_weight[..., 3])[..., numpy.newaxis] elif poldef == PolarisationFrame('circular'): vis_data = convert_circular_to_stokesI(vis.data['vis']) vis_weight = (vis.weight[..., 0] + vis.weight[..., 3])[..., numpy.newaxis] vis_imaging_weight = (vis.imaging_weight[..., 0] + vis.imaging_weight[..., 3])[..., numpy.newaxis] else: raise NameError("Polarisation frame %s unknown" % poldef) return Visibility(frequency=vis.frequency, channel_bandwidth=vis.channel_bandwidth, phasecentre=vis.phasecentre, configuration=vis.configuration, uvw=vis.uvw, time=vis.time, antenna1=vis.antenna1, antenna2=vis.antenna2, vis=vis_data, weight=vis_weight, imaging_weight=vis_imaging_weight, integration_time=vis.integration_time, polarisation_frame=polarisation_frame, cindex=vis.cindex, blockvis=vis.blockvis, source=vis.source, meta=vis.meta)
def test_predict_2d_point_block_IQUV(self): self.actualSetUp(zerow=True, block=True, image_pol=PolarisationFrame("stokesIQUV")) self.model.data[...] = 0.0 nchan, npol, ny, nx = self.model.shape self.model.data[0, 0, ny // 2, nx // 2] = 1.0 vis = predict_2d(self.vis, self.model) assert numpy.max(numpy.abs(vis.vis[...,0]-1.0)) < 1e-12 assert numpy.max(numpy.abs(vis.vis[...,1])) < 1e-12 assert numpy.max(numpy.abs(vis.vis[...,2])) < 1e-12 assert numpy.max(numpy.abs(vis.vis[...,3]-1.0)) < 1e-12
def test_insert_skycomponent_dft(self): self.sc = create_skycomponent(direction=self.phasecentre, flux=self.sc.flux, frequency=self.component_frequency, polarisation_frame=PolarisationFrame('stokesI')) self.vis.data['vis'][...] = 0.0 self.vis = predict_skycomponent_visibility(self.vis, self.sc) im, sumwt = invert_2d(self.vis, self.model) export_image_to_fits(im, '%s/test_skycomponent_dft.fits' % self.dir) assert numpy.max(numpy.abs(self.vis.vis.imag)) < 1e-3
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_expand_skymodel_voronoi(self): self.model = create_image( npixel=256, cellsize=0.001, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre) beam = create_low_test_beam(self.model, use_local=False) gleam_components = create_low_test_skycomponents_from_gleam( flux_limit=1.0, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.1) pb_gleam_components = apply_beam_to_skycomponent( gleam_components, beam) actual_components = filter_skycomponents_by_flux(pb_gleam_components, flux_min=1.0) _, actual_components = remove_neighbouring_components( actual_components, 0.05) for imask, mask in enumerate( image_voronoi_iter(self.model, actual_components)): mask.data *= beam.data assert isinstance(mask, Image) assert mask.data.dtype == "float" assert numpy.sum(mask.data) > 1 # import matplotlib.pyplot as plt # from rascil.processing_components.image.operations import show_image # show_image(mask) # plt.show(block=False) assert len(actual_components) == 9, len(actual_components) sm = SkyModel(image=self.model, components=actual_components) assert len(sm.components) == len(actual_components) scatter_sm = expand_skymodel_by_skycomponents(sm) assert len(scatter_sm) == len(actual_components) + 1 assert len(scatter_sm[0].components) == 1
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 import_image_from_fits(fitsfile: str) -> Image: """ Read an Image from fits :param fitsfile: FITS file in storage :return: Image See also :py:func:`rascil.processing_components.image.operations.export_image_to_array` """ fim = Image() warnings.simplefilter('ignore', FITSFixedWarning) hdulist = fits.open(fitsfile) fim.data = hdulist[0].data fim.wcs = WCS(fitsfile) hdulist.close() if len(fim.data) == 2: fim.polarisation_frame = PolarisationFrame('stokesI') else: try: fim.polarisation_frame = polarisation_frame_from_wcs( fim.wcs, fim.data.shape) # FITS and RASCIL polarisation conventions differ new_data = fim.data.copy() new_data[:, 3] = fim.data[:, 1] new_data[:, 1] = fim.data[:, 2] new_data[:, 2] = fim.data[:, 3] fim.data = new_data except ValueError: fim.polarisation_frame = PolarisationFrame('stokesI') log.debug( "import_image_from_fits: created %s image of shape %s, size %.3f (GB)" % (fim.data.dtype, str(fim.shape), image_sizeof(fim))) log.debug("import_image_from_fits: Max, min in %s = %.6f, %.6f" % (fitsfile, fim.data.max(), fim.data.min())) assert isinstance(fim, Image) return fim
def test_image_voronoi_iter(self): bright_components = create_low_test_skycomponents_from_gleam( flux_limit=1.0, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) model = create_image(npixel=512, cellsize=0.001, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI')) model.data[...] = 1.0 beam = create_low_test_beam(model, use_local=False) bright_components = apply_beam_to_skycomponent(bright_components, beam) bright_components = filter_skycomponents_by_flux(bright_components, flux_min=2.0) for im in image_voronoi_iter(model, bright_components): assert numpy.sum(im.data) > 1
def test_create_visibility_polarisation(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("linear")) assert self.vis.nvis == len(self.vis.time) assert self.vis.nvis == len(self.vis.frequency)
def test_invert_psf_weighting_block_IQUV(self): self.actualSetUp(zerow=False, block=True, image_pol = PolarisationFrame('stokesIQUV')) for weighting in ["natural", "uniform", "robust"]: self.vis = weight_blockvisibility(self.vis, self.model, weighting=weighting, robustness=-1.0) psf = invert_2d(self.vis, self.model, dopsf=True) error = numpy.max(psf[0].data) - 1.0 assert abs(error) < 1.0e-12, error rms = numpy.std(psf[0].data) print(weighting, rms, psf[1]) if self.persist: export_image_to_fits(psf[0], '%s/test_imaging_2d_psf_block_%s_IQUV.fits' % (self.dir, weighting)) assert numpy.max(numpy.abs(psf[0].data)), "Image is empty"
def convert_blockvisibility_to_stokes(vis): """Convert the polarisation frame data into Stokes parameters. :param vis: Visibility :return: Converted visibility data. """ poldef = vis.polarisation_frame if poldef == PolarisationFrame('linear'): vis.data['vis'] = convert_linear_to_stokes(vis.data['vis'], polaxis=4) vis.data['flags'] = \ numpy.logical_or(vis.flags[..., 0], vis.flags[..., 3])[ ..., numpy.newaxis] vis.polarisation_frame = PolarisationFrame('stokesIQUV') elif poldef == PolarisationFrame('circular'): vis.data['vis'] = convert_circular_to_stokes(vis.data['vis'], polaxis=4) vis.data['flags'] = \ numpy.logical_or(vis.flags[..., 0], vis.flags[..., 3])[ ..., numpy.newaxis] vis.polarisation_frame = PolarisationFrame('stokesIQUV') return vis
def test_raster_overlap(self): m31original = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) assert numpy.max(numpy.abs(m31original.data)), "Original is empty" flat = create_empty_image_like(m31original) for nraster, overlap in [(1, 0), (1, 16), (4, 8), (4, 16), (8, 8), (16, 4), (9, 5)]: m31model = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) for patch, flat_patch in zip( image_raster_iter(m31model, facets=nraster, overlap=overlap), image_raster_iter(flat, facets=nraster, overlap=overlap)): patch.data *= 2.0 flat_patch.data[...] += 1.0 assert numpy.max(numpy.abs( m31model.data)), "Raster is empty for %d" % nraster
def test_create_test_skycomponents_from_s3(self): self.frequency = numpy.linspace(0.8e9, 1.2e9, 5) sc = create_test_skycomponents_from_s3( flux_limit=3.0, phasecentre=self.phasecentre, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, radius=0.1) assert len( sc) == 9, "Only expected nine sources, actually found %d" % len(sc) assert sc[0].name == 'S3_36133023' self.assertAlmostEqual(sc[0].flux[0, 0], 4.54336938, 7)
def test_sum_visibility(self): self.vis = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, polarisation_frame=PolarisationFrame("linear"), weight=1.0) self.vis = predict_skycomponent_visibility(self.vis, self.comp) flux, weight = sum_visibility(self.vis, self.comp.direction) assert numpy.max(numpy.abs(flux - self.flux)) < 1e-7
def test_create_low_test_skycomponents_from_gleam(self): sc = create_low_test_skycomponents_from_gleam( flux_limit=1.0, phasecentre=SkyCoord("17h20m31s", "-00d58m45s"), polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, kind='cubic', radius=0.001) assert len( sc) == 1, "Only expected one source, actually found %d" % len(sc) assert sc[0].name == 'GLEAM J172031-005845' self.assertAlmostEqual(sc[0].flux[0, 0], 357.2599499089219, 7)
def test_create(self): fluxes = numpy.linspace(0, 1.0, 11) sc = [ create_skycomponent( direction=self.phasecentre, flux=numpy.array([[f]]), frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI')) for f in fluxes ] sm = SkyModel(image=self.model, components=sc, mask=self.mask) assert len(sm.components) == 11
def test_polarisation_frame_from_wcs(self): assert self.m31image.polarisation_frame == PolarisationFrame("stokesI") stokes = create_test_image( cellsize=0.0001, polarisation_frame=PolarisationFrame("stokesIQUV")) wcs = stokes.wcs.deepcopy() shape = stokes.shape assert polarisation_frame_from_wcs( wcs, shape) == PolarisationFrame("stokesIQUV") wcs = stokes.wcs.deepcopy().sub(['stokes']) wcs.wcs.crpix[0] = 1.0 wcs.wcs.crval[0] = -1.0 wcs.wcs.cdelt[0] = -1.0 assert polarisation_frame_from_wcs( wcs, shape) == PolarisationFrame('circular') wcs.wcs.crpix[0] = 1.0 wcs.wcs.crval[0] = -5.0 wcs.wcs.cdelt[0] = -1.0 assert polarisation_frame_from_wcs( wcs, shape) == PolarisationFrame('linear') wcs.wcs.crpix[0] = 1.0 wcs.wcs.crval[0] = -1.0 wcs.wcs.cdelt[0] = -1.0 assert polarisation_frame_from_wcs( wcs, shape) == PolarisationFrame('circular') with self.assertRaises(ValueError): wcs.wcs.crpix[0] = 1.0 wcs.wcs.crval[0] = -100.0 wcs.wcs.cdelt[0] = -1.0 polarisation_frame_from_wcs(wcs, shape)
def test_filter_flux(self): self.components = create_low_test_skycomponents_from_gleam( flux_limit=0.1, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) newsc = filter_skycomponents_by_flux(self.components, flux_min=0.3) assert len(newsc) < len(self.components), len(self.components) newsc = filter_skycomponents_by_flux(self.components, flux_min=5.0) assert len(newsc) == 138, len(newsc) newsc = filter_skycomponents_by_flux(self.components, flux_max=8.0) assert len(newsc) == 11744, len(newsc)
def test_griddata_predict_box(self): self.actualSetUp(zerow=True, image_pol=PolarisationFrame("stokesIQUV")) gcf, cf = create_box_convolutionfunction(self.model) modelIQUV = convert_stokes_to_polimage(self.model, self.vis.polarisation_frame) griddata = create_griddata_from_image(modelIQUV, self.vis) griddata = fft_image_to_griddata(modelIQUV, griddata, gcf) newvis = degrid_visibility_from_griddata(self.vis, griddata=griddata, cf=cf) newvis.data['vis'][...] -= self.vis.data['vis'][...] qa = qa_visibility(newvis) assert qa.data['rms'] < 58.0, str(qa)
def test_subtract(self): vis1 = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) vis1.data['vis'][...] = 1.0 vis2 = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) vis2.data['vis'][...] = 1.0 zerovis = subtract_visibility(vis1, vis2) qa = qa_visibility(zerovis, context='test_qa') self.assertAlmostEqual(qa.data['maxabs'], 0.0, 7)
def actualSetUp(self, freqwin=1, block=False, dopol=False, zerow=False): self.npixel = 1024 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = freqwin self.vis_list = list() self.ntimes = 5 self.cellsize = 0.0005 # Choose the interval so that the maximum change in w is smallish integration_time = numpy.pi * (24 / (12 * 60)) self.times = numpy.linspace(-integration_time * (self.ntimes // 2), integration_time * (self.ntimes // 2), self.ntimes) 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([1.0e8]) self.channelwidth = numpy.array([4e7]) if dopol: self.vis_pol = PolarisationFrame('linear') self.image_pol = PolarisationFrame('stokesIQUV') f = numpy.array([100.0, 20.0, -10.0, 1.0]) else: self.vis_pol = PolarisationFrame('stokesI') self.image_pol = PolarisationFrame('stokesI') f = numpy.array([100.0]) self.phasecentre = SkyCoord(ra=+30.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000') self.vis_list = [rsexecute.execute(ingest_unittest_visibility)(self.low, [self.frequency[freqwin]], [self.channelwidth[freqwin]], self.times, self.vis_pol, self.phasecentre, block=block, zerow=zerow) for freqwin, _ in enumerate(self.frequency)] self.vis_list = rsexecute.compute(self.vis_list)
def test_gather_channel(self): for nchan in [128, 16]: m31cube = create_test_image( polarisation_frame=PolarisationFrame('stokesI'), frequency=numpy.linspace(1e8, 1.1e8, nchan)) image_list = image_scatter_channels(m31cube, subimages=nchan) m31cuberec = image_gather_channels(image_list, None, subimages=nchan) assert m31cube.shape == m31cuberec.shape diff = m31cube.data - m31cuberec.data assert numpy.max( numpy.abs(diff)) == 0.0, "Scatter gather failed for %d" % nchan
def test_filter(self): fluxes = numpy.linspace(0, 1.0, 11) sc = [ create_skycomponent( direction=self.phasecentre, flux=numpy.array([[f]]), frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI')) for f in fluxes ] sm = partition_skymodel_by_flux(sc, self.model, flux_threshold=0.31) assert len(sm.components) == 7, len(sm.components)
def test_addnoise_blockvisibility(self): self.vis = create_blockvisibility( self.config, self.times, self.frequency, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('stokesIQUV'), channel_bandwidth=self.channel_bandwidth) original = copy_visibility(self.vis) self.vis = addnoise_visibility(self.vis) actual = numpy.std(numpy.abs(self.vis.vis - original.vis)) assert abs(actual - 0.01077958403015586) < 1e-4, actual
def test_select_neighbouring_components(self): self.components = create_low_test_skycomponents_from_gleam( flux_limit=0.1, phasecentre=self.phasecentre, frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.5) bright_components = filter_skycomponents_by_flux(self.components, flux_min=2.0) indices, d2d = select_neighbouring_components(self.components, bright_components) assert len(indices) == 11801, len(indices) assert numpy.max(indices) == (len(bright_components) - 1)
def actualSetup(self, atmosphere="ionosphere"): dec = -40.0 * u.deg self.times = numpy.linspace(-10.0, 10.0, 3) * numpy.pi / (3600.0 * 12.0) self.phasecentre = SkyCoord(ra=+0.0 * u.deg, dec=dec, frame='icrs', equinox='J2000') if atmosphere == "ionosphere": self.core = create_named_configuration('LOWBD2', rmax=300.0) self.frequency = numpy.array([1.0e8]) self.channel_bandwidth = numpy.array([5e7]) self.cellsize = 0.000015 else: self.core = create_named_configuration('MID', rmax=300.0) self.frequency = numpy.array([1.36e9]) self.channel_bandwidth = numpy.array([1e8]) self.cellsize = 0.00015 self.vis = create_blockvisibility( self.core, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('stokesI')) self.vis.data['vis'] *= 0.0 # Create model self.model = create_image( npixel=512, cellsize=0.000015, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre)
def setUp(self): self.persist = os.getenv("RASCIL_PERSIST", False) from rascil.data_models.parameters import rascil_path, rascil_data_path self.dir = rascil_path('test_results') self.lowcore = create_named_configuration('LOWBD2-CORE') self.times = (numpy.pi / (12.0)) * numpy.linspace(-3.0, 3.0, 7) self.frequency = numpy.array([1e8]) self.channel_bandwidth = numpy.array([1e6]) self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000') 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'), zerow=True) self.vis.data['vis'] *= 0.0 # Create model self.test_model = create_test_image(cellsize=0.001, phasecentre=self.vis.phasecentre, frequency=self.frequency) self.vis = predict_2d(self.vis, self.test_model) assert numpy.max(numpy.abs(self.vis.vis)) > 0.0 self.model = create_image_from_visibility( self.vis, npixel=512, cellsize=0.001, polarisation_frame=PolarisationFrame('stokesI')) self.dirty, sumwt = invert_2d(self.vis, self.model) self.psf, sumwt = invert_2d(self.vis, self.model, dopsf=True)