Notebook to test the Fit of SEDs to an IGMF model


In [1]:
%matplotlib inline

In [2]:
%load_ext autoreload


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [3]:
%autoreload 2

Imports


In [4]:
import os
import sys
from collections import OrderedDict

import yaml

import numpy as np
from astropy.io import fits
from astropy.table import Table, Column, join, hstack, vstack
import matplotlib.pyplot as plt
import matplotlib

import fermipy.utils as utils
from fermipy.spectrum import *
from fermipy.castro import CastroData

from haloanalysis.utils import create_mask, load_source_rows
from haloanalysis.sed import HaloSED
from haloanalysis.model import CascModel, CascLike
from haloanalysis.model import scan_igmf_likelihood

load the catalog

Catalog file:


In [5]:
cat = '../data/table_std_psf0123_joint2a_stdmodel_lnl_v14.fits'

In [6]:
tab_pars = Table.read(cat,hdu='SCAN_PARS')
tab_ebounds = Table.read(cat,hdu='EBOUNDS')

In [7]:
tables = [Table.read(cat,'CATALOG'),
            Table.read(cat,'LIKELIHOOD'),
            Table.read(cat,'SED')]

In [8]:
for i, t in enumerate(tables):
    if 'NAME' in t.columns:
        t['name'] = t['NAME']

In [9]:
print tables[0]['name'].data
print tables[1]['name'].data
print tables[2]['name'].data


['FHES J2359.8-3736' 'FHES J0001.2-0746' 'FHES J0001.5+2113' ...,
 'FHES J0538.0-6913' 'FHES J0535.6-6734' 'FHES J0524.7-6937']
['3FGL J0000.2-3738' '3FGL J0001.2-0748' '3FGL J0001.4+2120' ..., 'LMC P2'
 'LMC P3' 'LMC P4']
['3FGL J0000.2-3738' '3FGL J0001.2-0748' '3FGL J0001.4+2120' ..., 'LMC P2'
 'LMC P3' 'LMC P4']

Remove identifier so in names so that join operation works


In [10]:
for i, t in enumerate(tables):
    for j,n in enumerate(t['name']):
        tables[i]['name'][j] = n[5:-2] ### WORK AROUND

In [11]:
print tables[0]['name'].data


['J2359.8-37' 'J0001.2-07' 'J0001.5+21' ..., 'J0538.0-69' 'J0535.6-67'
 'J0524.7-69']

join the tables -- DOES ONLY WORK WITH WORKAROUND ABOVE


In [12]:
tab_casc = join(tables[0],tables[1]) # table is empty since 'name' names are different
tab_casc = join(tab_casc,tables[2])


WARNING: MergeConflictWarning: Cannot merge meta key 'EXTNAME' types <type 'str'> and <type 'str'>, choosing EXTNAME='LIKELIHOOD' [astropy.utils.metadata]
WARNING: MergeConflictWarning: Cannot merge meta key 'EXTNAME' types <type 'str'> and <type 'str'>, choosing EXTNAME='SED' [astropy.utils.metadata]

In [13]:
print tab_casc['name'].data.shape


(638,)

Load the TeV SEDs


In [14]:
tab_sed_tev = Table.read('../data/CompiledTeVSources.fits')

In [15]:
print tab_sed_tev


 3FGL_NAME             SOURCE_FULL            ...   NORM_ERRN [25]  
------------ -------------------------------- ... ------------------
J0232.8+2016       1ES0229+200_HESS_2005-2006 ...  1.3277e-08 .. nan
J0232.8+2016    1ES0229+200_VERITAS_2009-2012 ...     2.8e-08 .. nan
J0349.2-1158      1ES0347-121_HESS_2006-09-12 ...  3.5758e-08 .. nan
J0416.8+0104       1ES0414+009_HESS_2005-2009 ...   1.017e-07 .. nan
J0416.8+0104    1ES0414+009_VERITAS_2008-2011 ...    1.09e-08 .. nan
J0809.8+5218    1ES0806+524_VERITAS_2006-2008 ... 9.29454e-08 .. nan
J1015.0+4925           1ES1011+496_MAGIC_2007 ...    1.69e-06 .. nan
J1103.5-2329       1ES1101-232_HESS_2004-2005 ...  1.0851e-07 .. nan
J1217.8+3007           1ES1215+303_MAGIC_2011 ...    1.53e-06 .. nan
J1217.8+3007    1ES1215+303_VERITAS_2008-2012 ...  5.7133e-08 .. nan
         ...                              ... ...                ...
