def plot_coverage_maps(cov_array, cc_fits, colorbar_label="coverage", cmap="hsv"): ccimage = create_clean_image_from_fits_file(cc_fits) beam = ccimage.beam rms = rms_image(ccimage) print(rms) cov_array[cov_array == 0] = np.nan blc, trc = find_bbox(ccimage.image, 1.0 * rms, 10) fig = iplot(ccimage.image, colors=cov_array, x=ccimage.x, y=ccimage.y, min_abs_level=3.0 * rms, beam=beam, show_beam=True, blc=blc, trc=trc, close=False, colorbar_label=colorbar_label, show=True, cmap=cmap) return fig
def plot_coverage_maps(coverage_map, original_uv_fits_path, original_cc_fits_path, data_dir, outname, path_to_script): i_image_cc = create_clean_image_from_fits_file(original_cc_fits_path) i_image = create_image_from_fits_file(original_cc_fits_path) rms = rms_image_shifted(original_uv_fits_path, tmp_dir=data_dir, image_fits=original_cc_fits_path, path_to_script=path_to_script) blc, trc = find_bbox(i_image.image, 2 * rms, delta=int(i_image_cc._beam.bmaj / 2)) fig = iplot(i_image.image, coverage_map - 0.68, x=i_image.x, y=i_image.y, min_abs_level=2. * rms, outdir=data_dir, beam=i_image_cc.beam, show=True, beam_place='lr', show_beam=True, cmap='viridis', blc=blc, trc=trc, color_clim=[-0.3, 0.3], colors_mask=i_image.image < 2 * rms) fig.savefig(os.path.join(data_dir, '{}.pdf'.format(outname)), bbox_inches='tight', format='pdf', dpi=600)
def create_boot_pang_errors(self, cred_mass=0.68, n_sigma=None): """ Create dictionary with images of PANG errors calculated from bootstrap PANG maps. :param cred_mass: (optional) Credibility mass. (default: ``0.68``) :param n_sigma: (optional) Sigma clipping for mask. If ``None`` then use instance's value. (default: ``None``) :return: Dictionary with keys - frequencies & values - instances of ``Image`` class with error maps. """ print( "Calculating maps of PANG errors for each band using bootstrapped" " PANG maps...") result = dict() if n_sigma is not None: self.set_common_mask(n_sigma) # Fetch common size `I` map on highest frequency for plotting PANG error # maps i_image = self.cc_cs_image_dict[self.freqs[-1]]['I'] rms = rms_image(i_image) blc, trc = find_bbox(i_image.image, 2. * rms, delta=int(i_image._beam.beam[0])) for i, freq in enumerate(self.freqs): images = self.boot_images.create_pang_images(freq=freq, mask=self._cs_mask) pang_images = Images() pang_images.add_images(images) error_image = pang_images.create_error_image(cred_mass=cred_mass) # As this errors are used for linear fit judgement only - add EVPA # absolute calibration error in quadrature evpa_error = np.deg2rad(self.sigma_evpa[i]) * np.ones( error_image.image.shape) error_image.image = np.sqrt((error_image.image)**2. + evpa_error**2.) result[freq] = error_image fig = iplot(i_image.image, error_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0, 1], blc=blc, trc=trc, beam=self.common_beam, colorbar_label='sigma EVPA, [rad]', slice_points=None, show_beam=True, show=False, cmap='hsv') self.figures['EVPA_sigma_boot_{}'.format(freq)] = fig self.evpa_sigma_boot_dict = result return result
def clean_original_data(uvdata_dict, data_dir, beam=None, plot=False, mapsize_clean=(512, 0.1), outfname_postfix=None): if not isinstance(mapsize_clean, dict): assert len(mapsize_clean) == 2 mapsize_clean = {band: mapsize_clean for band in bands} for band in bands: print("Band - {}".format(band)) for stokes in ('I', 'Q', 'U'): print("Stokes - {}".format(stokes)) if outfname_postfix is None: outfname = "cc_{}_{}.fits".format(band, stokes) else: outfname = "cc_{}_{}_{}.fits".format(band, stokes, outfname_postfix) print("Cleaning {} to {}".format(uvdata_dict[band], outfname)) clean_difmap(fname=uvdata_dict[band], outfname=outfname, stokes=stokes.lower(), path=data_dir, outpath=data_dir, mapsize_clean=mapsize_clean[band], path_to_script=path_to_script, show_difmap_output=False, beam_restore=beam) # Rarely need this one if plot: if outfname_postfix is None: outfname = "cc_{}_{}.fits".format(band, "I") else: outfname = "cc_{}_{}_{}.fits".format(band, "I", outfname_postfix) ccimage = create_clean_image_from_fits_file( os.path.join(data_dir, outfname)) beam = ccimage.beam rms = rms_image(ccimage) blc, trc = find_bbox(ccimage.image, 1.0 * rms, 10) fig = iplot(ccimage.image, x=ccimage.x, y=ccimage.y, min_abs_level=2.0 * rms, beam=beam, show_beam=True, blc=blc, trc=trc, close=False, colorbar_label="Jy/beam", show=True) if outfname_postfix is None: outfname = "cc_{}.png".format(band) else: outfname = "cc_{}_{}.png".format(band, outfname_postfix) fig.savefig(os.path.join(data_dir, outfname))
'booted_cc_{}.fits'.format(i), 'I', (512, 0.1), path=data_dir, path_to_script=path_to_script, outpath=data_dir) booted_im_fits = glob.glob(os.path.join(data_dir, 'booted_cc*.fits')) images = Images() images.add_from_fits(booted_im_fits) error_image = images.create_error_image() orig_image = create_clean_image_from_fits_file(os.path.join(data_dir, im_fits)) rms = rms_image_shifted(os.path.join(data_dir, uv_fits), image_fits=os.path.join(data_dir, im_fits), path_to_script=path_to_script) blc, trc = find_bbox(orig_image.image, level=1.5 * rms) image_mask = orig_image.image < 2 * rms label_size = 14 matplotlib.rcParams['xtick.labelsize'] = label_size matplotlib.rcParams['ytick.labelsize'] = label_size matplotlib.rcParams['axes.titlesize'] = label_size matplotlib.rcParams['axes.labelsize'] = label_size matplotlib.rcParams['font.size'] = label_size matplotlib.rcParams['legend.fontsize'] = label_size # 3c273 # fig = iplot(orig_image.image, error_image.image/rms, x=orig_image.x, # y=orig_image.y, show_beam=True, min_abs_level=2*rms, cmap='hsv', # beam=orig_image.beam, colors_mask=image_mask, # beam_face_color='black', blc=(495, 385), trc=(680, 540)) # 1633 fig = iplot(orig_image.image,
sample_cc_fits_paths=sample_cc_fits_paths, sample_uv_fits_paths=sample_uv_fits_paths, n_rms=n_rms, alpha=alpha, n_boot=n_boot, n_cov=n_cov, stokes=stokes) np.savetxt(os.path.join(data_dir, "{}.txt".format(outfile)), coverage_map) original_cc_fits_path = os.path.join(data_dir, 'original_cc.fits') i_image_cc = create_clean_image_from_fits_file(original_cc_fits_path) i_image = create_image_from_fits_file(original_cc_fits_path) rms = rms_image_shifted(original_uv_fits_path, tmp_dir=data_dir, image_fits=original_cc_fits_path, path_to_script=path_to_script) blc, trc = find_bbox(i_image.image, 2 * rms, delta=int(i_image_cc._beam.bmaj / 2)) # iplot(i_image.image, coverage_map, x=i_image.x, y=i_image.y, # min_abs_level=2. * rms, outfile=outfile, outdir=data_dir, # beam=i_image_cc.beam, show=True, beam_corner='lr', # show_beam=True, cmap='viridis', blc=blc, trc=trc) fig = iplot(i_image.image, coverage_map - 0.68, x=i_image.x, y=i_image.y, min_abs_level=2. * rms, outfile=outfile, outdir=data_dir, beam=i_image_cc.beam, show=True, beam_corner='lr',
elif freq == 8.1: cellsize = 0.2 clean_difmap(uv_fits_save_fname, cc_fits_save_fname, 'I', (1024, cellsize), path=out_dir, path_to_script=path_to_script, show_difmap_output=False, outpath=out_dir) ccimage = create_clean_image_from_fits_file( os.path.join(out_dir, cc_fits_save_fname)) beam = ccimage.beam rms = rms_image(ccimage) blc, trc = find_bbox(ccimage.image, rms, 10) comps = import_difmap_model(out_dfm_model_fn, out_dir) plot_fitted_model(os.path.join(out_dir, uv_fits_save_fname), comps, savefig=os.path.join( out_dir, "difmap_model_uvplot_{}_{}.png".format( str(i).zfill(2), freq))) fig = iplot(ccimage.image, x=ccimage.x, y=ccimage.y, min_abs_level=3 * rms, beam=beam, show_beam=True, blc=blc,
def analyze_rotm_boot(self, n_sigma=None, cred_mass=0.68, rotm_slices=None, colors_clim=None, slice_ylim=None, use_conv_image_in_boot_slice=True): print("Estimate RM map and it's error using bootstrap...") if rotm_slices is None: rotm_slices = self.rotm_slices if n_sigma is not None: self.set_common_mask(n_sigma) # This accounts for D-terms and EVPA calibration errors. sigma_pangs_dict = self.create_boot_pang_errors() sigma_pang_arrays = [sigma_pangs_dict[freq].image for freq in self.freqs] # This is RM image made using bootstrap-based PANG errors that # originally account for D-terms and EVPA calibration errors. rotm_image, _, chisq_image = \ self.original_cs_images.create_rotm_image(sigma_pang_arrays, mask=self._cs_mask, return_chisq=True, mask_on_chisq=True) self._rotm_image_boot = rotm_image self._rotm_chisq_image_boot = chisq_image i_image = self.cc_cs_image_dict[self.freqs[-1]]['I'] rms = rms_image(i_image) blc, trc = find_bbox(i_image.image, 2.*rms, delta=int(i_image._beam.beam[0]/2)) fig = iplot(i_image.image, rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=colors_clim, blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, cmap='viridis', show_beam=True, show=False, beam_place="ul") self.figures['rotm_image_boot'] = fig fig = iplot(i_image.image, chisq_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0., self._chisq_crit], blc=blc, trc=trc, beam=self.common_beam, colorbar_label='Chi-squared', slice_points=rotm_slices, show_beam=True, show=False, cmap='viridis') self.figures['rotm_chisq_image_boot'] = fig self._boot_rotm_images =\ self.boot_images.create_rotm_images(mask=self._cs_mask, mask_on_chisq=False, sigma_evpa=self.sigma_evpa) sigma_rotm_image =\ self._boot_rotm_images.create_error_image(cred_mass=cred_mass) self._rotm_image_sigma_boot = sigma_rotm_image # Now ``self._boot_rotm_images`` doesn't contain contribution from # EVPA calibration error self._boot_rotm_images =\ self.boot_images.create_rotm_images(mask=self._cs_mask, mask_on_chisq=False, sigma_evpa=None) sigma_rotm_image_grad =\ self._boot_rotm_images.create_error_image(cred_mass=cred_mass) self._rotm_image_sigma_boot_grad = sigma_rotm_image_grad # This sigma take absolute EVPA calibration uncertainty into # account fig = iplot(i_image.image, sigma_rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, outfile='rotm_image_boot_sigma', outdir=self.data_dir, color_clim=[0, 200], blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, cmap='viridis', beam_place='ul', show_beam=True, show=False) self.figures['rotm_image_boot_sigma'] = fig # This sigma RM doesn't include EVPA fig = iplot(i_image.image, sigma_rotm_image_grad.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, outfile='rotm_image_boot_sigma', outdir=self.data_dir, color_clim=[0, 200], blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, cmap='viridis', beam_place='ul', show_beam=True, show=False) self.figures['rotm_image_boot_sigma_grad'] = fig if rotm_slices is not None: self.figures['slices_boot'] = dict() self.figures['slices_boot_conv'] = dict() for rotm_slice in rotm_slices: rotm_slice_ = i_image._convert_coordinates(rotm_slice[0], rotm_slice[1]) if use_conv_image_in_boot_slice: rotm_image_ = self._rotm_image_conv else: rotm_image_ = rotm_image # ``self._boot_rotm_images`` doesn't contain contribution from # EVPA calibration error - so used for RM gradient searches fig = analyze_rotm_slice(rotm_slice_, rotm_image_, rotm_images=self._boot_rotm_images, outdir=self.data_dir, beam_width=int(i_image._beam.beam[0]), outfname="ROTM_{}_slice_boot".format(rotm_slice), ylim=slice_ylim, show_dots_boot=True, # fig=self.figures['slices_conv'][str(rotm_slice)], fig=None) self.figures['slices_boot'][str(rotm_slice)] = fig fig = analyze_rotm_slice(rotm_slice_, rotm_image_, rotm_images=self._boot_rotm_images, outdir=self.data_dir, beam_width=int(i_image._beam.beam[0]), outfname="ROTM_{}_slice_boot".format(rotm_slice), ylim=slice_ylim, show_dots_boot=True, fig=self.figures['slices_conv'][str(rotm_slice)], # fig=None, ) self.figures['slices_boot_conv'][str(rotm_slice)] = fig return rotm_image, sigma_rotm_image
def analyze_rotm_conv(self, sigma_evpa=None, sigma_d_term=None, rotm_slices=None, colors_clim=None, n_sigma=None, pxls_plot=None, plot_points=None, slice_ylim=None): print("Estimate RM map and it's error using conventional method...") if sigma_evpa is None: sigma_evpa = self.sigma_evpa if sigma_d_term is None: sigma_d_term = self.sigma_d_term if rotm_slices is None: rotm_slices = self.rotm_slices if n_sigma is not None: self.set_common_mask(n_sigma) # Find EVPA error for each frequency print("Calculating maps of PANG errors for each band using Hovatta's" " approach...") # Fetch common size `I` map on highest frequency for plotting PANG error # maps i_image = self.cc_cs_image_dict[self.freqs[-1]]['I'] if pxls_plot is not None: pxls_plot = [i_image._convert_coordinate(pxl) for pxl in pxls_plot] rms = rms_image(i_image) blc, trc = find_bbox(i_image.image, 2.*rms, delta=int(i_image._beam.beam[0])) for i, freq in enumerate(self.freqs): n_ant = len(self.uvdata_dict[freq].antennas) n_if = self.uvdata_dict[freq].nif d_term = sigma_d_term[i] s_evpa = sigma_evpa[i] # Get number of scans if self.n_scans is not None: try: # If `NX` table is present n_scans = len(self.uvdata_dict[freq].scans) except TypeError: scans_dict = self.uvdata_dict[freq].scans_bl scan_lengths = list() for scans in scans_dict.values(): if scans is not None: scan_lengths.append(len(scans)) n_scans = mode(scan_lengths)[0][0] else: n_scans = self.n_scans[i] q = self.cc_cs_image_dict[freq]['Q'] u = self.cc_cs_image_dict[freq]['U'] i = self.cc_cs_image_dict[freq]['I'] # We need ``s_evpa = 0`` for testing RM gradient significance but # ``s_evpa != 0`` for calculating RM errors pang_std, ppol_std = hovatta_find_sigma_pang(q, u, i, s_evpa, d_term, n_ant, n_if, n_scans) self.evpa_sigma_dict[freq] = pang_std fig = iplot(i_image.image, pang_std, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0, 1], blc=blc, trc=trc, beam=self.common_beam, colorbar_label='sigma EVPA, [rad]', show_beam=True, show=False) self.figures['EVPA_sigma_{}'.format(freq)] = fig sigma_pang_arrays = [self.evpa_sigma_dict[freq] for freq in self.freqs] sigma_pang_arrays_grad = [np.sqrt(self.evpa_sigma_dict[freq]**2 - np.deg2rad(self.sigma_evpa[i])**2) for i, freq in enumerate(self.freqs)] rotm_image, sigma_rotm_image, chisq_image =\ self.original_cs_images.create_rotm_image(sigma_pang_arrays, mask=self._cs_mask, return_chisq=True, plot_pxls=pxls_plot, outdir=self.data_dir, mask_on_chisq=False) rotm_image_grad, sigma_rotm_image_grad, _ =\ self.original_cs_images.create_rotm_image(sigma_pang_arrays_grad, mask=self._cs_mask, return_chisq=True, plot_pxls=pxls_plot, outdir=self.data_dir, mask_on_chisq=False) self._rotm_image_conv = rotm_image self._rotm_image_conv_grad = rotm_image_grad self._rotm_image_sigma_conv = sigma_rotm_image self._rotm_image_sigma_conv_grad = sigma_rotm_image_grad self._rotm_chisq_image_conv = chisq_image i_image = self.cc_cs_image_dict[self.freqs[-1]]['I'] uv_fits_path = self.uvfits_dict[self.freqs[-1]] image_fits_path = self.cc_cs_fits_dict[self.freqs[-1]]['I'] # RMS using Hovatta-style rms = rms_image_shifted(uv_fits_path, image_fits=image_fits_path, path_to_script=self.path_to_script) blc, trc = find_bbox(i_image.image, 2.*rms, delta=int(i_image._beam.beam[0])) fig = iplot(i_image.image, rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=colors_clim, blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, show_beam=True, show=False, show_points=plot_points, cmap='viridis') self.figures['rotm_image_conv'] = fig fig = iplot(i_image.image, sigma_rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0, 200], blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, show_beam=True, show=False, cmap='viridis', beam_place='ul') self.figures['rotm_image_conv_sigma'] = fig fig = iplot(i_image.image, sigma_rotm_image_grad.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0, 200], blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, show_beam=True, show=False, cmap='viridis', beam_place='ul') self.figures['rotm_image_conv_sigma_grad'] = fig fig = iplot(i_image.image, chisq_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, outfile='rotm_chisq_image_conv', outdir=self.data_dir, color_clim=[0., self._chisq_crit], blc=blc, trc=trc, beam=self.common_beam, colorbar_label='Chi-squared', slice_points=rotm_slices, show_beam=True, show=False, cmap='viridis') self.figures['rotm_chisq_image_conv'] = fig if rotm_slices is not None: self.figures['slices_conv'] = dict() for rotm_slice in rotm_slices: rotm_slice_ = i_image._convert_coordinates(rotm_slice[0], rotm_slice[1]) # Here we use RM image error calculated using PANG errors # without contribution of the EVPA calibration errors. fig = analyze_rotm_slice(rotm_slice_, rotm_image, sigma_rotm_image=sigma_rotm_image_grad, outdir=self.data_dir, beam_width=int(i_image._beam.beam[0]), outfname="ROTM_{}_slice".format(rotm_slice), ylim=slice_ylim) self.figures['slices_conv'][str(rotm_slice)] = fig return rotm_image, sigma_rotm_image
def analyze_rotm_boot(self, n_sigma=None, cred_mass=0.68, rotm_slices=None, colors_clim=None, slice_ylim=None, use_conv_image_in_boot_slice=True): print("Estimate RM map and it's error using bootstrap...") if rotm_slices is None: rotm_slices = self.rotm_slices if n_sigma is not None: self.set_common_mask(n_sigma) sigma_pangs_dict = self.create_boot_pang_errors() sigma_pang_arrays = [ sigma_pangs_dict[freq].image for freq in self.freqs ] rotm_image, _, chisq_image = \ self.original_cs_images.create_rotm_image(sigma_pang_arrays, mask=self._cs_mask, return_chisq=True, mask_on_chisq=True) self._rotm_image_boot = rotm_image self._rotm_chisq_image_boot = chisq_image i_image = self.cc_cs_image_dict[self.freqs[-1]]['I'] rms = rms_image(i_image) blc, trc = find_bbox(i_image.image, 2. * rms, delta=int(i_image._beam.beam[0])) fig = iplot(i_image.image, rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=colors_clim, blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, cmap='hsv', show_beam=True, show=False) self.figures['rotm_image_boot'] = fig fig = iplot(i_image.image, chisq_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, color_clim=[0., self._chisq_crit], blc=blc, trc=trc, beam=self.common_beam, colorbar_label='Chi-squared', slice_points=rotm_slices, show_beam=True, show=False) self.figures['rotm_chisq_image_boot'] = fig self._boot_rotm_images =\ self.boot_images.create_rotm_images(mask=self._cs_mask, mask_on_chisq=False) sigma_rotm_image =\ self._boot_rotm_images.create_error_image(cred_mass=cred_mass) self._rotm_image_sigma_boot = sigma_rotm_image # This sigma doesn't take absolute EVPA calibration uncertainty into # account fig = iplot(i_image.image, sigma_rotm_image.image, x=i_image.x, y=i_image.y, min_abs_level=3. * rms, colors_mask=self._cs_mask, outfile='rotm_image_boot_sigma', outdir=self.data_dir, color_clim=[0, 200], blc=blc, trc=trc, beam=self.common_beam, slice_points=rotm_slices, cmap='hsv', show_beam=True, show=False, beam_face_color='black') self.figures['rotm_image_boot_sigma'] = fig if rotm_slices is not None: self.figures['slices_boot'] = dict() for rotm_slice in rotm_slices: rotm_slice_ = i_image._convert_coordinates( rotm_slice[0], rotm_slice[1]) if use_conv_image_in_boot_slice: rotm_image_ = self._rotm_image_conv else: rotm_image_ = rotm_image fig = analyze_rotm_slice( rotm_slice_, rotm_image_, rotm_images=self._boot_rotm_images, outdir=self.data_dir, beam_width=int(i_image._beam.beam[0]), outfname="ROTM_{}_slice_boot".format(rotm_slice), ylim=slice_ylim, show_dots_boot=False, fig=self.figures['slices_conv'][str(rotm_slice)]) self.figures['slices_boot'][str(rotm_slice)] = fig return rotm_image, sigma_rotm_image
uvdata.substitute([model]) uvdata.noise_add(noise) uvdata.save(os.path.join(data_dir, 'fake.uvf')) # Clean uv-data clean_difmap( 'fake.uvf', 'fake_cc.fits', 'I', (1024, 0.1), path=data_dir, path_to_script='/home/ilya/code/vlbi_errors/difmap/final_clean_nw', outpath=data_dir, show_difmap_output=True) image = create_clean_image_from_fits_file( os.path.join(data_dir, 'fake_cc.fits')) rms = rms_image(image) blc, trc = find_bbox(image.image, 2. * rms, delta=int(image._beam.beam[0])) # Plot image iplot(image.image, x=image.x, y=image.y, min_abs_level=3. * rms, outfile='fake_image', outdir=data_dir, blc=blc, trc=trc, beam=image.beam, show_beam=True) # Create posterior for data & model lnpost = LnPost(uvdata, model) ndim = model.size
def abc_simulations(param, z, freqs, uv_fits_templates, cc_images, pickle_history, out_dir=None): """ Simulation function for ABC. Simulates data given parameters (``b, n, los``) and returns the vector of the summary statistics. :param b: Value of the magnetic field at 1 pc [G]. :param n: Value of particle density at 1 pc [cm^(-3)]. :param los: Jet angle to the line of site [rad]. :param z: Redshift of the source. :param freqs: Iterable of frequencies. :param uv_fits_templates: Iterable of paths to FITS files with self-calibrated uv-data. In order of ``freqs``. :param cc_images: Iterable of paths to FITS files with CLEAN maps. In order of ``freqs``. :param out_dir: (optional) Directory to store files. If ``None`` then use CWD. (default: ``None``) :param pickle_history: Path to pickle file with dictionary of simulations history. :return: Summary statistics. E.g. "observed" core flux at highest frequency, "observed" core size at highest frequency, "observed" ellipticity of the core at highest frequency, core shift between frequencies (distance between "core shift - frequency" curves). """ if out_dir is None: out_dir = os.getcwd() else: # FIXME: Create directory if it doesn't exist if not os.path.exists(out_dir): pass result_dict = dict() b, n, los = np.exp(param) p = Parameters(b, n, los) with open(pickle_history, 'r') as fo: history = pickle.load(fo) print("Running simulations with b={}, n={}, los={}".format(b, n, los)) for freq, uv_fits_template, cc_image in zip(freqs, uv_fits_templates, cc_images): # Cleaning old results if any simulated_maps_old = glob.glob(os.path.join(exe_dir, "map*.txt")) for to_remove in simulated_maps_old: os.unlink(to_remove) # Checking if simulations on highest frequency are done. If so then use # scaled image parameters if freqs[0] in history[p].keys(): pixel_size_mas = history[p][ freqs[0]]["pixel_size_mas"] * freqs[0] / freq number_of_pixels = history[p][ freqs[0]]["number_of_pixels"] * freq / freqs[0] map_size = (number_of_pixels, pixel_size_mas) print("Using scaled image parameters: {}, {}".format( number_of_pixels, pixel_size_mas)) else: pixel_size_mas = 0.01 number_of_pixels = 400 map_size = None update_dict = { "jet": { "bfield": { "parameters": { "b_1": b } }, "nfield": { "parameters": { "n_1": n } } }, "observation": { "los_angle": los, "frequency_ghz": freq, "redshift": z }, "image": { "pixel_size_mas": pixel_size_mas, "number_of_pixels": number_of_pixels } } update_config(cfg_file, update_dict) # FIXME: Handle ``FailedFindBestImageParamsException`` during ABC run simulation_params = run_simulations(cfg_file, path_to_executable, map_size=map_size) # Find total flux on simulated image image = os.path.join(exe_dir, "map_i.txt") image = np.loadtxt(image) # Rare case of strange fluxes image[image < 0] = 0 image[image > 10.0] = 0 # Total model flux at current frequency total_flux = image.sum() # Maximum model pixel flux at current frequency max_flux = image.max() cc_image = create_clean_image_from_fits_file(cc_image) noise_factor = 1.0 initial_dfm_model = os.path.join(main_dir, 'initial_cg.mdl') out_dfm_model_fn = "bk_{}.mdl".format(freq) uv_fits_save_fname = "bk_{}.fits".format(freq) modelfit_simulation_result(exe_dir, initial_dfm_model, noise_factor=noise_factor, out_dfm_model_fn=out_dfm_model_fn, out_dir=out_dir, params=simulation_params, uv_fits_save_fname=uv_fits_save_fname, uv_fits_template=uv_fits_template) # Find measured and true distance of core to jet component dr_obs = find_core_separation_from_jet_using_difmap_model( os.path.join(out_dir, out_dfm_model_fn)) dr_true = find_core_separation_from_center_using_simulations( os.path.join(exe_dir, "map_i.txt"), simulation_params) # This means something wrong with jetshow if total_flux < 0: print("b = {}, n = {}, los = {}".format(b, n, los)) open( os.path.join( out_dir, "total_disaster_{}_{}_{}_{}.txt".format(b, n, los, freq)), 'a').close() raise TotalDisasterException # Plot map with components superimposed cc_fits_save_fname = "bk_cc_{}.fits".format(freq) # For 18 cm we need large pixel size cellsize = 0.1 if freq == 1.665: cellsize = 0.5 elif freq == 8.1: cellsize = 0.2 clean_difmap(uv_fits_save_fname, cc_fits_save_fname, 'I', (1024, cellsize), path=out_dir, path_to_script=path_to_script, show_difmap_output=False, outpath=out_dir) ccimage = create_clean_image_from_fits_file( os.path.join(out_dir, cc_fits_save_fname)) beam = ccimage.beam rms = rms_image(ccimage) blc, trc = find_bbox(ccimage.image, rms, 10) comps = import_difmap_model(out_dfm_model_fn, out_dir) plot_fitted_model(os.path.join(out_dir, uv_fits_save_fname), comps, savefig=os.path.join( out_dir, "difmap_model_uvplot_{}.png".format(freq))) fig = iplot(ccimage.image, x=ccimage.x, y=ccimage.y, min_abs_level=3 * rms, beam=beam, show_beam=True, blc=blc, trc=trc, components=comps, close=True, colorbar_label="Jy/beam") fig.savefig(os.path.join(out_dir, "cc_{}.png".format(freq))) # Move simulated images to data directory cut_image(os.path.join(exe_dir, "map_i.txt")) cut_image(os.path.join(exe_dir, "map_l.txt")) cut_image(os.path.join(exe_dir, "map_q.txt")) cut_image(os.path.join(exe_dir, "map_u.txt")) cut_image(os.path.join(exe_dir, "map_v.txt")) cut_image(os.path.join(exe_dir, "map_tau.txt")) for name in ('i', 'q', 'u', 'v', 'tau', 'l'): shutil.move( os.path.join(exe_dir, "map_{}.txt".format(name)), os.path.join(out_dir, "map_{}_{}.txt".format(name, freq))) # Calculate some info dr_pc = distance_from_SMBH(dr_true, los, z=z) b_core = b_field(b, dr_pc) t_syn_years = t_syn(b_core, freq) / (np.pi * 10**7) result_dict[freq] = dict() to_results = { "dr_obs": dr_obs, "dr_true": dr_true, "flux": total_flux, "flux_obs": comps[0].p[0], "bmaj_obs": comps[0].p[3], "tb_difmap": np.log10(tb_comp(comps[0].p[0], comps[0].p[3], freq, z=z)), "tb_pix": np.log10( tb(max_flux, freq, simulation_params[u'image'][u'pixel_size_mas'], z=z)), "b_core": b_core, "dr_core_pc": dr_pc, "t_syn_core": t_syn_years, "pixel_size_mas": simulation_params[u'image'][u'pixel_size_mas'], "number_of_pixels": simulation_params[u'image'][u'number_of_pixels'] } result_dict[freq] = to_results history[p] = result_dict with open(pickle_history, 'w') as fo: pickle.dump(history, fo) return create_summary_from_result_dict(result_dict, (0.3, 0.2, 0.1))