Exemplo n.º 1
0
def cmb_forecast_data(experiment,
                      map_params,
                      l,
                      cl,
                      cluster=None,
                      cl_residual=None,
                      bl=None,
                      nber_obs=1):
    nx, dx, ny, dy = map_params
    if cluster is not None:
        kappa_map = lensing.NFW(cluster[0], cluster[1], cluster[2],
                                1100).convergence_map(map_params)
        alpha_vec = lensing.deflection_from_convergence(map_params, kappa_map)

    sims_arr = []

    for i in range(nber_obs):
        sim = tools.make_gaussian_realization(map_params, l, cl)
        if cluster is not None:
            sim = lensing.lens_map(map_params, sim, alpha_vec)
        if cl_residual is not None:
            residual_noise_map = tools.make_gaussian_realization(
                map_params, l, cl_residual)
            sim += residual_noise_map
        if bl is not None:
            sim = tools.convolve(sim, l, np.sqrt(bl), map_params=map_params)
        sims_arr.append(sim)

    return sims_arr
Exemplo n.º 2
0
    def forward(self, X):
        """Forward propagate the output of the previous layer.

        Parameters
        ----------
        X : array-like, shape = [N, I, Y, X]
            input

        Returns
        -------
        y : array-like, shape = [N, J, Y_o, X_o]
            output
        """
        if np.prod(X.shape[1:]) != self.n_inputs:
            raise ValueError("shape = %s, expected %d inputs" %
                             (X.shape, self.n_inputs))

        self.X = X
        A = convolve(X,
                     self.W,
                     self.B,
                     stride_y=self.strides[0],
                     stride_x=self.strides[1])
        # Compute output
        self.Y = self.g(A)
        return self.Y
Exemplo n.º 3
0
def cmb_forecast_test_data(map_params,
                           l,
                           cl,
                           cluster=None,
                           cl_residuals=None,
                           bl=None,
                           nber_obs=1):

    if cluster is not None:
        kappa_map = lensing.NFW(cluster[0], cluster[1], cluster[2],
                                1100).convergence_map(map_params)
        alpha_vec = lensing.deflection_from_convergence(map_params, kappa_map)

    sims_arr_so, sims_arr_fyst, sims_arr_s4wide = [], [], []

    for i in range(nber_obs):
        sim = tools.make_gaussian_realization(map_params, l, cl)
        if cluster is not None:
            sim = lensing.lens_map(map_params, sim, alpha_vec)
        sim_so, sim_fyst, sim_s4wide = np.copy(sim), np.copy(sim), np.copy(sim)
        if cl_residuals is not None:
            residual_noise_map_so = tools.make_gaussian_realization(
                map_params, l, cl_residuals[0])
            sim_so += residual_noise_map_so
            residual_noise_map_fyst = tools.make_gaussian_realization(
                map_params, l, cl_residuals[1])
            sim_fyst += residual_noise_map_fyst
            residual_noise_map_s4wide = tools.make_gaussian_realization(
                map_params, l, cl_residuals[2])
            sim_s4wide += residual_noise_map_s4wide
        if bl is not None:
            sim_so = tools.convolve(sim_so,
                                    l,
                                    np.sqrt(bl),
                                    map_params=map_params)
            sim_fyst = tools.convolve(sim_fyst,
                                      l,
                                      np.sqrt(bl),
                                      map_params=map_params)
            sim_s4wide = tools.convolve(sim_s4wide,
                                        l,
                                        np.sqrt(bl),
                                        map_params=map_params)
        sims_arr_so.append(sim_so)
        sims_arr_fyst.append(sim_fyst)
        sims_arr_s4wide.append(sim_s4wide)
    return sims_arr_so, sims_arr_fyst, sims_arr_s4wide
Exemplo n.º 4
0
def get_aligned_cutout(map_params, image, l=None, cl=None, cl_noise=None):

    if cl_noise is None:
        l = np.arange(10000)
        cl = np.ones(max(l) + 1)
        cl_noise = np.zeros(max(l) + 1)

    _, dx, _, _ = map_params
    cutout = tools.central_cutout(map_params, image, 10)
    wiener_filter = tools.wiener_filter(l, cl, cl_noise)
    filtered_map = tools.convolve(image, l, wiener_filter, map_params)
    low_pass_filter = tools.low_pass_filter(l, 2000)
    filtered_map = tools.convolve(filtered_map, l, low_pass_filter, map_params)
    filtered_cutout = tools.central_cutout(map_params, filtered_map, 6)
    _, _, magnitude, angle = tools.gradient(filtered_cutout, dx)
    angle, magnitude_weight = np.median(angle), np.median(magnitude)
    cutout_aligned = tools.rotate(cutout, angle)
    cutout_aligned -= np.median(cutout_aligned)

    return cutout_aligned, magnitude_weight
