Ejemplo n.º 1
0
def savetxt_gaia_pm_nstar(dict_joint):
    pmra, pmdec, nrh, nbg = [], [], [], []
    pmra2rh, pmdec2rh, n2rh = [], [], []
    for i, name in enumerate(dict_joint['GalaxyName']):
        ra, dec = dict_joint['RA_deg'][i], dict_joint['Dec_deg'][i]
        dist = dict_joint['Distance_pc'][i]
        rh = dict_joint['rh(arcmins)'][i] / 60.
        width = 4. * rh
        database = 'gaia_dr2.gaia_source'
        cat_str = """ ra, dec, pmra, pmdec,
                      phot_g_mean_mag, astrometric_excess_noise """

        Patch = PatchMWSatellite(name, ra, dec, dist, width, database, cat_str)
        Patch.sql_get(HOST, USER, PASSWORD)
        Patch.mask_cut("phot_g_mean_mag", 17, 21)
        Patch.mask_g_mag_astro_noise_cut()

        mask2rh = dist2(Patch.datas['ra'], Patch.datas['dec'], ra, dec) <= (2.*rh)**2
        Patch.cut_datas(mask2rh)
        _n2rh = len(Patch.datas['pmra'])
        _pmra = np.nanmean(Patch.datas['pmra'])
        _pmdec = np.nanmean(Patch.datas['pmdec'])
        n2rh.append(_n2rh)
        pmra2rh.append(_pmra)
        pmdec2rh.append(_pmdec)

        maskrh = dist2(Patch.datas['ra'], Patch.datas['dec'], ra, dec) <= rh**2
        Patch.cut_datas(maskrh)
        _nrh = len(Patch.datas['pmra'])
        _pmra = np.nanmean(Patch.datas['pmra'])
        _pmdec = np.nanmean(Patch.datas['pmdec'])

        nrh.append(_nrh)
        pmra.append(_pmra)
        pmdec.append(_pmdec)

        _den = (_n2rh - _nrh) / np.pi / 3. / rh**2
        nbg.append(_den)    # Nbg / deg^2

    dict_joint['Nstar_rh'] = np.array(nrh)
    dict_joint['pmra_rh'] = np.array(pmra)
    dict_joint['pmdec_rh'] = np.array(pmdec)
    dict_joint['Nstar_2rh'] = np.array(n2rh)
    dict_joint['pmra_2rh'] = np.array(pmra2rh)
    dict_joint['pmdec_2rh'] = np.array(pmdec2rh)

    df = pd.DataFrame(dict_joint)
    df.to_csv('dwarf-csvs/dwarfs_pms.csv')



    dict_joint['Nbg_per_deg2'] = np.array(nbg)
    dict_joint['rh_deg'] = dict_joint['rh(arcmins)'] / 60.
    dict_joint['Nstar_per_rhdeg3'] = dict_joint['Nstar_rh'] / dict_joint['rh_deg']**3

    df = pd.DataFrame(dict_joint)
    df.to_csv('dwarf-csvs/dwarfs_detail.csv')
Ejemplo n.º 2
0
    def mask_pm_error(self, pmra0: float, pmdec0: float, n_error: int):
        """ Hard code the pm cut: dist(pm, pm_dwarf) < n_error * pm_error

        : pmra0 : pmra of the dwarf
        : pmdec0 : pmdec of the dwarf
        : n_error : select sources withing n_error errorbar
        """
        print("Applying proper motion cut with %d" % n_error)
        pmdist = dist2(self.datas['pmra'], self.datas['pmdec'], pmra0, pmdec0)
        pmdist = np.sqrt(pmdist)
        pmerror = dist2(self.datas['pmra_error'], self.datas['pmdec_error'], 0,
                        0)
        pmerror = np.sqrt(pmerror)
        mask = pmdist <= n_error * pmerror
        self.cut_datas(mask)
        print("    %d sources left \n" % self.n_source())
