Example #1
0
def build_roi(name, snrdata, latdata):

    snrdata=loaddict(snrdata)
    latdata=loaddict(latdata)

    roi_dir = SkyDir(*snrdata[name]['cel'])
    snrsize = snrdata[name]['size']

    if isinstance(snrsize,list) and len(snrsize) == 2:
        snrradius = math.sqrt(snrsize[0]*snrsize[1])/2.0
    else:
        snrradius = snrsize/2.0

    ds = DataSpecification(**latdata['data'])

    sa = SpectralAnalysis(ds,
                          binsperdec = 4,
                          emin       = 1e4,
                          emax       = 10**5.5,
                          irf        = "P7SOURCE_V6",
                          roi_dir    = roi_dir,
                          maxROI     = 10,
                          minROI     = 10,
                          event_class= 0)


    diffuse_sources = get_default_diffuse(**latdata['diffuse'])

    catalog = Catalog2FGL(**latdata['catalog'])

    roi=sa.roi(point_sources=[],
               diffuse_sources=diffuse_sources,
               catalogs=catalog)

    print 'bins',roi.bin_edges

    for source in roi.get_sources():
        if np.degrees(source.skydir.difference(roi_dir)) < snrradius + 0.5:
            roi.del_source(source)

    snr = ExtendedSource(
        name = name,
        model = PowerLaw(),
        spatial_model = Disk(sigma=snrradius, center=roi_dir)
    )

    roi.add_source(snr)

    return roi
Example #2
0
 def load_me_maybe(filename):
     if require_all_exists and not exists(filename): 
         raise ResultsException("%s does not exist" % filename)
     elif not exists(filename):
         print '%s does not exist' % filename
         return dict()
     return loaddict(filename)
Example #3
0
 def load_me_maybe(filename):
     if require_all_exists and not exists(filename):
         raise ResultsException("%s does not exist" % filename)
     elif not exists(filename):
         print '%s does not exist' % filename
         return dict()
     return loaddict(filename)
Example #4
0
    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))
Example #5
0
    def __init__(self, results, **kwargs):
        keyword_options.process(self, kwargs)

        if isinstance(results,dict):
            self.results = results
        elif isinstance(results, str):
            self.results = loaddict(results)
        else:
            raise Exception("Unrecognized results %s" % results)
Example #6
0
def overlay(ax):
    #results = loaddict('$simsrcdata/v12/merged.hdf5')

#    flux = np.asarray(results['flux_gtlike'])
#    flux_err = np.asarray(results['flux_gtlike_err'])

    #results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_point_emin_100_emax_100000_time_2years/merged.hdf5')

    #results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_point_emin_100_emax_100000_time_2fgl/merged.hdf5')

    #results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_point_emin_100_emax_100000_time_2years/merged.hdf5')
    #results = loaddict('$simsrcdata/v15/merged.hdf5')
    results = loaddict('$simsrcdata/v15/merged.hdf5')

    flux = np.asarray(results['flux_pointlike'])
    flux_err = np.asarray(results['flux_pointlike_err'])

    flux_mc = np.asarray(results['flux_mc'])
    glon = np.asarray(results['glon'])
    glat = np.asarray(results['glat'])
    ra = np.asarray(results['ra'])
    dec = np.asarray(results['dec'])
    phibins = np.asarray(results['phibins'])
    time = np.asarray(results['time'])

    pull = (flux-flux_mc)/flux_err
    perr = (flux-flux_mc)/flux_mc

    cut = (phibins == nphi)&(time=='2fgl')

    pull=pull[cut]
    perr=perr[cut]
    glon=glon[cut]
    glat=glat[cut]

    maxerr = 0.05
    print 'max',maxerr
    color_mapper=lambda perr: (min(perr/maxerr,1),0,0) if perr > 0 else (0,0,min(np.abs(perr)/maxerr,1))

    for _glon, _glat, _perr in zip(glon, glat, perr):
        ax['gal'].plot([_glon],[_glat], marker='o', color=color_mapper(_perr), markersize=10)
Example #7
0
    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))
