示例#1
0
    def clean_original_native(self, freq_stokes_dict=None):
        """
        Clean original FITS-files with uv-data using native resolution.
        """
        for freq in self.freqs:
            self.cc_image_dict.update({freq: dict()})
            self.cc_fits_dict.update({freq: dict()})
            self.cc_beam_dict.update({freq: dict()})

        if freq_stokes_dict is not None:
            print("Found CLEANed images of original uvdata with naitive map &"
                  " beam parameters...")
            for freq in self.freqs:
                for stokes in self.stokes:
                    image = create_clean_image_from_fits_file(
                        freq_stokes_dict[freq][stokes])
                    self.cc_image_dict[freq].update({stokes: image})
                    if stokes == 'I':
                        self.cc_beam_dict.update({freq: image.beam})

        else:
            print(
                "Clean original uv-data with native map & beam parameters...")
            for freq in self.freqs:
                print("Cleaning frequency {} with image "
                      "parameters {}".format(freq, self.imsizes_dict[freq]))
                uv_fits_path = self.uvfits_dict[freq]
                uv_dir, uv_fname = os.path.split(uv_fits_path)
                for stokes in self.stokes:
                    outfname = '{}_{}_cc.fits'.format(freq, stokes)
                    outpath = os.path.join(self.data_dir, outfname)
                    # Check if it is already done
                    if not os.path.exists(outpath):
                        clean_difmap(uv_fname,
                                     outfname,
                                     stokes,
                                     self.imsizes_dict[freq],
                                     path=uv_dir,
                                     path_to_script=self.path_to_script,
                                     outpath=self.data_dir,
                                     command_file=self._difmap_commands_file)
                    else:
                        print("Found CLEAN model in file {}".format(outfname))
                    self.cc_fits_dict[freq].update(
                        {stokes: os.path.join(self.data_dir, outfname)})
                    image = create_clean_image_from_fits_file(outpath)
                    self.cc_image_dict[freq].update({stokes: image})
                    if stokes == 'I':
                        self.cc_beam_dict.update({freq: image.beam})

        if self.clear_difmap_logs:
            print("Removing difmap log-files...")
            difmap_logs = glob.glob(os.path.join(self.data_dir, "difmap.log*"))
            for difmap_log in difmap_logs:
                os.unlink(difmap_log)
示例#2
0
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)
示例#3
0
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))
示例#4
0
    def clean_original_common(self, freq_stokes_dict=None):

        # if freq_stokes_dict is not None:
        #     print("Found CLEANed images of original uvdata with common map &"
        #           " beam parameters...")
        #     for freq in self.freqs:
        #         self.cc_cs_image_dict.update({freq: dict()})
        #         self.cc_cs_fits_dict.update({freq: dict()})
        #         for stokes in self.stokes:
        #             image = create_clean_image_from_fits_file(freq_stokes_dict[freq][stokes])
        #             self.cc_image_dict[freq].update({stokes: image})
        #             if stokes == 'I':
        #                 self.cc_beam_dict.update({freq: image.beam})

        print("Clean original uv-data with common map parameters "
              " {} and beam {}".format(self.common_imsize, self.common_beam))
        for freq in self.freqs:
            self.cc_cs_image_dict.update({freq: dict()})
            self.cc_cs_fits_dict.update({freq: dict()})

            uv_fits_path = self.uvfits_dict[freq]
            uv_dir, uv_fname = os.path.split(uv_fits_path)
            for stokes in self.stokes:
                outfname = 'cs_{}_{}_cc.fits'.format(freq, stokes)
                outpath = os.path.join(self.data_dir, outfname)
                # Check if it is already done
                if not os.path.exists(outpath):
                    clean_difmap(uv_fname,
                                 outfname,
                                 stokes,
                                 self.common_imsize,
                                 path=uv_dir,
                                 path_to_script=self.path_to_script,
                                 beam_restore=self.common_beam,
                                 outpath=self.data_dir,
                                 command_file=self._difmap_commands_file)
                else:
                    print("Found CLEAN model in file {}".format(outfname))
                self.cc_cs_fits_dict[freq].update(
                    {stokes: os.path.join(self.data_dir, outfname)})
                image = create_clean_image_from_fits_file(outpath)
                self.cc_cs_image_dict[freq].update({stokes: image})

        if self.clear_difmap_logs:
            print("Removing difmap log-files...")
            difmap_logs = glob.glob(os.path.join(self.data_dir, "difmap.log*"))
            for difmap_log in difmap_logs:
                os.unlink(difmap_log)