Ejemplo n.º 3
0
    def append_is_inside(self, ra_df: float, dec_df: float, radius: float):
        """ Assign a boolean value to specify if a source is in the area
        within the radius from the dwarf.

        : ra_df : ra of the dwarf
        : dec_df : dec of the dwarf
        : radius : the radius telling inside or outside
        """
        _dist2 = dist2(self.datas['ra'], self.datas['dec'], ra_df, dec_df)
        self.datas['is_inside'] = np.array(_dist2 < radius**2)
        print('Appended a boolean array telling is_inside. \n')
    def add_masks_on_pixels(self, ra_df: float, dec_df: float, radius: float):
        """ Get histogram 2d for the star distribution on the mesh

        : ra_df : ra of the dwarf
        : dec_df : dec of the dwarf
        : radius : the radius telling inside or outside
        """
        x2d, y2d = np.meshgrid(self.x_mesh, self.y_mesh)
        # reshape the array to be like hist2d
        x2d = 0.5 * (x2d[1:, 1:] + x2d[:-1, :-1])
        y2d = 0.5 * (y2d[1:, 1:] + y2d[:-1, :-1])
        _dist2 = dist2(x2d, y2d, ra_df, dec_df)
        self.is_inside_dwarf = _dist2 < radius**2
        print('Added a mask array telling if pixels are inside the dwarf. \n')
        self.is_overlap = _dist2 < (radius + self.sigma3)**2
        self.is_overlap = self.is_overlap & (~self.is_inside_dwarf)
        print(
            'Added a mask array telling if pixels overlap the dwarf and the outer aperture. \n'
        )
