Exemple #1
0
    def from_dataframe(
        cls,
        map_df: pd.DataFrame,
        opening_angle: float,
        quantity: str,
        dir_in: str,
        map_file: str,
        convert_unit: bool = True,
    ) -> "SkyMap":
        """
        Initialize class by reading the skymap data from pandas DataFrame. 

        Args:
            map_filename:
                File path with which skymap pd.DataFrame can be loaded.
            file_dsc:
                Dictionary pointing to a file via {path, root, extension}.
                Use when multiple skymaps need to be loaded.
        """
        if convert_unit:
            map_df = SkyUtils.convert_code_to_phy_units(quantity, map_df)
        map_array = SkyIO.transform_PandasSeries_to_NumpyNdarray(
            map_df[quantity]
        )
        return cls.from_array(
            map_array, opening_angle, quantity, dir_in, map_file
        )
Exemple #2
0
def test__analytic_Halo_alpha_signal_to_SkyArray():
    halo_dic = {
        "r200_deg": np.array([0.05]),
        "r200_pix": np.array([50]),
        "m200": np.array([7e13]),
        "c_NFW": np.array([2.]),
        "Dc": np.array([1050]),
        "theta1_pix": np.array([200]),
        "theta2_pix": np.array([200]),
        "theta1_tv": np.array([200]),
        "theta2_tv": np.array([200]),
    }
    map_np = SkyUtils.analytic_Halo_signal_to_SkyArray(
        halo_idx=np.array([0]),
        halo_cat=halo_dic,
        extent=20,
        direction=[0],
        suppress=True,
        suppression_R=10,
        npix=400,
        signal="alpha",
    )
    assert np.unravel_index(map_np.argmax(), map_np.shape) == (200, 233)
    npt.assert_almost_equal(map_np.min(), -3.1027e-05, decimal=8)
    npt.assert_almost_equal(map_np.mean(), -3.7073e-08, decimal=11)
    npt.assert_almost_equal(map_np.max(), 3.1027e-05, decimal=8)
Exemple #3
0
    def convert_deflection_to_shear(
        self,
        on: Optional[str] = None,
        img: Optional[np.ndarray] = None,
        rtn: bool = False,
        orig_data: str = None,
    ) -> Tuple[np.ndarray, np.ndarray]:
        """
        Args:
            on: String to indicate which map in self.data should be used.
            img: 2D convergence map.
            rtn: Bool to indicate whether to attach result to class object or return.
            orig_data: What to do with data of the image to be processed:
                e.g. no, shallow, or deep copy

        Returns:
            gamma_1,2: 2D shear map [-]
        """
        assert self.quantity in [
            "alpha"
        ], "Shear can only be calculated from the deflection angle map"
        img = self._manage_img_data(img, orig_data)
        gamma_1, gamma_2 = SkyUtils.convert_deflection_to_shear(
            img, self._npix, self._opening_angle * un.deg)
        if rtn:
            return gamma_2, gamma_1
        else:
            self.data["gammax"] = gamma_2
            self.data["gammay"] = gamma_1
Exemple #4
0
def test__convert_convergence_to_deflection_ctypes(map_np):
    alpha_1, alpha_2 = SkyUtils.convert_convergence_to_deflection_ctypes(
        map_np,
        100,
        10 * un.deg,
    )
    npt.assert_almost_equal(
        alpha_1.min(),
        -0.0146571,
        decimal=6,
    )
    npt.assert_almost_equal(
        alpha_1.mean(),
        6.49e-05,
        decimal=6,
    )
    npt.assert_almost_equal(
        alpha_1.max(),
        0.0151880,
        decimal=6,
    )
Exemple #5
0
    def convert_convergence_to_deflection(
        self,
        on: Optional[str] = None,
        img: Optional[np.ndarray] = None,
        npix: Optional[int] = None,
        opening_angle: Optional[float] = None,
        rtn: bool = True,
        orig_data: str = None,
    ) -> Tuple[np.ndarray, np.ndarray]:
        """
        Args:
            on: String to indicate which map in self.data should be used.
            img: 2D convergence map.
            rtn: Bool to indicate whether to attach result to class object or return.
            orig_data: What to do with data of the image to be processed:
                e.g. no, shallow, or deep copy

        Returns:
            alpha_1,2: 2D deflection angle map [rad]
        """
        #TODO handle tiles/multiple images
        assert self.quantity in [
            "kappa_1",
            "kappa_2",
        ], "Deflection angle can only be calculated from the kappa map"
        img = self._manage_img_data(img, orig_data)

        if npix is None: npix = self._npix
        if opening_angle is None: opening_angle = self._opening_angle

        alpha_1, alpha_2 = SkyUtils.convert_convergence_to_deflection_ctypes(
            img, npix, opening_angle * un.deg)
        if rtn:
            return alpha_2, alpha_1
        else:
            self.data["defltx"] = alpha_2
            self.data["deflty"] = alpha_1
Exemple #6
0
    def from_dataframe(
        cls,
        map_df: pd.DataFrame,
        opening_angle: float,
        quantity: str,
        dir_in: str,
        map_file: str,
        npix: Optional[int] = None,
        convert_unit: bool = True,
    ) -> "SkyArray":
        """
        Initialize class by reading the skymap data from pandas DataFrame. 

        Args:
            map_filename:
                File path with which skymap pd.DataFrame can be loaded.
            opening_angle: [deg]
        """
        if convert_unit:
            map_df = SkyUtils.convert_code_to_phy_units(quantity, map_df)
        map_array = SkyIO.transform_RayRamsesOutput_to_NumpyNdarray(
            map_df[quantity].values)
        return cls.from_array(map_array, opening_angle, quantity, dir_in,
                              map_file)