示例#5
0
    #     # aslice = rotm_im.slice(point1=(2.5, -2), point2=(-2.5, -2))
    #
    #     # x = np.arange(len(aslice))/beam_size
    #     # plt.plot(x, aslice, alpha=0.2, color='r')
    #
    # low_scb, up_scb = create_scb(boot_slices, original_slice)
    # plt.fill_between(x, low_scb, up_scb, alpha=0.35, label="SCB")
    # plt.fill_between(x, ci_low_slice, ci_high_slice, alpha=0.5, label="CB")
    # plt.legend(loc="upper left")

    # Plot coverage maps
    data_dir = "/home/ilya/data/revision_results"
    cov_ROTM = os.path.join(data_dir, "cov_ROTM.txt")
    cov_SPIX = os.path.join(data_dir, "cov_SPIX.txt")
    cov_ROTM_conv = os.path.join(data_dir, "cov_ROTM_conv.txt")
    cov_SPIX_conv = os.path.join(data_dir, "cov_SPIX_conv.txt")
    mask_cov_spix = cov_SPIX == 0
    mask_cov_spix_conv = cov_SPIX_conv == 0
    mask_cov_rotm = cov_ROTM == 0
    mask_cov_rotm_conv = cov_ROTM_conv == 0

    # Load original I and QU images
    i_image = create_clean_image_from_fits_file(
        "/home/ilya/data/revision/results/cc_u_I.fits")
    q_image = create_clean_image_from_fits_file(
        "/home/ilya/data/revision/results/cc_u_Q.fits")
    u_image = create_clean_image_from_fits_file(
        "/home/ilya/data/revision/results/cc_u_U.fits")
    from image_ops import pol_map
    ppol = pol_map(q_image.image, u_image.imsize, mask=mask_cov_rotm)
示例#6
0
        # Rare case of strange fluxes
        image[image < 0] = 0
        image[image > 10.0] = 0

        total_flux = image.sum()
        print("TOTAL FLUX = {}".format(total_flux))
        if freq == 15.4:
            if total_flux > total_flux_max or total_flux < total_flux_min:
                print("SKIPPING...")
                open(
                    os.path.join(
                        out_dir, "bad_total_flux_{}_{}_{}_{}.txt".format(
                            total_flux, b, n, los)), 'a').close()
                break
        max_flux = image.max()
        cc_image = create_clean_image_from_fits_file(cc_image)
        noise_factor = noise_scale * image.sum() / cc_image.cc.sum()

        initial_dfm_model = os.path.join(main_dir, 'initial_cg.mdl')
        out_dfm_model_fn = "bk_{}_{}.mdl".format(str(i).zfill(2), freq)
        uv_fits_save_fname = "bk_{}_{}.fits".format(str(i).zfill(2), 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)

        # Modelfit results of simulations by first FT model to template uv-fits,