Example #8
0
    def get_results(self,
                    pwn,
                    require_all_exists=True,
                    get_seds=True,
                    get_variability=True,
                    get_altdiff=True,
                    verbosity=None):
        filename = join(self.fitdir, pwn, 'results_%s_general.yaml' % pwn)
        if verbosity or (verbosity is None and self.verbosity):
            print 'Getting results for %s' % pwn

        if not exists(filename):
            raise PWNResultsException("%s does not exist" % filename)

        results = loaddict(filename)
        for hypothesis in self.all_hypotheses:
            results[hypothesis] = dict()

        for code in ['gtlike', 'pointlike']:
            for hypothesis in self.all_hypotheses:
                filename = join(
                    self.fitdir, pwn,
                    'results_%s_%s_%s.yaml' % (pwn, code, hypothesis))
                if exists(filename):
                    results[hypothesis][code] = loaddict(filename)
                else:
                    if require_all_exists:
                        raise PWNResultsException("%s does not exist" %
                                                  filename)

        if get_variability:
            for hypothesis in ['at_pulsar', 'point']:
                filename = join(
                    self.fitdir, pwn,
                    'results_%s_variability_%s.yaml' % (pwn, hypothesis))
                if exists(filename):
                    results[hypothesis]['variability'] = loaddict(filename)
                else:
                    if require_all_exists:
                        raise PWNResultsException('%s does not exist' %
                                                  filename)

        if get_altdiff:
            for hypothesis in ['point']:
                results[hypothesis]['gtlike']['altdiff'] = dict()
                for dist in ['SNR', 'Lorimer']:
                    for halo in [4, 10]:
                        for TS in [150, 100000]:
                            filename = join(
                                self.fitdir, pwn,
                                'results_%s_altdiff_dist_%s_halo_%s_TS_%s_%s.yaml'
                                % (pwn, dist, halo, TS, hypothesis))
                            if exists(filename):
                                results[hypothesis]['gtlike']['altdiff'][
                                    dist, halo, TS] = loaddict(filename)
                            else:
                                results[hypothesis]['gtlike']['altdiff'][
                                    dist, halo, TS] = None

        if get_seds:
            for hypothesis in self.all_hypotheses:
                for code, all_binning in [['gtlike', ['1bpd', '2bpd', '4bpd']],
                                          ['pointlike', ['4bpd']]]:
                    results[hypothesis][code]['seds'] = dict()
                    for binning in all_binning:
                        filename = join(
                            self.fitdir, pwn, 'seds', 'sed_%s_%s_%s_%s.yaml' %
                            (code, binning, hypothesis, pwn))
                        if exists(filename):
                            results[hypothesis][code]['seds'][
                                binning] = loaddict(filename)
                        else:
                            if require_all_exists:
                                raise PWNResultsException("%s does not exist" %
                                                          filename)

        results['raw_phase'] = PhaseRange(results['phase'])

        del results['phase']  # to avoid ambiguity

        if self.phase_shift is not None:
            import pickle
            ps = pickle.load(open(self.phase_shift))
            shift = float(ps[pwn.replace('PSRJ', 'J')]['shift'])
            """ Convention taken from private correspondence with Matthew Kerr:
                    me: ok
                        so i am trying to implement phase shifts in my analysis
                        did you decide the right way to do it
                    Matthew: it seems to be +
                    me: so correct_phase = phase_in_file + shift
                    Matthew: yes """
            results['shifted_phase'] = results['raw_phase'].offset(shift)

        return results
Example #9
0
 def get_sed(self, pwn, binning, hypothesis):
     sed = join(self.fitdir, pwn, 'seds',
                'sed_gtlike_%s_%s_%s.yaml' % (binning, hypothesis, pwn))
     return loaddict(sed)
Example #10
0
    def __init__(self, radiopsr_data, bigfile):

        self.radiopsr_data = loaddict(radiopsr_data)
        self.bigfile = pyfits.open(expandvars(bigfile))
Example #11
0
import scipy.stats 
from matplotlib.ticker import MaxNLocator


import numpy as np
import pylab as P


from lande.utilities.pubplot import set_latex_defaults
set_latex_defaults()

from lande.utilities.save import loaddict


name = expandvars(join('$fitdiffdata','v11','merged.hdf5'))
r = loaddict(name)

#plot_pull=True
plot_pull=False

#pointlike=True
pointlike=False

if pointlike:
    print 'pointlike'
    norm=np.asarray(r['pointlike_norm'])
    norm_err=np.asarray(r['pointlike_norm_err'])
    norm_mc=np.asarray(r['pointlike_norm_mc'])
else:
    print 'gtlike'
    norm=np.asarray(r['gtlike_norm'])