J2158.8-3013      PKS2155-304_HESS_2006-08-03 ...   2.947e-07 .. nan
J2158.8-3013      PKS2155-304_HESS_2008-08-09 ...    9.93e-08 .. nan
J2158.8-3013     PKS2155-304_MAGIC_2006-07_08 ...    8.22e-07 .. nan
J0319.8+1847        RBS0413_VERITAS_2008-2009 ...    6.48e-08 .. nan
J0152.6+0148           RGBJ0152+017_HESS_2007 ...   6.674e-08 .. nan
J0710.3+5908   RGBJ0710+591_VERITAS_2008-2009 ...    3.62e-08 .. nan
J0648.8+1516      RXJ0648.7+1516_VERITAS_2010 ...    4.18e-07 .. nan
J0721.9+7120       S50716+714_MAGIC_2007-2008 ...    3.25e-06 .. nan
J0013.9-1853 SHBLJ001355.9-185406_HESS_2008-2 ...   4.165e-09 .. nan
J0521.7+2113   VERJ0521+211_VERITAS_2009-2010 ... 3.19506e-07 .. nan
J1221.4+2814        WComae_VERITAS_2008-01-04 ... 2.23846e-07 .. nan
Length = 106 rows

Load the IGMF model cube


In [17]:
casc_model = CascModel.create_from_fits(
    '/nfs/farm/g/glast/u/mmeyer/projects/FermiHalo/Output/EBLm6/th_jet6.00/gam-2.00/results_merged_z_th6d_t1e7.fits',
)


/afs/slac.stanford.edu/u/gl/mmeyer/projects/python/haloanalysis/haloanalysis/model.py:809: RuntimeWarning: invalid value encountered in divide
  data_casc_flux = np.array(tab_casc_flux/tab_inj_flux[..., np.newaxis, np.newaxis])
/afs/slac.stanford.edu/u/gl/mmeyer/projects/python/haloanalysis/haloanalysis/model.py:810: RuntimeWarning: invalid value encountered in divide
  data_prim_flux = np.array(tab_prim_flux/tab_inj_flux)

In [20]:
casc_model.set_eblmodel(eblmodel = 'dominguez')

Loop over the sources and perform the likelihood fit

Spectra you want to include in the analysis


In [21]:
src_names = ['1ES0229+200_HESS_2005-2006']

In [22]:
nstep = 5
casc_scale = 1.
casc_r68_scale = 1.

In [23]:
tab_igmf = []

for name in src_names:

    rows_sed_tev = load_source_rows(tab_sed_tev, [name], key='SOURCE_FULL')
    #cat_names = [ 'FHES %s'%row['3FGL_NAME'] for row in rows_sed_tev ]
    cat_names = [ '%s'%row['3FGL_NAME'][:-2] for row in rows_sed_tev ] # WORK AROUND
    
    cat_names = np.unique(np.array(cat_names))
    rows_sed_gev = load_source_rows(tab_casc, cat_names, key='name')
    rows_casc = load_source_rows(tab_casc, cat_names, key='name')
    
    print rows_sed_tev
    print rows_sed_gev
    print rows_casc
    
    tab = scan_igmf_likelihood(casc_model, rows_sed_tev, rows_sed_gev,
                                rows_casc, tab_pars, tab_ebounds, nstep,
                                casc_scale, casc_r68_scale)
    #tab_igmf += [tab]


 3FGL_NAME          SOURCE_FULL         ...   NORM_ERRP [25]    NORM_ERRN [25] 
------------ -------------------------- ... ----------------- -----------------
J0232.8+2016 1ES0229+200_HESS_2005-2006 ... 1.3277e-08 .. nan 1.3277e-08 .. nan
   name         codename     ...       dloglike_scan [20,9]     
                             ...                                
---------- ----------------- ... -------------------------------
J0232.8+20 3fgl_j0232.8+2016 ... -4.2029670149 .. -2.70594689762
   name         codename     ...       dloglike_scan [20,9]     
                             ...                                