Exemplo n.º 5
0
def get_aligned_cutout(map_params,
                       image,
                       image_noiseless=None,
                       cutout_size_am=10,
                       cutout_size_for_grad_est_am=6,
                       l_cut=2000,
                       l=None,
                       cl=None,
                       cl_noise=None):

    if image_noiseless is None:
        image_noiseless = np.copy(image)

    if cl_noise is None:
        l = np.arange(10001)
        cl = np.ones(max(l) + 1)
        cl_noise = np.zeros(max(l) + 1)

    _, dx, _, _ = map_params
    cutout = tools.central_cutout(image_noiseless, cutout_size_am, map_params)
    wiener_filter = tools.wiener_filter(cl, cl_noise)
    low_pass_filter = tools.low_pass_filter(l, l_cut)
    filtered_map = tools.convolve(image, l, wiener_filter * low_pass_filter,
                                  map_params)
    filtered_cutout = tools.central_cutout(filtered_map,
                                           cutout_size_for_grad_est_am,
                                           map_params)
    angle, magnitude = get_angle_and_magnitude(filtered_cutout, dx)
    angle, magnitude_weight = np.median(angle), np.median(magnitude)
    cutout_aligned = scipy.ndimage.rotate(np.nan_to_num(cutout),
                                          np.nan_to_num(angle),
                                          reshape=False,
                                          mode='reflect')
    cutout_aligned -= np.median(cutout_aligned)

    return cutout_aligned, magnitude_weight
Exemplo n.º 6
0
def cmb_mock_data(map_params,
                  l,
                  cl,
                  cluster=None,
                  centroid_shift_value=0,
                  nber_ch=1,
                  cluster_corr_cutouts=None,
                  cl_extragal=None,
                  bl=None,
                  nl=None):

    nx, dx, ny, dy = map_params
    sim = tools.make_gaussian_realization(map_params, l, cl)

    if cluster is not None:
        M, c, z = cluster
        x_shift, y_shift = np.random.normal(
            loc=0.0, scale=centroid_shift_value), np.random.normal(
                loc=0.0, scale=centroid_shift_value)
        centroid_shift = [x_shift, y_shift]
        kappa_map = lensing.NFW(M, c, z, 1100).convergence_map(
            map_params, centroid_shift=centroid_shift)
        alpha_vec = lensing.deflection_from_convergence(map_params, kappa_map)
        sim = lensing.lens_map(map_params, sim, alpha_vec)

    sims_ch_arr = [np.copy(sim) for k in range(nber_ch)]

    if cluster_corr_cutouts is not None:
        if np.asarray(cluster_corr_cutouts).ndim is 3:
            cluster_corr_cutouts = [cluster_corr_cutouts]
        cluster_corr_cutout = cluster_corr_cutouts[0][0]
        nx_cutout, ny_cutout = cluster_corr_cutout.shape[
            0], cluster_corr_cutout.shape[1]
        s, e = int((nx - nx_cutout) / 2), int((ny + ny_cutout) / 2)
        rand_sel = random.randint(0, len(cluster_corr_cutouts[0]) - 1)
        rand_ang = random.randint(-180, 180)
        for j in range(nber_ch):
            cluster_corr_cutout = tools.rotate(
                cluster_corr_cutouts[j][rand_sel], rand_ang)
            sims_ch_arr[j][s:e,
                           s:e] = sims_ch_arr[j][s:e,
                                                 s:e] + cluster_corr_cutout

    if cl_extragal is not None:
        if isinstance(cl_extragal, list) is False:
            cl_extragal = [cl_extragal]
        extragal_maps = tools.make_gaussian_realization(
            map_params, l, cl_extragal)
        for j in range(nber_ch):
            sims_ch_arr[j] += extragal_maps[j]

    if bl is not None:
        if isinstance(bl, list) is False:
            bl = [bl]
        for j in range(nber_ch):
            sims_ch_arr[j] = tools.convolve(sims_ch_arr[j],
                                            l,
                                            np.sqrt(bl[j]),
                                            map_params=map_params)

    if nl is not None:
        if isinstance(nl, list) is False:
            nl = [nl]
        for j in range(nber_ch):
            noise_map = tools.make_gaussian_realization(map_params, l, nl[j])
            sims_ch_arr[j] += noise_map

    if nber_ch == 1:
        return sims_ch_arr[0]

    return sims_ch_arr
Exemplo n.º 7
0
def cmb_test_data(nber_maps,
                  validation_analyis=False,
                  clus_position_analysis=False,
                  extragal_bias_analysis=False):
    nx, dx, ny, dy = 240, 0.25, 240, 0.25
    map_params = [nx, dx, ny, dy]
    l, cl = CosmoCalc().cmb_power_spectrum()
    l, bl = exp.beam_power_spectrum(1.4)
    l, nl = exp.white_noise_power_spectrum(2.0)

    if validation_analyis is True:
        sims_clus_2e14, sims_clus_6e14, sims_clus_10e14 = [], [], []
        kappa_map_2e14 = lensing.NFW(2e14, 3, 1,
                                     1100).convergence_map(map_params)
        kappa_map_6e14 = lensing.NFW(6e14, 3, 1,
                                     1100).convergence_map(map_params)
        kappa_map_10e14 = lensing.NFW(10e14, 3, 1,
                                      1100).convergence_map(map_params)
        alpha_vec_2e14 = lensing.deflection_from_convergence(
            map_params, kappa_map_2e14)
        alpha_vec_6e14 = lensing.deflection_from_convergence(
            map_params, kappa_map_6e14)
        alpha_vec_10e14 = lensing.deflection_from_convergence(
            map_params, kappa_map_10e14)
        for i in range(nber_maps):
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_clus_2e14 = lensing.lens_map(map_params, sim, alpha_vec_2e14)
            sim_clus_6e14 = lensing.lens_map(map_params, sim, alpha_vec_6e14)
            sim_clus_10e14 = lensing.lens_map(map_params, sim, alpha_vec_10e14)
            sim_clus_2e14 = tools.convolve(sim_clus_2e14,
                                           l,
                                           np.sqrt(bl),
                                           map_params=map_params)
            sim_clus_6e14 = tools.convolve(sim_clus_6e14,
                                           l,
                                           np.sqrt(bl),
                                           map_params=map_params)
            sim_clus_10e14 = tools.convolve(sim_clus_10e14,
                                            l,
                                            np.sqrt(bl),
                                            map_params=map_params)
            noise_map = tools.make_gaussian_realization(map_params, l, nl)
            sim_clus_2e14 += noise_map
            sim_clus_6e14 += noise_map
            sim_clus_10e14 += noise_map
            sims_clus_2e14.append(sim_clus_2e14)
            sims_clus_6e14.append(sim_clus_6e14)
            sims_clus_10e14.append(sim_clus_10e14)
        return sims_clus_2e14, sims_clus_6e14, sims_clus_10e14

    if clus_position_analysis is True:
        sims_baseline, sims_centorid_shift = [], []
        kappa_map_6e14_baseline = lensing.NFW(2e14, 3, 1,
                                              1100).convergence_map(map_params)
        alpha_vec_6e14_baseline = lensing.deflection_from_convergence(
            map_params, kappa_map_6e14_baseline)
        for i in range(nber_maps):
            x_shift, y_shift = np.random.normal(
                loc=0.0, scale=0.5), np.random.normal(loc=0.0, scale=0.5)
            centroid_shift = [x_shift, y_shift]
            kappa_map_6e14_centroid_shift = lensing.NFW(
                6e14, 3, 1, 1100).convergence_map(map_params, centroid_shift)
            alpha_vec_6e14_centroid_shift = lensing.deflection_from_convergence(
                map_params, kappa_map_6e14_centroid_shift)
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_baseline = lensing.lens_map(map_params, sim,
                                            alpha_vec_6e14_baseline)
            sim_centroid_shift = lensing.lens_map(
                map_params, sim, alpha_vec_6e14_centroid_shift)
            sim_baseline = tools.convolve(sim_baseline,
                                          l,
                                          np.sqrt(bl),
                                          map_params=map_params)
            sim_centroid_shift = tools.convolve(sim_centroid_shift,
                                                l,
                                                np.sqrt(bl),
                                                map_params=map_params)
            noise_map = tools.make_gaussian_realization(map_params, l, nl)
            sim_baseline += noise_map
            sim_centroid_shift += noise_map
            sims_baseline.append(sim_baseline)
            sims_centroid_shift.append(sim_centroid_shift)
        return sims_baseline, sims_centroid_shift

    if extragal_bias_analysis is True:
        sims_baseline, sims_tsz, sims_ksz, sims_tsz_ksz = [], [], [], []
        c500 = concentration.concentration(2e14, '500c', 0.7)
        M200c, _, c200c = mass_defs.changeMassDefinition(2e14,
                                                         c500,
                                                         0.7,
                                                         '500c',
                                                         '200c',
                                                         profile='nfw')
        kappa_map_M200c = lensing.NFW(M200c, c200c, 0.7,
                                      1100).convergence_map(map_params)
        alpha_vec_M200c = lensing.deflection_from_convergence(
            map_params, kappa_map_M200c)
        fname = '/Volumes/Extreme_SSD/codes/master_thesis/code/data/mdpl2_cutouts_for_tszksz_clus_detection_M1.7e+14to2.3e+14_z0.6to0.8_15320haloes_boxsize20.0am.npz'
        cutouts_dic = np.load(fname, allow_pickle=1,
                              encoding='latin1')['arr_0'].item()
        mass_z_key = list(cutouts_dic.keys())[0]
        cutouts = cutouts_dic[mass_z_key]
        scale_fac = fg.compton_y_to_delta_Tcmb(145, uK=True)
        tsz_cutouts, ksz_cutouts = [], []
        for kcntr, keyname in enumerate(cutouts):
            tsz_cutout = cutouts[keyname]['y'] * scale_fac
            tsz_cutouts.append(tsz_cutout)
            ksz_cutout = cutouts[keyname]['ksz'] * random.randrange(-1, 2, 2)
            ksz_cutouts.append(ksz_cutout)
        s, e = int((nx - 40) / 2), int((ny + 40) / 2)
        for i in range(nber_maps):
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_M200c = lensing.lens_map(map_params, sim, alpha_vec_M200c)
            sim_baseline, sim_tsz, sim_ksz, sim_tsz_ksz = np.copy(
                sim_M200c), np.copy(sim_M200c), np.copy(sim_M200c), np.copy(
                    sim_M200c)
            tsz_cutout = tools.rotate(
                tsz_cutouts[random.randint(0,
                                           len(tsz_cutouts) - 1)],
                random.randint(-180, 180))
            ksz_cutout = tools.rotate(
                ksz_cutouts[random.randint(0,
                                           len(ksz_cutouts) - 1)],
                random.randint(-180, 180))
            tsz_ksz_cutout = tsz_cutout + ksz_cutout
            sim_tsz[s:e, s:e] = sim_tsz[s:e, s:e] + tsz_cutout
            sim_ksz[s:e, s:e] = sim_ksz[s:e, s:e] + ksz_cutout
            sim_tsz_ksz[s:e, s:e] = sim_tsz_ksz[s:e, s:e] + tsz_ksz_cutout
            sim_baseline = tools.convolve(sim_baseline,
                                          l,
                                          np.sqrt(bl),
                                          map_params=map_params)
            sim_tsz = tools.convolve(sim_tsz,
                                     l,
                                     np.sqrt(bl),
                                     map_params=map_params)
            sim_ksz = tools.convolve(sim_ksz,
                                     l,
                                     np.sqrt(bl),
                                     map_params=map_params)
            sim_tsz_ksz = tools.convolve(sim_tsz_ksz,
                                         l,
                                         np.sqrt(bl),
                                         map_params=map_params)
            noise_map = tools.make_gaussian_realization(map_params, l, nl)
            sim_baseline += noise_map
            sim_tsz += noise_map
            sim_ksz += noise_map
            sim_tsz_ksz += noise_map
            sims_baseline.append(sim_baseline)
            sims_tsz.append(sim_tsz)
            sims_ksz.append(sim_ksz)
            sims_tsz_ksz.append(sim_tsz_ksz)
        return sims_baseline, sims_tsz, sims_ksz, sims_tsz_ksz
Exemplo n.º 8
0
def models(nber_fit,
           map_params,
           l,
           cl,
           mass_int,
           z,
           centroid_shift_value=0,
           bl=None,
           cl_noise=None,
           cutout_size_am=10,
           use_magnitude_weights=True,
           use_noise_weights=False,
           apply_noise=True,
           average=1):

    nx, dx, ny, dy = map_params
    if cl_noise is None:
        cl_noise = np.zeros(max(l) + 1)

    mass_int = np.copy(mass_int) * 1e14

    models = []

    for k in range(average):
        cutouts_clus_arr = []
        magnitude_weights_clus_arr = []
        profile_models_arr = []

        for i in range(nber_fit):
            sim = sims.cmb_mock_data(map_params, l, cl)
            x_shift, y_shift = np.random.normal(
                loc=0.0,
                scale=centroid_shift_value / (2**0.5)), np.random.normal(
                    loc=0.0, scale=centroid_shift_value / (2**0.5))
            centroid_shift = [x_shift, y_shift]
            grid, _ = tools.make_grid(map_params, grid_shift=centroid_shift)
            theta = np.hypot(grid[0], grid[1])
            total_noise_map = tools.make_gaussian_realization(
                l, cl_noise, map_params)
            for j in range(len(mass_int)):
                c200c = cosmo.concentration_parameter(mass_int[j], z, 0.674)
                kappa_map = lensing.NFW_convergence(mass_int[j],
                                                    c200c,
                                                    z,
                                                    1100,
                                                    theta,
                                                    dim=2)
                alpha_vec = lensing.deflection_from_convergence(
                    kappa_map, map_params)
                sim_lensed = lensing.lens_map(sim, alpha_vec, map_params)
                sim_lensed_noise = np.copy(sim_lensed)
                sim_lensed_noise += total_noise_map
                if bl is not None:
                    sim_lensed = tools.convolve(sim_lensed,
                                                l,
                                                np.sqrt(bl),
                                                map_params=map_params)
                    sim_lensed_noise = tools.convolve(sim_lensed_noise,
                                                      l,
                                                      np.sqrt(bl),
                                                      map_params=map_params)
                if apply_noise is False:
                    sim_lensed_noise = np.copy(sim_lensed)

                cutout_aligned, magnitude_weight = get_aligned_cutout(
                    map_params,
                    sim_lensed_noise,
                    image_noiseless=sim_lensed,
                    cutout_size_am=cutout_size_am,
                    l=l,
                    cl=cl,
                    cl_noise=cl_noise)
                if use_magnitude_weights is False:
                    magnitude_weight = 1
                cutouts_clus_arr.append(cutout_aligned * magnitude_weight)
                magnitude_weights_clus_arr.append(magnitude_weight)

        stack_bg = np.sum(cutouts_clus_arr[0::len(mass_int)], axis=0) / np.sum(
            magnitude_weights_clus_arr[0::len(mass_int)])
        for i in range(len(mass_int)):
            stack_clus = np.sum(
                cutouts_clus_arr[i::len(mass_int)], axis=0) / np.sum(
                    magnitude_weights_clus_arr[i::len(mass_int)])
            stack_dipole = stack_clus - stack_bg
            profile_model = np.mean(stack_dipole, axis=0)
            profile_models_arr.append(profile_model)

        models.append(profile_models_arr)

    return np.mean(models, axis=0)