示例#7
0
文件: mojave.py 项目: akutkin/SACA
def get_stacked_map(source, mojave_dir=None, out_dir=None, imsize=(512, 0.1),
                    path_to_script=None, epochs_slice=None):
    """
    Functions that returns stacked image of given source using MOJAVE 15 GHz
    data downloading it directly from MOJAVE DB or getting it from
    user-specified directory.

    :param source:
        Source name [B1950].
    :param mojave_dir: (optional)
        Path to directory with MOJAVE 15 GHz data. If ``None`` then download
        from MOJAVE DB. (default: ``None``)
    :param out_dir: (optional)
        Directory where to store files. If ``None`` then use CWD. (default:
        ``None``)
    :param imsize: (optional)
        Tuple of image size [pix], pixel size [mas] to put to difmap. (default:
        ``(512, 0.1)``)
    :param path_to_script: (optional)
        Path to difmap CLEANing script. If ``None`` then use CWD. (default:
        ``None``)
    :param epochs_slice: (optional)
        Slice of epochs sorted to process. If ``None`` then use all available
        epochs. (default: ``None``)

    :return:
        Numpy 2D array with stacked image.
    """
    if path_to_script is None:
        path_to_script = os.getcwd()
    epochs = get_epochs_for_source(source)
    if epochs_slice is not None:
        epochs = epochs[::-1][epochs_slice]
    if out_dir is None:
        out_dir = os.getcwd()
    elif not os.path.exists(out_dir):
        os.makedirs(out_dir)

    if mojave_dir is None:
        download_mojave_uv_fits(source, epochs, bands=['u'],
                                download_dir=out_dir)
    else:
        out_dir = mojave_dir
    beams_dict = dict()

    print "Output directory : {}".format(out_dir)

    # First clean and restore with native beam
    epoch_stokes_dict = dict()
    for epoch in sorted(epochs):
        print "Cleaning epoch {} with naitive restoring beam".format(epoch)
        uv_fits_fname = mojave_uv_fits_fname(source, 'u', epoch)
        uvdata = UVData(os.path.join(out_dir, uv_fits_fname))
        uv_stokes = uvdata.stokes
        if 'RR' in uv_stokes and 'LL' in uv_stokes:
            stokes = 'I'
        elif 'RR' in uv_stokes and 'LL' not in uv_stokes:
            stokes = 'RR'
        elif 'LL' in uv_stokes and 'RR' not in uv_stokes:
            stokes = 'LL'
        else:
            continue
        epoch_stokes_dict.update({epoch: stokes})
        im_fits_fname = "{}_{}_{}_{}.fits".format(source, 'U', epoch, stokes)
        print "Difmap params: "
        print "uv_fits_fname : {}".format(uv_fits_fname)
        print "im_fits_fname : {}".format(im_fits_fname)
        print "path : {}".format(out_dir)
        print "outpath: {}".format(out_dir)
        clean_difmap(uv_fits_fname, im_fits_fname, stokes, imsize,
                     path=out_dir, path_to_script=path_to_script,
                     outpath=out_dir)
        ccimage = create_clean_image_from_fits_file(os.path.join(out_dir,
                                                                 im_fits_fname))
        beam = ccimage.beam
        print "Beam for epoch {} : {} [mas, mas, deg]".format(epoch, beam)
        beams_dict.update({epoch: (beam[0], beam[0], 0)})

    circ_beam = np.mean([beam[0] for beam in beams_dict.values()])

    # Now clean and restore with circular beam
    images = list()
    for epoch in sorted(epochs):
        stokes = epoch_stokes_dict[epoch]
        uv_fits_fname = mojave_uv_fits_fname(source, 'u', epoch)
        im_fits_fname = "{}_{}_{}_{}_circ.fits".format(source, 'U', epoch,
                                                       stokes)
        print "Difmap params: "
        print "uv_fits_fname : {}".format(uv_fits_fname)
        print "im_fits_fname : {}".format(im_fits_fname)
        print "path : {}".format(out_dir)
        print "outpath: {}".format(out_dir)
        clean_difmap(uv_fits_fname, im_fits_fname, stokes, imsize,
                     path=out_dir, path_to_script=path_to_script,
                     outpath=out_dir, beam_restore=(circ_beam, circ_beam, 0))
        image = create_image_from_fits_file(os.path.join(out_dir,
                                                         im_fits_fname))
        images.append(image.image.copy())

    images = np.dstack(images)
    return np.mean(images, axis=2)
示例#8
0
        # First, download lowest frequency band and find beam width
        download_mojave_uv_fits(source,
                                epochs=[epoch],
                                bands=[bands[0]],
                                download_dir=data_dir)
        uv_fits_fname = mojave_uv_fits_fname(source, bands[0], epoch)
        cc_fits_fname = "{}_{}_{}_{}_naitive_cc.fits".format(
            source, epoch, bands[0], 'I')
        clean_difmap(uv_fits_fname,
                     cc_fits_fname,
                     'I',
                     mapsize_dict[bands[0]],
                     path=data_dir,
                     path_to_script=path_to_script,
                     outpath=data_dir)
        cc_image = create_clean_image_from_fits_file(
            os.path.join(data_dir, cc_fits_fname))
        image = create_image_from_fits_file(
            os.path.join(data_dir, cc_fits_fname))
        # Beam in mas, mas, deg
        beam = cc_image.beam
        print "BW [mas, mas, deg] : ", beam
        # pixel size in mas
        pixsize = abs(cc_image.pixsize[0]) / mas_to_rad
        # This is characteristic jet width in pixels (half of that)
        width = 0.5 * (beam[0] + beam[1]) / pixsize
        print "BW scale [pixels] : ", width

        # Now find flux at distance > 1 BW from core & maximum flux at core
        len_y = cc_image.imsize[0]
        len_x = cc_image.imsize[1]
        y, x = np.mgrid[-len_y / 2:len_y / 2, -len_x / 2:len_x / 2]
