def test_crosssubtract_datamodel(self): self.actualSetUp(zerow=True) skymodel_vislist = predict_skymodel_list_serial_workflow( self.vis_list[0], self.skymodel_list, context='2d', docal=True) vobs = sum_predict_results(skymodel_vislist) skymodel_vislist = crosssubtract_datamodels_skymodel_list_serial_workflow( vobs, skymodel_vislist) result_skymodel = [ SkyModel(components=None, image=self.skymodel_list[-1].image) for v in skymodel_vislist ] results = invert_skymodel_list_serial_workflow(skymodel_vislist, result_skymodel, context='2d', docal=True) assert numpy.max(numpy.abs(results[0][0].data)) > 0.0 assert numpy.max(numpy.abs(results[0][1])) > 0.0 if self.plot: import matplotlib.pyplot as plt from wrappers.serial.image.operations import show_image show_image(results[0][0], title='Dirty image after cross-subtraction', vmax=0.1, vmin=-0.01) plt.show()
def progress(res, tl_list, gt_list, it, context='MPCCAL'): print('Iteration %d' % it) print( qa_image(res, context='%s residual image: iteration %d' % (context, it))) export_image_to_fits( res, arl_path( "test_results/low-sims-mpc-%s-residual_iteration%d_rmax%.1f.fits" % (context, it, rmax))) show_image(res, title='%s residual image: iteration %d' % (context, it)) plt.show(block=block_plots) combined_model = calculate_skymodel_equivalent_image(tl_list) print( qa_image(combined_model, context='Combined model: iteration %d' % it)) export_image_to_fits( combined_model, arl_path( "test_results/low-sims-mpc-%s-model_iteration%d_rmax%.1f.fits" % (context, it, rmax))) plt.clf() for i in range(len(tl_list)): plt.plot( numpy.angle(tl_list[i].gaintable.gain[:, :, 0, 0, 0]).flatten(), numpy.angle(gt_list[i]['T'].gain[:, :, 0, 0, 0]).flatten(), '.') plt.xlabel('Current phase') plt.ylabel('Update to phase') plt.title("%s iteration%d: Change in phase" % (context, it)) plt.savefig( arl_path( "test_results/low-sims-mpc-%s-skymodel-phase-change_iteration%d.jpg" % (context, it))) plt.show(block=block_plots) return tl_list
if ix >= iy: p = int(round(centre[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \ int(round(centre[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1]))) sc = pixel_to_skycoord(p[0], p[1], model.wcs) log.info("Component at (%f, %f) [0-rel] %s" % (p[0], p[1], str(sc))) flux = numpy.array([[100.0 + 2.0 * ix + iy * 20.0]]) comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc, polarisation_frame=PolarisationFrame('stokesI')) original_comps.append(comp) insert_skycomponent(model, comp) predict_skycomponent_visibility(vt, original_comps) cmodel = smooth_image(model) show_image(cmodel) plt.title("Smoothed model image") plt.savefig('1.jpg') #plt.show() comps = find_skycomponents(cmodel, fwhm=1.0, threshold=10.0, npixels=5) plt.clf() for i in range(len(comps)): ocomp, sep = find_nearest_skycomponent(comps[i].direction, original_comps) plt.plot((comps[i].direction.ra.value - ocomp.direction.ra.value)/cmodel.wcs.wcs.cdelt[0], (comps[i].direction.dec.value - ocomp.direction.dec.value)/cmodel.wcs.wcs.cdelt[1], '.', color='r') plt.xlabel('delta RA (pixels)') plt.ylabel('delta DEC (pixels)') plt.title("Recovered - Original position offsets")
def create_simulation_components(context, phasecentre, frequency, pbtype, offset_dir, flux_limit, pbradius, pb_npixel, pb_cellsize, show=False): """ Construct components for simulation :param context: :param phasecentre: :param frequency: :param pbtype: :param offset_dir: :param flux_limit: :param pbradius: :param pb_npixel: :param pb_cellsize: :return: """ HWHM_deg, null_az_deg, null_el_deg = find_pb_width_null(pbtype, frequency) dec = phasecentre.dec.deg ra = phasecentre.ra.deg if context == 'singlesource': log.info("create_simulation_components: Constructing single component") offset = [HWHM_deg * offset_dir[0], HWHM_deg * offset_dir[1]] log.info( "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg" % (offset[0], offset[1])) # The point source is offset to approximately the halfpower point offset_direction = SkyCoord( ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) * units.deg, dec=(dec + offset[1]) * units.deg, frame='icrs', equinox='J2000') original_components = [ Skycomponent(flux=[[1.0]], direction=offset_direction, frequency=frequency, polarisation_frame=PolarisationFrame('stokesI')) ] elif context == 'null': log.info( "create_simulation_components: Constructing single component at the null" ) offset = [null_az_deg * offset_dir[0], null_el_deg * offset_dir[1]] HWHM = HWHM_deg * numpy.pi / 180.0 log.info( "create_simulation_components: Offset from pointing centre = %.3f, %.3f deg" % (offset[0], offset[1])) # The point source is offset to approximately the null point offset_direction = SkyCoord( ra=(ra + offset[0] / numpy.cos(numpy.pi * dec / 180.0)) * units.deg, dec=(dec + offset[1]) * units.deg, frame='icrs', equinox='J2000') original_components = [ Skycomponent(flux=[[1.0]], direction=offset_direction, frequency=frequency, polarisation_frame=PolarisationFrame('stokesI')) ] else: offset = [0.0, 0.0] # Make a skymodel from S3 max_flux = 0.0 total_flux = 0.0 log.info("create_simulation_components: Constructing s3sky components") from wrappers.serial.simulation.testing_support import create_test_skycomponents_from_s3 original_components = create_test_skycomponents_from_s3( flux_limit=flux_limit / 100.0, phasecentre=phasecentre, polarisation_frame=PolarisationFrame("stokesI"), frequency=numpy.array(frequency), radius=pbradius) log.info( "create_simulation_components: %d components before application of primary beam" % (len(original_components))) pbmodel = create_image(npixel=pb_npixel, cellsize=pb_cellsize, phasecentre=phasecentre, frequency=frequency, polarisation_frame=PolarisationFrame("stokesI")) pb = create_pb(pbmodel, "MID_GAUSS", pointingcentre=phasecentre, use_local=False) pb_feko = create_pb(pbmodel, pbtype, pointingcentre=phasecentre, use_local=True) pb.data = pb_feko.data[:, 0, ...][:, numpy.newaxis, ...] pb_applied_components = [ copy_skycomponent(c) for c in original_components ] pb_applied_components = apply_beam_to_skycomponent( pb_applied_components, pb) filtered_components = [] for icomp, comp in enumerate(pb_applied_components): if comp.flux[0, 0] > flux_limit: total_flux += comp.flux[0, 0] if abs(comp.flux[0, 0]) > max_flux: max_flux = abs(comp.flux[0, 0]) filtered_components.append(original_components[icomp]) log.info( "create_simulation_components: %d components > %.3f Jy after application of primary beam" % (len(filtered_components), flux_limit)) log.info( "create_simulation_components: Strongest components is %g (Jy)" % max_flux) log.info( "create_simulation_components: Total flux in components is %g (Jy)" % total_flux) original_components = [ copy_skycomponent(c) for c in filtered_components ] if show: plt.clf() show_image(pb, components=original_components) plt.show(block=False) log.info("create_simulation_components: Created %d components" % len(original_components)) # Primary beam points to the phasecentre offset_direction = SkyCoord(ra=ra * units.deg, dec=dec * units.deg, frame='icrs', equinox='J2000') return original_components, offset_direction
channel_model = create_image_from_visibility( vt, cellsize=cellsize, npixel=512, nchan=1, imagecentre=vis_list[0].phasecentre, polarisation_frame=PolarisationFrame('stokesIQUV')) beam = create_pb(channel_model, telescope='VLA', pointingcentre=vt.phasecentre, use_local=False) beam.data /= numpy.max(beam.data) dirty, sumwt = invert_list_serial_workflow([vt], [channel_model])[0] print(sumwt) mosaic.data += dirty.data * beam.data mosaicsens.data += beam.data**2 show_image(dirty) plt.show() show_image(mosaic, cm='Greys', title='Linear mosaic') plt.show() show_image(mosaicsens, cm='Greys', title='Linear mosaic sensitivity') plt.show() from wrappers.serial.image.operations import export_image_to_fits export_image_to_fits(mosaic, "mosaics.fits") export_image_to_fits(mosaicsens, "mosaicsens.fits") # %%
npixels=12) for comp in all_components[:args.ninitial]: ical_components.append(comp) # ### Remove weaker of components that are too close (0.02 rad) idx, ical_components = remove_neighbouring_components( ical_components, 0.02) ical_components = sorted(ical_components, key=lambda comp: numpy.max(comp.flux), reverse=True) print("Voronoi decomposition based on %d point sources" % len(ical_components)) print(qa_image(ical_restored, context='ICAL restored image')) show_image(ical_restored, title='ICAL restored image', vmax=0.3, vmin=-0.03) show_image(ical_restored, title='ICAL restored image', components=ical_components, vmax=0.3, vmin=-0.03) plt.show(block=block_plots) gaintable = create_gaintable_from_blockvisibility(block_vis) mpccal_skymodel = initialize_skymodel_voronoi(model, ical_components, ical_skymodel[0].gaintable) ical_fluxes = [comp.flux[0, 0] for comp in ical_components] plt.clf() plt.semilogy(numpy.arange(len(all_fluxes)), all_fluxes, marker='.')
work_vis = arlexecute.compute(result, sync=True) for w in work_vis: all_skymodel_vis.data['vis'] += w.data['vis'] assert numpy.max(numpy.abs(all_skymodel_vis.data['vis'])) > 0.0 all_skymodel_blockvis = convert_visibility_to_blockvisibility(all_skymodel_vis) ####################################################################################################### # Now proceed to run MPCCAL in ICAL mode i.e. with only one skymodel def progress(res, tl_list, gt_list, it, context='MPCCAL'): print('Iteration %d' % it) print(qa_image(res, context='%s residual image: iteration %d' % (context, it))) export_image_to_fits(res, arl_path("test_results/low-sims-mpc-%s-residual_iteration%d_rmax%.1f.fits" % (context, it, rmax))) show_image(res, title='%s residual image: iteration %d' % (context, it)) plt.show(block=block_plots) combined_model = calculate_skymodel_equivalent_image(tl_list) print(qa_image(combined_model, context='Combined model: iteration %d' % it)) export_image_to_fits(combined_model, arl_path("test_results/low-sims-mpc-%s-model_iteration%d_rmax%.1f.fits" % (context, it, rmax))) plt.clf() for i in range(len(tl_list)): plt.plot(numpy.angle(tl_list[i].gaintable.gain[:, :, 0, 0, 0]).flatten(), numpy.angle(gt_list[i]['T'].gain[:, :, 0, 0, 0]).flatten(), '.') plt.xlabel('Current phase') plt.ylabel('Update to phase') plt.title("%s iteration%d: Change in phase" % (context, it))
for ix in locations: if True: # ix >= iy: p = int(round(centre[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \ int(round(centre[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1]))) sc = pixel_to_skycoord(p[0], p[1], model.wcs) # log.info("Component at (%f, %f) [0-rel] %s" % (p[0], p[1], str(sc))) flux = numpy.array([[200.0]]) # numpy.array([[100.0 + 2.0 * ix + iy * 20.0]]) comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc, polarisation_frame=PolarisationFrame('stokesI')) original_comps.append(comp) insert_skycomponent(model, comp) predict_skycomponent_visibility(vt, original_comps) cmodel = smooth_image(model) show_image(cmodel) plt.title("Smoothed model image") plt.savefig('%s/%s_smoothed.pdf' % (storedir, str(freq))) # plt.show() comps = find_skycomponents(cmodel, fwhm=1.0, threshold=10.0, npixels=5) plt.clf() log.info('Total components = %s' % len(comps)) for i in range(len(comps)): ocomp, sep = find_nearest_skycomponent(comps[i].direction, original_comps) log.info('Position offset %d %f %f' % (i, comps[i].direction.ra.value - ocomp.direction.ra.value, comps[i].direction.dec.value - ocomp.direction.dec.value)) plt.plot((comps[i].direction.ra.value - ocomp.direction.ra.value), (comps[i].direction.dec.value - ocomp.direction.dec.value), '.', color='r') # plt.plot((comps[i].direction.ra.value - ocomp.direction.ra.value) / cmodel.wcs.wcs.cdelt[0],
flux_limit=flux_limit, phasecentre=phasecentre, frequency=frequency, polarisation_frame=PolarisationFrame('stokesI'), radius=0.2) pb_gleam_components = apply_beam_to_skycomponent(original_gleam_components, beam) bright_components = filter_skycomponents_by_flux(pb_gleam_components, flux_min=0.3) all_components = filter_skycomponents_by_flux(pb_gleam_components, flux_min=flux_limit) show_image(beam, components=bright_components, cm='Greys', title='Primary beam plus bright components', vmax=3.0) lprint("Number of bright components %d" % len(bright_components)) # In[7]: idx, filtered_bright_components = remove_neighbouring_components( bright_components, 0.01) show_image(beam, components=filtered_bright_components, cm='Greys', title='Primary beam plus filtered bright components', vmax=3.0)
comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc, polarisation_frame=PolarisationFrame("stokesI")) comps.append(comp) predict_skycomponent_visibility(vt, comps) arlexecute.set_client(use_dask=True) dirty = create_image_from_visibility(vt, npixel=npixel, cellsize=cellsize, polarisation_frame=PolarisationFrame("stokesI")) vt = weight_visibility(vt, dirty) future = invert_list_arlexecute_workflow([vt], [dirty], context='2d') dirty, sumwt = arlexecute.compute(future, sync=True)[0] if doplot: show_image(dirty,cm='hot') plt.title("Dirty image") plt.savefig('%s/%s_dirty.pdf' % (results_dir, str(freq))) print("Max, min in dirty image = %.6f, %.6f, sumwt = %f" % (dirty.data.max(), dirty.data.min(), sumwt)) export_image_to_fits(dirty, '%s/imaging-wterm_dirty.fits' % (results_dir)) dirtyFacet = create_image_from_visibility(vt, npixel=npixel, npol=1, cellsize=cellsize) future = invert_list_arlexecute_workflow([vt], [dirtyFacet], facets=4, context='facets') dirtyFacet, sumwt = arlexecute.compute(future, sync=True)[0] dirtyFacet = smooth_image(dirtyFacet) if doplot: show_image(dirtyFacet,cm='hot') plt.title("Smoothed model image") plt.savefig('%s/%s_smooth.pdf' % (results_dir, str(freq)))
# plt.show() arlexecute.set_client(use_dask=True) dirty = create_image_from_visibility( vt, npixel=npixel, cellsize=cellsize, polarisation_frame=PolarisationFrame("stokesI")) vt = weight_visibility(vt, dirty) future = invert_list_arlexecute_workflow([vt], [dirty], context='2d') dirty, sumwt = arlexecute.compute(future, sync=True)[0] cmodel = smooth_image(dirty) show_image(cmodel) plt.title("Smoothed model image") plt.savefig('%s/%s_smoothed.pdf' % ('./', str(freq))) plt.show() comps = find_skycomponents(cmodel, fwhm=1.0, threshold=500.0, npixels=npixel // 60) plt.clf() log.info('Total components = %s' % len(comps)) discomp.append(len(comps)) x_offset = [] y_offset = [] offset = []