def __init__(self, name, maptree, response_file, roi, flat_sky_pixels_size=0.17): # Store ROI self._roi = roi # Set up the flat-sky projection self._flat_sky_projection = roi.get_flat_sky_projection(flat_sky_pixels_size) # Read map tree (data) self._maptree = map_tree_factory(maptree, roi=roi) # Read detector response_file self._response = hawc_response_factory(response_file) # Use a renormalization of the background as nuisance parameter # NOTE: it is fixed to 1.0 unless the user explicitly sets it free (experimental) self._nuisance_parameters = collections.OrderedDict() self._nuisance_parameters['%s_bkg_renorm' % name] = Parameter('%s_bkg_renorm' % name, 1.0, min_value=0.5, max_value=1.5, delta=0.01, desc="Renormalization for background map", free=False, is_normalization=False) # Instance parent class super(HAL, self).__init__(name, self._nuisance_parameters) self._likelihood_model = None # These lists will contain the maps for the point sources self._convolved_point_sources = ConvolvedSourcesContainer() # and this one for extended sources self._convolved_ext_sources = ConvolvedSourcesContainer() # All energy/nHit bins are loaded in memory self._all_planes = list(self._maptree.analysis_bins_labels) # The active planes list always contains the list of *indexes* of the active planes self._active_planes = None # Set up the transformations from the flat-sky projection to Healpix, as well as the list of active pixels # (one for each energy/nHit bin). We make a separate transformation because different energy bins might have # different nsides self._active_pixels = collections.OrderedDict() self._flat_sky_to_healpix_transform = collections.OrderedDict() for bin_id in self._maptree: this_maptree = self._maptree[bin_id] this_nside = this_maptree.nside this_active_pixels = roi.active_pixels(this_nside) this_flat_sky_to_hpx_transform = FlatSkyToHealpixTransform(self._flat_sky_projection.wcs, 'icrs', this_nside, this_active_pixels, (self._flat_sky_projection.npix_width, self._flat_sky_projection.npix_height), order='bilinear') self._active_pixels[bin_id] = this_active_pixels self._flat_sky_to_healpix_transform[bin_id] = this_flat_sky_to_hpx_transform # This will contain a list of PSF convolutors for extended sources, if there is any in the model self._psf_convolutors = None # Pre-compute the log-factorial factor in the likelihood, so we do not keep to computing it over and over # again. self._log_factorials = collections.OrderedDict() # We also apply a bias so that the numerical value of the log-likelihood stays small. This helps when # fitting with algorithms like MINUIT because the convergence criterium involves the difference between # two likelihood values, which would be affected by numerical precision errors if the two values are # too large self._saturated_model_like_per_maptree = collections.OrderedDict() # The actual computation is in a method so we can recall it on clone (see the get_simulated_dataset method) self._compute_likelihood_biases() # This will save a clone of self for simulations self._clone = None # Integration method for the PSF (see psf_integration_method) self._psf_integration_method = "exact"
def test_healpixRoi(geminga_maptree, geminga_response): #test to make sure writing a model with HealpixMapROI works fine ra, dec = 101.7, 16. data_radius = 9. model_radius = 24. m = np.zeros(hp.nside2npix(NSIDE)) vec = Sky2Vec(ra, dec) m[hp.query_disc(NSIDE, vec, (data_radius * u.degree).to(u.radian).value, inclusive=False)] = 1 #hp.fitsfunc.write_map("roitemp.fits" , m, nest=False, coord="C", partial=False, overwrite=True ) map_roi = HealpixMapROI(data_radius=data_radius, ra=ra, dec=dec, model_radius=model_radius, roimap=m) #fits_roi = HealpixMapROI(data_radius=data_radius, ra=ra, dec=dec, model_radius=model_radius, roifile="roitemp.fits") hawc = HAL("HAWC", geminga_maptree, geminga_response, map_roi) hawc.set_active_measurements(1, 9) ''' Define model: Two sources, 1 point, 1 extended Same declination, but offset in RA Different spectral idnex, but both power laws ''' pt_shift = 3.0 ext_shift = 2.0 # First soource spectrum1 = Powerlaw() source1 = PointSource("point", ra=ra + pt_shift, dec=dec, spectral_shape=spectrum1) spectrum1.K = 1e-12 / (u.TeV * u.cm**2 * u.s) spectrum1.piv = 1 * u.TeV spectrum1.index = -2.3 spectrum1.piv.fix = True spectrum1.K.fix = True spectrum1.index.fix = True # Second source shape = Gaussian_on_sphere(lon0=ra - ext_shift, lat0=dec, sigma=0.3) spectrum2 = Powerlaw() source2 = ExtendedSource("extended", spatial_shape=shape, spectral_shape=spectrum2) spectrum2.K = 1e-12 / (u.TeV * u.cm**2 * u.s) spectrum2.piv = 1 * u.TeV spectrum2.index = -2.0 spectrum2.piv.fix = True spectrum2.K.fix = True spectrum2.index.fix = True shape.lon0.fix = True shape.lat0.fix = True shape.sigma.fix = True model = Model(source1, source2) hawc.set_model(model) # Write the model map model_map_tree = hawc.write_model_map("test.hd5", test_return_map=True) # Read the model back hawc_model = map_tree_factory('test.hd5', map_roi) # Check written model and read model are the same check_map_trees(hawc_model, model_map_tree) os.remove("test.hd5")
action="store_true", help='Overwrite existing files') args = parser.parse_args() filename = os.path.expandvars(args.input) outfile = os.path.expandvars(args.output) clobber = args.overwrite if not os.path.isfile(filename): print("You must enter a valid file!") exit(1) # Export the entire map tree (full sky) # this throws a warning for partial map trees but its OK maptree = map_tree_factory(filename, None) now = datetime.now() startMJD = 56987.9286332 #FIRST HEADER ''' COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy COMMENT and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H DATE = '2018-12-01T02:31:14' / file creation date (YYYY-MM-DDThh:mm:ss UT) STARTMJD= 56987.9286332451 / MJD of first event STOPMJD = 58107.2396848326 / MJD of last event NEVENTS = -1. / Number of events in map TOTDUR = 24412.9020670185 / Total integration time [hours] DURATION= 1.9943578604616 / Avg integration time [hours] MAPTYPE = 'duration' / e.g. Skymap, Moonmap
def test_model_residual_maps(geminga_maptree, geminga_response, geminga_roi): #data_radius = 5.0 #model_radius = 7.0 output = dirname(geminga_maptree) ra_src, dec_src = 101.7, 16.0 maptree, response, roi = geminga_maptree, geminga_response, geminga_roi hawc = HAL("HAWC", maptree, response, roi) # Use from bin 1 to bin 9 hawc.set_active_measurements(1, 9) # Display information about the data loaded and the ROI hawc.display() ''' Define model: Two sources, 1 point, 1 extended Same declination, but offset in RA Different spectral index, but both power laws ''' pt_shift = 3.0 ext_shift = 2.0 # First source spectrum1 = Powerlaw() source1 = PointSource("point", ra=ra_src + pt_shift, dec=dec_src, spectral_shape=spectrum1) spectrum1.K = 1e-12 / (u.TeV * u.cm**2 * u.s) spectrum1.piv = 1 * u.TeV spectrum1.index = -2.3 spectrum1.piv.fix = True spectrum1.K.fix = True spectrum1.index.fix = True # Second source shape = Gaussian_on_sphere(lon0=ra_src - ext_shift, lat0=dec_src, sigma=0.3) spectrum2 = Powerlaw() source2 = ExtendedSource("extended", spatial_shape=shape, spectral_shape=spectrum2) spectrum2.K = 1e-12 / (u.TeV * u.cm**2 * u.s) spectrum2.piv = 1 * u.TeV spectrum2.index = -2.0 shape.lon0.fix = True shape.lat0.fix = True shape.sigma.fix = True spectrum2.piv.fix = True spectrum2.K.fix = True spectrum2.index.fix = True # Define model with both sources model = Model(source1, source2) # Define the data we are using data = DataList(hawc) # Define the JointLikelihood object (glue the data to the model) jl = JointLikelihood(model, data, verbose=False) # This has the effect of loading the model cache fig = hawc.display_spectrum() # the test file names model_file_name = "{0}/test_model.hdf5".format(output) residual_file_name = "{0}/test_residual.hdf5".format(output) # Write the map trees for testing model_map_tree = hawc.write_model_map(model_file_name, poisson_fluctuate=True, test_return_map=True) residual_map_tree = hawc.write_residual_map(residual_file_name, test_return_map=True) # Read the maps back in hawc_model = map_tree_factory(model_file_name, roi) hawc_residual = map_tree_factory(residual_file_name, roi) check_map_trees(hawc_model, model_map_tree) check_map_trees(hawc_residual, residual_map_tree)