---------- ----------------- ... -------------------------------
J0232.8+20 3fgl_j0232.8+2016 ... -4.2029670149 .. -2.70594689762
((0, 0), -65.160357820170816, [0.14000000000000001, -4.0, -20.0], [1.7781826364287957e-13, -1.6174113205437628, 4929568.6279378785])
((0, 1), -66.836542031590739, [0.14000000000000001, -4.0, -18.0], [1.7762412008607477e-13, -1.6170279304050668, 4767466.1198864076])
((0, 2), -66.83815682287144, [0.14000000000000001, -4.0, -16.0], [1.7738444004800579e-13, -1.6166937705650695, 4731065.6839127652])
((0, 3), -43.494818195866571, [0.14000000000000001, -4.0, -14.0], [1.4117519672012145e-13, -1.5599374835585258, 4932934.8212527884])
((0, 4), -1.3676466982752231, [0.14000000000000001, -4.0, -12.0], [1.4177680811967108e-13, -1.5665464849540465, 1000000000.0])
((1, 0), -66.614325869801633, [0.14000000000000001, -2.0, -20.0], [1.7469007517898494e-13, -1.6131080125624144, 4703161.5979660433])
((1, 1), -66.848921468377853, [0.14000000000000001, -2.0, -18.0], [1.7747582439015461e-13, -1.6168165000845958, 4783337.4357691128])
((1, 2), -66.584487750577694, [0.14000000000000001, -2.0, -16.0], [1.7106289923039231e-13, -1.6078701827403796, 4569508.2096964689])
((1, 3), -18.240996515859649, [0.14000000000000001, -2.0, -14.0], [1.4075447085082791e-13, -1.5562037539304123, 11252712.105404187])
((1, 4), -0.16688540943351882, [0.14000000000000001, -2.0, -12.0], [1.4177664898770916e-13, -1.5665461991910119, 1000000000.0])
((2, 0), -66.668118508718635, [0.14000000000000001, 0.0, -20.0], [1.7762864085326297e-13, -1.6170305047989149, 4751214.1980042029])
((2, 1), -66.82630407688157, [0.14000000000000001, 0.0, -18.0], [1.7756404610205552e-13, -1.6169161773711491, 4734115.0822582236])
((2, 2), -59.819590991028065, [0.14000000000000001, 0.0, -16.0], [1.4177370951790379e-13, -1.5657736695900173, 4142557.721222748])
((2, 3), -13.509798510765393, [0.14000000000000001, 0.0, -14.0], [1.4142534479138305e-13, -1.5629162096494305, 19369145.190135602])
((2, 4), -0.17338843595695153, [0.14000000000000001, 0.0, -12.0], [1.4177664742329643e-13, -1.566546201124744, 1000000000.0])
((3, 0), -66.668039648633624, [0.14000000000000001, 2.0, -20.0], [1.7763353904161134e-13, -1.617035972731945, 4750712.1614743778])
((3, 1), -66.69404454337041, [0.14000000000000001, 2.0, -18.0], [1.7744841660757729e-13, -1.6167132550183454, 4722723.2859606165])
((3, 2), -59.662908508356736, [0.14000000000000001, 2.0, -16.0], [1.4176705531294116e-13, -1.5657329797328601, 4030484.8804183053])
((3, 3), -13.509798510765393, [0.14000000000000001, 2.0, -14.0], [1.4142534479138305e-13, -1.5629162096494305, 19369145.190135602])
((3, 4), -0.17338843595695153, [0.14000000000000001, 2.0, -12.0], [1.4177664742329643e-13, -1.566546201124744, 1000000000.0])
((4, 0), -66.668039648633624, [0.14000000000000001, 4.0, -20.0], [1.7763353904161134e-13, -1.617035972731945, 4750712.1614743778])
((4, 1), -66.69404454337041, [0.14000000000000001, 4.0, -18.0], [1.7744841660757729e-13, -1.6167132550183454, 4722723.2859606165])
((4, 2), -59.662908508356736, [0.14000000000000001, 4.0, -16.0], [1.4176705531294116e-13, -1.5657329797328601, 4030484.8804183053])
((4, 3), -13.509798510765393, [0.14000000000000001, 4.0, -14.0], [1.4142534479138305e-13, -1.5629162096494305, 19369145.190135602])
((4, 4), -0.17338843595695153, [0.14000000000000001, 4.0, -12.0], [1.4177664742329643e-13, -1.566546201124744, 1000000000.0])

((0, 0), -65.160357820170816, [0.14000000000000001, -4.0, -20.0], [1.7781826364287957e-13, -1.6174113205437628, 4929568.6279378785])


In [ ]: