Beispiel #1
0
def create_bootstrap_sample(uvdata_dict, ccfits_dict, data_dir, n_boot=10):
    """
    Create ``n_boot`` bootstrap replications of the original UV-data with
    given several Stokes CC-models for each band.

    :param uvdata_dict:
        Dictionary with keys - bands and values - files with uv-data.
    :param ccfits_dict:
        Dictionary with keys - bands, stokes and values - files with CC-fits
        files with models for given band and Stokes.

    Creates ``n_boot`` UV-data files for each band with names
    ``boot_band_i.uvf`` in ``data_dir``.
    """
    print("Bootstrap uv-data with CLEAN-models...")
    for band, uv_fits in uvdata_dict.items():
        uvdata = UVData(os.path.join(data_dir, uv_fits))
        # print("Band = {}".format(band))
        models = list()
        for stokes, cc_fits in ccfits_dict[band].items():
            # print("Stokes = {}".format(stokes))
            ccmodel = create_model_from_fits_file(
                os.path.join(data_dir, cc_fits))
            models.append(ccmodel)

        boot = CleanBootstrap(models, uvdata)
        curdir = os.getcwd()
        os.chdir(data_dir)
        boot.run(n=n_boot,
                 nonparametric=False,
                 use_v=False,
                 use_kde=True,
                 outname=['boot_{}'.format(band), '.uvf'])
        os.chdir(curdir)
Beispiel #2
0
def bootstrap_uvfits_with_difmap_model(
        uv_fits_path,
        dfm_model_path,
        nonparametric=False,
        use_kde=False,
        use_v=False,
        n_boot=100,
        stokes='I',
        boot_dir=None,
        recenter=True,
        pairs=False,
        niter=100,
        bootstrapped_uv_fits=None,
        additional_noise=None,
        boot_mdl_outname_base="bootstrapped_model"):
    dfm_model_dir, dfm_model_fname = os.path.split(dfm_model_path)
    comps = import_difmap_model(dfm_model_fname, dfm_model_dir)
    if boot_dir is None:
        boot_dir = os.getcwd()
    if bootstrapped_uv_fits is None:
        uvdata = UVData(uv_fits_path)
        model = Model(stokes=stokes)
        model.add_components(*comps)
        boot = CleanBootstrap([model],
                              uvdata,
                              additional_noise=additional_noise)
        os.chdir(boot_dir)
        boot.run(nonparametric=nonparametric,
                 use_kde=use_kde,
                 recenter=recenter,
                 use_v=use_v,
                 n=n_boot,
                 pairs=pairs)
        bootstrapped_uv_fits = sorted(
            glob.glob(os.path.join(boot_dir, 'bootstrapped_data*.fits')))
    for j, bootstrapped_fits in enumerate(bootstrapped_uv_fits):
        modelfit_difmap(bootstrapped_fits,
                        dfm_model_fname,
                        '{}_{}.mdl'.format(boot_mdl_outname_base, j),
                        path=boot_dir,
                        mdl_path=dfm_model_dir,
                        out_path=boot_dir,
                        niter=niter)
    booted_mdl_paths = glob.glob(
        os.path.join(boot_dir, '{}*'.format(boot_mdl_outname_base)))

    # Clean uv_fits
    for file_ in bootstrapped_uv_fits:
        os.unlink(file_)
    logs = glob.glob(os.path.join(boot_dir, "*.log*"))
    for file_ in logs:
        os.unlink(file_)
    comms = glob.glob(os.path.join(boot_dir, "*commands*"))
    for file_ in comms:
        os.unlink(file_)

    return booted_mdl_paths
Beispiel #3
0
def bootstrap_uv_fits(uv_fits_path,
                      cc_fits_paths,
                      n,
                      outpath=None,
                      outname=None):
    """
    Function that bootstraps uv-data in user-specified FITS-files and
    FITS-files with clean components.

    :param uv_fits_path:
        Path to fits file with self-calibrated uv-data.
    :param cc_fits_paths:
        Iterable of paths to files with CC models.
    :param n:
        Number of bootstrap realizations.
    :param outpath: (optional)
        Directory to save bootstrapped uv-data FITS-files. If ``None``
        then use CWD. (default: ``None``)
    :param outname: (optional)
        How to name bootstrapped uv-data FITS-files. If ``None`` then
        use default for ``Bootstap.run`` method. (default: ``None``)

    """

    uvdata = UVData(uv_fits_path)

    models = list()
    for cc_fits_path in cc_fits_paths:
        ccmodel = create_model_from_fits_file(cc_fits_path)
        models.append(ccmodel)

    boot = CleanBootstrap(models, uvdata)
    if outpath is not None:
        if not os.path.exists(outpath):
            os.makedirs(outpath)
    curdir = os.getcwd()
    os.chdir(outpath)
    boot.run(n=n,
             outname=outname,
             nonparametric=False,
             use_v=False,
             use_kde=True)
    os.chdir(curdir)
Beispiel #4
0
    def bootstrap_uvdata(self):
        print("Bootstrap self-calibrated uv-data with CLEAN-models...")
        for freq, uv_fits_path in self.uvfits_dict.items():

            # Check if it is already done
            files = glob.glob(
                os.path.join(self.data_dir, 'boot_{}*.uvf'.format(freq)))
            # If number of found files doesn't equal to ``n_boot`` - remove them
            if not len(files) == self.n_boot:
                for file in files:
                    os.unlink(file)

                # and bootstrap current frequency again
                cc_fits_paths = [
                    self.cc_fits_dict[freq][stokes] for stokes in self.stokes
                ]
                uvdata = self.uvdata_dict[freq]

                models = list()
                for cc_fits_path in cc_fits_paths:
                    ccmodel = create_model_from_fits_file(cc_fits_path)
                    models.append(ccmodel)

                boot = CleanBootstrap(models, uvdata)
                curdir = os.getcwd()
                os.chdir(self.data_dir)
                boot.run(n=self.n_boot,
                         nonparametric=False,
                         use_v=False,
                         use_kde=True,
                         outname=['boot_{}'.format(freq), '.uvf'])
                os.chdir(curdir)

                files = glob.glob(
                    os.path.join(self.data_dir, 'boot_{}*.uvf'.format(freq)))
            print("Found bootstraped uvdata files!")
            self.uvfits_boot_dict.update({freq: sorted(files)})
Beispiel #5
0
import os
from uv_data import UVData
from model import Model
from spydiff import import_difmap_model
from bootstrap import CleanBootstrap

data_dir = '/home/ilya/code/vlbi_errors/tests/ft'
uv_fits = '1308+326.U1.2009_08_28.UV_CAL'
uvdata = UVData(os.path.join(data_dir, uv_fits))
model = Model(stokes='I')
comps = import_difmap_model('1308+326.U1.2009_08_28.mdl', data_dir)
model.add_components(*comps)
boot = CleanBootstrap([model], uvdata)
fig = boot.data.uvplot()
boot.model_data.uvplot(fig=fig, color='r')
# boot.find_outliers_in_residuals()
# boot.find_residuals_centers(split_scans=False)
# boot.fit_residuals_kde(split_scans=False, combine_scans=False,
#                        recenter=True)
Beispiel #6
0
data_dir = '/home/ilya/vlbi_errors/examples/coverage/1749+701/errors'
# uv_fits = '1633+382.l22.2010_05_21.uvf'
# im_fits = '1633+382.l22.2010_05_21.icn.fits'
# uv_fits = '1226+023.u.2006_03_09.uvf'
uv_fits = '1749+701.x.2006_04_05.uvf'
# im_fits = '3c273_orig_cc.fits'
im_fits = '1749+701.x.2006_04_05.cc.fits'
clean_difmap(uv_fits,
             im_fits,
             'I', (512, 0.1),
             path=data_dir,
             path_to_script=path_to_script,
             outpath=data_dir)
model = create_model_from_fits_file(os.path.join(data_dir, im_fits))
uvdata = UVData(os.path.join(data_dir, uv_fits))
boot = CleanBootstrap([model], uvdata)
os.chdir(data_dir)
boot.run(100, nonparametric=False, use_v=False)
booted_uv_fits = glob.glob(os.path.join(data_dir, 'bootstrapped_data*.fits'))
for i, boot_uv in enumerate(sorted(booted_uv_fits)):
    clean_difmap(boot_uv,
                 '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()
Beispiel #7
0
def coverage_of_model(original_uv_fits,
                      original_mdl_file,
                      outdir=None,
                      n_cov=100,
                      n_boot=300,
                      mapsize=(1024, 0.1),
                      path_to_script=None):
    """
    Conduct coverage analysis of uv-data & model

    :param original_uv_fits:
        Self-calibrated uv-fits file.
    :param original_mdl_file:
        Difmap txt-file with model.
    :param outdir:
        Output directory to store results.
    :param n_cov:
        Number of samples to create.
    """
    # Create sample of 100 uv-fits data & models
    sample_uv_fits_paths, sample_model_paths = create_sample(original_uv_fits,
                                                             original_mdl_file,
                                                             outdir=outdir,
                                                             n_sample=n_cov)

    # For each sample uv-fits & model find 1) conventional errors & 2) bootstrap
    # errors
    for j, (sample_uv_fits_path, sample_mdl_path) in enumerate(
            zip(sample_uv_fits_paths, sample_model_paths)):
        sample_uv_fits, dir = os.path.split(sample_uv_fits_path)
        sample_mdl_file, dir = os.path.split(sample_mdl_path)
        try:
            comps = import_difmap_model(sample_mdl_file, dir)
        except ValueError:
            print('Problem import difmap model')
        model = Model(stokes='I')
        model.add_components(*comps)

        # Find errors by using Fomalont way
        # 1. Clean uv-data
        clean_difmap(sample_uv_fits,
                     'sample_cc_{}.fits'.format(j),
                     'I',
                     mapsize,
                     path=dir,
                     path_to_script=path_to_script,
                     outpath=dir)
        # 2. Get beam
        ccimage = create_clean_image_from_fits_file(
            os.path.join(dir, 'sample_cc_{}.fits'.format(j)))
        beam = ccimage.beam_image

        # 2. Subtract components convolved with beam
        ccimage.substract_model(model)

        # Find errors by using Lee way
        # a) fit uv-data and find model
        # b) CLEAN uv-data
        # c) substract model from CLEAN image
        # d) find errors
        pass

        # Find errors by using bootstrap
        # FT model to uv-plane
        uvdata = UVData(sample_uv_fits_path)
        try:
            boot = CleanBootstrap([model], uvdata)
        # If uv-data contains only one Stokes parameter (e.g. `0838+133`)
        except IndexError:
            print('Problem bootstrapping')
        curdir = os.getcwd()
        os.chdir(dir)
        boot.run(n=n_boot, nonparametric=True, outname=[outname, '.fits'])
        os.chdir(curdir)

        booted_uv_paths = sorted(
            glob.glob(os.path.join(data_dir, outname + "*")))
        # Modelfit bootstrapped uvdata
        for booted_uv_path in booted_uv_paths:
            path, booted_uv_file = os.path.split(booted_uv_path)
            i = booted_uv_file.split('_')[-1].split('.')[0]
            modelfit_difmap(booted_uv_file,
                            dfm_model_fname,
                            dfm_model_fname + '_' + i,
                            path=path,
                            mdl_path=data_dir,
                            out_path=data_dir)

        # Get params of initial model used for bootstrap
        comps = import_difmap_model(dfm_model_fname, data_dir)
        comps_params0 = {i: [] for i in range(len(comps))}
        for i, comp in enumerate(comps):
            comps_params0[i].extend(list(comp.p))

        # Load bootstrap models
        booted_mdl_paths = glob.glob(
            os.path.join(data_dir, dfm_model_fname + "_*"))
        comps_params = {i: [] for i in range(len(comps))}
        for booted_mdl_path in booted_mdl_paths:
            path, booted_mdl_file = os.path.split(booted_mdl_path)
            comps = import_difmap_model(booted_mdl_file, path)
            for i, comp in enumerate(comps):
                comps_params[i].extend(list(comp.p))

        # Print 65-% intervals (1 sigma)
        for i, comp in enumerate(comps):
            errors_fname = '68_{}_{}_comp{}.txt'.format(source, last_epoch, i)
            fn = open(os.path.join(data_dir, errors_fname), 'w')
            print "Component #{}".format(i + 1)
            for j in range(len(comp)):
                low, high, mean, median = hdi_of_mcmc(np.array(
                    comps_params[i]).reshape((n_boot, len(comp))).T[j],
                                                      cred_mass=0.68,
                                                      return_mean_median=True)
                fn.write("{} {} {} {} {}".format(comp.p[j], low, high, mean,
                                                 median))
                fn.write("\n")
            fn.close()

    # For source in sources with component close to core
    # 1. Find residuals or estimate noise
    # 2. N times add resampled residuals (or just gaussian noise) to model and
    # create N new datasets
    # 3. Fit them using difmap.
    # 4. Find errors using Fomalont, Yee and using bootstrap. Check coverage.
    base_dir = '/home/ilya/vlbi_errors/model_cov'
    n_boot = 300
    outname = 'boot_uv'
    names = [
        'source', 'id', 'trash', 'epoch', 'flux', 'r', 'pa', 'bmaj', 'e', 'bpa'
    ]
    df = pd.read_table(os.path.join(base_dir, 'asu.tsv'),
                       sep=';',
                       header=None,
                       names=names,
                       dtype={key: str
                              for key in names},
                       index_col=False)

    # Mow for all sources get the latest epoch and create directory for analysis
    for source in df['source'].unique():
        epochs = df.loc[df['source'] == source]['epoch']
        last_epoch_ = list(epochs)[-1]
        last_epoch = last_epoch_.replace('-', '_')
        data_dir = os.path.join(base_dir, source, last_epoch)
        if not os.path.exists(data_dir):
            os.makedirs(data_dir)
        try:
            download_mojave_uv_fits(source,
                                    epochs=[last_epoch],
                                    bands=['u'],
                                    download_dir=data_dir)
        except:
            open(
                'problem_download_from_mojave_{}_{}'.format(
                    source, last_epoch), 'a').close()
            continue
        uv_fits_fname = mojave_uv_fits_fname(source, 'u', last_epoch)

        # Create instance of Model and bootstrap uv-data
        dfm_model_fname = 'dfmp_original_model.mdl'
        fn = open(os.path.join(data_dir, dfm_model_fname), 'w')
        model_df = df.loc[np.logical_and(df['source'] == source,
                                         df['epoch'] == last_epoch_)]
        for (flux, r, pa, bmaj, e, bpa) in np.asarray(
                model_df[['flux', 'r', 'pa', 'bmaj', 'e', 'bpa']]):
            print flux, r, pa, bmaj, e, bpa
            if not r.strip(' '):
                r = '0.0'
            if not pa.strip(' '):
                pa = '0.0'

            if not bmaj.strip(' '):
                bmaj = '0.0'
            if not e.strip(' '):
                e = "1.0"

            if np.isnan(float(bpa)):
                bpa = "0.0"
            else:
                bpa = bpa + 'v'

            if bmaj == '0.0':
                type_ = 0
                bpa = "0.0"
            else:
                bmaj = bmaj + 'v'
                type_ = 1
            fn.write("{}v {}v {}v {} {} {} {} {} {}".format(
                flux, r, pa, bmaj, e, bpa, type_, "0", "0\n"))
        fn.close()
Beispiel #8
0
def create_coverage_map(original_uv_fits_path,
                        ci_type,
                        original_cc_fits_path=None,
                        imsize=None,
                        outdir=None,
                        n_boot=200,
                        path_to_script=None,
                        alpha=0.68,
                        n_cov=100,
                        n_rms=1.,
                        stokes='I',
                        boot_cc_fits_paths=None,
                        sample_cc_fits_paths=None):
    """
    Conduct coverage analysis of image pixels flux CI. Find number of times
    when CI of `observed` value contains values of `samples`.

    :param original_uv_fits_path:
        Path to original FITS-file with uv-data.
    :param ci_type:
        Type of CI to test. ``boot`` or ``rms``. If ``boot`` then use residuals
        bootstrap CI. If ``rms`` then use Hovatta corrected image rms CI.
    :param original_cc_fits_path: (optional)
        Path to original FITS-file with CC model. If ``None`` then use
        ``imsize`` parameter to get `original` CC model from
        ``original_uv_fits_path``. (default: ``None``)
    :param imsize: (optional)
        Image parameters (image size [pix], pixel size [mas]) to use
        when doing first CC with ``original_cc_fits_path = None``. (default:
        ``None``)
    :param outdir: (optional)
        Directory to store intermediate results. If ``None`` then use CWD.
        (default: ``None``)
    :param n_boot: (optional)
        Number of bootstrap replications to use when calculating bootstrap CI
        for ``ci_type = boot`` option when ``boot_cc_fits_paths`` hasn't
        specified. (default: ``200``)
    :param path_to_script: (optional)
        Path to Dan Homan's script for final clean. If ``None`` then use CWD.
        (default: ``None``)
    :param alpha: (optional)
        Level of significance when calculating bootstrap CI for ``ci_type =
        boot`` case. E.g. ``0.68`` corresponds to `1 \sigma`. (default:
        ``0.68``)
    :param n_cov: (optional)
        Number of `samples` from infinite population to consider in coverage
        analysis of intervals. Here `samples` - observations of known source
        with different realisations of noise with known parameters. (default:
         ``100``)
    :param n_rms: (optional)
        Number of rms to use in ``ci_type = rms`` case. (default: ``1.``)
    :param stokes: (optional)
        Stokes parameter to use. If ``None`` then use ``I``. (default: ``None``)
    :param boot_cc_fits_paths: (optional)
        If ``ci_type = boot`` then this parameter could specify paths to cleaned
        bootstrapped uv-data.
    :param sample_cc_fits_paths: (optional)
        Path to FITS-files with CLEAN models of `sample` uv-data. If ``None``
        then create ``n_cov`` `sample` uv-data from noise of `original` uv-data
        and `original` CLEAN model. (default: ``None``)

    :return:
        Coverage map. Each pixel contain frequency of times when samples from
        population hit inside CI for given pixel.

    """

    # If not given `original` CLEAN model - get it by cleaning `original`
    # uv-data
    if original_cc_fits_path is None:
        print(
            "No `original` CLEAN model specified! Will CLEAN `original`"
            " uv-data.")
        if imsize is None:
            raise Exception("Specify ``imsize``")
        uv_fits_dir, uv_fits_fname = os.path.split(original_uv_fits_path)
        print("Cleaning `original` uv-data to"
              " {}".format(os.path.join(outdir, 'cc.fits')))
        clean_difmap(uv_fits_fname,
                     'cc.fits',
                     stokes,
                     imsize,
                     path=uv_fits_dir,
                     path_to_script=path_to_script,
                     outpath=outdir)
        original_cc_fits_path = os.path.join(outdir, 'cc.fits')

    original_uv_data = UVData(original_uv_fits_path)
    noise = original_uv_data.noise()
    original_model = create_model_from_fits_file(original_cc_fits_path)
    # Find images parameters for cleaning if necessary
    if imsize is None:
        print(
            "Getting image parameters from `original`"
            " CLEAN FITS file {}.".format(original_cc_fits_path))
        image_params = get_fits_image_info(original_cc_fits_path)
        imsize = (image_params['imsize'][0],
                  abs(image_params['pixsize'][0]) / mas_to_rad)

    # Substitute uv-data with original model and create `model` uv-data
    print("Substituting original uv-data with CLEAN model...")
    model_uv_data = copy.deepcopy(original_uv_data)
    model_uv_data.substitute([original_model])

    # Add noise to `model` uv-data to get `observed` uv-data
    observed_uv_data = copy.deepcopy(model_uv_data)
    observed_uv_data.noise_add(noise)
    observed_uv_fits_path = os.path.join(outdir, 'observed_uv.uvf')
    if os.path.isfile(observed_uv_fits_path):
        os.unlink(observed_uv_fits_path)
    print("Adding noise to `model` uv-data to get `observed` uv-data...")
    observed_uv_data.save(fname=observed_uv_fits_path)

    observed_cc_fits_path = os.path.join(outdir, 'observed_cc.fits')
    if os.path.isfile(observed_cc_fits_path):
        os.unlink(observed_cc_fits_path)
    # Clean `observed` uv-data to get `observed` image and model
    print("Cleaning `observed` uv-data to `observed` CLEAN model...")
    clean_difmap('observed_uv.uvf',
                 'observed_cc.fits',
                 original_model.stokes,
                 imsize,
                 path=outdir,
                 path_to_script=path_to_script,
                 outpath=outdir)
    # Get `observed` model and image
    observed_model = create_model_from_fits_file(observed_cc_fits_path)
    observed_image = create_image_from_fits_file(observed_cc_fits_path)

    # Testing coverage of bootstrapped CI
    if ci_type == 'boot':
        # Bootstrap and clean only when necessary
        if boot_cc_fits_paths is None:
            # Bootstrap `observed` uv-data with `observed` model
            boot = CleanBootstrap([observed_model], observed_uv_data)
            cwd = os.getcwd()
            path_to_script = path_to_script or cwd
            os.chdir(outdir)
            print("Bootstrapping uv-data with {} replications".format(n_boot))
            boot.run(outname=['observed_uv_boot', '.uvf'], n=n_boot)
            os.chdir(cwd)

            boot_uv_fits_paths = sorted(
                glob.glob(os.path.join(outdir, 'observed_uv_boot*.uvf')))
            # Clean each bootstrapped uv-data
            for i, uv_fits_path in enumerate(boot_uv_fits_paths):
                uv_fits_dir, uv_fits_fname = os.path.split(uv_fits_path)
                print("Cleaning {} bootstrapped observed"
                      " uv-data to {}".format(
                          uv_fits_path,
                          os.path.join(
                              outdir,
                              'observed_cc_boot_{}.fits'.format(i + 1))))
                clean_difmap(uv_fits_fname,
                             'observed_cc_boot_{}.fits'.format(i + 1),
                             original_model.stokes,
                             imsize,
                             path=uv_fits_dir,
                             path_to_script=path_to_script,
                             outpath=outdir)

            boot_cc_fits_paths = glob.glob(
                os.path.join(outdir, 'observed_cc_*.fits'))

        # Calculate bootstrap CI
        # hdi_low, hdi_high = boot_ci_bc(boot_cc_fits_paths,
        #                                observed_cc_fits_path, alpha=alpha)
        hdi_low, hdi_high = boot_ci(boot_cc_fits_paths,
                                    observed_cc_fits_path,
                                    alpha=alpha)
    elif ci_type == 'rms':
        # Calculate ``n_rms`` CI
        rms = observed_image.rms(region=(50, 50, 50, None))
        rms = np.sqrt(rms**2. + (1.5 * rms**2.)**2.)
        hdi_low = observed_image.image - rms
        hdi_high = observed_image.image + rms
    else:
        raise Exception("CI intervals must be `boot` or `rms`!")

    # Create `sample` uv-data and clean it only when necessary
    if sample_cc_fits_paths is None:
        # Add noise to `model` uv-data ``n_cov`` times and get ``n_cov``
        # `samples` from population
        sample_uv_fits_paths = list()
        for i in range(n_cov):
            sample_uv_data = copy.deepcopy(model_uv_data)
            sample_uv_data.noise_add(noise)
            sample_uv_fits_path = os.path.join(outdir,
                                               'samle_uv_{}.uvf'.format(i + 1))
            sample_uv_data.save(sample_uv_fits_path)
            sample_uv_fits_paths.append(sample_uv_fits_path)

        # Clean each `sample` FITS-file
        for i, uv_fits_path in enumerate(sample_uv_fits_paths):
            uv_fits_dir, uv_fits_fname = os.path.split(uv_fits_path)
            print("Cleaning {} sample uv-data to"
                  " {}".format(
                      uv_fits_path,
                      os.path.join(outdir, 'sample_cc_{}.fits'.format(i + 1))))
            clean_difmap(uv_fits_fname,
                         'sample_cc_{}.fits'.format(i + 1),
                         original_model.stokes,
                         imsize,
                         path=uv_fits_dir,
                         path_to_script=path_to_script,
                         outpath=outdir)

        sample_cc_fits_paths = glob.glob(
            os.path.join(outdir, 'sample_cc_*.fits'))

    sample_images = list()
    for sample_cc_fits_path in sample_cc_fits_paths:
        image = create_image_from_fits_file(sample_cc_fits_path)
        sample_images.append(image.image)

    # For each pixel check how often flux in `sample` images lies in CI derived
    # for observed image.
    cov_array = np.zeros((imsize[0], imsize[0]), dtype=float)
    print("calculating CI intervals")
    for (x, y), value in np.ndenumerate(cov_array):
        for image in sample_images:
            cov_array[x, y] += float(
                np.logical_and(hdi_low[x, y] < image[x, y],
                               image[x, y] < hdi_high[x, y]))

    return cov_array / n_cov
Beispiel #9
0
# Clean original uv-data with common beam
clean_difmap(uv_fits_x, 'x_cc_same.fits', 'I', (1024, 0.1), path=uvdata_dir,
             path_to_script=path_to_script, show_difmap_output=True,
             outpath=data_dir)
ccimage_x = create_clean_image_from_fits_file(os.path.join(data_dir, 'x_cc.fits'))
clean_difmap(uv_fits_u, 'u_cc_same.fits', 'I', (1024, 0.1), path=uvdata_dir,
             path_to_script=path_to_script, show_difmap_output=True,
             outpath=data_dir, beam_restore=ccimage_x.beam)

u_model = create_model_from_fits_file(os.path.join(data_dir, 'u_cc.fits'))
x_model = create_model_from_fits_file(os.path.join(data_dir, 'x_cc.fits'))
u_uvdata = UVData(os.path.join(uvdata_dir, uv_fits_u))
x_uvdata = UVData(os.path.join(uvdata_dir, uv_fits_x))

# Bootstrap uv-data with original CLEAN models
xboot = CleanBootstrap([x_model], x_uvdata)
xboot.run(100, nonparametric=True, use_v=False, outname=['boot_x', '.fits'])
uboot = CleanBootstrap([u_model], u_uvdata)
uboot.run(100, nonparametric=True, use_v=False, outname=['boot_u', '.fits'])

# Clean bootstrapped uv-data with common parameters
x_boot_uvfits = sorted(glob.glob('boot_x_*.fits'))
u_boot_uvfits = sorted(glob.glob('boot_u_*.fits'))
for i, x_boot_uv in enumerate(x_boot_uvfits):
    clean_difmap(x_boot_uv, 'x_cc_same_{}.fits'.format(str(i+1).zfill(3)), 'I',
                 (1024, 0.1),
                 path_to_script=path_to_script, show_difmap_output=True,
                 outpath=data_dir, beam_restore=ccimage_x.beam)
for i, u_boot_uv in enumerate(u_boot_uvfits):
    clean_difmap(u_boot_uv, 'u_cc_same_{}.fits'.format(str(i+1).zfill(3)), 'I',
                 (1024, 0.1),
Beispiel #10
0
def generate_boot_data(sources,
                       epochs,
                       bands,
                       stokes,
                       n_boot=10,
                       base_path=None):
    """
    :param sources:
        Iterable of sources names.
    :param epochs:
        Iterable of sources epochs.
    :param bands:
        Iterable of bands.
    :param stokes:
        Iterable of stokes parameters.
    :param n_boot: (optional)
        Number of bootstrap replications to create. (default: ``10``)
    :param base_path: (optional)
        Path to route of directory tree. If ``None`` then use current directory.

    """
    if base_path is None:
        base_path = os.getcwd()
    elif not base_path.endswith("/"):
        base_path += "/"

    curdir = os.getcwd()
    print "Generating bootstrapped data..."
    for source in sources:
        print " for source ", source
        for epoch in epochs:
            print " for epoch ", epoch
            for band in bands:
                print " for band ", band
                uv_path = uv_fits_path(source,
                                       band.upper(),
                                       epoch,
                                       base_path=base_path)
                uv_fname = uv_path + 'sc_uv.fits'
                if not os.path.isfile(uv_fname):
                    print "...skipping absent file ", uv_fname
                    continue
                print "  Using uv-file (data): ", uv_fname
                uvdata = UVData(uv_fname)
                models = list()
                for stoke in stokes:
                    print "  Adding model with stokes parameter ", stoke
                    map_path = im_fits_path(source,
                                            band,
                                            epoch,
                                            stoke,
                                            base_path=base_path)
                    map_fname = map_path + 'cc.fits'
                    print "  from CC-model file ", map_fname
                    ccmodel = create_model_from_fits_file(map_fname,
                                                          stokes=stoke.upper())
                    models.append(ccmodel)
                boot = CleanBootstrap(models, uvdata)
                os.chdir(uv_path)
                boot.run(n=n_boot, outname=['boot', '.fits'])
    os.chdir(curdir)
Beispiel #11
0
path_to_script = '/home/ilya/Dropbox/Zhenya/to_ilya/clean/final_clean_nw'
data_dir = '/home/ilya/sandbox/modelfit/2models'
uv_fname = '1226+023.q1.2009_08_16.uvp'
mdl_fname = '1226+023.q1.2009_08_16.mdl'
outname = 'boot_uv'
n = 300


if __name__ == '__main__':

    uvdata = UVData(os.path.join(data_dir, uv_fname))
    model = Model(stokes='I')
    comps = import_difmap_model(mdl_fname, data_dir)
    model.add_components(*comps)
    boot = CleanBootstrap([model], uvdata)
    curdir = os.getcwd()
    os.chdir(data_dir)
    boot.run(n=n, nonparametric=True, outname=[outname, '.fits'])
    os.chdir(curdir)

    # # Radplot uv-data and model
    # comps = import_difmap_model(mdl_fname, data_dir)
    # uvdata.uvplot(style='a&p')
    # uvdata.substitute([model])
    # uvdata.uvplot(style='a&p', sym='.r')

    # # Radplot residuals
    # uvdata_ = create_uvdata_from_fits_file(os.path.join(data_dir, uv_fname_cc))
    # res_uvdata = uvdata_ - uvdata
    # res_uvdata.uvplot(style='re&im')