def residmap(self, prefix='', **kwargs): """Generate 2-D spatial residual maps using the current ROI model and the convolution kernel defined with the `model` argument. Parameters ---------- prefix : str String that will be prefixed to the output residual map files. {options} Returns ------- maps : dict A dictionary containing the `~fermipy.utils.Map` objects for the residual significance and amplitude. """ timer = Timer.create(start=True) self.logger.info('Generating residual maps') schema = ConfigSchema(self.defaults['residmap']) config = schema.create_config(self.config['residmap'], **kwargs) # Defining default properties of test source model config['model'].setdefault('Index', 2.0) config['model'].setdefault('SpectrumType', 'PowerLaw') config['model'].setdefault('SpatialModel', 'PointSource') config['model'].setdefault('Prefactor', 1E-13) o = self._make_residual_map(prefix, **config) if config['make_plots']: plotter = plotting.AnalysisPlotter(self.config['plotting'], fileio=self.config['fileio'], logging=self.config['logging']) plotter.make_residmap_plots(o, self.roi) self.logger.info('Finished residual maps') outfile = utils.format_filename(self.workdir, 'residmap', prefix=[o['name']]) if config['write_fits']: o['file'] = os.path.basename(outfile) + '.fits' self._make_residmap_fits(o, outfile + '.fits') if config['write_npy']: np.save(outfile + '.npy', o) self.logger.info('Execution time: %.2f s', timer.elapsed_time) return o
def run_analysis(self, argv): """Run this analysis""" args = self._parser.parse_args(argv) if not HAVE_ST: raise RuntimeError( "Trying to run fermipy analysis, but don't have ST") if args.load_baseline: gta = GTAnalysis.create(args.roi_baseline, args.config) else: gta = GTAnalysis(args.config, logging={'verbosity': 3}, fileio={'workdir_regex': '\.xml$|\.npy$'}) gta.setup() if is_not_null(args.input_pars): gta.load_parameters_from_yaml(args.input_pars) gta.write_roi(args.roi_baseline, save_model_map=True, save_weight_map=True, make_plots=args.make_plots) src_list = get_src_names(gta) plotter = plotting.AnalysisPlotter(gta.config['plotting'], fileio=gta.config['fileio'], logging=gta.config['logging']) if is_null(args.fit_strategy): return fit_strategy = load_yaml(args.fit_strategy) npred_current = None npred_prev = None plots_only = False for fit_stage in fit_strategy: mask = fit_stage.get('mask', None) npred_threshold = fit_stage.get('npred_threshold', 1.0e4) frac_threshold = fit_stage.get('frac_threshold', 0.5) npred_frac = fit_stage.get('npred_frac', 0.9999) if plots_only: gta.load_roi("%s.npy" % fit_stage['key']) npred_current = set_wts_get_npred_wt(gta, mask) skip_list_region = get_unchanged(src_list, npred_current, npred_prev, frac_threshold=frac_threshold) else: npred_current = set_wts_get_npred_wt(gta, mask) skip_list_region = get_unchanged(src_list, npred_current, npred_prev, frac_threshold=frac_threshold) gta.optimize(npred_frac=npred_frac, npred_threshold=npred_threshold, skip=skip_list_region) snapshot(gta, plotter, fit_stage['key'], make_plots=args.make_plots) npred_prev = npred_current npred_current = build_srcdict(gta, 'npred_wt')
def tscube(self, prefix='', **kwargs): """Generate a spatial TS map for a source component with properties defined by the `model` argument. This method uses the `gttscube` ST application for source fitting and will simultaneously fit the test source normalization as well as the normalizations of any background components that are currently free. The output of this method is a dictionary containing `~fermipy.skymap.Map` objects with the TS and amplitude of the best-fit test source. By default this method will also save maps to FITS files and render them as image files. Parameters ---------- prefix : str Optional string that will be prepended to all output files (FITS and rendered images). model : dict Dictionary defining the properties of the test source. do_sed : bool Compute the energy bin-by-bin fits. nnorm : int Number of points in the likelihood v. normalization scan. norm_sigma : float Number of sigma to use for the scan range. tol : float Critetia for fit convergence (estimated vertical distance to min < tol ). tol_type : int Absoulte (0) or relative (1) criteria for convergence. max_iter : int Maximum number of iterations for the Newton's method fitter remake_test_source : bool If true, recomputes the test source image (otherwise just shifts it) st_scan_level : int make_plots : bool Write image files. write_fits : bool Write a FITS file with the results of the analysis. Returns ------- maps : dict A dictionary containing the `~fermipy.skymap.Map` objects for TS and source amplitude. """ self.logger.info('Generating TS cube') schema = ConfigSchema(self.defaults['tscube']) schema.add_option('make_plots', True) schema.add_option('write_fits', True) schema.add_option('write_npy', True) config = schema.create_config(self.config['tscube'], **kwargs) maps = self._make_ts_cube(prefix, **config) if config['make_plots']: plotter = plotting.AnalysisPlotter(self.config['plotting'], fileio=self.config['fileio'], logging=self.config['logging']) plotter.make_tsmap_plots(maps, self.roi, suffix='tscube') self.logger.info("Finished TS cube") return maps
def tsmap(self, prefix='', **kwargs): """Generate a spatial TS map for a source component with properties defined by the `model` argument. The TS map will have the same geometry as the ROI. The output of this method is a dictionary containing `~fermipy.skymap.Map` objects with the TS and amplitude of the best-fit test source. By default this method will also save maps to FITS files and render them as image files. This method uses a simplified likelihood fitting implementation that only fits for the normalization of the test source. Before running this method it is recommended to first optimize the ROI model (e.g. by running :py:meth:`~fermipy.gtanalysis.GTAnalysis.optimize`). Parameters ---------- prefix : str Optional string that will be prepended to all output files. {options} Returns ------- tsmap : dict A dictionary containing the `~fermipy.skymap.Map` objects for TS and source amplitude. """ timer = Timer.create(start=True) schema = ConfigSchema(self.defaults['tsmap']) schema.add_option('loglevel', logging.INFO) schema.add_option('map_skydir', None, '', astropy.coordinates.SkyCoord) schema.add_option('map_size', 1.0) schema.add_option('threshold', 1E-2, '', float) schema.add_option('use_pylike', True, '', bool) schema.add_option('outfile', None, '', str) config = schema.create_config(self.config['tsmap'], **kwargs) # Defining default properties of test source model config['model'].setdefault('Index', 2.0) config['model'].setdefault('SpectrumType', 'PowerLaw') config['model'].setdefault('SpatialModel', 'PointSource') self.logger.log(config['loglevel'], 'Generating TS map') o = self._make_tsmap_fast(prefix, **config) if config['make_plots']: plotter = plotting.AnalysisPlotter(self.config['plotting'], fileio=self.config['fileio'], logging=self.config['logging']) plotter.make_tsmap_plots(o, self.roi) self.logger.log(config['loglevel'], 'Finished TS map') outfile = config.get('outfile', None) if outfile is None: outfile = utils.format_filename(self.workdir, 'tsmap', prefix=[o['name']]) else: outfile = os.path.join(self.workdir, os.path.splitext(outfile)[0]) if config['write_fits']: o['file'] = os.path.basename(outfile) + '.fits' self._make_tsmap_fits(o, outfile + '.fits') if config['write_npy']: np.save(outfile + '.npy', o) self.logger.log(config['loglevel'], 'Execution time: %.2f s', timer.elapsed_time) return o
def tsmap(self, prefix='', **kwargs): """Generate a spatial TS map for a source component with properties defined by the `model` argument. The TS map will have the same geometry as the ROI. The output of this method is a dictionary containing `~fermipy.skymap.Map` objects with the TS and amplitude of the best-fit test source. By default this method will also save maps to FITS files and render them as image files. This method uses a simplified likelihood fitting implementation that only fits for the normalization of the test source. Before running this method it is recommended to first optimize the ROI model (e.g. by running :py:meth:`~fermipy.gtanalysis.GTAnalysis.optimize`). Parameters ---------- prefix : str Optional string that will be prepended to all output files (FITS and rendered images). model : dict Dictionary defining the properties of the test source. exclude : list Source or sources that will be removed from the model when computing the TS map. loge_bounds : list Restrict the analysis to an energy range (emin,emax) in log10(E/MeV) that is a subset of the analysis energy range. By default the full analysis energy range will be used. If either emin/emax are None then only an upper/lower bound on the energy range wil be applied. max_kernel_radius : float Set the maximum radius of the test source kernel. Using a smaller value will speed up the TS calculation at the loss of accuracy. The default value is 3 degrees. make_plots : bool Generate plots. write_fits : bool Write the output to a FITS file. write_npy : bool Write the output dictionary to a numpy file. Returns ------- maps : dict A dictionary containing the `~fermipy.skymap.Map` objects for TS and source amplitude. """ self.logger.info('Generating TS map') schema = ConfigSchema(self.defaults['tsmap']) schema.add_option('make_plots', False) schema.add_option('write_fits', True) schema.add_option('write_npy', True) schema.add_option('map_skydir', None, '', astropy.coordinates.SkyCoord) schema.add_option('map_size', 1.0) schema.add_option('exclude', None, '', list) schema.add_option('threshold', 1E-2, '', float) config = schema.create_config(self.config['tsmap'], **kwargs) # Defining default properties of test source model config['model'].setdefault('Index', 2.0) config['model'].setdefault('SpectrumType', 'PowerLaw') config['model'].setdefault('SpatialModel', 'PointSource') config['model'].setdefault('Prefactor', 1E-13) maps = self._make_tsmap_fast(prefix, **config) if config['make_plots']: plotter = plotting.AnalysisPlotter(self.config['plotting'], fileio=self.config['fileio'], logging=self.config['logging']) plotter.make_tsmap_plots(maps, self.roi) self.logger.info('Finished TS map') return maps
def residmap(self, prefix='', **kwargs): """Generate 2-D spatial residual maps using the current ROI model and the convolution kernel defined with the `model` argument. Parameters ---------- prefix : str String that will be prefixed to the output residual map files. model : dict Dictionary defining the properties of the convolution kernel. exclude : str or list of str Source or sources that will be removed from the model when computing the residual map. loge_bounds : list Restrict the analysis to an energy range (emin,emax) in log10(E/MeV) that is a subset of the analysis energy range. By default the full analysis energy range will be used. If either emin/emax are None then only an upper/lower bound on the energy range wil be applied. make_plots : bool Generate plots. write_fits : bool Write the output to a FITS file. write_npy : bool Write the output dictionary to a numpy file. Returns ------- maps : dict A dictionary containing the `~fermipy.utils.Map` objects for the residual significance and amplitude. """ self.logger.info('Generating residual maps') config = copy.deepcopy(self.config['residmap']) config = utils.merge_dict(config, kwargs, add_new_keys=True) # Defining default properties of test source model config['model'].setdefault('Index', 2.0) config['model'].setdefault('SpectrumType', 'PowerLaw') config['model'].setdefault('SpatialModel', 'PointSource') config['model'].setdefault('Prefactor', 1E-13) make_plots = kwargs.get('make_plots', False) maps = self._make_residual_map(prefix, config, **kwargs) if make_plots: plotter = plotting.AnalysisPlotter(self.config['plotting'], fileio=self.config['fileio'], logging=self.config['logging']) plotter.make_residmap_plots(maps, self.roi) self.logger.info('Finished residual maps') return maps