def get_default_sources(self): point_sources, diffuse_sources = [], [] model = PowerLaw(index=self.powerlaw_index, e0=np.sqrt(self.emin*self.emax)) model.set_flux(self.flux, emin=self.emin, emax=self.emax) ps = PointSource( name = 'source', model = model.copy(), skydir = self.roi_dir) point_sources.append(ps) if self.isotropic_bg: ds = get_sreekumar() diffuse_sources.append(ds) if self.nearby_source: ps = PointSource( name = 'nearby_source', model = model.copy(), skydir = SkyDir(self.roi_dir.ra(),self.roi_dir.dec()+3) ) point_sources.append(ps) return point_sources, diffuse_sources
class PointSource(object): """ combine name, skydir, model """ def __init__(self, skydir, name, model=None, free_parameters=True, leave_parameters=False): self.name = name self.skydir = skydir self.model = PowerLaw() if model is None else model #if not free_parameters: if not leave_parameters: for i in xrange(len(self.model.free)): self.model.free[i] = free_parameters self.duplicate = False def __str__(self): return '\n'.join([ '\n', '=' * 60, 'Name:\t\t%s' % (self.name), 'R.A. (J2000):\t%.5f' % (self.skydir.ra()), 'Dec. (J2000):\t%.5f' % (self.skydir.dec()), 'Model:\t\t%s' % (self.model.full_name()), '\t' + self.model.__str__(indent='\t'), ]) def copy(self): """ Create a deep copy of the point source. """ return PointSource(SkyDir(self.skydir.ra(), self.skydir.dec()), self.name, self.model.copy(), leave_parameters=True)
def get_default_sources(self): point_sources, diffuse_sources = [], [] model = PowerLaw(index=self.powerlaw_index, e0=np.sqrt(self.emin * self.emax)) model.set_flux(self.flux, emin=self.emin, emax=self.emax) ps = PointSource(name='source', model=model.copy(), skydir=self.roi_dir) point_sources.append(ps) if self.isotropic_bg: ds = get_sreekumar() diffuse_sources.append(ds) if self.nearby_source: ps = PointSource(name='nearby_source', model=model.copy(), skydir=SkyDir(self.roi_dir.ra(), self.roi_dir.dec() + 3)) point_sources.append(ps) return point_sources, diffuse_sources
type = args.type, mc = dict( extension=extension_mc, gal=[ skydir_mc.l(), skydir_mc.b() ], cel=[ skydir_mc.ra(), skydir_mc.dec() ], model=spectrum_to_dict(model_mc), flux=pointlike_model_to_flux(model_mc, emin, emax), ) ) tempdir = mkdtemp() point = 'point' ps = PointSource( name=point, model=model_mc.copy(), skydir = skydir_mc ) extended = 'extended_%f' % extension_mc if extension_mc > 0: sm = Disk(sigma=extension_mc, center=skydir_mc) es = ExtendedSource( name=extended, model=model_mc, spatial_model=sm, ) sim_es = es else: # If the source has no extension, # simulate the source as point-like
for fit_irf in ["P7SOURCE_V6", "P7SOURCE_V4"]: print "Using fit_irf %s" % fit_irf sa = SpectralAnalysis( ds, irf=fit_irf, roi_dir=skydir_mc, maxROI=10, minROI=10, event_class=0, # not this is necessary for MC data emin=emin, emax=emax, ) source_guess = PointSource(name=name_mc, skydir=skydir_mc, model=model_mc.copy()) roi = sa.roi( roi_dir=skydir_mc, diffuse_sources=[j.copy() for j in diffuse_sources], point_sources=[source_guess] ) print "bins = ", roi.bin_edges print "Fitting unmodified ROI" def fit(): try: roi.fit(use_gradient=True) except Exception, err: print "\n\n\n\nERROR FITTING: %s\n\n\n" % (str(err))
class ExtendedSource(DiffuseSource): """ Class inherting from DiffuseSource but implementing a spatial source. The main difference is the requirement of a spatial model to accomany a spectral model. """ defaults = ( ('name', None, 'The name of the extended source.'), ('model', None, 'a Model object.'), ('spatial_model', None, """The spatial model to use. This is a SpatialModel object."""), ) @keyword_options.decorate(defaults) def __init__(self, **kwargs): """ Make the naming consistent with the PointSource object so that extended sources 'feel' like point sources. """ keyword_options.process(self, kwargs) if self.model == None: self.model = PowerLaw() if self.spatial_model == None: self.spatial_model = Disk() if not isinstance(self.spatial_model, SpatialModel): raise Exception( "The diffuse_model passed to an Extended Source must inherit from SpatialModel." ) super(ExtendedSource, self).__init__(diffuse_model=self.spatial_model, scaling_model=self.model, name=self.name) self.model.background = False @property def skydir(self): return self.spatial_model.center @property def smodel(self): """ No reason to keep a model & smodel. """ return self.model @smodel.setter def smodel(self, value): self.model = value def __str__(self, indent=''): return indent + ('\n' + indent).join([ '\n', '=' * 60, 'Name:\t\t%s' % (self.name), 'R.A. (J2000):\t\t%.5f' % (self.spatial_model.center.ra()), 'Dec. (J2000):\t\t%.5f' % (self.spatial_model.center.dec()), 'Model:\t\t%s' % (self.model.full_name()), '\t' + self.model.__str__(indent='\t'), 'SpatialModel:\t%s' % (self.spatial_model.full_name()), '\t' + self.spatial_model.__str__(indent='\t') ]) def copy(self): """ Create a deep copy of an extended source. """ return ExtendedSource(name=self.name, spatial_model=self.spatial_model.copy(), model=self.model.copy())