Exemple #7
0
def test__convert_code_to_phy_units_of_shear(map_df):
    map_df = SkyUtils.convert_code_to_phy_units("shear_x", map_df)
    assert map_df.shear_x.values[0] == 1.0
Exemple #8
0
def test__convert_code_to_phy_units_of_iswrs(map_df):
    map_df = SkyUtils.convert_code_to_phy_units("isw_rs", map_df)
    assert map_df.isw_rs.values[0] == 1.0
Exemple #9
0
def test__convert_code_to_phy_units_of_kappa(map_df):
    map_df = SkyUtils.convert_code_to_phy_units("kappa_2", map_df)
    assert map_df.kappa_2.values[0] == 1.0
Exemple #10
0
def test__convert_code_to_phy_units_of_deflt(map_df):
    map_df = SkyUtils.convert_code_to_phy_units("deflt_x", map_df)
    assert map_df.deflt_x.values[0] == 1.0
Exemple #11
0
    def from_halo_catalogue_to_temperature_perturbation_map(
        cls,
        halo_cat: pd.DataFrame,
        extent: float = 1,
        direction: List[int] = [0, 1],
        suppress: bool = False,
        suppression_R: float = 1,
        npix: int = 8192,
        opening_angle: float = 20.,
        ncpus: int = 1,
    ) -> "SkyArray":
        """
        The Rees-Sciama / Birkinshaw-Gull / moving cluster of galaxies effect.

        Args:
            vel: transverse to the line-of-sight velocity, [km/sec]

        Returns:
            Temperature perturbation map, \Delta T / T_CMB
        """
        halo_dict = halo_cat[[
            "r200_deg",
            "r200_pix",
            "m200",
            "c_NFW",
            "Dc",
            "theta1_pix",
            "theta2_pix",
            "theta1_tv",
            "theta2_tv",
        ]].to_dict(orient='list')
        halo_idx = range(len(halo_dict["m200"]))

        if ncpus == 1:
            map_array = SkyUtils.analytic_Halo_signal_to_SkyArray(
                halo_idx, halo_dict, extent, direction, suppress,
                suppression_R, npix)
        else:
            halo_idx_batches = np.array_split(halo_idx, ncpus)
            map_sub_arrays = Parallel(n_jobs=ncpus)(
                delayed(SkyUtils.analytic_Halo_signal_to_SkyArray)(
                    halo_idx_batch,
                    halo_dict,
                    extent,
                    direction,
                    suppress,
                    suppression_R,
                    npix,
                ) for halo_idx_batch in halo_idx_batches)
            map_array = sum(map_sub_arrays)
        map_array[np.isinf(map_array)] = 0.

        if 1 in direction and 0 in direction:
            quantity = "isw_rs"
        elif 0 in direction:
            quantity = "isw_rs_x"
        else:
            quantity = "isw_rs_y"
        return cls(map_array,
                   opening_angle,
                   quantity,
                   dirs=None,
                   map_file=None)
Exemple #12
0
    def from_halo_dataframe(
        cls,
        halo_cat: Union[pd.DataFrame, pd.Series],
        npix: int = 8192,
        extent: float = 1,
        direction: List[int] = [0, 1],
        suppress: bool = False,
        suppression_R: float = 1,
        opening_angle: float = 20.,
        ncpus: int = 1,
        to: str = "dT",
    ) -> "SkyArray":
        """
        Identical to SkyArray.from_halo_series() but for a pd.DataFrame.
        """
        halo_dict = halo_cat[[
            "r200_deg",
            "r200_pix",
            "m200",
            "c_NFW",
            "Dc",
            "theta1_pix",
            "theta2_pix",
        ]].to_dict(orient='list')
        halo_idx = np.arange(len(halo_dict["m200"]))

        # set quantity label of SkyArray and method variable
        if to == "dT":
            quantity = "rs"
            halo_dict["theta1_tv"] = halo_cat["theta1_tv"].values
            halo_dict["theta2_tv"] = halo_cat["theta2_tv"].values
        elif to == "alpha":
            quantity = "alpha"
        else:
            SkyArrayWarning("The routine for this quantity is not implemented")

        # place directional indicator in SkyArray quantity label
        if 1 in direction and 0 in direction:
            pass
        elif 0 in direction:
            quantity += "_x"
        else:
            quantity += "_y"

        if ncpus == 1:
            map_array = SkyUtils.analytic_Halo_signal_to_SkyArray(
                halo_idx,
                halo_dict,
                extent,
                direction,
                suppress,
                suppression_R,
                npix,
                to,
            )
        else:
            halo_idx_batches = np.array_split(halo_idx, ncpus)
            map_sub_arrays = Parallel(n_jobs=ncpus)(
                delayed(SkyUtils.analytic_Halo_signal_to_SkyArray)(
                    halo_idx_batch,
                    halo_dict,
                    extent,
                    direction,
                    suppress,
                    suppression_R,
                    npix,
                    to,
                ) for halo_idx_batch in halo_idx_batches)
            map_array = sum(map_sub_arrays)
        map_array = np.nan_to_num(
            map_array,
            copy=False,
            nan=0.0,
            posinf=0.0,
            neginf=0.0,
        )

        return cls(map_array,
                   opening_angle,
                   quantity,
                   dirs=None,
                   map_file=None)