Ejemplo n.º 5
0
def plot_hips_sky_image(ra: float, dec: float, sig_p: float, sigma1: float,
                        hips_surveys: List, outpath: str, name: str,
                        label: int, res: int):
    """ Plot sky image using hips

    : ra : ra of the pixel
    : dec : dec of the pixel
    : sig_p : sig_poisson of the pixel
    : sigma1 : sigma1 of the map
    : hips_surveys : list of surveys
    : outpath : output dir path
    : name : name of the system (dwarf and more info)
    : label : label of a cluster pixels
    : res : resolution of the image (number of pixels for the image)
    """
    width = WIDTH_FAC * sigma1

    # Compute the sky image
    geometry = WCSGeometry.create(skydir=SkyCoord(ra,
                                                  dec,
                                                  unit='deg',
                                                  frame='icrs'),
                                  width=res,
                                  height=res,
                                  fov="%f deg" % width,
                                  coordsys='icrs',
                                  projection='AIT')

    name_split = name.split('-')
    short_name = name_split[0]
    data = np.load('results/{}'.format(
        name.replace('-poisson' or '-gaussian', '/queried-data.npy'))).item()

    ra_data, dec_data = data['ra'], data['dec']
    pmra_data, pmdec_data = data['pmra'], data['pmdec']
    bp_rp_data, g_mag_data = data['bp_rp'], data['phot_g_mean_mag']

    data = None  # free memory

    ra_min = ra - 0.5 * width
    ra_max = ra + 0.5 * width
    dec_min = dec - 0.5 * width
    dec_max = dec + 0.5 * width

    mask1 = (ra_min < ra_data) & (ra_data < ra_max)
    mask2 = (dec_min < dec_data) & (dec_data < dec_max)
    mask = mask1 & mask2

    ra_data, dec_data = ra_data[mask], dec_data[mask]
    pmra_data, pmdec_data = pmra_data[mask], pmdec_data[mask]
    bp_rp_data, g_mag_data = bp_rp_data[mask], g_mag_data[mask]

    is_in = dist2(ra_data, dec_data, ra, dec) <= sigma1**2
    n_star_in = len(ra_data[is_in])

    if n_star_in < NSTAR_MIN:
        _s1 = 'skipping image for %s ' % short_name
        _s2 = 'because there are only %d stars in the kernel' % n_star_in
        print(_s1 + _s2)
        return  # skip plotting image with fewer than NSTAR_MIN stars

    print('plotting image for %s' % short_name)
    sns.set(style="white", color_codes=True, font_scale=1)

    if 'gaia' in short_name:
        fig, axes = plt.subplots(2, 3, figsize=(15, 10))

        name_hue = ['inside' if i else 'outside' for i in is_in]
        ms = 20

        circle = plt.Circle((ra, dec),
                            sigma1,
                            color='k',
                            fill=False,
                            lw=2,
                            alpha=0.6)
        axes[0, 0].add_artist(circle)

        sns.scatterplot(x=ra_data,
                        y=dec_data,
                        s=ms,
                        hue=name_hue,
                        ax=axes[0, 0])
        sns.scatterplot(x=bp_rp_data,
                        y=g_mag_data,
                        s=ms,
                        hue=name_hue,
                        ax=axes[0, 1])
        sns.scatterplot(x=pmra_data,
                        y=pmdec_data,
                        s=ms,
                        hue=name_hue,
                        ax=axes[0, 2])

        sns.scatterplot(x=ra_data[is_in],
                        y=dec_data[is_in],
                        s=ms,
                        ax=axes[0, 0])
        sns.scatterplot(x=bp_rp_data[is_in],
                        y=g_mag_data[is_in],
                        s=ms,
                        ax=axes[0, 1])
        sns.scatterplot(x=pmra_data[is_in],
                        y=pmdec_data[is_in],
                        s=ms,
                        ax=axes[0, 2])

        axes[0, 0].set_title('stellar distribution')
        axes[0, 0].set_xlabel('ra (deg)')
        axes[0, 0].set_ylabel('dec (deg)')
        axes[0, 0].set_xlim([ra_min, ra_max])
        axes[0, 0].set_ylim([dec_min, dec_max])
        axes[0, 0].invert_xaxis()

        axes[0, 1].set_title('color mag diagram (CMD)')
        axes[0, 1].invert_yaxis()
        axes[0, 1].set_xlabel('Bp-Rp (mag)')
        axes[0, 1].set_ylabel('G (mag)')

        axes[0, 2].set_title('proper motions')
        axes[0, 2].set_xlabel('pmra (mas/yr)')
        axes[0, 2].set_ylabel('pmdec (mas/yr)')

        for u in range(3):
            for v in range(2):
                _asp = np.diff(axes[0, u].get_xlim())[0]
                _asp /= np.diff(axes[0, u].get_ylim())[0]
                axes[1, u].set_aspect(np.abs(_asp))

        cnt = 0  # counter for how many images have been plotted
        for hips_survey in hips_surveys:
            try:
                result = make_sky_image(geometry=geometry,
                                        hips_survey=hips_survey,
                                        tile_format='jpg',
                                        progress_bar=False)
                axes[1, cnt].imshow(result.image, origin='lower')
                axes[1, cnt].set_title(hips_survey)
                cnt += 1
            except:
                pass

            if cnt == 3:
                break

        for u in range(3):
            axes[1, u].tick_params(axis='both',
                                   which='both',
                                   labelleft=False,
                                   labelbottom=False)
    else:
        fig, axes = plt.subplots(1, 4, figsize=(15, 5))

        # plot sources
        axes[0].set_title(short_name)
        sns.scatterplot(ra_data, dec_data, ax=axes[0])
        axes[0].set_xlim([ra_min, ra_max])
        axes[0].set_ylim([dec_min, dec_max])
        _asp = np.diff(axes[0].get_xlim())[0] / np.diff(axes[0].get_ylim())[0]
        axes[0].set_aspect(_asp)
        axes[0].set_xlim(axes[0].set_xlim([ra_min, ra_max])[::-1])  # flipping

        # plot circle of the size of sigma1
        circle = plt.Circle((ra, dec),
                            sigma1,
                            color='orange',
                            fill=False,
                            lw=2)
        axes[0].add_artist(circle)

        cnt = 0  # counter for how many images have been plotted
        for hips_survey in hips_surveys:
            try:
                result = make_sky_image(geometry=geometry,
                                        hips_survey=hips_survey,
                                        tile_format='jpg',
                                        progress_bar=False)
                cnt += 1
                axes[cnt].imshow(result.image, origin='lower')
                axes[cnt].set_title(hips_survey)
            except:
                pass

            if cnt == 3:
                break

        for i in range(4):
            axes[i].tick_params(axis='both',
                                which='both',
                                labelleft=False,
                                labelbottom=False)

    _st1 = '{}-label{}-sigp%0.2f-'.format(short_name, label) % sig_p
    _st2 = '-width{}'.format(width)
    fig.suptitle('{}(%0.4f,%0.4f){}'.format(_st1, _st2) % (ra, dec))

    _str = '{}/{}-target{}-'.format(outpath, name, label)
    _str = '{}ra%0.4f-dec%0.4f-sigp%0.2f.jpg'.format(_str) % (ra, dec, sig_p)
    plt.savefig(_str, bbox_inches='tight', dpi=300)
