def test_load_3fgl_catalog_fits(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['3FGL'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175 src_name = '3FGL J1747.7-2904' assert(rm[src_name]['SpectrumType'] == 'PowerLaw') check_src_params(rm, src_name, ['Prefactor', 'Index', 'Scale'], [1.1100788257e-12, -2.5154423713, 2248.0983886]) src_name = '3FGL J1747.0-2828' assert(rm[src_name]['SpectrumType'] == 'LogParabola') check_src_params(rm, src_name, ['norm', 'alpha', 'beta', 'Eb'], [6.2330737793e-11, 2.04547667503, 0.417130559682, 742.2312011]) src_name = '3FGL J1747.2-2958' assert(rm[src_name]['SpectrumType'] == 'PLSuperExpCutoff') check_src_params(rm, src_name, ['Prefactor', 'Index1', 'Index2', 'Scale', 'Cutoff'], [2.07284068381e-11, -1.79597532749, 1.0, 1468.48669433, 3328.21728515]) rm = ROIModel(catalogs=['gll_psc_v16.fit'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175
def test_load_3fgl_catalog_fits(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['3FGL'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175 rm = ROIModel(catalogs=['gll_psc_v16.fit'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175
def test_write_ds9region(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['3FGL'], skydir=skydir, src_radius=8.0) assert len(rm.sources) == 62 rm.write_ds9region("test1.reg", header=True) with open("test1.reg") as fo: dd = fo.readlines() assert len(dd) == 63 # including header rm.write_ds9region("test1.reg", header=False) with open("test1.reg") as fo: dd = fo.readlines() assert len(dd) == 62 # excluding header # check free sources free_sources = rm.get_nearby_sources("3FGL J1745.6-2859c", 1)[-1] nfree = 0 for source in free_sources: if 'Prefactor' in source.spectral_pars: source.spectral_pars['Prefactor']['free'] = True nfree += 1 nfixed = len(rm.sources) - nfree rm.write_ds9region("test1.reg", header=False, free='diamond', fixed="cross") with open("test1.reg") as fo: dd = fo.readlines() ffree = np.array([1 if "diamond" in d else 0 for d in dd]).sum() ffix = np.array([1 if "cross" in d else 0 for d in dd]).sum() assert nfree == ffree assert nfixed == ffix
def test_write_ds9region(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['3FGL'], skydir=skydir, src_radius=8.0) assert len(rm.sources) == 62 rm.write_ds9region("test1.reg",header=True) with open("test1.reg") as fo: dd = fo.readlines() assert len(dd) == 63 # including header rm.write_ds9region("test1.reg", header=False) with open("test1.reg") as fo: dd = fo.readlines() assert len(dd) == 62 # excluding header # check free sources free_sources = rm.get_nearby_sources("3FGL J1745.6-2859c", 1)[-1] nfree = 0 for source in free_sources: if 'Prefactor' in source.spectral_pars: source.spectral_pars['Prefactor']['free'] = True nfree+=1 nfixed = len(rm.sources) - nfree rm.write_ds9region("test1.reg", header=False, free='diamond', fixed="cross") with open("test1.reg") as fo: dd = fo.readlines() ffree = np.array([1 if "diamond" in d else 0 for d in dd]).sum() ffix = np.array([1 if "cross" in d else 0 for d in dd]).sum() assert nfree == ffree assert nfixed== ffix
def test_load_roi_from_dict(tmppath): sources = [{'name': 'ptsrc0', 'SpectrumType': 'PowerLaw', 'ra': 121.0, 'dec': 32.56, 'Index': 2.3, 'Prefactor': 1.3E-15, 'Scale': 1452.0}, {'name': 'ptsrc1', 'SpectrumType': 'PowerLaw', 'ra': 121.0, 'dec': 32.56, 'Index': {'value': 2.3, 'scale': 2.4, 'min': 0.3, 'max': 5.7}, 'Prefactor': {'value': 1.2, 'scale': 3E-12, 'min': 0.0, 'max': 5.0}, 'Scale': {'value': 134.1, 'scale': 1.2, 'min': 0.0, 'max': 2000.0}, }] roi = ROIModel(sources=sources) src = roi['ptsrc0'] assert_allclose(src['dec'], sources[0]['dec']) assert_allclose(src['ra'], sources[0]['ra']) sp = src['spectral_pars'] assert_allclose(sp['Index']['value'], sources[0]['Index']) assert_allclose(sp['Scale']['value'], 1.452) assert_allclose(sp['Prefactor']['value'], 1.3) for p in ['Index', 'Prefactor', 'Scale']: if p == 'Index': assert_allclose(sources[0][p], -src.params[p]['value']) else: assert_allclose(sources[0][p], src.params[p]['value']) src = roi['ptsrc1'] sp = src['spectral_pars'] for p in ['Index', 'Prefactor', 'Scale']: assert_allclose(sources[1][p]['value'] * sources[1][p]['scale'], src.params[p]['value'])
def test_load_3fgl_catalog_xml(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['gll_psc_v16.xml'], extdir='Extended_archive_v15', skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175
def test_load_4fgldr2_catalog_fits(): #tested against web interface: https://heasarc.gsfc.nasa.gov/db-perl/W3Browse/w3query.pl skydir = SkyCoord(0.0, 30.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['4FGL-DR2'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 140 src_name = '4FGL J1605.1-1140' assert(rm[src_name]['SpectrumType'] == 'PowerLaw') check_src_params(rm, src_name, ['Prefactor', 'Index', 'Scale'], [3.5487e-14, -2.0657, 3045.06]) src_name = '4FGL J1557.9-1404' assert(rm[src_name]['SpectrumType'] == 'LogParabola') check_src_params(rm, src_name, ['norm', 'alpha', 'beta', 'Eb'], [1.7456e-13, 2.2619, 0.7502, 1771.59]) src_name = '4FGL J1623.0-0315' assert(rm[src_name]['SpectrumType'] == 'PLSuperExpCutoff2') prefactor = 6.4947e-13 * np.exp( 0.00669 * 1433.48 ** 0.6666667 ) #apparently, the Expfactor is supposed to be 0.006691, but the web interface says 0.00669. check_src_params(rm, src_name, ['Prefactor', 'Index1', 'Index2', 'Expfactor', 'Scale'], [prefactor, -1.4861, 0.6666667, 0.00669, 1433.48], rtol=1e-3)
def read_sources_from_numpy_file(npfile): """ Open a numpy pickle file and read all the new sources into a dictionary Parameters ---------- npfile : file name The input numpy pickle file Returns ------- tab : `~astropy.table.Table` """ srcs = np.load(npfile).flat[0]['sources'] roi = ROIModel() roi.load_sources(srcs.values()) return roi.create_table()
def read_sources_from_yaml_file(yamlfile): """ Open a yaml file and read all the new sources into a dictionary Parameters ---------- yaml : file name The input yaml file Returns ------- tab : `~astropy.table.Table` """ f = open(yamlfile) dd = yaml.safe_load(f) srcs = dd['sources'] f.close() roi = ROIModel() roi.load_sources(srcs.values()) return roi.create_table()
def read_sources_from_yaml_file(yamlfile): """ Open a yaml file and read all the new sources into a dictionary Parameters ---------- yaml : file name The input yaml file Returns ------- tab : `~astropy.table.Table` """ f = open(yamlfile) dd = yaml.load(f) srcs = dd['sources'] f.close() roi = ROIModel() roi.load_sources(srcs.values()) return roi.create_table()
def test_load_3fgl_catalog_xml(): skydir = SkyCoord(0.0, 0.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['gll_psc_v16.xml'], extdir='Extended_archive_v15', skydir=skydir, src_radius=20.0) assert len(rm.sources) == 175 src_name = '3FGL J1747.2-2958' assert (rm[src_name]['SpectrumType'] == 'PLSuperExpCutoff') check_src_params( rm, src_name, ['Prefactor', 'Index1', 'Index2', 'Scale', 'Cutoff'], [1.08110111000e-10, -1.79597532749, 1.0, 585.4324476, 3328.21728515])
def test_create_roi_from_source(): rm = ROIModel.create_from_source('3FGL J2021.0+4031e', {'catalogs': ['3FGL'], 'src_radius': 2.0}) assert len(rm.sources) == 9 src = rm.sources[0] assert src.name == '3FGL J2021.0+4031e' assert src['SpatialType'] == 'SpatialMap' assert src['SourceType'] == 'DiffuseSource' assert src['SpectrumType'] == 'PowerLaw' assert_allclose(src['ra'], 305.26999, rtol=1E-5) assert_allclose(src['dec'], 40.52, rtol=1E-5) assert_allclose(src.spectral_pars['Index']['value'], 1.53, rtol=1E-4) assert_allclose(src.spectral_pars['Prefactor']['value'] * src.spectral_pars['Prefactor']['scale'], 0.4003659112E-12, rtol=1E-4) assert_allclose(src.spatial_pars['Prefactor']['value'], 1.0, rtol=1E-4) assert_allclose(src.spatial_pars['Prefactor']['scale'], 1.0, rtol=1E-4)
def test_create_roi_from_source_4fgldr2(): rm = ROIModel.create_from_source('4FGL J2021.0+4031e', {'catalogs': ['4FGL-DR2'], 'src_radius': 2.0}) assert len(rm.sources) == 6 src = rm.sources[0] assert src.name == '4FGL J2021.0+4031e' assert src['SpatialType'] == 'RadialDisk' assert src['SourceType'] == 'DiffuseSource' assert src['SpectrumType'] == 'LogParabola' assert_allclose(src['ra'], 305.27, rtol=1E-5) assert_allclose(src['dec'], 40.52, rtol=1E-5) assert_allclose(src.spectral_pars['alpha']['value'], 1.8747, rtol=1E-4) assert_allclose(src.spectral_pars['beta']['value'] * src.spectral_pars['beta']['scale'], 0.0606, rtol=1E-4) assert_allclose(src.spectral_pars['norm']['value'] * src.spectral_pars['norm']['scale'], 2.0858e-13, rtol=1E-4) assert_allclose(src.spatial_pars['Radius']['value'], 0.63, rtol=1E-4) assert_allclose(src.spatial_pars['DEC']['scale'], 1.0, rtol=1E-4)
def test_load_fl8y_catalog_fits(): skydir = SkyCoord(0.0, 30.0, unit='deg', frame='galactic').icrs rm = ROIModel(catalogs=['FL8Y'], skydir=skydir, src_radius=20.0) assert len(rm.sources) == 146 src_name = 'FL8Y J1605.1-1140' assert (rm[src_name]['SpectrumType'] == 'PowerLaw') check_src_params(rm, src_name, ['Prefactor', 'Index', 'Scale'], [3.0401211875e-14, -2.06162524223, 3171.08496]) src_name = 'FL8Y J1557.9-1402' assert (rm[src_name]['SpectrumType'] == 'LogParabola') check_src_params( rm, src_name, ['norm', 'alpha', 'beta', 'Eb'], [1.4193160535e-13, 2.11492037773, 0.99977719783, 1884.4001464]) src_name = 'FL8Y J1614.5-2230' assert (rm[src_name]['SpectrumType'] == 'PLSuperExpCutoff2') check_src_params(rm, src_name, ['Prefactor', 'Index1', 'Index2', 'Scale', 'Expfactor'], [ 2.34444495067e-11, -0.099147409200, 0.66666668653, 1415.79650878, 0.016446555033 ])
def test_load_composite_source_from_xml(tmppath): values = { 'ptsrc_dec': 52.6356, 'ptsrc_ra': 252.367, 'ptsrc_Index_value': 2.21879, 'ptsrc_Index_scale': 1.2, 'ptsrc_Index_min': 1.3, 'ptsrc_Index_max': 3.5, 'ptsrc_Prefactor_value': 0.727, 'ptsrc_Prefactor_scale': 1e-12, 'ptsrc_Prefactor_min': 0.01, 'ptsrc_Prefactor_max': 10.0, 'ptsrc_Scale_value': 1.3, 'ptsrc_Scale_scale': 1e3, 'ptsrc_Scale_min': 0.01, 'ptsrc_Scale_max': 113.0 } xmlmodel = """ <source_library title="source_library"> <source name="CompositeSource" type="CompositeSource"> <spectrum type="ConstantValue"> <parameter free="1" max="3.402823466e+38" min="-3.402823466e+38" name="Value" scale="1" value="1" /> </spectrum> <source_library> <source name="SourceA" type="PointSource"> <spatialModel type="SkyDirFunction"> <parameter free="0" max="90.0" min="-90.0" name="DEC" scale="1.0" value="{ptsrc_dec}"/> <parameter free="0" max="360.0" min="-360.0" name="RA" scale="1.0" value="{ptsrc_ra}"/> </spatialModel> <spectrum type="PowerLaw"> <parameter free="0" max="{ptsrc_Index_max}" min="{ptsrc_Index_min}" name="Index" scale="{ptsrc_Index_scale}" value="{ptsrc_Index_value}"/> <parameter free="0" max="{ptsrc_Scale_max}" min="{ptsrc_Scale_min}" name="Scale" scale="{ptsrc_Scale_scale}" value="{ptsrc_Scale_value}"/> <parameter free="0" max="{ptsrc_Prefactor_max}" min="{ptsrc_Prefactor_min}" name="Prefactor" scale="{ptsrc_Prefactor_scale}" value="{ptsrc_Prefactor_value}"/> </spectrum> </source> <source name="SourceB" type="PointSource"> <spatialModel type="SkyDirFunction"> <parameter free="0" max="90.0" min="-90.0" name="DEC" scale="1.0" value="{ptsrc_dec}"/> <parameter free="0" max="360.0" min="-360.0" name="RA" scale="1.0" value="{ptsrc_ra}"/> </spatialModel> <spectrum type="PowerLaw"> <parameter free="0" max="{ptsrc_Index_max}" min="{ptsrc_Index_min}" name="Index" scale="{ptsrc_Index_scale}" value="{ptsrc_Index_value}"/> <parameter free="0" max="{ptsrc_Scale_max}" min="{ptsrc_Scale_min}" name="Scale" scale="{ptsrc_Scale_scale}" value="{ptsrc_Scale_value}"/> <parameter free="0" max="{ptsrc_Prefactor_max}" min="{ptsrc_Prefactor_min}" name="Prefactor" scale="{ptsrc_Prefactor_scale}" value="{ptsrc_Prefactor_value}"/> </spectrum> </source> </source_library> </source> </source_library> """.format(**values) root = ElementTree.fromstring(xmlmodel) xmlfile = str(tmppath.join('test.xml')) ElementTree.ElementTree(root).write(xmlfile) roi = ROIModel(config={'catalogs': [xmlfile]}) src = roi['CompositeSource'] assert (type(src) == roi_model.CompositeSource) assert (src['SpectrumType'] == 'ConstantValue') assert (src['SpatialType'] == 'CompositeSource') assert (src['SpatialModel'] == 'CompositeSource') assert (src['SourceType'] == 'CompositeSource') attribs = ['value', 'scale', 'min', 'max'] par_names = ['Index', 'Prefactor', 'Scale'] for s in src.nested_sources: pars = s.spectral_pars for par_name in par_names: for x in attribs: assert_allclose(pars[par_name][x], values['ptsrc_%s_%s' % (par_name, x)])
def test_load_source_from_xml(tmppath): values = { 'ptsrc_dec': 52.6356, 'ptsrc_ra': 252.367, 'ptsrc_index_value': 2.21879, 'ptsrc_index_scale': 1.2, 'ptsrc_index_min': 1.3, 'ptsrc_index_max': 3.5, 'ptsrc_prefactor_value': 0.727, 'ptsrc_prefactor_scale': 1e-12, 'ptsrc_prefactor_min': 0.01, 'ptsrc_prefactor_max': 10.0, 'ptsrc_scale_value': 1.3, 'ptsrc_scale_scale': 1e3, 'ptsrc_scale_min': 0.01, 'ptsrc_scale_max': 113.0 } xmlmodel = """ <source_library title="source_library"> <source name="3FGL J1649.4+5238" type="PointSource"> <spatialModel type="SkyDirFunction"> <parameter free="0" max="90.0" min="-90.0" name="DEC" scale="1.0" value="{ptsrc_dec}"/> <parameter free="0" max="360.0" min="-360.0" name="RA" scale="1.0" value="{ptsrc_ra}"/> </spatialModel> <spectrum type="PowerLaw"> <parameter free="0" max="{ptsrc_index_max}" min="{ptsrc_index_min}" name="Index" scale="{ptsrc_index_scale}" value="{ptsrc_index_value}"/> <parameter free="0" max="{ptsrc_scale_max}" min="{ptsrc_scale_min}" name="Scale" scale="{ptsrc_scale_scale}" value="{ptsrc_scale_value}"/> <parameter free="0" max="{ptsrc_prefactor_max}" min="{ptsrc_prefactor_min}" name="Prefactor" scale="{ptsrc_prefactor_scale}" value="{ptsrc_prefactor_value}"/> </spectrum> </source> <source name="isodiff" type="DiffuseSource"> <spectrum ctype="-1" file="$(FERMIPY_ROOT)/data/iso_P8R2_SOURCE_V6_v06.txt" type="FileFunction"> <parameter free="0" max="1000.0" min="0.001" name="Normalization" scale="1.0" value="1.2"/> </spectrum> <spatialModel type="ConstantValue"> <parameter free="0" max="10" min="0" name="Value" scale="1" value="1"/> </spatialModel> </source> <source name="galdiff" type="DiffuseSource"> <spectrum type="PowerLaw"> <parameter free="0" max="1.0" min="-1.0" name="Index" scale="-1" value="0.0"/> <parameter free="0" max="1000.0" min="1000.0" name="Scale" scale="1" value="1000.0"/> <parameter free="0" max="10.0" min="0.1" name="Prefactor" scale="1" value="1.0"/> </spectrum> <spatialModel file="$(FERMIPY_WORKDIR)/gll_iem_v06_extracted.fits" type="MapCubeFunction"> <parameter free="0" max="10" min="0" name="Normalization" scale="1" value="1"/> </spatialModel> </source> </source_library> """.format(**values) root = ElementTree.fromstring(xmlmodel) xmlfile = str(tmppath.join('test.xml')) ElementTree.ElementTree(root).write(xmlfile) roi = ROIModel(config={'catalogs': [xmlfile]}) src = roi['3FGL J1649.4+5238'] assert_allclose(src['dec'], values['ptsrc_dec']) assert_allclose(src['ra'], values['ptsrc_ra']) assert (src['SpectrumType'] == 'PowerLaw') assert (src['SpatialType'] == 'SkyDirFunction') assert (src['SpatialModel'] == 'PointSource') assert (src['SourceType'] == 'PointSource') sp = src['spectral_pars'] attribs = ['value', 'scale', 'min', 'max'] for x in attribs: assert_allclose(sp['Index'][x], values['ptsrc_index_%s' % x]) for x in attribs: assert_allclose(sp['Prefactor'][x], values['ptsrc_prefactor_%s' % x]) for x in attribs: assert_allclose(sp['Scale'][x], values['ptsrc_scale_%s' % x]) assert_allclose( src.params['Prefactor']['value'], values['ptsrc_prefactor_value'] * values['ptsrc_prefactor_scale']) assert_allclose(src.params['Index']['value'], values['ptsrc_index_value'] * values['ptsrc_index_scale']) assert_allclose(src.params['Scale']['value'], values['ptsrc_scale_value'] * values['ptsrc_scale_scale']) src = roi['galdiff'] assert (src['SpatialType'] == 'MapCubeFunction') assert (src['SpatialModel'] == 'MapCubeFunction') src = roi['isodiff'] assert (src['SpatialType'] == 'ConstantValue') assert (src['SpatialModel'] == 'ConstantValue')
def test_load_2fhl_catalog_fits(): rm = ROIModel(catalogs=['2FHL']) assert len(rm.sources) == 360
import yaml import sys from fermipy.utils import * from fermipy.roi_model import ROIModel roi = ROIModel({"catalogs": ["3FGL"]}) # cuts0 = [('TS_value',10000.0,None),('GLAT',5.,90.)] # cuts1 = [('TS_value',10000.0,None),('GLAT',-90.,-5.)] cuts0 = [("GLAT", 5.0, 90.0)] cuts1 = [("GLAT", -90.0, -5.0)] srcs = roi.get_sources(cuts0) + roi.get_sources(cuts1) config = {"selection": {}} configs = {} for s in srcs: if s.extended: continue c = copy.deepcopy(config) c["selection"]["target"] = s.name configs[s.name.lower().replace(" ", "_")] = c # srcs_dict[s.name] = s print "found ", len(configs), " sources"
config = {} config['model'] = dict( src_roiwidth = 10.0, galdiff = '/nfs/slac/g/ki/ki20/cta/mdwood/fermi/diffuse/v5r0/gll_iem_v06.fits', isodiff = '/nfs/slac/g/ki/ki20/cta/mdwood/fermi/diffuse/v5r0/iso_P8R2_SOURCE_V6_v06.txt', catalogs = ['gll_psc_v14.fit']) config['run'] = {} roi = ROIModel(config['model']) roi.load() o = {'ts_ext' : [], 'ext_ul95' : [], 'ext' : [], 'name' : [], 'assoc' : []} for d in sys.argv[1:]: # results = yaml.load(open(os.path.join(d,'fit1.yaml')),Loader=yaml.CLoader) results_file = os.path.join(d,'fit1.npy') if not os.path.isfile(results_file): print 'Skipping ', d continue