Example #12
0
 def __init__(self, gamma_quiet_psrs_data, analysisdir, verbosity=True):
     self.gamma_quiet_psrs_data = loaddict(expandvars(gamma_quiet_psrs_data))
     self.analysisdir = expandvars(analysisdir)
     self.verbosity = verbosity
Example #13
0
import pylab as P
import yaml
import h5py
import numpy as np
from scipy.stats import chi2

from lande.utilities.arrays import almost_equal
from lande.utilities.pubplot import set_latex_defaults, get_bw, save
from lande.utilities.save import loaddict

set_latex_defaults()

bw = get_bw()

r = loaddict('$tsext_plane_data/v12/merged.hdf5')
print r.keys()

flux_list = np.asarray(r['flux_mc'])
index_list = np.asarray(r['index_mc'])
ts_point_list = np.asarray(r['TS_point'])
ts_ext_list = np.asarray(r['TS_ext'])
ts_ext_list = np.where(ts_ext_list > 0, ts_ext_list, 0)

l_list = np.asarray(r['glon'])
b_list = np.asarray(r['glat'])

max_ts_ext = max(ts_ext_list)

fig = P.figure(None, figsize=(6,6))
axes = fig.add_subplot(111)
Example #14
0
    def __init__(self, radiopsr_data, bigfile):

        self.radiopsr_data = loaddict(radiopsr_data)
        self.bigfile = pyfits.open(expandvars(bigfile))
Example #15
0
from lande.utilities.save import loaddict
import numpy as np
import pylab as P

from lande.utilities.arrays import almost_equal

#r = loaddict('$simsrcdata/v22/merged.hdf5')
#r = loaddict('$simsrcdata/v21/merged.hdf5')
r = loaddict('$simsrcdata/v19/merged.hdf5')

ax1=P.subplot(211)
ax2=P.subplot(212)

flux=np.asarray(r['flux_gtlike'])
flux_mc=np.asarray(r['flux_mc'])
flux_err=np.asarray(r['flux_gtlike_err'])
spatial=np.asarray(r['spatial'])
#binsz=np.asarray(r['binsz'])
#rfactor=np.asarray(r['rfactor'])

#for label,cut in [('binsz=0.1 & rfactor=2',(almost_equal(binsz,0.1)&almost_equal(rfactor,2))),
#                  ('binsz=0.05 & rfactor=4',(almost_equal(binsz,0.05)&almost_equal(rfactor,4))),
#                  ('binsz=0.025 & rfactor=8',(almost_equal(binsz,0.025)&almost_equal(rfactor,8)))]:
for label in ['?']:
    cut = spatial == 'w44'

    #P.hist((flux-flux_mc)/flux_err)
    print label
    ax1.hist((flux[cut]-flux_mc[cut])/flux_mc[cut], label=label, histtype='step')
    #ax1.hist((flux[cut]-flux_mc[cut])/flux_err[cut], label=label, histtype='step')
Example #16
0
 def get_sed(self,pwn,binning,hypothesis):
     sed=join(self.fitdir,pwn,'seds','sed_gtlike_%s_%s_%s.yaml' % (binning, hypothesis, pwn))
     return loaddict(sed)
Example #17
0
from lande.utilities.save import loaddict
from lande.utilities.jobtools import JobBuilder

superfile='$superfile/snrdata.yaml'

snrs=loaddict(superfile).keys()
params=dict(name=snrs,
            snrdata='$superfile/snrdata.yaml',
            latdata='$snrlimcode/snrlatdata.yaml')


b = JobBuilder(
    savedir='$snrlimdata/v1',
    params=params,
    code='$snrlimcode/run.py',
    short_folder_names=True)