示例#9
0
path_to_script = '/home/ilya/Dropbox/Zhenya/to_ilya/clean/final_clean_nw'
im_path_c1 = os.path.join(base_path, '1458+718.c1.2007_03_01.i.fits')
im_path_x1 = os.path.join(base_path, '1458+718.x1.2007_03_01.i.fits')
map_info_c1 = get_fits_image_info(im_path_c1)
map_info_x1 = get_fits_image_info(im_path_x1)
imsize_c1, pixref_c1, pixrefval_c1, (
    bmaj_c1,
    bmin_c1,
    bpa_c1,
), pixsize_c1, _, _ = map_info_c1
imsize_x1, pixref_x1, pixrefval_x1, (
    bmaj_x1,
    bmin_x1,
    bpa_x1,
), pixsize_x1, _, _ = map_info_x1
image_c1 = create_clean_image_from_fits_file(im_path_c1)
# # Plot real image
# plot(contours=image_c1.image, min_rel_level=0.125, x=image_c1.x[0],
#      y=image_c1.y[:, 0])
#
# mask = create_mask(imsize_c1, (pixref_c1[0], pixref_c1[1], 5, None))
# image_c1._image[mask] = 0.
# # Plot core-cutted image
# plot(contours=image_c1.image, min_rel_level=0.125, x=image_c1.x[0],
#      y=image_c1.y[:, 0])

# Add core shifts from map center
sc1 = (0.03, -0.03)
sx1 = (0.02, -0.02)

ccmodel = create_ccmodel_from_fits_file(im_path_c1)
示例#10
0
文件: zhenya.py 项目: akutkin/SACA
                fname = os.path.join(map_path, "cc_{}.fits".format(i))
                images.add_from_fits(fnames=[fname])
        rotm_image, s_rotm_image = images.create_rotm_image(mask=ppol_mask)
        rotm_images_list.append(rotm_image)

    # Stack ROTM images
    rotm_images_boot = Images()
    rotm_images_boot.add_images(rotm_images_list)
    fig = plt.figure()
    for image in rotm_images_boot.images:
        plt.plot(np.arange(500, 550, 1), image.slice((550, 500), (550, 550)),
                 '.k')

    # Plot I, ROTM image
    i_path = im_fits_path(source, bands[-1], epoch, 'i', base_path=base_path)
    i_image = create_clean_image_from_fits_file(
        os.path.join(i_path, 'cc_orig.fits'))
    # Create original ROTM image
    rotm_images = Images()
    for band in bands:
        for stoke in stokes:
            map_path = im_fits_path(source,
                                    band,
                                    epoch,
                                    stoke,
                                    base_path=base_path)
            fname = os.path.join(map_path, "cc_orig.fits")
            images.add_from_fits(fnames=[fname])
    s_pang_arrays = [pang_error_images_dict[band].image for band in bands]
    rotm_image, s_rotm_image = images.create_rotm_image(
        s_pang_arrays=s_pang_arrays, mask=ppol_mask)
    plot(contours=i_image.image,
示例#11
0
# uvdata = UVData('/home/ilya/Dropbox/ACC/3c120/uvdata/0430+052.u.2006_05_24.uvf')
# uvdata = UVData('/home/ilya/Dropbox/ACC/3c120/uvdata/0430+052.u.2006_05_24.uvf')
model = Model(stokes='I')
model.add_component(icomp)
noise = uvdata.noise(use_V=True)
uvdata.substitute([model])
for bl in noise:
    noise[bl] *= 10
uvdata.noise_add(noise)
uvdata.save(os.path.join(data_dir, '0952_15GHz_BK.fits'))

# clean_difmap('15GHz_BK.fits', 'u_BK_cc.fits', 'I', (1024, 0.1), path=data_dir,
#              path_to_script=path_to_script, show_difmap_output=True,
#              outpath=data_dir)

ccimage = create_clean_image_from_fits_file(os.path.join(data_dir, '0952_15GHz_BK_cc.fits'))
beam = ccimage.beam
rms = rms_image(ccimage)
blc, trc = find_bbox(ccimage.image, rms, 10)
iplot(ccimage.image, x=ccimage.x, y=ccimage.y, min_abs_level=3*rms, beam=beam,
      show_beam=True, blc=blc, trc=trc, core=tuple(p_map))

r = np.array([9.86391978e-01, 6.43996321e-01, 3.53391595e-01])
r_15 = np.array(r) - r[-1]
nu = np.array([5., 8., 15.])
w = np.array([1.38312167e+00, 9.95582942e-01, 5.60287022e-01])

rt = np.array([110, 68, 36])
rt = rt * 0.005
rt_15 = rt - rt[-1]
示例#12
0
        'I', (512, 0.1),
        niter=niter,
        path_to_script=path_to_script,
        outpath=data_dir,
        show_difmap_output=True,
        shift=(1000, 0))

