def test_scatter_gather_facet(self): m31original = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) assert numpy.max(numpy.abs(m31original.data)), "Original is empty" for nraster in [1, 4, 8]: m31model = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) image_list = image_scatter_facets(m31model, facets=nraster) for patch in image_list: assert patch.data.shape[3] == (m31model.data.shape[3] // nraster), \ "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[3], (m31model.data.shape[3] // nraster)) assert patch.data.shape[2] == (m31model.data.shape[2] // nraster), \ "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[2], (m31model.data.shape[2] // nraster)) patch.data[...] = 1.0 m31reconstructed = create_empty_image_like(m31model) m31reconstructed = image_gather_facets(image_list, m31reconstructed, facets=nraster) flat = image_gather_facets(image_list, m31reconstructed, facets=nraster, return_flat=True) assert numpy.max(numpy.abs( flat.data)), "Flat is empty for %d" % nraster assert numpy.max(numpy.abs( m31reconstructed.data)), "Raster is empty for %d" % nraster
def test_partition_skycomponent_neighbours(self): all_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 = 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) all_components = apply_beam_to_skycomponent(all_components, beam) all_components = filter_skycomponents_by_flux(all_components, flux_min=0.1) bright_components = apply_beam_to_skycomponent(bright_components, beam) bright_components = filter_skycomponents_by_flux(bright_components, flux_min=2.0) comps_lists = partition_skycomponent_neighbours( all_components, bright_components) assert len(comps_lists) == len(bright_components) assert len(comps_lists[0]) > 0 assert len(comps_lists[-1]) > 0
def setUp(self): self.persist = os.getenv("RASCIL_PERSIST", False) from rascil.data_models.parameters import rascil_path dec = -40.0 * u.deg self.lowcore = create_named_configuration('LOWBD2', rmax=300.0) self.dir = rascil_path('test_results') self.times = numpy.linspace(-10.0, 10.0, 3) * numpy.pi / (3600.0 * 12.0) self.frequency = numpy.array([1e8, 1.5e8, 2.0e8]) self.channel_bandwidth = numpy.array([5e7, 5e7, 5e7]) self.phasecentre = SkyCoord(ra=+0.0 * u.deg, dec=dec, frame='icrs', equinox='J2000') self.vis = create_blockvisibility( self.lowcore, 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 test_apply_jones(self): nsucceeded = 0 nfailures = 0 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") cpol = PolarisationFrame("stokesIQUV") cflux = convert_pol_frame(flux, cpol, vpol, 0).reshape([2,2]) diagonal = numpy.array([[1.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 1.0 + 0.0]]) skew = numpy.array([[0.0 + 0.0j, 1.0 + 0.0j], [1.0 + 0.0j, 0.0 + 0.0]]) leakage = numpy.array([[1.0 + 0.0j, 0.0 + 0.1j], [0.0 - 0.1j, 1.0 + 0.0]]) unbalanced = numpy.array([[100.0 + 0.0j, 0.0 + 0.0j], [0.0 + 0.0j, 0.03 + 0.0]]) for ej in (diagonal, skew, leakage, unbalanced): try: jflux = apply_jones(ej, cflux, inverse=False) rflux = apply_jones(ej, jflux, inverse=True).reshape([4]) rflux = convert_pol_frame(rflux, vpol, cpol, 0) assert_array_almost_equal(flux, numpy.real(rflux), 12) # print("{0} {1} {2} succeeded".format(vpol, str(ej), str(flux))) nsucceeded += 1 except AssertionError as e: print(e) print("{0} {1} {2} failed".format(vpol, str(ej), str(flux))) nfailures += 1 assert nfailures == 0, "{0} tests succeeded, {1} failed".format(nsucceeded, nfailures)
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.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) 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 convert_polimage_to_stokes(im: Image): """Convert a polarisation image to stokes IQUV (complex) For example: imIQUV = convert_polimage_to_stokes(impol) :param im: Complex Image in linear or circular :returns: Complex image See also :py:func:`rascil.processing_components.image.convert_stokes_to_polimage` :py:func:`rascil.data_models.polarisation.convert_stokes_to_circular` :py:func:`rascil.data_models.polarisation.convert_stokes_to_linear` """ assert isinstance(im, Image) assert im.data.dtype == 'complex' if im.polarisation_frame == PolarisationFrame('linear'): cimarr = convert_linear_to_stokes(im.data) return create_image_from_array(cimarr, im.wcs, PolarisationFrame('stokesIQUV')) elif im.polarisation_frame == PolarisationFrame('circular'): cimarr = convert_circular_to_stokes(im.data) return create_image_from_array(cimarr, im.wcs, PolarisationFrame('stokesIQUV')) else: raise ValueError("Cannot convert %s to stokes" % (im.polarisation_frame.type))
def convert_stokes_to_polimage(im: Image, polarisation_frame: PolarisationFrame): """Convert a stokes image in IQUV to polarisation_frame For example:: impol = convert_stokes_to_polimage(imIQUV, Polarisation_Frame('linear')) :param im: Image to be converted :param polarisation_frame: desired polarisation frame :returns: Complex image See also :py:func:`rascil.processing_components.image.convert_polimage_to_stokes` :py:func:`rascil.data_models.polarisation.convert_circular_to_stokes` :py:func:`rascil.data_models.polarisation.convert_linear_to_stokes` """ assert isinstance(im, Image) assert image_is_canonical(im) assert isinstance(polarisation_frame, PolarisationFrame) if polarisation_frame == PolarisationFrame('linear'): cimarr = convert_stokes_to_linear(im.data) return create_image_from_array(cimarr, im.wcs, polarisation_frame) elif polarisation_frame == PolarisationFrame('circular'): cimarr = convert_stokes_to_circular(im.data) return create_image_from_array(cimarr, im.wcs, polarisation_frame) else: raise ValueError("Cannot convert stokes to %s" % (polarisation_frame.type))
def create_test_image( canonical=True, cellsize=None, frequency=None, channel_bandwidth=None, phasecentre=None, polarisation_frame=PolarisationFrame("stokesI")) -> Image: """Create a useful test image This is the test image M31 widely used in ALMA and other simulations. It is actually part of an Halpha region in M31. :param canonical: Make the image into a 4 dimensional image :param cellsize: :param frequency: Frequency (array) in Hz :param channel_bandwidth: Channel bandwidth (array) in Hz :param phasecentre: Phase centre of image (SkyCoord) :param polarisation_frame: Polarisation frame :return: Image """ check_data_directory() if frequency is None: frequency = [1e8] im = import_image_from_fits(rascil_path("data/models/M31.MOD")) if canonical: if polarisation_frame is None: im.polarisation_frame = PolarisationFrame("stokesI") elif isinstance(polarisation_frame, PolarisationFrame): im.polarisation_frame = polarisation_frame else: raise ValueError("polarisation_frame is not valid") im = replicate_image(im, frequency=frequency, polarisation_frame=im.polarisation_frame) if cellsize is not None: im.wcs.wcs.cdelt[0] = -180.0 * cellsize / numpy.pi im.wcs.wcs.cdelt[1] = +180.0 * cellsize / numpy.pi if frequency is not None: im.wcs.wcs.crval[3] = frequency[0] if channel_bandwidth is not None: im.wcs.wcs.cdelt[3] = channel_bandwidth[0] else: if len(frequency) > 1: im.wcs.wcs.cdelt[3] = frequency[1] - frequency[0] else: im.wcs.wcs.cdelt[3] = 0.001 * frequency[0] im.wcs.wcs.radesys = 'ICRS' im.wcs.wcs.equinox = 2000.00 if phasecentre is not None: im.wcs.wcs.crval[0] = phasecentre.ra.deg im.wcs.wcs.crval[1] = phasecentre.dec.deg # WCS is 1 relative im.wcs.wcs.crpix[0] = im.data.shape[3] // 2 + 1 im.wcs.wcs.crpix[1] = im.data.shape[2] // 2 + 1 return im
def test_fit_visibility(self): # Sum the visibilities in the correct_visibility direction. This is limited by numerical precision methods = [ 'CG', 'BFGS', 'Powell', 'trust-ncg', 'trust-exact', 'trust-krylov' ] for method in methods: self.actualSetup() 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) initial_comp = Skycomponent( direction=self.comp_start_direction, frequency=self.frequency, flux=2.0 * self.flux, polarisation_frame=PolarisationFrame("stokesI")) sc, res = fit_visibility(self.vismodel, initial_comp, niter=200, tol=1e-5, method=method, verbose=False) assert sc.direction.separation(self.comp_actual_direction).to('rad').value < 1e-6, \ sc.direction.separation(self.comp_actual_direction).to('rad')
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 = predict_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 = predict_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 actualSetUp(self, nfreqwin=3, dospectral=True, dopol=False, amp_errors=None, phase_errors=None, zerow=True): if amp_errors is None: amp_errors = {'T': 0.0, 'G': 0.1} if phase_errors is None: phase_errors = {'T': 1.0, 'G': 0.0} self.npixel = 512 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = nfreqwin self.vis_list = list() self.ntimes = 1 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin) if self.freqwin > 1: self.channelwidth = numpy.array(self.freqwin * [self.frequency[1] - self.frequency[0]]) else: self.channelwidth = numpy.array([1e6]) 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]) 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=-60.0 * u.deg, frame='icrs', equinox='J2000') self.blockvis_list = \ [rsexecute.execute(ingest_unittest_visibility, nout=1)(self.low, [self.frequency[i]], [self.channelwidth[i]], self.times, self.vis_pol, self.phasecentre, block=True, zerow=zerow) for i in range(nfreqwin)] self.blockvis_list = rsexecute.compute(self.blockvis_list, sync=True) for v in self.blockvis_list: v.data['vis'][...] = 1.0 + 0.0j self.error_blockvis_list = [rsexecute.execute(copy_visibility(v)) for v in self.blockvis_list] gt = rsexecute.execute(create_gaintable_from_blockvisibility)(self.blockvis_list[0]) gt = rsexecute.execute(simulate_gaintable)\ (gt, phase_error=0.1, amplitude_error=0.0, smooth_channels=1, leakage=0.0, seed=180555) self.error_blockvis_list = [rsexecute.execute(apply_gaintable)(self.error_blockvis_list[i], gt) for i in range(self.freqwin)] self.error_blockvis_list = rsexecute.compute(self.error_blockvis_list, sync=True) assert numpy.max(numpy.abs(self.error_blockvis_list[0].vis - self.blockvis_list[0].vis)) > 0.0
def actualSetUp(self, add_errors=False, freqwin=1, block=False, dospectral=True, dopol=False): self.npixel = 256 self.low = create_named_configuration('LOWBD2', rmax=750.0) self.freqwin = freqwin self.vis_list = list() self.ntimes = 5 self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0 self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin) if freqwin > 1: self.channelwidth = numpy.array(freqwin * [self.frequency[1] - self.frequency[0]]) else: self.channelwidth = numpy.array([1e6]) 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]) 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=-60.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) self.model = create_unittest_model(self.vis, self.image_pol, npixel=self.npixel)
def test_create_gaintable_from_pointingtable_GRASP(self): self.vis = create_blockvisibility( self.midcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('linear')) comp = create_skycomponent( direction=self.phasecentre, flux=[[1.0, 0.0, 0.0, 0.0]], frequency=self.frequency, polarisation_frame=PolarisationFrame('stokesIQUV')) pt = create_pointingtable_from_blockvisibility(self.vis) pt = simulate_pointingtable(pt, pointing_error=0.0, static_pointing_error=None, global_pointing_error=[0.0, 0.01]) vp = create_vp(self.model, 'MID_GRASP') gt = simulate_gaintable_from_pointingtable(self.vis, [comp], pt, vp) if self.doplot: import matplotlib.pyplot as plt plt.clf() plt.plot(gt[0].time, numpy.real(1.0 / gt[0].gain[:, 0, 0, 0, 0]), '.') plt.plot(gt[0].time, numpy.imag(1.0 / gt[0].gain[:, 0, 0, 0, 0]), '.') plt.title( 'test_create_gaintable_from_pointingtable_global_dynamic') plt.show(block=False) assert gt[0].gain.shape == (self.ntimes, self.nants, 1, 2, 2), gt[0].gain.shape
def test_create_vis_iter_with_model(self): model = create_test_image(canonical=True, cellsize=0.001, frequency=self.frequency, phasecentre=self.phasecentre) comp = Skycomponent(direction=self.phasecentre, frequency=self.frequency, flux=self.flux, polarisation_frame=PolarisationFrame('stokesI')) vis_iter = create_blockvisibility_iterator( self.config, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame('stokesI'), integration_time=30.0, number_integrations=3, model=model, components=comp) fullvis = None totalnvis = 0 for i, bvis in enumerate(vis_iter): assert bvis.phasecentre == self.phasecentre assert bvis.nvis if i == 0: fullvis = bvis totalnvis = bvis.nvis else: fullvis = append_visibility(fullvis, bvis) totalnvis += bvis.nvis assert fullvis.nvis == totalnvis
def test_elevation_block(self): self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=+15.0 * u.deg, frame='icrs', equinox='J2000') self.times = (numpy.pi / 43200.0) * numpy.arange( -43200, +43200, 3600.0) self.vis = create_blockvisibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV"), elevation_limit=numpy.pi * 15.0 / 180.0) n_elevation_limit = len(numpy.unique(self.vis.time)) self.vis = create_blockvisibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV"), elevation_limit=None) assert len(numpy.unique(self.vis.time)) >= n_elevation_limit
def fill_vis_for_psf(svis): """ Fill the visibility for calculation of PSF :param im: :param svis: :return: visibility with unit vis """ if svis.polarisation_frame == PolarisationFrame("linear"): svis.data['vis'][..., 0] = 1.0 + 0.0j svis.data['vis'][..., 1:3] = 0.0 + 0.0j svis.data['vis'][..., 3] = 1.0 + 0.0j elif svis.polarisation_frame == PolarisationFrame("circular"): svis.data['vis'][..., 0] = 1.0 + 0.0j svis.data['vis'][..., 1:3] = 0.0 + 0.0j svis.data['vis'][..., 3] = 1.0 + 0.0j elif svis.polarisation_frame == PolarisationFrame("linearnp"): svis.data['vis'][...] = 1.0 + 0.0j elif svis.polarisation_frame == PolarisationFrame("circularnp"): svis.data['vis'][...] = 1.0 + 0.0j elif svis.polarisation_frame == PolarisationFrame("stokesI"): svis.data['vis'][...] = 1.0 + 0.0j else: raise ValueError("Cannot calculate PSF for {}".format( svis.polarisation_frame)) return svis
def test_divide_visibility(self): self.vis = create_blockvisibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) self.vis.data['vis'][..., :] = [2.0 + 0.0j, 0.0j, 0.0j, 2.0 + 0.0j] self.othervis = create_blockvisibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) self.othervis.data['vis'][..., :] = [ 1.0 + 0.0j, 0.0j, 0.0j, 1.0 + 0.0j ] self.ratiovis = divide_visibility(self.vis, self.othervis) assert self.ratiovis.nvis == self.vis.nvis assert numpy.max(numpy.abs(self.ratiovis.vis)) == 2.0, numpy.max( numpy.abs(self.ratiovis.vis))
def test_phase_rotation_block(self): self.vis = create_blockvisibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) self.vismodel = predict_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_blockvisibility( self.lowcore, self.times + ha_diff, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.compabsdirection, weight=1.0, polarisation_frame=PolarisationFrame("stokesIQUV")) vismodel2 = predict_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_vis_auto_conversion_I(self): stokes = numpy.array(random.uniform(-1.0, 1.0, [1000, 3, 1])) ipf = PolarisationFrame('stokesI') opf = PolarisationFrame('stokesI') cir = convert_pol_frame(stokes, ipf, opf, polaxis=2) st = convert_pol_frame(cir, opf, ipf, polaxis=2) assert_array_almost_equal(st.real, stokes, 15)
def setUp(self): from rascil.data_models.parameters import rascil_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)
def setUp(self): from rascil.data_models.parameters import rascil_path self.doplot = False self.midcore = create_named_configuration('MID', rmax=300.0) self.nants = len(self.midcore.names) self.dir = rascil_path('test_results') self.ntimes = 30 self.times = numpy.linspace(-5.0, 5.0, self.ntimes) * numpy.pi / (12.0) self.frequency = numpy.array([1e9]) self.channel_bandwidth = numpy.array([1e7]) self.phasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-45.0 * u.deg, frame='icrs', equinox='J2000') self.vis = create_blockvisibility( self.midcore, 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.00015, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre)
def test_image_auto_conversion_I(self): stokes = numpy.array(random.uniform(-1.0, 1.0, [3, 4, 128, 128])) ipf = PolarisationFrame('stokesI') opf = PolarisationFrame('stokesI') cir = convert_pol_frame(stokes, ipf, opf) st = convert_pol_frame(cir, opf, ipf) assert_array_almost_equal(st.real, stokes, 15)
def setUp(self): from rascil.data_models.parameters import rascil_path self.lowcore = create_named_configuration('LOWBD2-CORE') self.dir = rascil_path('test_results') self.times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 7) self.image_frequency = numpy.linspace(0.9e8, 1.1e8, 5) self.component_frequency = numpy.linspace(0.8e8, 1.2e8, 7) self.channel_bandwidth = numpy.array(5*[1e7]) 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.image_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.model = create_test_image(cellsize=0.0015, phasecentre=self.vis.phasecentre, frequency=self.image_frequency) self.model.data[self.model.data > 1.0] = 1.0 self.vis = predict_2d(self.vis, self.model) assert numpy.max(numpy.abs(self.vis.vis)) > 0.0 dphasecentre = SkyCoord(ra=+181.0 * u.deg, dec=-58.0 * u.deg, frame='icrs', equinox='J2000') flux = [[numpy.power(f/1e8, -0.7)] for f in self.component_frequency] self.sc = create_skycomponent(direction=dphasecentre, flux=flux, frequency=self.component_frequency, polarisation_frame=PolarisationFrame('stokesI'))
def actualSetUp(self, time=None, frequency=None, dospectral=False, dopol=False): self.lowcore = create_named_configuration('LOWBD2', rmax=600) self.times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 5) if time is not None: self.times = time log.info("Times are %s" % (self.times)) if dospectral: self.nchan = 3 self.frequency = numpy.array([0.9e8, 1e8, 1.1e8]) self.channel_bandwidth = numpy.array([1e7, 1e7, 1e7]) else: self.frequency = numpy.array([1e8]) self.channel_bandwidth = numpy.array([1e7]) if dopol: self.vis_pol = PolarisationFrame('linear') self.image_pol = PolarisationFrame('stokesIQUV') else: self.vis_pol = PolarisationFrame('stokesI') self.image_pol = PolarisationFrame('stokesI') if dopol: f = numpy.array([100.0, 20.0, -10.0, 1.0]) else: f = numpy.array([100.0]) if dospectral: numpy.array([f, 0.8 * f, 0.6 * f]) else: numpy.array([f]) self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000') self.componentvis = create_visibility( self.lowcore, self.times, self.frequency, channel_bandwidth=self.channel_bandwidth, phasecentre=self.phasecentre, weight=1.0, polarisation_frame=self.vis_pol) self.uvw = self.componentvis.data['uvw'] self.componentvis.data['vis'] *= 0.0 # Create model self.model = create_image_from_visibility( self.componentvis, npixel=self.npixel, cellsize=0.0005, nchan=len(self.frequency), polarisation_frame=self.image_pol)
def test_circular_to_linear(self): stokes = numpy.array(random.uniform(-1.0, 1.0, [3, 4, 128, 128])) ipf = PolarisationFrame('stokesIQUV') opf = PolarisationFrame('circular') cir = convert_pol_frame(stokes, ipf, opf) wrong_pf = PolarisationFrame('linear') with self.assertRaises(ValueError): convert_pol_frame(cir, opf, wrong_pf)
def actualSetUp(self, freqwin=1, block=True, dopol=False, zerow=False): self.npixel = 1024 self.low = create_named_configuration('LOWBD2', rmax=550.0) self.freqwin = freqwin self.blockvis_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=+0.0 * u.deg, dec=-40.0 * u.deg, frame='icrs', equinox='J2000') self.blockvis_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.blockvis_list = rsexecute.compute(self.blockvis_list, sync=True) self.vis_list = [rsexecute.execute(convert_blockvisibility_to_visibility)(bv) for bv in self.blockvis_list] self.vis_list = rsexecute.compute(self.vis_list, sync=True) self.skymodel_list = [rsexecute.execute(create_low_test_skymodel_from_gleam) (npixel=self.npixel, cellsize=self.cellsize, frequency=[self.frequency[f]], phasecentre=self.phasecentre, polarisation_frame=PolarisationFrame("stokesI"), flux_limit=0.6, flux_threshold=1.0, flux_max=5.0) for f, freq in enumerate(self.frequency)] self.skymodel_list = rsexecute.compute(self.skymodel_list, sync=True) assert isinstance(self.skymodel_list[0].image, Image), self.skymodel_list[0].image assert isinstance(self.skymodel_list[0].components[0], Skycomponent), self.skymodel_list[0].components[0] assert len(self.skymodel_list[0].components) == 35, len(self.skymodel_list[0].components) self.skymodel_list = expand_skymodel_by_skycomponents(self.skymodel_list[0]) assert len(self.skymodel_list) == 36, len(self.skymodel_list) assert numpy.max(numpy.abs(self.skymodel_list[-1].image.data)) > 0.0, "Image is empty" self.vis_list = [copy_visibility(self.vis_list[0], zero=True) for i, _ in enumerate(self.skymodel_list)]
def create_image(npixel=512, cellsize=0.000015, polarisation_frame=PolarisationFrame("stokesI"), frequency=numpy.array([1e8]), channel_bandwidth=numpy.array([1e6]), phasecentre=None, nchan=None, dtype='float64') -> Image: """Create an empty template image consistent with the inputs. :param npixel: Number of pixels :param cellsize: cellsize in radians :param polarisation_frame: Polarisation frame (default PolarisationFrame("stokesI")) :param frequency: Array of frequencies (Hz) :param channel_bandwidth: Array of Channel width (Hz) :param phasecentre: phasecentre (SkyCoord) :param nchan: Number of channels in image :param dtype: Python data type for array :return: Image See also :py:func:`rascil.processing_components.image.operations.testing_support.create_image_from_array` :py:func:`rascil.processing_components.imaging.base.create_image_from_visibility` :py:func:`rascil.processing_components.simulation.create_test_image` :py:mod:`rascil.processing_components.simulation` """ if phasecentre is None: phasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') if polarisation_frame is None: polarisation_frame = PolarisationFrame("stokesI") npol = polarisation_frame.npol if nchan is None: nchan = len(frequency) shape = [nchan, npol, npixel, npixel] w = WCS(naxis=4) # The negation in the longitude is needed by definition of RA, DEC w.wcs.cdelt = [ -cellsize * 180.0 / numpy.pi, cellsize * 180.0 / numpy.pi, 1.0, channel_bandwidth[0] ] w.wcs.crpix = [npixel // 2 + 1, npixel // 2 + 1, 1.0, 1.0] w.wcs.ctype = ["RA---SIN", "DEC--SIN", 'STOKES', 'FREQ'] w.wcs.crval = [phasecentre.ra.deg, phasecentre.dec.deg, 1.0, frequency[0]] w.naxis = 4 w.wcs.radesys = 'ICRS' w.wcs.equinox = 2000.0 return create_image_from_array(numpy.zeros(shape, dtype=dtype), w, polarisation_frame=polarisation_frame)
def polarisation_frame_from_wcs(wcs, shape) -> PolarisationFrame: """Convert wcs to polarisation_frame See FITS definition in Table 29 of https://fits.gsfc.nasa.gov/standard40/fits_standard40draft1.pdf or subsequent revision 1 I Standard Stokes unpolarized 2 Q Standard Stokes linear 3 U Standard Stokes linear 4 V Standard Stokes circular −1 RR Right-right circular −2 LL Left-left circular −3 RL Right-left cross-circular −4 LR Left-right cross-circular −5 XX X parallel linear −6 YY Y parallel linear −7 XY XY cross linear −8 YX YX cross linear stokesI [1] stokesIQUV [1,2,3,4] circular [-1,-2,-3,-4] linear [-5,-6,-7,-8] For example:: pol_frame = polarisation_frame_from_wcs(im.wcs, im.shape) :param wcs: World Coordinate System :param shape: Shape corresponding to wcs :returns: Polarisation_Frame object """ # The third axis should be stokes: polarisation_frame = None if len(shape) == 2: polarisation_frame = PolarisationFrame("stokesI") else: npol = shape[1] pol = wcs.sub(['stokes']).wcs_pix2world(range(npol), 0)[0] pol = numpy.array(pol, dtype='int') for key in PolarisationFrame.fits_codes.keys(): keypol = numpy.array(PolarisationFrame.fits_codes[key]) if numpy.array_equal(pol, keypol): polarisation_frame = PolarisationFrame(key) return polarisation_frame if polarisation_frame is None: raise ValueError("Cannot determine polarisation code") assert isinstance(polarisation_frame, PolarisationFrame) return polarisation_frame
def get_polarisation_map(vis: Visibility, im: Image = None): """ Get the mapping of visibility polarisations to image polarisations """ assert image_is_canonical(im) if vis.polarisation_frame == im.polarisation_frame: if vis.polarisation_frame == PolarisationFrame('stokesI'): return "stokesI->stokesI", lambda pol: 0 elif vis.polarisation_frame == PolarisationFrame('stokesIQUV'): return "stokesIQUV->stokesIQUV", lambda pol: pol return "unknown", lambda pol: pol
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)