Ejemplo n.º 6
0
def main(rh,
         nstar,
         rmax,
         sigma1,
         sigma2,
         sigma3,
         width,
         pixelsize,
         surface_density,
         is_plot=True,
         is_detail=True):

    ra_center_patch = 0.
    dec_center_patch = 0.

    ra_dwarf = 0.
    dec_dwarf = 0.

    aplummer = rh / 1.3
    fac_plummer = 3. * nstar / 4. / np.pi / rh**3

    xedges = np.linspace(-rmax, rmax)
    yedges = np.linspace(-rmax, rmax)
    extent = [xedges.min(), xedges.max(), yedges.min(), yedges.max()]

    kdepatch = KDE_MWSatellite(ra_center_patch, dec_center_patch, width,
                               pixelsize, sigma1, sigma2, sigma3, rh)

    xmesh, ymesh = np.meshgrid(kdepatch.x_mesh, kdepatch.y_mesh)
    xmesh = 0.5 * (xmesh[1:, 1:] + xmesh[:-1, :-1])
    ymesh = 0.5 * (ymesh[1:, 1:] + ymesh[:-1, :-1])
    dist2_ = dist2(xmesh, ymesh, ra_dwarf, dec_dwarf)

    xplummer = xmesh - ra_dwarf
    yplummer = ymesh - dec_dwarf
    true_bg = plummer2d(aplummer, xplummer, yplummer,
                        fac_plummer) * pixelsize**2
    true_bg += surface_density * pixelsize**2
    kdepatch.true_bg_parser(true_bg)
    kdepatch.add_masks_on_pixels(ra_dwarf, dec_dwarf, rh)

    kdepatch.compound_sig_poisson()
    lambda_out = kdepatch.bg_estimate

    mask_out_rh = dist2_ > rh**2
    mask_in_boundary = dist2_ < (rmax - sigma3)**2
    mask_in_sigma3 = dist2_ < (rh + sigma3)**2

    true_sigs = [2, 5, 8]
    sigs_dict = {}
    for true_sig in true_sigs:
        x = kdepatch.inv_z_score_poisson(true_bg, true_sig)
        sig_estimate = kdepatch.z_score_poisson(kdepatch.bg_estimate, x)
        sig_estimate *= mask_in_boundary

        if is_plot:
            plot_sig(true_sig, sig_estimate, mask_in_boundary, extent, rh,
                     sigma3)

        _s = sig_estimate[~mask_out_rh].flatten()
        sigs_dict['in_dwarf_s%d' % true_sig] = np.array(
            [np.min(_s), np.max(_s),
             np.mean(_s), np.std(_s)])
        _s = sig_estimate[mask_out_rh & mask_in_sigma3
                          & mask_in_boundary].flatten()
        sigs_dict['overlap_s%d' % true_sig] = np.array(
            [np.min(_s), np.max(_s),
             np.mean(_s), np.std(_s)])
        _s = sig_estimate[~mask_in_sigma3 & mask_in_boundary].flatten()
        sigs_dict['outskirt_s%d' % true_sig] = np.array(
            [np.min(_s), np.max(_s),
             np.mean(_s), np.std(_s)])
        del _s

    res_out = lambda_out - true_bg
    res_out /= true_bg
    res_out *= mask_in_boundary

    if is_plot:
        lambda_out *= mask_in_boundary
        plot_detail(true_bg, lambda_out, res_out, mask_out_rh,
                    mask_in_boundary, mask_in_sigma3, extent, rh, sigma1,
                    sigma3)

    del lambda_out

    res_mean = np.mean(res_out)
    res_std = np.std(res_out)

    res_dict = {}
    _s = res_out[~mask_out_rh].flatten()
    res_dict['in_dwarf_res_nbg'] = np.array(
        [np.min(_s), np.max(_s),
         np.mean(_s), np.std(_s)])
    _s = res_out[mask_out_rh & mask_in_sigma3 & mask_in_boundary].flatten()
    res_dict['overlap_res_nbg'] = np.array(
        [np.min(_s), np.max(_s),
         np.mean(_s), np.std(_s)])
    _s = res_out[~mask_in_sigma3 & mask_in_boundary].flatten()
    res_dict['outskirt_res_nbg'] = np.array(
        [np.min(_s), np.max(_s),
         np.mean(_s), np.std(_s)])

    if not is_plot:
        return res_dict, sigs_dict