def from_file( npix: int, theta: float, quantity: str, dir_in: str, map_file: Optional[str] = None, convert_unit: bool = True, sky_type: str = "array", ) -> Union[SkyArray, SkyHealpix]: """ Initialize class by reading the skymap data from pandas hdf5 file or numpy array. The file can be pointed at via map_filename or file_dsc. 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. sky_type: Indicate what format the sky should have, as it makes different operations are accessible: [healpix, array] """ assert sky_type in [ "array", "healpix", ], "The declared 'sky_type' is not known" assert map_file, SkyMapWarning("There is no file being pointed at") file_extension = map_file.split(".")[-1] if sky_type == "array": if file_extension == "h5": map_df = pd.read_hdf(map_file, key="df") skymap = SkyArray.from_dataframe( map_df, npix, theta, quantity, dir_in, map_file, convert_unit, ) elif file_extension in [ "npy", "fits", ]: # only possible for SkyArray if file_extension == "npy": map_array = np.load(map_file) elif file_extension == "fits": map_array = ConvergenceMap.load(map_file, format="fits").data skymap = SkyArray.from_array(map_array, npix, theta, quantity, dir_in, map_file) elif sky_type == "healpix": skymap = SkyHealpix.from_file(map_file, npix, theta, quantity, dir_in) return skymap
def from_array( map_array: np.array, npix: int, theta: float, quantity: str, dir_in: str, map_file: Optional[str] = None, sky_type: str = "array", ) -> Union[SkyArray, SkyHealpix]: """ Initialize class by reading the skymap data from np.ndarray. 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 sky_type == "array": skymap = SkyArray.from_array(map_array, npix, theta, quantity, dir_in, map_file) elif sky_type == "healpix": skymap = SkyHealpix.from_array(map_array, npix, theta, quantity, dir_in, map_file) return skymap
def from_dataframe( npix: int, theta: float, quantity: str, dir_in: str, map_df: pd.DataFrame, map_file: str, convert_unit: bool = True, sky_type: str = "array", ) -> Union[SkyArray, SkyHealpix]: """ 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. sky_type: Indicate what format the sky should have, as it makes different operations are accessible: [healpix, array] """ assert sky_type in [ "array", "healpix", ], "The declared 'sky_type' is not known" if sky_type == "array": skymap = SkyArray.from_dataframe(map_df, npix, theta, quantity, dir_in, map_file, convert_unit) elif sky_type == "healpix": skymap = SkyHealpix.from_dataframe(npix, theta, quantity, dir_in, map_df, map_file, convert_unit) return skymap
def get_dipole_image(img: Type[SkyArray], cen_pix: tuple, extend_pix: int, extend_deg: float) -> Type[SkyArray]: xlim = np.array([cen_pix[1] - extend_pix, cen_pix[1] + extend_pix]).astype(int) ylim = np.array([cen_pix[0] - extend_pix, cen_pix[0] + extend_pix]).astype(int) img_zoom = SkyArray.from_array( img.crop(xlim, ylim, of="orig", rtn=True), opening_angle=2 * extend_deg, quantity=None, dir_in=None, ) return img_zoom
def dipole_cross_section( dipole_index: List[int], dipoles: pd.DataFrame, filepath_map: str, extent: float, theta: float = 20., arrow_scale: Optional[float] = None, ) -> mpl.figure.Figure: """ """ fig, axis = plt.subplots( 1, len(dipole_index), figsize=(16, 5), facecolor="w", edgecolor="k", ) # Load Map skyarray = SkyArray.from_file( filepath_map, opening_angle=theta, quantity="isw_rs", dir_in=None, convert_unit=True, ) for idx, ax in enumerate(axis.reshape(-1)): dip = dipoles[dipoles["id"] == dipole_index[idx]] zoom = Dipoles.get_dipole_image( skyarray, (dip.theta1_pix.values[0], dip.theta2_pix.values[0]), dip.r200_pix.values[0] * extent, dip.r200_deg.values[0] * extent, ) ax.plot( zoom[:, len(zoom)//2], color=color(0), ) ax.text( 0.5, 0.5, "dip.idx = %.2f" % dipole_index[idx], color='black', horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, zorder=3, ) ax.set_ylabel(r"pixels") axis[0].set_ylabel(r"$\theta_2 \quad $[K]") return fig
def test__skyarray_from_halo_series_alpha(halo): sky_alpha = SkyArray.from_halo_series( halo, npix=int(2 * halo.r200_pix * 10) + 1, extent=10, direction=[0, 1], suppress=True, suppression_R=10, to="alpha", ) assert sky_alpha.data["orig"].min() == pytest.approx(-9.02262751486356e-05, rel=1e-3) assert sky_alpha.data["orig"].mean() == pytest.approx( 1.647689725443215e-21, rel=1e-15) assert sky_alpha.data["orig"].max() == pytest.approx(9.022627514863563e-05, rel=1e-13)
def test__skyarray_from_halo_series_dT(halo): sky_dt = SkyArray.from_halo_series( halo, npix=int(2 * halo.r200_pix * 10) + 1, extent=10, direction=[0, 1], suppress=True, suppression_R=10, to="dT", ) assert sky_dt.data["orig"].min() == pytest.approx(-1.7028239210299853e-07, rel=1e-5) assert sky_dt.data["orig"].mean() == pytest.approx(-1.9386409608471563e-25, rel=1e-15) assert sky_dt.data["orig"].max() == pytest.approx(1.7028239210299855e-07, rel=1e-5)
def maps_with_vel_field( halos: pd.DataFrame, vel_key: str, filepaths_map: str, box_nrs: List[int], snap_nrs: List[int], redshifts: List[float], theta: float = 20., npix: int = 40, ) -> mpl.figure.Figure: """ Plot sky-data contained in filepaths_map overlayed by halo-data in halos """ fig, axis = plt.subplots( 1, len(redshifts), figsize=(16, 5), sharex=True, sharey=True, facecolor="w", edgecolor="k", ) fig.subplots_adjust(hspace=0.05, wspace=0.05) for idx, ax in enumerate(axis.reshape(-1)): # Load Map skyarray = SkyArray.from_file( filepaths_map[idx], opening_angle=theta, quantity="isw_rs", dir_in=None, convert_unit=True, ) skyarray.data["orig"] *= 1e6 # Select halos halos_sel = halos[ (halos["box_nr"] == box_nrs[idx]) & (halos["ray_nr"] == snap_nrs[idx]) ] # get velocity field coord_x, coord_y, vel_x, vel_y = _get_velocity_field(halos_sel, vel_key, npix) mi = np.min(skyarray.data["orig"]) * 0.9 ma = np.max(skyarray.data["orig"]) * 0.9 divnorm = colors.DivergingNorm(vmin=mi, vcenter=0.0, vmax=ma) if idx == 0: bkg = ax.imshow( skyarray.data["orig"], extent=[ 0, skyarray.opening_angle, 0, skyarray.opening_angle, ], cmap=cm.RdBu_r, norm=divnorm, origin="lower", zorder=0, ) else: ax.imshow( skyarray.data["orig"], extent=[ 0, skyarray.opening_angle, 0, skyarray.opening_angle, ], cmap=cm.RdBu_r, norm=divnorm, origin="lower", zorder=0, ) ax.quiver( coord_x, coord_y, vel_x, vel_y, zorder=1, ) ax.text( skyarray.opening_angle/10, skyarray.opening_angle/10, "z=%.2f" % redshifts[idx], color='black', bbox=dict( facecolor='w', edgecolor='w', alpha=0.8, boxstyle='round,pad=0.5', ), ) ax.set_xlabel(r"$\theta_1$ [deg]", size=14) ax.set_xticks(ax.get_xticks()) ax.set_yticks(ax.get_yticks()[::2]) axis[0].set_ylabel(r"$\theta_2$ [deg]", size=14) axcolor = fig.add_axes([0.92,0.22,0.009,0.6]) cbar = fig.colorbar(bkg, cax=axcolor, pad=0.1) cbar.set_label(r'$\Delta T_{\rm ISW} \quad [\mu {\rm K}]$') return fig
def analytical_dipole_maps( dipole_index: List[int], dipoles: pd.DataFrame, extent: float, theta: float = 20., arrow_scale: Optional[float] = None, ) -> mpl.figure.Figure: """ Plot analytical RS-map obtained from NFW profile. """ fig, axis = plt.subplots( 1, len(dipole_index), figsize=(16, 5), facecolor="w", edgecolor="k", ) for idx, ax in enumerate(axis.reshape(-1)): dip = dipoles[dipoles["id"] == dipole_index[idx]].squeeze() # Load Map skyrs = SkyArray.from_halo_series( dip, npix=int(2 * dip.r200_pix * 10) + 1, extent=20, direction=[0, 1], suppress=True, suppression_R=10, to="dT", ) skyalpha = SkyArray.from_halo_series( dip, npix=int(2 * dip.r200_pix * 10) + 1, extent=20, direction=[0, 1], suppress=True, suppression_R=10, to="alpha", ) skymaps = Dipoles.apply_filter_on_single_dipole_image( dip, {"rs": skyrs, "alpha": skyalpha}, ) theta1_mvel, theta2_mvel = Dipoles.get_single_transverse_velocity_from_sky( skymaps["rs_x"], skymaps["rs_y"], skymaps["alpha_x"], skymaps["alpha_y"], ) print(theta1_mvel, theta2_mvel) ax.imshow( skyrs.data["orig"] * 1e6, extent=[ dip.theta1_deg - dip.r200_deg*extent, dip.theta1_deg + dip.r200_deg*extent, dip.theta2_deg - dip.r200_deg*extent, dip.theta2_deg + dip.r200_deg*extent, ], cmap=cm.RdBu_r, origin="lower", zorder=0, ) ax.add_artist( plt.Circle( (dip.theta1_deg, dip.theta2_deg), dip.r200_deg, fill=False, #alpha=0.5, color="lime", #edgecolor="w", linewidth=1, zorder=1, ) ) ax.quiver( dip.theta1_deg, dip.theta2_deg, theta1_mvel, theta2_mvel, facecolor="grey", edgecolor="w", scale=arrow_scale, zorder=2, ) ax.quiver( dip.theta1_deg, dip.theta2_deg, dip.theta1_tv, dip.theta2_tv, linestyle='dotted', linewidth=1, facecolor='none', edgecolor="k", scale=arrow_scale, zorder=2, ) ax.text( dip.theta1_deg - dip.r200_deg*extent*0.9, dip.theta2_deg - dip.r200_deg*extent*0.9, "dip.idx = %.2f" % dipole_index[idx], color='black', bbox=dict( facecolor='w', edgecolor='w', alpha=0.8, boxstyle='round,pad=0.5', ), zorder=3, ) ax.set_xlabel(r"$\theta_1 \quad $[deg]") axis[0].set_ylabel(r"$\theta_2 \quad $[deg]") return fig
def simulated_dipole_maps( dipole_index: List[int], dipoles: pd.DataFrame, skymap: Union[str, np.ndarray], extent: float, theta: float = 20., arrow_scale: Optional[float] = None, mlabel: str = "_mtvel_ana", ) -> mpl.figure.Figure: """ Plot sky-data contained in filepaths_map overlayed by halo-data in halos """ fig, axis = plt.subplots( 1, len(dipole_index), figsize=(16, 5), facecolor="w", edgecolor="k", ) # Load Map if isinstance(skymap, str): skyarray = SkyArray.from_file( skymap, opening_angle=theta, quantity="isw_rs", dir_in=None, convert_unit=True, ) if isinstance(skymap, np.ndarray): skyarray = SkyArray.from_array( skymap, opening_angle=theta, quantity="isw_rs", dir_in=None, ) for idx, ax in enumerate(axis.reshape(-1)): dip = dipoles[dipoles["id"] == dipole_index[idx]] zoom = Dipoles.get_dipole_image( skyarray, (dip.theta1_pix.values[0], dip.theta2_pix.values[0]), dip.r200_pix.values[0] * extent, dip.r200_deg.values[0] * extent, ) ax.imshow( zoom.data["orig"] * 1e6, extent=[ dip.theta1_deg.values[0] - dip.r200_deg.values[0]*extent, dip.theta1_deg.values[0] + dip.r200_deg.values[0]*extent, dip.theta2_deg.values[0] - dip.r200_deg.values[0]*extent, dip.theta2_deg.values[0] + dip.r200_deg.values[0]*extent, ], cmap=cm.RdBu_r, origin="lower", zorder=0, ) ax.add_artist( plt.Circle( (dip.theta1_deg.values[0], dip.theta2_deg.values[0]), dip.r200_deg.values[0], fill=False, #alpha=0.5, color="lime", #edgecolor="w", linewidth=1, zorder=1, ) ) ax.quiver( dip.theta1_deg.values[0], dip.theta2_deg.values[0], dip["theta1" + mlabel].values[0], dip["theta2" + mlabel].values[0], facecolor="grey", edgecolor="w", scale=arrow_scale, zorder=2, ) ax.quiver( dip.theta1_deg, dip.theta2_deg, dip.theta1_tv, dip.theta2_tv, linestyle='dotted', linewidth=1, facecolor='none', edgecolor="k", scale=arrow_scale, zorder=2, ) ax.text( dip.theta1_deg.values[0] - dip.r200_deg.values[0]*extent*0.9, dip.theta2_deg.values[0] - dip.r200_deg.values[0]*extent*0.9, "dip.idx = %.2f" % dipole_index[idx], color='black', bbox=dict( facecolor='w', edgecolor='w', alpha=0.8, boxstyle='round,pad=0.5', ), zorder=3, ) ax.set_xlabel(r"$\theta_1 \quad $[deg]") axis[0].set_ylabel(r"$\theta_2 \quad $[deg]") return fig