b.build()
Example #18
0
    def get_results(self, pwn, require_all_exists=True, get_seds=True, get_variability=True, get_altdiff=True, verbosity=None):
        filename = join(self.fitdir,pwn,'results_%s_general.yaml' % pwn)
        if verbosity or (verbosity is None and self.verbosity):
            print 'Getting results for %s' % pwn

        if not exists(filename): 
            raise PWNResultsException("%s does not exist" % filename)

        results = loaddict(filename)
        for hypothesis in self.all_hypotheses:
            results[hypothesis] = dict()

        for code in ['gtlike','pointlike']:
            for hypothesis in self.all_hypotheses:
                filename=join(self.fitdir,pwn,'results_%s_%s_%s.yaml' % (pwn,code,hypothesis))
                if exists(filename):
                    results[hypothesis][code] = loaddict(filename)
                else:
                    if require_all_exists:
                        raise PWNResultsException("%s does not exist" % filename)

        if get_variability:
            for hypothesis in ['at_pulsar','point']:
                filename =join(self.fitdir,pwn,'results_%s_variability_%s.yaml' % (pwn,hypothesis))
                if exists(filename):
                    results[hypothesis]['variability'] = loaddict(filename)
                else:
                    if require_all_exists:
                        raise PWNResultsException('%s does not exist' % filename)

        if get_altdiff:
            for hypothesis in ['point']:
                results[hypothesis]['gtlike']['altdiff'] = dict()
                for dist in ['SNR','Lorimer']:
                    for halo in [4,10]:
                        for TS in [150,100000]:
                            filename =join(self.fitdir,pwn,'results_%s_altdiff_dist_%s_halo_%s_TS_%s_%s.yaml' % (pwn,dist,halo,TS,hypothesis))
                            if exists(filename):
                                results[hypothesis]['gtlike']['altdiff'][dist,halo,TS] = loaddict(filename)
                            else:
                                results[hypothesis]['gtlike']['altdiff'][dist,halo,TS] = None

        if get_seds:
            for hypothesis in self.all_hypotheses:
                for code,all_binning in [['gtlike',['1bpd','2bpd','4bpd']], ['pointlike',['4bpd']]]:
                    results[hypothesis][code]['seds'] = dict()
                    for binning in all_binning:
                        filename = join(self.fitdir,pwn,'seds','sed_%s_%s_%s_%s.yaml' % (code,binning,hypothesis,pwn))
                        if exists(filename):
                            results[hypothesis][code]['seds'][binning] = loaddict(filename)
                        else:
                            if require_all_exists:
                                raise PWNResultsException("%s does not exist" % filename)

        
        results['raw_phase'] = PhaseRange(results['phase'])

        del results['phase'] # to avoid ambiguity

        if self.phase_shift is not None:
            import pickle
            ps=pickle.load(open(self.phase_shift))
            shift = float(ps[pwn.replace('PSRJ','J')]['shift'])
            """ Convention taken from private correspondence with Matthew Kerr:
                    me: ok
                        so i am trying to implement phase shifts in my analysis
                        did you decide the right way to do it
                    Matthew: it seems to be +
                    me: so correct_phase = phase_in_file + shift
                    Matthew: yes """
            results['shifted_phase'] = results['raw_phase'].offset(shift)

        return results
Example #19
0
#results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_extended_emin_100_emax_100000_time_2fgl/merged.hdf5')

#results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_point_emin_100_emax_100000_time_2years/merged.hdf5')

#results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_extended_emin_100_emax_100000_time_2years/merged.hdf5')
#results = loaddict('$simsrcdata/v11/sim_flux_1e-06_position_allsky_phibins_0_spatial_point_emin_100_emax_100000_time_2fgl/merged.hdf5')
#results = loaddict('$simsrcdata/v12/merged.hdf5')

#results = loaddict('$simsrcdata/v14/merged.hdf5')
#results = loaddict('$simsrcdata/v17/merged.hdf5')

#results = loaddict('$simsrcdata/v18/merged.hdf5')
#results = loaddict('$simsrcdata/v19/merged.hdf5')
#results = loaddict('$simsrcdata/v20/merged.hdf5')
#results = loaddict('$simsrcdata/v21/merged.hdf5')
results = loaddict('$simsrcdata/v22/merged.hdf5')

print results.keys()
print len(results['flux_gtlike'])

flux_gtlike = np.asarray(results['flux_gtlike'])
flux_gtlike_err = np.asarray(results['flux_gtlike_err'])
flux_pointlike = np.asarray(results['flux_pointlike'])
flux_pointlike_err = np.asarray(results['flux_pointlike_err'])

flux_mc = np.asarray(results['flux_mc'])
glon = np.asarray(results['glon'])
glat = np.asarray(results['glat'])
ra = np.asarray(results['ra'])
dec = np.asarray(results['dec'])
i = np.asarray(results['i'])
Example #20
0
 def __init__(self, gamma_quiet_psrs_data, analysisdir, verbosity=True):
     self.gamma_quiet_psrs_data = loaddict(
         expandvars(gamma_quiet_psrs_data))
     self.analysisdir = expandvars(analysisdir)
     self.verbosity = verbosity