Exemplo n.º 9
0
def model_profiles(nber_clus_fit,
                   nber_rand_fit,
                   map_params,
                   l,
                   cl,
                   mass_int,
                   c200c,
                   z,
                   centroid_shift_value=0,
                   cl_extragal=None,
                   bl=None,
                   cl_noise=None,
                   use_magnitude_weights=True,
                   use_noise_weights=False,
                   apply_noise=True):

    nx, dx, ny, dy = map_params
    if cl_noise is None:
        cl_noise = np.zeros(max(l) + 1)

    mass_int = np.copy(mass_int) * 1e14

    cutouts_clus_arr = []
    magnitude_weights_clus_arr = []
    for i in tqdm(range(nber_clus_fit)):
        sim = sims.cmb_mock_data(map_params, l, cl)
        x_shift, y_shift = np.random.normal(
            loc=0.0, scale=centroid_shift_value), np.random.normal(
                loc=0.0, scale=centroid_shift_value)
        centroid_shift = [x_shift, y_shift]
        for j in range(len(mass_int)):
            kappa_map = lensing.NFW(mass_int[j], c200c, z,
                                    1100).convergence_map(
                                        map_params,
                                        centroid_shift=centroid_shift)
            alpha_vec = lensing.deflection_from_convergence(
                map_params, kappa_map)
            sim_lensed = lensing.lens_map(map_params, sim, alpha_vec)
            sim_lensed_noise = np.copy(sim_lensed)
            total_noise_map = tools.make_gaussian_realization(
                map_params, l, cl_noise)
            sim_lensed_noise += total_noise_map
            if bl is not None:
                sim_lensed = tools.convolve(sim_lensed,
                                            l,
                                            np.sqrt(bl),
                                            map_params=map_params)
                sim_lensed_noise = tools.convolve(sim_lensed_noise,
                                                  l,
                                                  np.sqrt(bl),
                                                  map_params=map_params)
            if apply_noise is False:
                sim_lensed_noise = np.copy(sim_lensed)
            cutout = tools.central_cutout(map_params, sim_lensed, 10)
            wiener_filter = tools.wiener_filter(l, cl, cl_noise)
            filtered_map = tools.convolve(sim_lensed_noise, l, wiener_filter,
                                          map_params)
            low_pass_filter = tools.low_pass_filter(l, 2000)
            filtered_map = tools.convolve(filtered_map, l, low_pass_filter,
                                          map_params)
            filtered_cutout = tools.central_cutout(map_params, filtered_map, 6)
            _, _, magnitude, angle = tools.gradient(filtered_cutout, dx)
            angle, magnitude_weight = np.median(angle), np.median(magnitude)
            cutout_aligned = tools.rotate(cutout, angle)
            cutout_aligned -= np.median(cutout_aligned)
            if use_magnitude_weights is False:
                magnitude_weight = 1
            cutouts_clus_arr.append(cutout_aligned * magnitude_weight)
            magnitude_weights_clus_arr.append(magnitude_weight)

    cutouts_rand_arr = []
    magnitude_weights_rand_arr = []
    for i in tqdm(range(nber_rand_fit)):
        sim = sims.cmb_mock_data(map_params, l, cl)
        sim_noise = np.copy(sim)
        total_noise_map = tools.make_gaussian_realization(
            map_params, l, cl_noise)
        sim_noise += total_noise_map
        if bl is not None:
            sim = tools.convolve(sim, l, np.sqrt(bl), map_params=map_params)
            sim_noise = tools.convolve(sim_noise,
                                       l,
                                       np.sqrt(bl),
                                       map_params=map_params)
        if apply_noise is False:
            sim_noise = np.copy(sim)
        cutout = tools.central_cutout(map_params, sim, 10)
        wiener_filter = tools.wiener_filter(l, cl, cl_noise)
        filtered_map = tools.convolve(sim_noise, l, wiener_filter, map_params)
        low_pass_filter = tools.low_pass_filter(l, 2000)
        filtered_map = tools.convolve(filtered_map, l, low_pass_filter,
                                      map_params)
        filtered_cutout = tools.central_cutout(map_params, filtered_map, 6)
        _, _, magnitude, angle = tools.gradient(filtered_cutout, dx)
        angle, magnitude_weight = np.median(angle), np.median(magnitude)
        cutout_aligned = tools.rotate(cutout, angle)
        cutout_aligned -= np.median(cutout_aligned)
        cutouts_rand_arr.append(cutout_aligned)
        magnitude_weights_rand_arr.append(magnitude_weight)
    if use_magnitude_weights is False:
        magnitude_weights_rand_arr = np.ones(nber_rand_fit)
    weighted_cutouts = [
        cutouts_rand_arr[i] * magnitude_weights_rand_arr[i]
        for i in range(nber_rand_fit)
    ]
    stack_bg = np.sum(weighted_cutouts,
                      axis=0) / np.sum(magnitude_weights_rand_arr)

    profile_models_arr = []
    for i in tqdm(range(len(mass_int))):
        stack_clus = np.sum(cutouts_clus_arr[i::len(mass_int)],
                            axis=0) / np.sum(
                                magnitude_weights_clus_arr[i::len(mass_int)])
        stack_dipole = stack_clus - stack_bg
        profile_model = np.mean(stack_dipole, axis=0)
        profile_models_arr.append(profile_model)

    return profile_models_arr
