def display_stacked_image(self, smoothing_kernel_sigma=0.5): """ Display a map with all active analysis bins stacked together. :param smoothing_kernel_sigma: sigma for the Gaussian smoothing kernel to apply :return: a matplotlib.Figure instance """ # This is the resolution (i.e., the size of one pixel) of the image in arcmin resolution = 3.0 # The image is going to cover the diameter plus 20% padding xsize = self._get_optimal_xsize(resolution) active_planes_bins = [self._maptree[x] for x in self._active_planes] # Get the center of the projection for this plane this_ra, this_dec = self._roi.ra_dec_center # Healpix uses longitude between -180 and 180, while R.A. is between 0 and 360. We need to fix that: longitude = ra_to_longitude(this_ra) # Declination is already between -90 and 90 latitude = this_dec total = None for i, data_analysis_bin in enumerate(active_planes_bins): # Plot data background_map = data_analysis_bin.background_map.as_dense() this_data = data_analysis_bin.observation_map.as_dense( ) - background_map idx = np.isnan(this_data) # this_data[idx] = hp.UNSEEN if i == 0: total = this_data else: # Sum only when there is no UNSEEN, so that the UNSEEN pixels will stay UNSEEN total[~idx] += this_data[~idx] delta_coord = (self._roi.data_radius.to("deg").value * 2.0) / 15.0 fig, sub = plt.subplots(1, 1) proj = self._represent_healpix_map(fig, total, longitude, latitude, xsize, resolution, smoothing_kernel_sigma) cax = sub.imshow(proj, origin='lower') fig.colorbar(cax) sub.axis('off') hp.graticule(delta_coord, delta_coord) return fig
def display_fit(self, smoothing_kernel_sigma=0.1, display_colorbar=False): """ Make a figure containing 4 maps for each active analysis bins with respectively model, data, background and residuals. The model, data and residual maps are smoothed, the background map is not. :param smoothing_kernel_sigma: sigma for the Gaussian smoothing kernel, for all but background maps :param display_colorbar: whether or not to display the colorbar in the residuals :return: a matplotlib.Figure """ n_point_sources = self._likelihood_model.get_number_of_point_sources() n_ext_sources = self._likelihood_model.get_number_of_extended_sources() # This is the resolution (i.e., the size of one pixel) of the image resolution = 3.0 # arcmin # The image is going to cover the diameter plus 20% padding xsize = self._get_optimal_xsize(resolution) n_active_planes = len(self._active_planes) n_columns = 4 fig, subs = plt.subplots(n_active_planes, n_columns, figsize=(2.7 * n_columns, n_active_planes * 2), squeeze=False) with progress_bar(len(self._active_planes), title='Smoothing maps') as prog_bar: images = ['None'] * n_columns for i, plane_id in enumerate(self._active_planes): data_analysis_bin = self._maptree[plane_id] # Get the center of the projection for this plane this_ra, this_dec = self._roi.ra_dec_center # Make a full healpix map for a second whole_map = self._get_model_map(plane_id, n_point_sources, n_ext_sources).as_dense() # Healpix uses longitude between -180 and 180, while R.A. is between 0 and 360. We need to fix that: longitude = ra_to_longitude(this_ra) # Declination is already between -90 and 90 latitude = this_dec # Background and excess maps bkg_subtracted, _, background_map = self._get_excess(data_analysis_bin, all_maps=True) # Make all the projections: model, excess, background, residuals proj_model = self._represent_healpix_map(fig, whole_map, longitude, latitude, xsize, resolution, smoothing_kernel_sigma) # Here we removed the background otherwise nothing is visible # Get background (which is in a way "part of the model" since the uncertainties are neglected) proj_data = self._represent_healpix_map(fig, bkg_subtracted, longitude, latitude, xsize, resolution, smoothing_kernel_sigma) # No smoothing for this one (because a goal is to check it is smooth). proj_bkg = self._represent_healpix_map(fig, background_map, longitude, latitude, xsize, resolution, None) proj_residuals = proj_data - proj_model # Common color scale range for model and excess maps vmin = min(np.nanmin(proj_model), np.nanmin(proj_data)) vmax = max(np.nanmax(proj_model), np.nanmax(proj_data)) # Plot model images[0] = subs[i][0].imshow(proj_model, origin='lower', vmin=vmin, vmax=vmax) subs[i][0].set_title('model, bin {}'.format(data_analysis_bin.name)) # Plot data map images[1] = subs[i][1].imshow(proj_data, origin='lower', vmin=vmin, vmax=vmax) subs[i][1].set_title('excess, bin {}'.format(data_analysis_bin.name)) # Plot background map. images[2] = subs[i][2].imshow(proj_bkg, origin='lower') subs[i][2].set_title('background, bin {}'.format(data_analysis_bin.name)) # Now residuals images[3] = subs[i][3].imshow(proj_residuals, origin='lower') subs[i][3].set_title('residuals, bin {}'.format(data_analysis_bin.name)) # Remove numbers from axis for j in range(n_columns): subs[i][j].axis('off') if display_colorbar: for j, image in enumerate(images): plt.colorbar(image, ax=subs[i][j]) prog_bar.increase() fig.set_tight_layout(True) return fig