def test_scatter_gather(self): m31original = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) assert numpy.max(numpy.abs(m31original.data)), "Original is empty" for nraster in [2, 4, 8]: m31model = create_test_image( polarisation_frame=PolarisationFrame('stokesI')) image_list = image_scatter(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 *= 2.0 m31model = image_gather(image_list, m31model, facets=nraster) diff = m31model.data - 2.0 * m31original.data assert numpy.max(numpy.abs( m31model.data)), "Raster is empty for %d" % nraster assert numpy.max( numpy.abs(diff)) == 0.0, "Raster set failed for %d" % nraster
def test_create_low_test_beam(self): im = create_test_image( canonical=True, cellsize=0.002, frequency=numpy.array([1e8 - 5e7, 1e8, 1e8 + 5e7]), channel_bandwidth=numpy.array([5e7, 5e7, 5e7]), polarisation_frame=PolarisationFrame("stokesIQUV"), phasecentre=self.phasecentre) bm = create_low_test_beam(model=im) export_image_to_fits(bm, '%s/test_low_beam.fits' % (self.dir)) assert bm.data.shape[0] == 3 assert bm.data.shape[1] == 4 assert bm.data.shape[2] == im.data.shape[2] assert bm.data.shape[3] == im.data.shape[3] # Check to see if the beam scales as expected for i in [30, 40]: assert numpy.max( numpy.abs(bm.data[0, 0, 128, 128 - 2 * i] - bm.data[1, 0, 128, 128 - i])) < 0.02 assert numpy.max( numpy.abs(bm.data[0, 0, 128, 128 - 3 * i] - bm.data[2, 0, 128, 128 - i])) < 0.02 assert numpy.max( numpy.abs(bm.data[0, 0, 128 - 2 * i, 128] - bm.data[1, 0, 128 - i, 128])) < 0.02 assert numpy.max( numpy.abs(bm.data[0, 0, 128 - 3 * i, 128] - bm.data[2, 0, 128 - i, 128])) < 0.02
def setUp(self): self.dir = './test_results' os.makedirs(self.dir, exist_ok=True) self.m31image = create_test_image(cellsize=0.0001) self.cellsize = 180.0 * 0.0001 / numpy.pi
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 pf = polarisation_frame_from_wcs(wcs, shape)
def test_fftim(self): self.m31image = create_test_image(cellsize=0.001, frequency=[1e8], canonical=True) m31_fft = fft_image(self.m31image) m31_fft_ifft = fft_image(m31_fft, self.m31image) numpy.testing.assert_array_almost_equal(self.m31image.data, m31_fft_ifft.data.real, 12) m31_fft.data = numpy.abs(m31_fft.data) export_image_to_fits(m31_fft, fitsfile='%s/test_m31_fft.fits' % (self.dir))
def setUp(self): self.dir = './test_results' os.makedirs(self.dir, exist_ok=True) 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=2000.0) 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.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) window = numpy.zeros(shape=self.model.shape, dtype=numpy.bool) window[..., 129:384, 129:384] = True self.innerquarter = create_image_from_array(window, self.model.wcs)
def test_channelise(self): m31cube = create_test_image(polarisation_frame=PolarisationFrame('stokesI'), frequency=numpy.linspace(1e8,1.1e8, 128)) for subimages in [128, 16, 8, 2, 1]: for slab in image_channel_iter(m31cube, subimages=subimages): assert slab.data.shape[0] == 128 // subimages
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, vis in enumerate(vis_iter): assert vis.phasecentre == self.phasecentre assert vis.nvis if i == 0: fullvis = vis totalnvis = vis.nvis else: fullvis = append_visibility(fullvis, vis) totalnvis += vis.nvis assert fullvis.nvis == totalnvis
def setUp(self): self.dir = './test_results' self.lowcore = create_named_configuration('LOWBD2-CORE') os.makedirs(self.dir, exist_ok=True) 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')) self.vis.data['vis'] *= 0.0 # Create model self.model = create_test_image(cellsize=0.0015, phasecentre=self.vis.phasecentre, frequency=self.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 export_image_to_fits( self.model, '%s/test_solve_skycomponent_model.fits' % (self.dir)) self.bigmodel = create_image_from_visibility(self.vis, cellsize=0.0015, npixel=512)
def setUp(self): self.dir = './test_results' os.makedirs(self.dir, exist_ok=True) lowcore = create_named_configuration('LOWBD2-CORE') times = numpy.arange(-3.0, +3.0, 1.0) * numpy.pi / 12.0 vnchan = 8 frequency = numpy.linspace(0.8e8, 1.20e8, vnchan) channel_bandwidth = numpy.array(vnchan * [frequency[1] - frequency[0]]) # Define the component and give it some polarisation and spectral behaviour f = numpy.array([100.0, 20.0, -10.0, 1.0]) self.flux = numpy.array( [f, 0.9 * f, 0.8 * f, 0.7 * f, 0.6 * f, 0.5 * f, 0.4 * f, 0.3 * f]) self.phasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox='J2000') self.compabsdirection = SkyCoord(ra=17.0 * u.deg, dec=-36.5 * u.deg, frame='icrs', equinox='J2000') self.comp = create_skycomponent(flux=self.flux, frequency=frequency, direction=self.compabsdirection) self.image = create_test_image( frequency=frequency, phasecentre=self.phasecentre, cellsize=0.001, polarisation_frame=PolarisationFrame('stokesIQUV')) self.blockvis = create_blockvisibility_iterator( lowcore, times=times, frequency=frequency, channel_bandwidth=channel_bandwidth, phasecentre=self.phasecentre, weight=1, polarisation_frame=PolarisationFrame('linear'), integration_time=1.0, number_integrations=1, predict=predict_2d, components=self.comp, phase_error=0.1, amplitude_error=0.01, sleep=1.0) self.vis = create_blockvisibility( lowcore, times=times, frequency=frequency, channel_bandwidth=channel_bandwidth, phasecentre=self.phasecentre, weight=1, polarisation_frame=PolarisationFrame('stokesIQUV'), integration_time=1.0) self.vis = predict_2d(self.vis, self.image)
def test_create_test_image(self): im = create_test_image(canonical=False) assert len(im.data.shape) == 2 im = create_test_image(canonical=True) assert len(im.data.shape) == 4 im = create_test_image(canonical=True, frequency=numpy.array([1e8]), polarisation_frame=PolarisationFrame( 'stokesI')) assert len(im.data.shape) == 4 assert im.data.shape[0] == 1 assert im.data.shape[1] == 1 im = create_test_image(canonical=True, frequency=numpy.array([1e8]), polarisation_frame=PolarisationFrame( 'stokesIQUV')) assert len(im.data.shape) == 4 assert im.data.shape[0] == 1 assert im.data.shape[1] == 4
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_convert_image_to_kernel(self): m31image = create_test_image(cellsize=0.001, frequency=[1e8], canonical=True) screen = create_w_term_like(m31image, w=20000.0, remove_shift=True) screen_fft = fft_image(screen) converted = convert_image_to_kernel(screen_fft, 8, 8) assert converted.shape == (1, 1, 8, 8, 8, 8) with self.assertRaises(AssertionError): converted = convert_image_to_kernel(m31image, 15, 1) with self.assertRaises(AssertionError): converted = convert_image_to_kernel(m31image, 15, 1000)
def test_create_w_term_image(self): m31image = create_test_image(cellsize=0.001) im = create_w_term_like(m31image, w=20000.0, remove_shift=True) im.data = im.data.real for x in [64, 64 + 128]: for y in [64, 64 + 128]: self.assertAlmostEqual(im.data[0, 0, y, x], 0.84946344276442431, 7) export_image_to_fits(im, '%s/test_wterm.fits' % self.dir) assert im.data.shape == (1, 1, 256, 256) self.assertAlmostEqual(numpy.max(im.data.real), 1.0, 7)
def main(): m31image = create_test_image(frequency=frequency, cellsize=cellsize) nchan, npol, ny, nx = m31image.data.shape vt = load(3) m31image.wcs.wcs.crval[0] = vt.phasecentre.ra.deg m31image.wcs.wcs.crval[1] = vt.phasecentre.dec.deg m31image.wcs.wcs.crpix[0] = float(nx // 2) m31image.wcs.wcs.crpix[1] = float(ny // 2) fig = show_image(m31image) fig.savefig('%s_created_img.png' % sys.argv[2]) dump(1, m31image)
def test_fftim_factors(self): for i in [3, 5, 7]: npixel = 256 * i m31image = create_test_image(cellsize=0.001, frequency=[1e8], canonical=True) padded = pad_image(m31image, [1, 1, npixel, npixel]) assert padded.shape == (1, 1, npixel, npixel) padded_fft = fft_image(padded) padded_fft_ifft = fft_image(padded_fft, m31image) numpy.testing.assert_array_almost_equal(padded.data, padded_fft_ifft.data.real, 12) padded_fft.data = numpy.abs(padded_fft.data) export_image_to_fits(padded_fft, fitsfile='%s/test_m31_fft_%d.fits' % (self.dir, npixel))
def test_pad_image(self): m31image = create_test_image(cellsize=0.001, frequency=[1e8], canonical=True) padded = pad_image(m31image, [1, 1, 1024, 1024]) assert padded.shape == (1, 1, 1024, 1024) padded = pad_image(m31image, [3, 4, 2048, 2048]) assert padded.shape == (3, 4, 2048, 2048) with self.assertRaises(ValueError): padded = pad_image(m31image, [1, 1, 100, 100]) with self.assertRaises(IndexError): padded = pad_image(m31image, [1, 1])
def test_stokes_conversion(self): assert self.m31image.polarisation_frame == PolarisationFrame("stokesI") stokes = create_test_image(cellsize=0.0001, polarisation_frame=PolarisationFrame("stokesIQUV")) assert stokes.polarisation_frame == PolarisationFrame("stokesIQUV") for pol_name in ['circular', 'linear']: polarisation_frame = PolarisationFrame(pol_name) polimage = convert_stokes_to_polimage(stokes, polarisation_frame=polarisation_frame) assert polimage.polarisation_frame == polarisation_frame polarisation_frame_from_wcs(polimage.wcs, polimage.shape) rstokes = convert_polimage_to_stokes(polimage) assert polimage.data.dtype == 'complex' assert rstokes.data.dtype == 'complex' numpy.testing.assert_array_almost_equal(stokes.data, rstokes.data.real, 12)
def test_create_low_test_skycomponents_from_gleam_apply_beam(self): sc = create_low_test_skycomponents_from_gleam(flux_limit=10.0, polarisation_frame=PolarisationFrame("stokesI"), frequency=self.frequency, kind='cubic') assert len(sc) > 1 assert sc[190].name == 'GLEAM J172031-005845' # self.assertAlmostEqual(sc[190].flux[0,0], 301.4964434927922, 7) im = create_test_image(canonical=True, cellsize=0.002, frequency=self.frequency, channel_bandwidth=self.channel_bandwidth, polarisation_frame=PolarisationFrame("stokesI"), phasecentre=self.phasecentre) bm = create_low_test_beam(model=im) sc = apply_beam_to_skycomponent(sc, bm) assert len(sc) > 1, "No components inside image"
def setUp(self): self.dir = './test_results' os.makedirs(self.dir, exist_ok=True) self.lowcore = create_named_configuration('LOWBD2-CORE') self.times = numpy.linspace(-3, +3, 13) * (numpy.pi / 12.0) self.frequency = numpy.array([1e8]) self.channel_bandwidth = numpy.array([1e7]) # Define the component and give it some polarisation and spectral behaviour f = numpy.array([100.0]) self.flux = numpy.array([f]) self.phasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-35.0 * u.deg, frame='icrs', equinox=2000.0) self.compabsdirection = SkyCoord(ra=17.0 * u.deg, dec=-36.5 * u.deg, frame='icrs', equinox=2000.0) self.comp = create_skycomponent( flux=self.flux, frequency=self.frequency, direction=self.compabsdirection, polarisation_frame=PolarisationFrame('stokesI')) self.image = create_test_image( frequency=self.frequency, phasecentre=self.phasecentre, cellsize=0.001, polarisation_frame=PolarisationFrame('stokesI')) self.image.data[self.image.data < 0.0] = 0.0 self.image_graph = delayed(create_test_image)( frequency=self.frequency, phasecentre=self.phasecentre, cellsize=0.001, polarisation_frame=PolarisationFrame('stokesI'))
def test_null(self): m31cube = create_test_image(polarisation_frame=PolarisationFrame('stokesI'), frequency=numpy.linspace(1e8, 1.1e8, 128)) for i, im in enumerate(image_null_iter(m31cube)): assert i<1, "Null iterator returns more than one value"