Exemplo n.º 10
0
def cmb_test_data(map_params,
                  l,
                  cl,
                  cluster=None,
                  centroid_shift_value=0,
                  cluster_corr_cutouts=None,
                  bl=None,
                  nl=None,
                  nber_obs=1,
                  estimator_validation=False,
                  noise_comparison=False,
                  clus_positions=False,
                  foreground_bias=False):

    if estimator_validation is True:
        sims_clus_arr = []
        kappa_maps = [
            lensing.NFW(cluster[i][0], cluster[i][1], cluster[i][2],
                        1100).convergence_map(map_params)
            for i in range(len(cluster))
        ]
        alpha_vecs = [
            lensing.deflection_from_convergence(map_params, kappa_maps[i])
            for i in range(len(cluster))
        ]
        for i in range(nber_obs):
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sims_lensed = [
                lensing.lens_map(map_params, sim, alpha_vecs[i])
                for i in range(len(cluster))
            ]
            if bl is not None:
                for j in range(len(sims_lensed)):
                    sims_lensed[j] = tools.convolve(sims_lensed[j],
                                                    l,
                                                    np.sqrt(bl),
                                                    map_params=map_params)
            if nl is not None:
                noise_map = tools.make_gaussian_realization(map_params, l, nl)
                for j in range(len(sims_lensed)):
                    sims_lensed[j] += noise_map
            sims_clus_arr.append(sims_lensed)
        sims_mass_sorted = []
        for i in range(len(cluster)):
            maps_at_mass_i = []
            for j in range(nber_obs):
                maps_at_mass_i.append(sims_clus_arr[j][i])
            sims_mass_sorted.append(maps_at_mass_i)
        return sims_mass_sorted

    if noise_comparison is True:
        sims_noise_arr = []
        kappa_map = lensing.NFW(cluster[0], cluster[1], cluster[2],
                                1100).convergence_map(map_params)
        alpha_vec = lensing.deflection_from_convergence(map_params, kappa_map)
        for i in range(nber_obs):
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_lensed = lensing.lens_map(map_params, sim, alpha_vec)
            if bl is not None:
                sim_lensed = tools.convolve(sim_lensed,
                                            l,
                                            np.sqrt(bl),
                                            map_params=map_params)
            sims_noise = [np.copy(sim_lensed) for i in range(len(nl))]
            noise_maps = [
                tools.make_gaussian_realization(map_params, l, nl[i])
                for i in range(len(nl))
            ]
            for i in range(len(sims_noise)):
                sims_noise[i] += noise_maps[i]
            sims_noise_arr.append(sims_noise)
        sims_noise_sorted = []
        for i in range(len(nl)):
            maps_at_noise_i = []
            for j in range(nber_obs):
                maps_at_noise_i.append(sims_noise_arr[j][i])
            sims_noise_sorted.append(maps_at_noise_i)
        return sims_noise_sorted

    if clus_positions is True:
        sims_clus_baseline, sims_clus_centroid_shift = [], []
        kappa_map_baseline = lensing.NFW(cluster[0], cluster[1], cluster[2],
                                         1100).convergence_map(map_params)
        alpha_vec_baseline = lensing.deflection_from_convergence(
            map_params, kappa_map_baseline)
        for i in range(nber_obs):
            x_shift, y_shift = np.random.normal(
                loc=0.0, scale=centroid_shift_value), np.random.normal(
                    loc=0.0, scale=centroid_shift_value)
            centroid_shift = [x_shift, y_shift]
            kappa_map_centroid_shift = lensing.NFW(
                cluster[0], cluster[1], cluster[2],
                1100).convergence_map(map_params, centroid_shift)
            alpha_vec_centroid_shift = lensing.deflection_from_convergence(
                map_params, kappa_map_centroid_shift)
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_lensed_baseline = lensing.lens_map(map_params, sim,
                                                   alpha_vec_baseline)
            sim_lensed_centroid_shift = lensing.lens_map(
                map_params, sim, alpha_vec_centroid_shift)
            if bl is not None:
                sim_lensed_baseline = tools.convolve(sim_lensed_baseline,
                                                     l,
                                                     np.sqrt(bl),
                                                     map_params=map_params)
                sim_lensed_centroid_shift = tools.convolve(
                    sim_lensed_centroid_shift,
                    l,
                    np.sqrt(bl),
                    map_params=map_params)
            if nl is not None:
                noise_map = tools.make_gaussian_realization(map_params, l, nl)
                sim_lensed_baseline += noise_map
                sim_lensed_centroid_shift += noise_map
            sims_clus_baseline.append(sim_lensed_baseline)
            sims_clus_centroid_shift.append(sim_lensed_centroid_shift)
        return sims_clus_baseline, sims_clus_centroid_shift

    if foreground_bias is True:
        fname = '/Volumes/Extreme_SSD/codes/master_thesis/code/data/mdpl2_cutouts_for_tszksz_clus_detection_M1.7e+14to2.3e+14_z0.6to0.8_15320haloes_boxsize10.0am_dx0.5am.npz'
        cutouts_dic = np.load(fname, allow_pickle=1,
                              encoding='latin1')['arr_0'].item()
        mass_z_key = list(cutouts_dic.keys())[0]
        cutouts = cutouts_dic[mass_z_key]
        scale_fac = fg.compton_y_to_delta_Tcmb(150, uK=True)
        tsz_cutouts, ksz_cutouts = [], []
        for kcntr, keyname in enumerate(cutouts):
            tsz_cutout = cutouts[keyname]['y'] * scale_fac
            tsz_cutouts.append(tsz_cutout)
            ksz_cutout = cutouts[keyname]['ksz'] * random.randrange(-1, 2, 2)
            ksz_cutouts.append(ksz_cutout)
        nx, dx, ny, dy = map_params
        cluster_corr_cutout = ksz_cutouts[0]
        nx_cutout, ny_cutout = cluster_corr_cutout.shape[
            0], cluster_corr_cutout.shape[1]
        s, e = int((nx - nx_cutout) / 2), int((ny + ny_cutout) / 2)


        sims_clus_baseline, sims_clus_tsz, sims_clus_ksz, sims_clus_tsz_ksz = [], [], [], []
        kappa_map = lensing.NFW(cluster[0], cluster[1], cluster[2],
                                1100).convergence_map(map_params)
        alpha_vec = lensing.deflection_from_convergence(map_params, kappa_map)
        for i in range(nber_obs):
            sim = tools.make_gaussian_realization(map_params, l, cl)
            sim_lensed = lensing.lens_map(map_params, sim, alpha_vec)
            sim_lensed_baseline, sim_lensed_tsz, sim_lensed_ksz, sim_lensed_tsz_ksz = np.copy(
                sim_lensed), np.copy(sim_lensed), np.copy(sim_lensed), np.copy(
                    sim_lensed)
            tsz_cutout = tools.rotate(
                tsz_cutouts[random.randint(0,
                                           len(tsz_cutouts) - 1)],
                random.randint(-180, 180))
            ksz_cutout = tools.rotate(
                ksz_cutouts[random.randint(0,
                                           len(ksz_cutouts) - 1)],
                random.randint(-180, 180))
            tsz_ksz_cutout = tsz_cutout + ksz_cutout
            sim_lensed_tsz[s:e, s:e] = sim_lensed_tsz[s:e, s:e] + tsz_cutout
            sim_lensed_ksz[s:e, s:e] = sim_lensed_ksz[s:e, s:e] + ksz_cutout
            sim_lensed_tsz_ksz[s:e,
                               s:e] = sim_lensed_tsz_ksz[s:e,
                                                         s:e] + tsz_ksz_cutout
            if bl is not None:
                sim_lensed_baseline = tools.convolve(sim_lensed_baseline,
                                                     l,
                                                     np.sqrt(bl),
                                                     map_params=map_params)
                sim_lensed_tsz = tools.convolve(sim_lensed_tsz,
                                                l,
                                                np.sqrt(bl),
                                                map_params=map_params)
                sim_lensed_ksz = tools.convolve(sim_lensed_ksz,
                                                l,
                                                np.sqrt(bl),
                                                map_params=map_params)
                sim_lensed_tsz_ksz = tools.convolve(sim_lensed_tsz_ksz,
                                                    l,
                                                    np.sqrt(bl),
                                                    map_params=map_params)
            if nl is not None:
                noise_map = tools.make_gaussian_realization(map_params, l, nl)
                sim_lensed_baseline += noise_map
                sim_lensed_tsz += noise_map
                sim_lensed_ksz += noise_map
                sim_lensed_tsz_ksz += noise_map
            sims_clus_baseline.append(sim_lensed_baseline)
            sims_clus_tsz.append(sim_lensed_tsz)
            sims_clus_ksz.append(sim_lensed_ksz)
            sims_clus_tsz_ksz.append(sim_lensed_tsz_ksz)
        return sims_clus_baseline, sims_clus_tsz, sims_clus_ksz, sims_clus_tsz_ksz
