def gtlike_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) cutoff = (not self.no_cutoff) and hypothesis in ['at_pulsar', 'point'] upper_limit = hypothesis=='at_pulsar' if cutoff: modify = import_module(self.modify) cutoff_model=modify.get_gtlike_cutoff_model(name) if cutoff_model is None: pointlike_results = loaddict('results_%s_pointlike_%s.yaml' % (name,hypothesis)) cutoff_model=pointlike_results['test_cutoff']['hypothesis_1']['spectrum'] cutoff_model=pointlike_dict_to_spectrum(cutoff_model) cutoff_model.set_default_limits(oomp_limits=True) print 'override cutoff_model = ',cutoff_model else: cutoff_model=None results=gtlike_analysis(roi, name=name, cutoff_model = cutoff_model, max_free = self.max_free, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, hypothesis=hypothesis, upper_limit=upper_limit, cutoff=cutoff, do_bandfitter=True, do_sed=True, ) savedict(results,'results_%s_gtlike_%s.yaml' % (name,hypothesis))
def altdiff_followup(self, hypothesis, dist, halo, TS): name = self.name print dist, halo, TS # Note, Gulli's alt diffuse models only go to 100GeV! roi = self.reload_roi(hypothesis, fit_emax=1e5) roi.print_summary() print roi.get_source(name) for source in get_background(roi): roi.del_source(source) print 'Loading alternate diffuse models' diff = get_gulli_diffuse(dist=dist, halo=halo, TS=TS, version=2, event_class='source', verbosity=True) print 'Adding alternate diffuse models to ROI' for source in diff: roi.add_source(source) print 'Freezing insignificant diffuse models' freeze_insignificant_diffuse(roi,allowed_fraction=0.03, verbosity=True) print 'Printing ROI' roi.print_summary() print 'Doing gtlike analysis' results=gtlike_analysis(roi, name=name, max_free = self.max_free, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, hypothesis='%s_altdiff_dist_%s_halo_%s_TS_%s' % (hypothesis,dist,halo,TS), upper_limit=False, do_bandfitter=False, do_sed=True, ) savedict(results,'results_%s_altdiff_dist_%s_halo_%s_TS_%s_%s.yaml' % (name,dist,halo,TS,hypothesis))
def fit(type): spatial_model = get_spatial(type) print 'Fitting %s with %s spatial model' % (name,type) roi.modify(which=name, spatial_model=spatial_model) likelihood_state.restore(just_spectra=True) roi.fit() if isinstance(roi.get_source(name), PointSource): roi.localize(which=name, update=True) else: roi.fit_extension(which=name) roi.fit() roi.print_summary(galactic=True) results[type] = dict(pointlike=sourcedict(roi,name)) gtlike = Gtlike(roi, enable_edisp=(args.edisp==True), binsz=0.05, chatter=4, minbinsz=0.05, rfactor=2, ) like = gtlike.like like.fit(covar=True) results[type]['gtlike'] = sourcedict(like,name) savedict('results_%s.yaml' % istr,results)
def gtlike_followup(self, hypothesis): roi = self.reload_roi(hypothesis) results = gtlike_analysis(self, roi, self.name, hypothesis, upper_limit=hypothesis == 'at_pulsar') savedict(results, 'results_%s_gtlike_%s.yaml' % (self.name, hypothesis))
def extul_followup(self, hypothesis): print 'Calculating extension upper limit' name = self.name roi = self.reload_roi(hypothesis) r=roi.extension_upper_limit(which=name, confidence=0.95, spatial_model=Gaussian) results = {hypothesis:{'pointlike':{'extension_upper_limit':r}}} savedict(results,'results_%s_extul_%s.yaml' % (name,hypothesis))
def variability_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) modify = import_module(self.modify) good_interval = modify.get_variability_time_cuts(name) nbins = 36 if good_interval is not None: ft1files = roi.sa.pixeldata.ft1files earliest_time, latest_time = CombinedVariabilityTester.get_time_range( ft1files) bins = b = np.round( np.linspace(earliest_time, latest_time, nbins + 1)).astype(int) starts = b[:-1] stops = b[1:] print 'Initial binning:' print ' * starts=', starts print ' * stops=', stops starts,stops = zip(*[(start,stop) \ for (start,stop) in zip(starts,stops) \ if good_interval(start,stop)]) print 'Initial binning:' print ' * starts=', starts print ' * stops=', stops kwargs = dict(tstarts=starts, tstops=stops) else: kwargs = dict(nbins=nbins) frozen = freeze_far_away(roi, roi.get_source(name).skydir, self.max_free) v = CombinedVariabilityTester(roi, name, use_pointlike_ltcube=True, refit_background=True, refit_other_sources=True, verbosity=4, **kwargs) unfreeze_far_away(roi, frozen) results = v.todict() savedict(results, 'results_%s_variability_%s.yaml' % (name, hypothesis)) try: v.plot(filename='plots/variability_%s_hypothesis_%s.pdf' % (name, hypothesis)) except Exception, ex: print 'ERROR plotting variability tester:', ex traceback.print_exc(file=sys.stdout)
def extul_followup(self, hypothesis): print 'Calculating extension upper limit' name = self.name roi = self.reload_roi(hypothesis) r = roi.extension_upper_limit(which=name, confidence=0.95, spatial_model=Gaussian) results = {hypothesis: {'pointlike': {'extension_upper_limit': r}}} savedict(results, 'results_%s_extul_%s.yaml' % (name, hypothesis))
def altdiff_followup(self, hypothesis, dist, halo, TS): name = self.name print dist, halo, TS # Note, Gulli's alt diffuse models only go to 100GeV! roi = self.reload_roi(hypothesis, fit_emax=1e5) roi.print_summary() print roi.get_source(name) for source in get_background(roi): roi.del_source(source) print 'Loading alternate diffuse models' diff = get_gulli_diffuse(dist=dist, halo=halo, TS=TS, version=2, event_class='source', verbosity=True) print 'Adding alternate diffuse models to ROI' for source in diff: roi.add_source(source) print 'Freezing insignificant diffuse models' freeze_insignificant_diffuse(roi, allowed_fraction=0.03, verbosity=True) print 'Printing ROI' roi.print_summary() print 'Doing gtlike analysis' results = gtlike_analysis( roi, name=name, max_free=self.max_free, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, hypothesis='%s_altdiff_dist_%s_halo_%s_TS_%s' % (hypothesis, dist, halo, TS), upper_limit=False, do_bandfitter=False, do_sed=True, ) savedict( results, 'results_%s_altdiff_dist_%s_halo_%s_TS_%s_%s.yaml' % (name, dist, halo, TS, hypothesis))
def main(self): name=self.name rb = RadioPSRROIBuilder(radiopsr_loader=self.radiopsr_loader) roi = rb.build_roi(name=name, fast=self.fast) modify = import_module(self.modify) new_sources, deleted_sources = modify.modify_roi(roi,name) roi.extra = dict(new_sources=new_sources, deleted_sources=deleted_sources) results = self.input_kwargs savedict(results,'results_%s_general.yaml' % name) hypothesis = 'at_pulsar' results=pointlike_analysis(self, roi, name, hypothesis=hypothesis, upper_limit=True) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis)) if not self.no_point: hypothesis = 'point' results=pointlike_analysis(self, roi, name, hypothesis=hypothesis, localize=True) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis)) if not self.no_extended: hypothesis = 'extended' roi.modify(which=name, spatial_model=Gaussian(sigma=0.1), keep_old_center=True) results=pointlike_analysis(self, roi, name, hypothesis=hypothesis, fit_extension=True) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis))
def run(name, snrdata, latdata): force_gradient(use_gradient=False) roi = build_roi(name, snrdata, latdata) results = dict() kwargs = dict(plotdir='plotdir') results['pointlike']=pointlike_analysis(roi,name, **kwargs) pointlike_plots(roi) results['gtlike']=gtlike_analysis(roi,name, **kwargs) savedict(results, 'results_%s.yaml' % name)
def run(name, snrdata, latdata): force_gradient(use_gradient=False) roi = build_roi(name, snrdata, latdata) results = dict() kwargs = dict(plotdir='plotdir') results['pointlike'] = pointlike_analysis(roi, name, **kwargs) pointlike_plots(roi) results['gtlike'] = gtlike_analysis(roi, name, **kwargs) savedict(results, 'results_%s.yaml' % name)
def variability_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) modify = import_module(self.modify) good_interval = modify.get_variability_time_cuts(name) nbins=36 if good_interval is not None: ft1files=roi.sa.pixeldata.ft1files earliest_time, latest_time = CombinedVariabilityTester.get_time_range(ft1files) bins = b = np.round(np.linspace(earliest_time, latest_time, nbins+1)).astype(int) starts = b[:-1] stops = b[1:] print 'Initial binning:' print ' * starts=',starts print ' * stops=',stops starts,stops = zip(*[(start,stop) \ for (start,stop) in zip(starts,stops) \ if good_interval(start,stop)]) print 'Initial binning:' print ' * starts=',starts print ' * stops=',stops kwargs=dict(tstarts=starts, tstops=stops) else: kwargs=dict(nbins=nbins) frozen = freeze_far_away(roi, roi.get_source(name).skydir, self.max_free) v = CombinedVariabilityTester(roi,name, use_pointlike_ltcube=True, refit_background=True, refit_other_sources=True, verbosity=4, **kwargs) unfreeze_far_away(roi, frozen) results = v.todict() savedict(results,'results_%s_variability_%s.yaml' % (name,hypothesis)) try: v.plot(filename='plots/variability_%s_hypothesis_%s.pdf' % (name,hypothesis)) except Exception, ex: print 'ERROR plotting variability tester:', ex traceback.print_exc(file=sys.stdout)
def gtlike_followup(self, hypothesis): name = self.name roi = self.reload_roi(hypothesis) cutoff = (not self.no_cutoff) and hypothesis in ['at_pulsar', 'point'] upper_limit = hypothesis == 'at_pulsar' if cutoff: modify = import_module(self.modify) cutoff_model = modify.get_gtlike_cutoff_model(name) if cutoff_model is None: pointlike_results = loaddict('results_%s_pointlike_%s.yaml' % (name, hypothesis)) cutoff_model = pointlike_results['test_cutoff'][ 'hypothesis_1']['spectrum'] cutoff_model = pointlike_dict_to_spectrum(cutoff_model) cutoff_model.set_default_limits(oomp_limits=True) print 'override cutoff_model = ', cutoff_model else: cutoff_model = None results = gtlike_analysis( roi, name=name, cutoff_model=cutoff_model, max_free=self.max_free, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, hypothesis=hypothesis, upper_limit=upper_limit, cutoff=cutoff, do_bandfitter=True, do_sed=True, ) savedict(results, 'results_%s_gtlike_%s.yaml' % (name, hypothesis))
def main(self): name = self.name rb = RadioPSRROIBuilder(radiopsr_loader=self.radiopsr_loader) roi = rb.build_roi(name=name, fast=self.fast) modify = import_module(self.modify) new_sources, deleted_sources = modify.modify_roi(roi, name) roi.extra = dict(new_sources=new_sources, deleted_sources=deleted_sources) results = self.input_kwargs savedict(results, 'results_%s_general.yaml' % name) hypothesis = 'at_pulsar' results = pointlike_analysis(self, roi, name, hypothesis=hypothesis, upper_limit=True) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis)) if not self.no_point: hypothesis = 'point' results = pointlike_analysis(self, roi, name, hypothesis=hypothesis, localize=True) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis)) if not self.no_extended: hypothesis = 'extended' roi.modify(which=name, spatial_model=Gaussian(sigma=0.1), keep_old_center=True) results = pointlike_analysis(self, roi, name, hypothesis=hypothesis, fit_extension=True) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis))
def gtlike_followup(self, hypothesis): roi = self.reload_roi(hypothesis) results=gtlike_analysis(self, roi, self.name, hypothesis, upper_limit=hypothesis=='at_pulsar') savedict(results,'results_%s_gtlike_%s.yaml' % (self.name,hypothesis))
s=roi.plot_sed(which=which,filename='sed_pointlike.pdf', use_ergs=True) pointlike_sed_to_yaml(s,'sed_pointlike.yaml') roi.toXML('results_pointlike.xml') state.restore() gtlike = Gtlike(roi, savedir='gtlike_savedir', enable_edisp=True) like = gtlike.like results['gtlike'] = dict() results['gtlike']['mc'] = sourcedict(like,name,errors=False) like.fit(covar=True) results['gtlike']['fit'] = sourcedict(like,name) if args.debug: # in debug mode, there is no background model, # so the TS is meaningless. Here, instead # just always plot the data points. sed_kwargs=dict(min_ts=float('-inf')) else: sed_kwargs=dict() sed = SuperSED(like, name=name, **sed_kwargs) sed.save('sed_gtlike.yaml') sed.plot('sed_gtlike.pdf') savedict('results_%s.yaml' % name,results) like.writeXml('results_gtlike.xml')
from lande.fermi.pipeline.pwncat2.interp.classify import PWNAutomaticClassifier from lande.utilities.save import savedict if True: """ pwndata="$pwndata/pwncat2_data_lande.yaml" fitdir='$pwnpipeline/v35/analysis' c=PWNAutomaticClassifier.get_automatic_classify(pwndata, fitdir) print c savedict(c,'$pwnpipeline/v35/classify/automatic_classifications.yaml') """ pwndata="$pwndata/pwncat2_data_lande.yaml" fitdir='$pwnpipeline/v36/analysis' c=PWNAutomaticClassifier.get_automatic_classify(pwndata, fitdir) print c savedict(c,'$pwnpipeline/v36/classify/automatic_classifications.yaml') if False: pass """ # don't run again c=make_manual_classify( pwndata="$pwndata/pwncat2_data_lande.yaml", fitdir='$pwnpipeline/v35/analysis') savedict(c,'$pwnclassify/manual_classifications.yaml') """
roi.fit() roi.print_summary(galactic=True) roi.localize(which=name, update=True) roi.fit() roi.print_summary(galactic=True) r['point'] = sourcedict(roi, errors=True, **kwargs) roi.modify(which=name, spatial_model=Disk(sigma=.1)) roi.print_summary(galactic=True) roi.fit() roi.fit_extension(which=name, estimate_errors=False) roi.fit() roi.print_summary(galactic=True) r['extended'] = sourcedict(roi, errors=True, **kwargs) r['extended']['TS_ext']=roi.TS_ext(which=name) results.append(r) savedict('results_%s.yaml' % istr,results) shutil.rmtree(tempdir)
def main(self): do_at_pulsar = not self.no_at_pulsar do_point = not self.no_point do_extended = not self.no_extended do_cutoff = not self.no_cutoff name = self.name if not self.fast: emin = self.emin emax = self.emax binsperdec = self.binsperdec free_radius = 5 roi_size = 10 max_free = self.max_free else: emin = 1e4 emax = 1e5 binsperdec = 2 free_radius = 2 roi_size = 5 max_free = 2 pwnphase = yaml.load(open(self.pwnphase))[name] phase = pwnphase['phase'] print 'phase = ', phase pwndata = yaml.load(open(self.pwndata))[name] savedir = None if self.no_savedir else join(os.getenv('PWD'), 'savedir') results = r = defaultdict(lambda: defaultdict(dict)) results['name'] = name results['phase'] = phase pointlike_kwargs = dict(name=name, max_free=max_free) print 'Building the ROI' reg = PWNRegion(pwndata=self.pwndata, savedir=savedir) roi = reg.get_roi(name=name, phase=phase, catalog_kwargs=dict(free_radius=free_radius, max_free=max_free), fit_emin=emin, fit_emax=emax, binsperdec=binsperdec, roi_size=roi_size, extended=False) modify = import_module(self.modify) roi.extra['phase'] = phase roi.extra['new_sources'] = modify.modify_roi(name, roi) roi.extra['pwnphase'] = pwnphase savedict(results, 'results_%s_general.yaml' % name) assert all_params_limited(roi, except_sources=[name]) mapper0 = roi.get_model(which=name).get_mapper(0) assert isinstance(mapper0, LogMapper) or (type(mapper0) == type and issubclass(mapper0, LogMapper)) cutoff_model = modify.get_cutoff_model(name) override_localization = modify.get_override_localization(name) if do_at_pulsar: hypothesis = 'at_pulsar' results = pointlike_analysis(roi, hypothesis=hypothesis, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, cutoff=do_cutoff, **pointlike_kwargs) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis)) if do_point: hypothesis = 'point' results = pointlike_analysis( roi, hypothesis=hypothesis, localize=True, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, cutoff=do_cutoff, cutoff_model=cutoff_model, override_localization=override_localization, **pointlike_kwargs) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis)) if do_extended: hypothesis = 'extended' roi.modify(which=name, spatial_model=Gaussian(sigma=0.1), keep_old_center=True) results = pointlike_analysis(roi, hypothesis=hypothesis, cutoff=False, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, fit_extension=True, **pointlike_kwargs) savedict(results, 'results_%s_pointlike_%s.yaml' % (name, hypothesis))
def main(self): do_at_pulsar = not self.no_at_pulsar do_point = not self.no_point do_extended = not self.no_extended do_cutoff = not self.no_cutoff name=self.name if not self.fast: emin=self.emin emax=self.emax binsperdec=self.binsperdec free_radius=5 roi_size=10 max_free=self.max_free else: emin=1e4 emax=1e5 binsperdec=2 free_radius=2 roi_size=5 max_free=2 pwnphase=yaml.load(open(self.pwnphase))[name] phase=pwnphase['phase'] print 'phase = ',phase pwndata=yaml.load(open(self.pwndata))[name] savedir=None if self.no_savedir else join(os.getenv('PWD'),'savedir') results=r=defaultdict(lambda: defaultdict(dict)) results['name']=name results['phase']=phase pointlike_kwargs=dict(name=name, max_free=max_free) print 'Building the ROI' reg=PWNRegion(pwndata=self.pwndata, savedir=savedir) roi=reg.get_roi(name=name, phase=phase, catalog_kwargs=dict(free_radius=free_radius, max_free=max_free), fit_emin=emin, fit_emax=emax, binsperdec=binsperdec, roi_size=roi_size, extended=False) modify = import_module(self.modify) roi.extra['phase'] = phase roi.extra['new_sources'] = modify.modify_roi(name,roi) roi.extra['pwnphase'] = pwnphase savedict(results,'results_%s_general.yaml' % name) assert all_params_limited(roi, except_sources=[name]) mapper0 = roi.get_model(which=name).get_mapper(0) assert isinstance(mapper0,LogMapper) or (type(mapper0)==type and issubclass(mapper0,LogMapper)) cutoff_model=modify.get_cutoff_model(name) override_localization=modify.get_override_localization(name) if do_at_pulsar: hypothesis = 'at_pulsar' results=pointlike_analysis(roi, hypothesis=hypothesis, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, cutoff=do_cutoff, **pointlike_kwargs) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis)) if do_point: hypothesis = 'point' results=pointlike_analysis(roi, hypothesis=hypothesis, localize=True, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, cutoff=do_cutoff, cutoff_model = cutoff_model, override_localization=override_localization, **pointlike_kwargs) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis)) if do_extended: hypothesis = 'extended' roi.modify(which=name, spatial_model=Gaussian(sigma=0.1), keep_old_center=True) results=pointlike_analysis(roi, hypothesis=hypothesis, cutoff=False, seddir=self.seddir, datadir=self.datadir, plotdir=self.plotdir, fit_extension=True, **pointlike_kwargs) savedict(results,'results_%s_pointlike_%s.yaml' % (name,hypothesis))
parser.add_argument('--test', default=False, action='store_true') args = parser.parse_args() name=args.name conv_type=args.conv_type if args.test: emin=1e2 emax=1e5 binsperdec=2 else: emin=10**1.75 # 56 MeV emax=1e5 binsperdec=8 save=lambda: savedict('results_%s.yaml' % name, results) ft2='/u/gl/bechtol/disk/drafts/radio_quiet/36m_gtlike/trial_v1/ft2-30s_239557414_334152027.fits' if name == 'W44': ft1='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W44/SmoothBrokenPowerlaw/W44-ft1.fits' ltcube='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W44/ltcube_239557414_334152002.fits' roi_dir=SkyDir(283.98999023,1.35500002) elif name == 'IC443': ft1='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/IC443/SmoothBrokenPowerlaw/IC443-ft1.fits' ltcube='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/IC443/ltcube_239557414_334152002.fits' roi_dir=SkyDir(94.30999756,22.57999992) elif name == 'W51C': ft1='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W51C/SmoothBrokenPowerlaw/W51C-ft1.fits' ltcube='/u/gl/funk/data3/ExtendedSources/NewAnalysis/gtlike/W51C/ltcube_239557414_334152002.fits'
for pwn in pwnlist.keys(): var_results = join(folder,pwn, 'results_%s_variability_point.yaml' % pwn) gtlike_results = join(folder,pwn, 'results_%s_gtlike_point.yaml' % pwn) if not exists(var_results) or not exists(gtlike_results): print "Skipping %s b/c results doesn't exist" % pwn continue else: print pwn f=yaml.load(open(var_results)) g=yaml.load(open(gtlike_results)) ts_var.append(f['point']['variability']['TS_var']['gtlike']) ts_point.append(g['point']['gtlike']['TS']) names.append(pwn) return ts_point,ts_var,names ts_point,ts_var,names = get_ts() d = dict( ts_point=ts_point, ts_var=ts_var, pwn=names, ) savedict(d,join(outdir,'ts_var.yaml'))
mc = diffusedict(roi) ll_0 = logLikelihood(roi) roi.print_summary() roi.fit(use_gradient=False) roi.print_summary() ll_1 = logLikelihood(roi) fit = diffusedict(roi) results["pointlike"] = dict(mc=mc, fit=fit, ll_0=ll_0, ll_1=ll_1) state.restore(just_spectra=True) gtlike = Gtlike(roi, bigger_roi=False, savedir=tempdir) like = gtlike.like mc = diffusedict(like) ll_0 = logLikelihood(like) like.fit(covar=True) fit = diffusedict(like) ll_1 = logLikelihood(like) results["gtlike"] = dict(mc=mc, fit=fit, ll_0=ll_0, ll_1=ll_1) savedict("results_%s.yaml" % istr, results) shutil.rmtree(tempdir)