image = create_image_from_fits_file(out_fits_path)
plt.matshow(image.image)
patch = image.image[250:300, 250:300]
_, _, _, C, A = variogram(patch)
# Image already shifted so don't need rms_image_shifted here
rms = rms_image(image)

from image import plot as iplot
ccimage = create_clean_image_from_fits_file(beam_fits_path)
fig = iplot(image.image,
            image.image,
            x=image.x,
            y=image.y,
            show_beam=True,
            min_abs_level=rms,
            cmap='viridis',
            beam=ccimage.beam,
            beam_face_color='black',
            blc=(250, 250),
            trc=(300, 300))
# fig.savefig(os.path.join(data_dir, 'ngc315_niter1000_shifted1000_patch.pdf'),
#             bbox_inches='tight', format='pdf', dpi=1200)

label_size = 14
示例#13
0
                                download_dir=data_dir)
        for epoch in [source_epoch_dict[source]]:
            print "Working with epoch {}".format(epoch)
            fname = mojave_uv_fits_fname(source, 'u', epoch)
            for stokes in ('I', 'Q', 'U'):
                # First restore with naitive beam to get it parameters
                print "Cleaning stokes {} with naitive restoring" \
                      " beam".format(stokes)
                clean_difmap(fname,
                             map_fname(source, epoch, stokes),
                             stokes,
                             mapsize,
                             path=data_dir,
                             path_to_script=path_to_script,
                             outpath=data_dir)
            i_image = create_clean_image_from_fits_file(
                os.path.join(data_dir, map_fname(source, epoch, 'I')))
            imsize = i_image.imsize
            # Get beam parameters
            beam = i_image.beam
            print "Beam : {} [mas, mas, deg]".format(beam)
            circ_beam = (beam[0], beam[0], 0)
            print "Using circular beam {} [mas, mas, deg]".format(circ_beam)

            # Now clean and restore with circular beam
            for stokes in ('I', 'Q', 'U'):
                print "Cleaning stokes {} with circular restoring" \
                      " beam".format(stokes)
                # First restore with naitive beam to get it parameters
                clean_difmap(fname,
                             map_fname(source, epoch, stokes, 'circ'),
                             stokes,
示例#14
0
from knuth_hist import histogram
from utils import percent
import matplotlib
import matplotlib.pyplot as plt


data_dir = '/home/ilya/vlbi_errors/article/0952+179'
shifts = list()
shifts_values = list()
shifts_mask_size = dict()
for i in range(1, 2):
    files = sorted(glob.glob(os.path.join(data_dir,
                                          "cs_boot_*_I_cc_{}.fits".format(str(i).zfill(3)))))
    f15 = files[0]
    f5 = files[1]
    i15 = create_clean_image_from_fits_file(f15)
    i5 = create_clean_image_from_fits_file(f5)
    print("Finding shift between {} & {}".format(os.path.split(f15)[-1], os.path.split(f5)[-1]))
    beam_pxl = int(i15._beam.bmaj)

    shift = find_shift(i15, i5, max_shift=2 * beam_pxl, shift_step=1,
                       max_mask_r=5 * beam_pxl, mask_step=1,
                       upsample_factor=1000)
    shift_value = math.sqrt(shift[0]**2 + shift[1]**2)
    shifts.append(shift)
    shifts_values.append(shift_value)
    print("Found shift : {} with value {}".format(shift, shift_value))
    shifts_mask_size[i] = list()
    print("Found shift dependence on mask size for current bootstrap sample")
    for r in range(0, 100, 5):
        sh = i15.cross_correlate(i5, region1=(i15.x_c, i15.y_c, r, None),
示例#15
0
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))
示例#16
0
def analyze_source(uv_fits_paths,
                   n_boot,
                   imsizes=None,
                   common_imsize=None,
                   common_beam=None,
                   find_shifts=False,
                   outdir=None,
                   path_to_script=None,
                   clear_difmap_logs=True,
                   rotm_slices=None):
    """
    Function that uses multifrequency self-calibration data for in-depth
    analysis.

    :param uv_fits_paths:
        Iterable of paths to self-calibrated uv-data FITS-files.
    :param n_boot:
        Number of bootstrap replications to use in analysis.
    :param imsizes: (optional)
        Iterable of image parameters (imsize, pixsize) that should be used for
        CLEANing of uv-data if no CLEAN-images are supplied. Should be sorted in
        increasing frequency order. If ``None`` then specify parameters by CLEAN
        images. (default: ``None``)
    :param common_imsize: (optional)
        Image parameters that will be used in making common size images for
        multifrequency analysis. If ``None`` then use physical image size of
        lowest frequency and pixel size of highest frequency. (default:
        ``None``)
    :param outdir: (optional)
        Output directory. This directory will be used for saving picture, data,
        etc. If ``None`` then use CWD. (default: ``None``)
    :param path_to_script: (optional)
        Path ot difmap CLEAN script. If ``None`` then use CWD. (default:
        ``None``)

    :notes:
        Workflow:
        1) Чистка с родным разрешением всех N диапазонов и получение родных
        моделей I, Q, U.
        2) Выбор общей ДН из N возможных
        3) (Опционально) Выбор uv-tapering
        4) Чистка uv-данных для всех диапазонов с (опционально применяемым
            uv-tapering) общей ДН
        5) Оценка сдвига ядра
        6) Создание B наборов из N многочастотных симулированных данных
            используя родные модели
        7) (Опционально) Чистка B наборов из N многочастотных симданных с
            родным разрешением для получения карт ошибок I для каждой из N
            частот
        8) Чистка B наборов из N многочастотных симданных для всех диапазонов с
            (опционально применяемым uv-tapering) общей ДН
        9) Оценка ошибки определения сдвига ядра
        10) Оценка RM и ее ошибки
        11) Оценка alpha и ее ошибки

    """

    # Fail early
    if imsizes is None:
        raise Exception("Provide imsizes argument!")
    if common_imsize is not None:
        print("Using common image size {}".format(common_imsize))
    else:
        raise Exception("Provide common_imsize argument!")

    # Setting up the output directory
    if outdir is None:
        outdir = os.getcwd()
    print("Using output directory {}".format(outdir))
    os.chdir(outdir)

    # Assume input self-calibrated uv-data FITS files have different frequencies
    n_freq = len(uv_fits_paths)
    print("Using {} frequencies".format(n_freq))

    # Assuming full multifrequency analysis
    stokes = ('I', 'Q', 'U')

    # Container for original self-calibrated uv-data
    uv_data_dict = dict()
    # Container for original self-calibrated uv-data FITS-file paths
    uv_fits_dict = dict()
    for uv_fits_path in uv_fits_paths:
        uvdata = UVData(uv_fits_path)
        # Mark frequencies by total band center [Hz] for consistency with image.
        uv_data_dict.update({uvdata.band_center: uvdata})
        uv_fits_dict.update({uvdata.band_center: uv_fits_path})

    # Lowest frequency goes first
    freqs = sorted(uv_fits_dict.keys())
    print("Frequencies are: {}".format(freqs))
    # Assert we have original map parameters for all frequencies
    assert len(imsizes) == n_freq

    # Container for original CLEAN-images of self-calibrated uv-data
    cc_image_dict = dict()
    # Container for paths to FITS-files with original CLEAN-images of
    # self-calibrated uv-data
    cc_fits_dict = dict()
    # Container for original CLEAN-image's beam parameters
    cc_beam_dict = dict()
    for freq in freqs:
        cc_image_dict.update({freq: dict()})
        cc_fits_dict.update({freq: dict()})
        cc_beam_dict.update({freq: dict()})

    # 1.
    # Clean original uv-data with specified map parameters
    print("1. Clean original uv-data with specified map parameters...")
    imsizes_dict = dict()
    for i, freq in enumerate(freqs):
        imsizes_dict.update({freq: imsizes[i]})
    for freq in freqs:
        uv_fits_path = uv_fits_dict[freq]
        uv_dir, uv_fname = os.path.split(uv_fits_path)
        for stoke in stokes:
            outfname = '{}_{}_cc.fits'.format(freq, stoke)
            outpath = os.path.join(outdir, outfname)
            clean_difmap(uv_fname,
                         outfname,
                         stoke,
                         imsizes_dict[freq],
                         path=uv_dir,
                         path_to_script=path_to_script,
                         outpath=outdir)
            cc_fits_dict[freq].update({stoke: os.path.join(outdir, outfname)})
            image = create_clean_image_from_fits_file(outpath)
            cc_image_dict[freq].update({stoke: image})
            if stoke == 'I':
                cc_beam_dict.update({freq: image.beam})

    # Containers for images and paths to FITS files with common size images
    cc_cs_image_dict = dict()
    cc_cs_fits_dict = dict()
    # 2.
    # Choose common beam size
    print("2. Choosing common beam size...")
    if common_beam is None:
        common_beam = cc_beam_dict[freqs[0]]
    print("Using common beam [mas, mas, deg] : {}".format(common_beam))

    # 3.
    # Optionally uv-tapering uv-data
    print("3. Optionally uv-tapering uv-data...")
    print("skipping...")

    # 4.
    # Clean original uv-data with common map parameters
    print("4. Clean original uv-data with common map parameters...")
    for freq in freqs:
        cc_cs_image_dict.update({freq: dict()})
        cc_cs_fits_dict.update({freq: dict()})

        uv_fits_path = uv_fits_dict[freq]
        uv_dir, uv_fname = os.path.split(uv_fits_path)
        for stoke in stokes:
            outfname = 'cs_{}_{}_cc.fits'.format(freq, stoke)
            outpath = os.path.join(outdir, outfname)
            # clean_difmap(uv_fname_cc, outfname, stoke, common_imsize,
            #              path=uv_dir, path_to_script=path_to_script,
            #              outpath=outdir, show_difmap_output=False)
            cc_cs_fits_dict[freq].update(
                {stoke: os.path.join(outdir, outfname)})
            image = create_image_from_fits_file(outpath)
            cc_cs_image_dict[freq].update({stoke: image})

    # 5.
    # Optionally find shifts between original CLEAN-images
    print("5. Optionally find shifts between original CLEAN-images...")
    if find_shifts:
        print("Determining images shift...")
        shift_dict = dict()
        freq_1 = freqs[0]
        image_1 = cc_image_dict[freq_1]['I']

        for freq_2 in freqs[1:]:
            image_2 = cc_image_dict[freq_2]['I']
            # Coarse grid of possible shifts
            shift = find_shift(image_1,
                               image_2,
                               100,
                               5,
                               max_mask_r=200,
                               mask_step=5)
            # More accurate grid of possible shifts
            print("Using fine grid for accurate estimate")
            coarse_grid = range(0, 100, 5)
            idx = coarse_grid.index(shift)
            if idx > 0:
                min_shift = coarse_grid[idx - 1]
            else:
                min_shift = 0
            shift = find_shift(image_1,
                               image_2,
                               coarse_grid[idx + 1],
                               1,
                               min_shift=min_shift,
                               max_mask_r=200,
                               mask_step=5)

            shift_dict.update({str((
                freq_1,
                freq_2,
            )): shift})

        # Dumping shifts to json file in target directory
        with open(os.path.join(outdir, "shifts_original.json"), 'w') as fp:
            json.dump(shift_dict, fp)
    else:
        print("skipping...")

    # 6.
    # Bootstrap self-calibrated uv-data with CLEAN-models
    print("6. Bootstrap self-calibrated uv-data with CLEAN-models...")
    uv_boot_fits_dict = dict()
    for freq, uv_fits_path in uv_fits_dict.items():
        # cc_fits_paths = [cc_fits_dict[freq][stoke] for stoke in stokes]
        # bootstrap_uv_fits(uv_fits_path, cc_fits_paths, n_boot, outpath=outdir,
        #                   outname=('boot_{}'.format(freq), '_uv.fits'))
        files = glob.glob(os.path.join(outdir, 'boot_{}*.fits'.format(freq)))
        uv_boot_fits_dict.update({freq: sorted(files)})

    # 7.
    # Optionally clean bootstrap replications with original restoring beams and
    # map sizes to get error estimates for original resolution maps of I, PPOL,
    # FPOL, ...
    print(
        "7. Optionally clean bootstrap replications with original restoring"
        " beams and map sizes...")
    print("skipping...")

    # 8.
    # Optionally clean bootstrap replications with common restoring beams and
    # map sizes
    print(
        "8. Optionally clean bootstrap replications with common restoring"
        " beams and map sizes...")
    cc_boot_fits_dict = dict()
    for freq in freqs:
        cc_boot_fits_dict.update({freq: dict()})
        uv_fits_paths = uv_boot_fits_dict[freq]
        for stoke in stokes:
            for i, uv_fits_path in enumerate(uv_fits_paths):
                uv_dir, uv_fname = os.path.split(uv_fits_path)
                outfname = 'boot_{}_{}_cc_{}.fits'.format(
                    freq, stoke,
                    str(i + 1).zfill(3))
                # clean_difmap(uv_fname_cc, outfname, stoke, common_imsize,
                #              path=uv_dir, path_to_script=path_to_script,
                #              outpath=outdir, show_difmap_output=False)
            files = sorted(
                glob.glob(
                    os.path.join(outdir,
                                 'boot_{}_{}_cc_*.fits'.format(freq, stoke))))
            cc_boot_fits_dict[freq].update({stoke: files})

    # 9. Optionally estimate RM map and it's error
    print("9. Optionally estimate RM map and it's error...")
    original_cs_images = Images()
    for freq in freqs:
        for stoke in stokes:
            original_cs_images.add_images(cc_cs_image_dict[freq][stoke])

    # Find rough mask for creating bootstrap images of RM, alpha, ...
    print("Finding rough mask for creating bootstrap images of RM, alpha, ...")
    cs_mask = pol_mask(
        {stoke: cc_cs_image_dict[freqs[-1]][stoke]
         for stoke in stokes},
        n_sigma=3.)

    rotm_image, _ = original_cs_images.create_rotm_image(mask=cs_mask)

    boot_images = Images()
    fnames = sorted(glob.glob(os.path.join(data_dir, "boot_*_*_cc_*.fits")))
    for freq in freqs:
        for stoke in stokes:
            boot_images.add_from_fits(cc_boot_fits_dict[freq][stoke])
    boot_rotm_images = boot_images.create_rotm_images(mask=cs_mask)
    s_rotm_image = boot_rotm_images.create_error_image(cred_mass=0.95)

    if rotm_slices is not None:
        fnames = [
            'rotm_slice_spread_{}.png'.format(i + 1)
            for i in range(len(rotm_slices))
        ]
        for rotm_slice, fname in zip(rotm_slices, fnames):
            analyze_rotm_slice(rotm_slice,
                               rotm_image,
                               boot_rotm_images,
                               outdir=outdir,
                               outfname=fname)

    # # Calculate simulataneous confidence bands
    # # Bootstrap slices
    # slices = list()
    # for image in rotm_images_sym.images:
    #     slice_ = image.slice((216, 276), (296, 276))
    #     slices.append(slice_[~np.isnan(slice_)])

    # # Find means
    # obs_slice = rotm_image_sym.slice((216, 276), (296, 276))
    # x = np.arange(216, 296, 1)
    # x = x[~np.isnan(obs_slice)]
    # obs_slice = obs_slice[~np.isnan(obs_slice)]
    # # Find sigmas
    # slices_ = [arr.reshape((1, len(obs_slice))) for arr in slices]
    # sigmas = hdi_of_arrays(slices_).squeeze()
    # means = np.mean(np.vstack(slices), axis=0)
    # diff = obs_slice - means
    # # Move bootstrap curves to original simulated centers
    # slices_ = [slice_ + diff for slice_ in slices]
    # # Find low and upper confidence band
    # low, up = create_sim_conf_band(slices_, obs_slice, sigmas,
    #                                alpha=conf_band_alpha)

    # # Plot confidence bands and model values
    # fig = plt.figure()
    # ax = fig.add_subplot(1, 1, 1)
    # ax.plot(x, low[::-1], 'g')
    # ax.plot(x, up[::-1], 'g')
    # [ax.plot(x, slice_[::-1], 'r', lw=0.15) for slice_ in slices_]
    # ax.plot(x, obs_slice[::-1], '.k')
    # # Plot ROTM model
    # ax.plot(np.arange(216, 296, 1),
    #         rotm_grad_value * (np.arange(216, 296, 1) - 256.)[::-1] +
    #         rotm_value_0)
    # fig.savefig(os.path.join(data_dir, 'rotm_slice_spread.png'),
    #             bbox_inches='tight', dpi=200)
    # plt.close()

    if clear_difmap_logs:
        print("Removing difmap log-files...")
        difmap_logs = glob.glob(os.path.join(outdir, "difmap.log*"))
        for difmpa_log in difmap_logs:
            os.unlink(difmpa_log)