Exemplo n.º 11
0
def cmb_mock_data(map_params,
                  l,
                  cl,
                  cluster=None,
                  centroid_shift_value=0,
                  nber_ch=1,
                  cluster_corr_cutouts=None,
                  cl_extragal=None,
                  bl=None,
                  nl=None,
                  nber_obs=1):

    nx, dx, ny, dy = map_params

    sims = []
    for i in range(nber_obs):

        sim = tools.make_gaussian_realization(l, cl, map_params)

        if cluster is not None:
            M, c, z = cluster
            x_shift, y_shift = np.random.normal(
                loc=0.0,
                scale=centroid_shift_value / (2**0.5)), np.random.normal(
                    loc=0.0, scale=centroid_shift_value / (2**0.5))
            centroid_shift = [x_shift, y_shift]
            grid, _ = tools.make_grid(map_params, grid_shift=centroid_shift)
            theta = np.hypot(grid[0], grid[1])
            kappa_map = lensing.NFW_convergence(M, c, z, 1100, theta, dim=2)
            alpha_vec = lensing.deflection_from_convergence(
                kappa_map, map_params)
            sim = lensing.lens_map(sim, alpha_vec, map_params)

        sims_ch_arr = [np.copy(sim) for k in range(nber_ch)]

        if cluster_corr_cutouts is not None:
            if np.asarray(cluster_corr_cutouts).ndim == 3:
                cluster_corr_cutouts = [cluster_corr_cutouts]
            cluster_corr_cutout = cluster_corr_cutouts[0][0]
            nx_cutout, ny_cutout = cluster_corr_cutout.shape[
                0], cluster_corr_cutout.shape[1]
            s, e = int((nx - nx_cutout) / 2), int((ny + ny_cutout) / 2)
            rand_sel = random.randint(0, len(cluster_corr_cutouts[0]) - 1)
            rand_ang = random.randint(-180, 180)
            for j in range(nber_ch):
                cluster_corr_cutout = scipy.ndimage.rotate(
                    np.nan_to_num(cluster_corr_cutouts[j][rand_sel]),
                    np.nan_to_num(rand_ang),
                    reshape=False,
                    mode='reflect')
                sims_ch_arr[j][s:e,
                               s:e] = sims_ch_arr[j][s:e,
                                                     s:e] + cluster_corr_cutout

        if cl_extragal is not None:
            if isinstance(cl_extragal, list) is False:
                cl_extragal = [cl_extragal]
            for j in range(nber_ch):
                extragal_noise_map = tools.make_gaussian_realization(
                    l, cl_extragal[j], map_params, random_seed=0)
                sims_ch_arr[j] += extragal_noise_map

        if bl is not None:
            if isinstance(bl, list) is False:
                bl = [bl]
            for j in range(nber_ch):
                sims_ch_arr[j] = tools.convolve(sims_ch_arr[j],
                                                l,
                                                np.sqrt(bl[j]),
                                                mapparams=map_params)

        if nl is not None:
            if isinstance(nl, list) is False:
                nl = [nl]
            for j in range(nber_ch):
                noise_map = tools.make_gaussian_realization(
                    l, nl[j], map_params)
                sims_ch_arr[j] += noise_map

        sims.append(sims_ch_arr)

    if nber_ch == 1 and nber_obs == 1:
        return sims[0][0]

    if nber_ch == 1:
        sims_one_freq = []
        for i in range(len(sims)):
            sims_one_freq.append(sims[i][0])
        return sims_one_freq

    if nber_obs == 1:
        return sims[0]

    sims_freq_sorted = []
    for i in range(nber_ch):
        maps_at_freq_i = []
        for j in range(nber_obs):
            maps_at_freq_i.append(sims[j][i])
        sims_freq_sorted.append(maps_at_freq_i)

    return sims_freq_sorted