Example #1
0
def test_set_spatial_model(tmppath):
    ra = 252.367
    dec = 52.6356

    src_dict = {'SpatialModel': 'GaussianSource', 'ra': ra, 'dec': dec}
    src = Source('testsrc', src_dict)

    src.set_spatial_model('PointSource')
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'
Example #2
0
def test_create_point_source(tmppath):
    ra = 252.367
    dec = 52.6356
    prefactor = 2.3

    src_dict = {
        'SpatialModel': 'PointSource',
        'SpectrumType': 'PowerLaw',
        'ra': ra,
        'dec': dec,
        'spectral_pars': {
            'Prefactor': {
                'value': prefactor
            }
        }
    }
    src = Source('testsrc', src_dict)

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src.spatial_pars['RA']['value'], ra)
    assert_allclose(src.spatial_pars['DEC']['value'], dec)
    assert_allclose(src.spectral_pars['Prefactor']['value'], prefactor)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SpectrumType'] == 'PowerLaw'
Example #3
0
def test_is_source_free():
    ra = 252.367
    dec = 52.6356
    src1 = Source.create_from_dict(
        {
            'name': 'testsrc',
            'SpatialModel': 'PointSource',
            'SpectrumType': 'PowerLaw',
            'Index': 2.3,
            'Prefactor': {
                'value': 1.3,
                'scale': 1E-8,
                'min': 0.15,
                'max': 10.0,
                'free': True
            },
            'ra': ra,
            'dec': dec
        },
        rescale=True)
    src2 = Source.create_from_dict(
        {
            'name': 'testsrc',
            'SpatialModel': 'PointSource',
            'SpectrumType': 'PowerLaw',
            'Index': 2.3,
            'Prefactor': {
                'value': 1.3,
                'scale': 1E-8,
                'min': 0.15,
                'max': 10.0,
                'free': False
            },
            'ra': ra,
            'dec': dec
        },
        rescale=True)
    assert src1.is_free == True
    assert src2.is_free == False
Example #4
0
def test_is_source_free():
    ra = 252.367
    dec = 52.6356
    src1 = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'PointSource',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3,
                                   'Prefactor': {'value': 1.3, 'scale': 1E-8,
                                                 'min': 0.15, 'max': 10.0,
                                                 'free': True},
                                   'ra': ra, 'dec': dec},
                                  rescale=True)
    src2 = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'PointSource',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3,
                                   'Prefactor': {'value': 1.3, 'scale': 1E-8,
                                                 'min': 0.15, 'max': 10.0,
                                                 'free': False},
                                   'ra': ra, 'dec': dec},
                                  rescale=True)
    assert src1.is_free == True
    assert src2.is_free == False
Example #5
0
def test_create_gaussian_source(tmppath):
    ra = 252.367
    dec = 52.6356
    sigma = 0.5

    src_dict = {'SpatialModel': 'RadialGaussian',
                'ra': ra, 'dec': dec, 'spatial_pars': {'Sigma': {'value': sigma}}}
    src = Source('testsrc', src_dict)

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    if src['SpatialType'] == 'RadialGaussian':
        assert_allclose(src.spatial_pars['RA']['value'], ra)
        assert_allclose(src.spatial_pars['DEC']['value'], dec)
        assert_allclose(src.spatial_pars['Sigma']['value'], sigma)
    assert src['SpatialModel'] == 'RadialGaussian'
Example #6
0
def test_create_source(tmppath):
    ra = 252.367
    dec = 52.6356
    sigma = 0.5

    src_dict = {
        'SpatialModel': 'GaussianSource',
        'ra': ra,
        'dec': dec,
        'Sigma': sigma
    }
    src = Source('testsrc', src_dict)

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src['Sigma'], sigma)
    assert src['SpatialModel'] == 'GaussianSource'
Example #7
0
    def _make_ts_cube(self, prefix, **kwargs):

        skywcs = kwargs.get('wcs', self.geom.wcs)
        npix = kwargs.get('npix', self.npix)

        galactic = wcs_utils.is_galactic(skywcs)
        ref_skydir = wcs_utils.wcs_to_skydir(skywcs)
        refdir = pyLike.SkyDir(ref_skydir.ra.deg, ref_skydir.dec.deg)
        pixsize = np.abs(skywcs.wcs.cdelt[0])

        skyproj = pyLike.FitScanner.buildSkyProj(str("AIT"), refdir, pixsize,
                                                 npix, galactic)

        src_dict = copy.deepcopy(kwargs.setdefault('model', {}))
        src_dict = {} if src_dict is None else src_dict

        xpix, ypix = (np.round(
            (self.npix - 1.0) / 2.), np.round((self.npix - 1.0) / 2.))
        skydir = wcs_utils.pix_to_skydir(xpix, ypix, skywcs)

        src_dict['ra'] = skydir.ra.deg
        src_dict['dec'] = skydir.dec.deg
        src_dict.setdefault('SpatialModel', 'PointSource')
        src_dict.setdefault('SpatialWidth', 0.3)
        src_dict.setdefault('Index', 2.0)
        src_dict.setdefault('Prefactor', 1E-13)
        src_dict['name'] = 'tscube_testsource'

        src = Source.create_from_dict(src_dict)

        modelname = utils.create_model_name(src)

        optFactory = pyLike.OptimizerFactory_instance()
        optObject = optFactory.create(str("MINUIT"),
                                      self.components[0].like.logLike)

        pylike_src = self.components[0]._create_source(src)
        fitScanner = pyLike.FitScanner(self.like.composite, optObject, skyproj,
                                       npix, npix)

        pylike_src.spectrum().normPar().setBounds(0, 1E6)

        fitScanner.setTestSource(pylike_src)

        self.logger.info("Running tscube")
        outfile = utils.format_filename(self.config['fileio']['workdir'],
                                        'tscube.fits',
                                        prefix=[prefix])

        try:
            fitScanner.run_tscube(True, kwargs['do_sed'], kwargs['nnorm'],
                                  kwargs['norm_sigma'], kwargs['cov_scale_bb'],
                                  kwargs['cov_scale'], kwargs['tol'],
                                  kwargs['max_iter'], kwargs['tol_type'],
                                  kwargs['remake_test_source'],
                                  kwargs['st_scan_level'], str(''),
                                  kwargs['init_lambda'])
        except Exception:
            fitScanner.run_tscube(True, kwargs['do_sed'], kwargs['nnorm'],
                                  kwargs['norm_sigma'], kwargs['cov_scale_bb'],
                                  kwargs['cov_scale'], kwargs['tol'],
                                  kwargs['max_iter'], kwargs['tol_type'],
                                  kwargs['remake_test_source'],
                                  kwargs['st_scan_level'])

        self.logger.info("Writing FITS output")

        fitScanner.writeFitsFile(str(outfile), str("gttscube"))

        convert_tscube(str(outfile), str(outfile))

        tscube = castro.TSCube.create_from_fits(outfile)
        ts_map = tscube.tsmap
        norm_map = tscube.normmap
        npred_map = copy.deepcopy(norm_map)
        npred_map.data *= tscube.refSpec.ref_npred.sum()
        amp_map = copy.deepcopy(norm_map)
        amp_map.data *= src_dict['Prefactor']

        sqrt_ts_map = copy.deepcopy(ts_map)
        sqrt_ts_map.data[...] = np.abs(sqrt_ts_map.data)**0.5

        o = {
            'name': utils.join_strings([prefix, modelname]),
            'src_dict': copy.deepcopy(src_dict),
            'file': os.path.basename(outfile),
            'ts': ts_map,
            'sqrt_ts': sqrt_ts_map,
            'npred': npred_map,
            'amplitude': amp_map,
            'config': kwargs,
            'tscube': tscube
        }

        if not kwargs['write_fits']:
            os.remove(outfile)
            os['file'] = None

        self.logger.info("Done")
        return o
Example #8
0
    def _make_ts_cube(self, prefix, **kwargs):

        map_skydir = kwargs.get('map_skydir')
        map_size = kwargs.get('map_size')
        exclude = kwargs.get('exclude', [])

        # We take the coordinate system and the bin size from the underlying map
        skywcs = self._geom.wcs
        galactic = wcs_utils.is_galactic(skywcs)
        pixsize = max(np.abs(skywcs.wcs.cdelt))

        # If the map_size is not specified we need to find the right number of pixels
        if map_size is None:
            npix = max(self._geom.npix)[0]
            map_size = pixsize * npix
        else:
            npix = int(np.round(map_size / pixsize))

        saved_state = LikelihoodState(self.like)

        for ex_src in exclude:
            self.zero_source(ex_src)

        if map_skydir is None:
            # Take the center of the wcs
            map_geom = self._geom.to_image()
            frame = coordsys_to_frame(map_geom.coordsys)
            map_skydir = SkyCoord(*map_geom.pix_to_coord(
                self._geom.wcs.wcs.crpix),
                                  frame=frame,
                                  unit='deg')
            map_skydir = map_skydir.transform_to('icrs')

        refdir = pyLike.SkyDir(map_skydir.ra.deg, map_skydir.dec.deg)

        src_dict = copy.deepcopy(kwargs.setdefault('model', {}))
        src_dict = {} if src_dict is None else src_dict

        src_dict['ra'] = map_skydir.ra.deg
        src_dict['dec'] = map_skydir.dec.deg
        src_dict.setdefault('SpatialModel', 'PointSource')
        src_dict.setdefault('SpatialWidth', 0.3)
        src_dict.setdefault('Index', 2.0)
        src_dict.setdefault('Prefactor', 1.0)
        src_dict['name'] = 'tscube_testsource'

        src = Source.create_from_dict(src_dict)
        if 'Prefactor' in src.spectral_pars:
            src.spectral_pars['Prefactor']['scale'] = 1.0e-10

        modelname = utils.create_model_name(src)
        pylike_src = self.components[0]._create_source(src)
        pylike_src.spectrum().normPar().setBounds(0, 1E6)

        skyproj = pyLike.FitScanner.buildSkyProj(str("AIT"), refdir, pixsize,
                                                 npix, galactic)

        optFactory = pyLike.OptimizerFactory_instance()
        optObject = optFactory.create(str("MINUIT"), self.like.composite)

        fitScanner = pyLike.FitScanner(self.like.composite, optObject, skyproj,
                                       npix, npix)
        fitScanner.set_quiet(True)
        fitScanner.setTestSource(pylike_src)

        self.logger.info("Running tscube")
        outfile = utils.format_filename(self.config['fileio']['workdir'],
                                        'tscube.fits',
                                        prefix=[prefix])

        fitScanner.run_tscube(True, kwargs['do_sed'], kwargs['nnorm'],
                              kwargs['norm_sigma'], kwargs['cov_scale_bb'],
                              kwargs['cov_scale'], kwargs['tol'],
                              kwargs['max_iter'], kwargs['tol_type'],
                              kwargs['remake_test_source'],
                              kwargs['st_scan_level'], str(''),
                              kwargs['init_lambda'])

        self.logger.info("Writing FITS output")

        fitScanner.writeFitsFile(str(outfile), str("gttscube"), "", False,
                                 pyLike.FitScanner.TSMAP_ONLY)
        saved_state.restore()

        convert_tscube(str(outfile), str(outfile))

        tscube = castro.TSCube.create_from_fits(outfile)
        ts_map = tscube.tsmap
        norm_map = tscube.normmap
        npred_map = copy.deepcopy(norm_map)
        npred_map.data *= tscube.refSpec.ref_npred.sum()
        amp_map = copy.deepcopy(norm_map)
        amp_map.data *= pylike_src.spectrum().normPar().getValue()
        sqrt_ts_map = copy.deepcopy(ts_map)
        sqrt_ts_map.data[...] = np.abs(sqrt_ts_map.data)**0.5

        o = {
            'name': utils.join_strings([prefix, modelname]),
            'src_dict': copy.deepcopy(src_dict),
            'file': os.path.basename(outfile),
            'ts': ts_map,
            'sqrt_ts': sqrt_ts_map,
            'npred': npred_map,
            'amplitude': amp_map,
            'config': kwargs,
            'tscube': tscube
        }

        if not kwargs['write_fits']:
            os.remove(outfile)
            o['file'] = None

        self.logger.info("Done")
        return o
Example #9
0
def test_set_spatial_model(tmppath):
    ra = 252.367
    dec = 52.6356

    src_dict = {'SpatialModel': 'RadialGaussian', 'ra': ra, 'dec': dec}
    src = Source('testsrc', src_dict)

    src.set_spatial_model('PointSource', {'ra': 1.0, 'dec': 2.0})
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'

    src_dict = {'SpatialModel': 'RadialGaussian', 'ra': ra, 'dec': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model('PointSource', {
        'RA': {
            'value': 1.0
        },
        'DEC': {
            'value': 2.0
        }
    })
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model('PointSource', {
        'RA': {
            'value': 1.0
        },
        'DEC': {
            'value': 2.0
        }
    })
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model('RadialDisk', {'ra': 2.0, 'dec': 3.0, 'Radius': 1.0})

    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['RA']['value'], 2.0)
        assert_allclose(src.spatial_pars['DEC']['value'], 3.0)
        assert_allclose(src.spatial_pars['Radius']['value'], 1.0)
    assert_allclose(src['ra'], 2.0)
    assert_allclose(src['dec'], 3.0)
    assert src['SpatialModel'] == 'RadialDisk'
    assert src['SourceType'] == 'DiffuseSource'

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model('RadialDisk', {
        'ra': 2.0,
        'dec': 3.0,
        'SpatialWidth': 2.0
    })

    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['RA']['value'], 2.0)
        assert_allclose(src.spatial_pars['DEC']['value'], 3.0)
    assert_allclose(src['ra'], 2.0)
    assert_allclose(src['dec'], 3.0)
    assert_allclose(src['SpatialWidth'], 2.0)
    assert src['SpatialModel'] == 'RadialDisk'
    assert src['SourceType'] == 'DiffuseSource'
Example #10
0
def test_create_source_from_dict(tmppath):
    ra = 252.367
    dec = 52.6356

    src = Source.create_from_dict(
        {
            'name': 'testsrc',
            'SpatialModel': 'PointSource',
            'SpectrumType': 'PowerLaw',
            'Index': 2.3,
            'Prefactor': 1.3E-9,
            'ra': ra,
            'dec': dec
        },
        rescale=True)

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src.spectral_pars['Prefactor']['value'], 1.3)
    assert_allclose(src.spectral_pars['Prefactor']['scale'], 1E-9)
    assert_allclose(src.spectral_pars['Index']['value'], 2.3)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'
    assert src.extended is False
    assert 'Prefactor' not in src.data
    assert 'Index' not in src.data

    src = Source.create_from_dict(
        {
            'name': 'testsrc',
            'SpatialModel': 'PointSource',
            'SpectrumType': 'PowerLaw',
            'Index': 2.3,
            'Prefactor': {
                'value': 1.3,
                'scale': 1E-8,
                'min': 0.15,
                'max': 10.0,
                'free': False
            },
            'ra': ra,
            'dec': dec
        },
        rescale=True)

    assert_allclose(src.spectral_pars['Prefactor']['value'], 1.3)
    assert_allclose(src.spectral_pars['Prefactor']['scale'], 1E-8)
    assert_allclose(src.spectral_pars['Prefactor']['min'], 0.15)
    assert_allclose(src.spectral_pars['Prefactor']['max'], 10.0)
    assert src.spectral_pars['Prefactor']['free'] is False

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'RadialGaussian',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert src['SpatialModel'] == 'RadialGaussian'
    assert (src['SpatialType'] == 'SpatialMap') or (src['SpatialType']
                                                    == 'RadialGaussian')
    assert src['SourceType'] == 'DiffuseSource'
    assert src.extended is True

    # src = Source.create_from_dict({'name': 'testsrc',
    #                               'SpatialModel': 'RadialGaussian',
    #                               'SpectrumType': 'PowerLaw',
    #                               'Index': 2.3, 'Sigma': 0.5,
    #                               'ra': ra, 'dec': dec})

    #assert_allclose(src['ra'], ra)
    #assert_allclose(src['dec'], dec)
    #assert_allclose(src['SpatialWidth'], 0.5*1.5095921854516636)
    # if src['SpatialType'] == 'RadialGaussian':
    #    assert_allclose(src.spatial_pars['Sigma']['value'], 0.5)

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'RadialDisk',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'SpatialWidth': 0.5,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src['SpatialWidth'], 0.5)
    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['Radius']['value'],
                        0.5 / 0.8246211251235321)
Example #11
0
    def _make_ts_cube(self, prefix, **kwargs):

        skywcs = kwargs.get('wcs', self.geom.wcs)
        npix = kwargs.get('npix', self.npix)

        galactic = wcs_utils.is_galactic(skywcs)
        ref_skydir = wcs_utils.wcs_to_skydir(skywcs)
        refdir = pyLike.SkyDir(ref_skydir.ra.deg,
                               ref_skydir.dec.deg)
        pixsize = np.abs(skywcs.wcs.cdelt[0])

        skyproj = pyLike.FitScanner.buildSkyProj(str("AIT"),
                                                 refdir, pixsize, npix,
                                                 galactic)

        src_dict = copy.deepcopy(kwargs.setdefault('model', {}))
        src_dict = {} if src_dict is None else src_dict

        xpix, ypix = (np.round((self.npix - 1.0) / 2.),
                      np.round((self.npix - 1.0) / 2.))
        skydir = wcs_utils.pix_to_skydir(xpix, ypix, skywcs)

        src_dict['ra'] = skydir.ra.deg
        src_dict['dec'] = skydir.dec.deg
        src_dict.setdefault('SpatialModel', 'PointSource')
        src_dict.setdefault('SpatialWidth', 0.3)
        src_dict.setdefault('Index', 2.0)
        src_dict.setdefault('Prefactor', 1E-13)
        src_dict['name'] = 'tscube_testsource'

        src = Source.create_from_dict(src_dict)

        modelname = utils.create_model_name(src)

        optFactory = pyLike.OptimizerFactory_instance()
        optObject = optFactory.create(str("MINUIT"),
                                      self.components[0].like.logLike)

        pylike_src = self.components[0]._create_source(src)
        fitScanner = pyLike.FitScanner(self.like.composite, optObject, skyproj,
                                       npix, npix)

        pylike_src.spectrum().normPar().setBounds(0, 1E6)

        fitScanner.setTestSource(pylike_src)

        self.logger.info("Running tscube")
        outfile = utils.format_filename(self.config['fileio']['workdir'],
                                        'tscube.fits',
                                        prefix=[prefix])

        try:
            fitScanner.run_tscube(True,
                                  kwargs['do_sed'], kwargs['nnorm'],
                                  kwargs['norm_sigma'],
                                  kwargs['cov_scale_bb'], kwargs['cov_scale'],
                                  kwargs['tol'], kwargs['max_iter'],
                                  kwargs['tol_type'],
                                  kwargs['remake_test_source'],
                                  kwargs['st_scan_level'],
                                  str(''),
                                  kwargs['init_lambda'])
        except Exception:
            fitScanner.run_tscube(True,
                                  kwargs['do_sed'], kwargs['nnorm'],
                                  kwargs['norm_sigma'],
                                  kwargs['cov_scale_bb'], kwargs['cov_scale'],
                                  kwargs['tol'], kwargs['max_iter'],
                                  kwargs['tol_type'],
                                  kwargs['remake_test_source'],
                                  kwargs['st_scan_level'])

        self.logger.info("Writing FITS output")

        fitScanner.writeFitsFile(str(outfile), str("gttscube"))

        convert_tscube(str(outfile), str(outfile))

        tscube = castro.TSCube.create_from_fits(outfile)
        ts_map = tscube.tsmap
        norm_map = tscube.normmap
        npred_map = copy.deepcopy(norm_map)
        npred_map.data *= tscube.refSpec.ref_npred.sum()
        amp_map = copy.deepcopy(norm_map)
        amp_map.data *= src_dict['Prefactor']

        sqrt_ts_map = copy.deepcopy(ts_map)
        sqrt_ts_map.data[...] = np.abs(sqrt_ts_map.data)**0.5

        o = {'name': utils.join_strings([prefix, modelname]),
             'src_dict': copy.deepcopy(src_dict),
             'file': os.path.basename(outfile),
             'ts': ts_map,
             'sqrt_ts': sqrt_ts_map,
             'npred': npred_map,
             'amplitude': amp_map,
             'config': kwargs,
             'tscube': tscube
             }

        if not kwargs['write_fits']:
            os.remove(outfile)
            os['file'] = None

        self.logger.info("Done")
        return o
Example #12
0
def test_create_source_from_dict(tmppath):
    ra = 252.367
    dec = 52.6356

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'PointSource',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'
    assert src.extended is False

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'GaussianSource',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert src['SpatialModel'] == 'GaussianSource'
    assert src['SpatialType'] == 'SpatialMap'
    assert src['SourceType'] == 'DiffuseSource'
    assert src.extended is True

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'RadialGaussian',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'Sigma': 0.5,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src['Sigma'], 0.5)
    if src['SpatialType'] == 'RadialGaussian':
        assert_allclose(src.spatial_pars['Sigma']['value'], 0.5)

    assert src['SpatialModel'] == 'RadialGaussian'
    assert src['SourceType'] == 'DiffuseSource'
    assert src.extended is True

    src = Source.create_from_dict({
        'name': 'testsrc',
        'SpatialModel': 'RadialDisk',
        'SpectrumType': 'PowerLaw',
        'Index': 2.3,
        'Radius': 0.5,
        'ra': ra,
        'dec': dec
    })

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src['Radius'], 0.5)
    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['Radius']['value'], 0.5)

    assert src['SpatialModel'] == 'RadialDisk'
    assert src['SourceType'] == 'DiffuseSource'
    assert src.extended is True
Example #13
0
def test_set_spatial_model(tmppath):
    ra = 252.367
    dec = 52.6356

    src_dict = {'SpatialModel': 'RadialGaussian', 'ra': ra, 'dec': dec}
    src = Source('testsrc', src_dict)

    src.set_spatial_model('PointSource', {'ra': 1.0, 'dec': 2.0})
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'

    src_dict = {'SpatialModel': 'RadialGaussian', 'ra': ra, 'dec': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model(
        'PointSource', {'RA': {'value': 1.0}, 'DEC': {'value': 2.0}})
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model(
        'PointSource', {'RA': {'value': 1.0}, 'DEC': {'value': 2.0}})
    assert_allclose(src.spatial_pars['RA']['value'], 1.0)
    assert_allclose(src.spatial_pars['DEC']['value'], 2.0)
    assert_allclose(src['ra'], 1.0)
    assert_allclose(src['dec'], 2.0)

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model('RadialDisk', {'ra': 2.0, 'dec': 3.0, 'Radius': 1.0})

    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['RA']['value'], 2.0)
        assert_allclose(src.spatial_pars['DEC']['value'], 3.0)
        assert_allclose(src.spatial_pars['Radius']['value'], 1.0)
    assert_allclose(src['ra'], 2.0)
    assert_allclose(src['dec'], 3.0)
    assert src['SpatialModel'] == 'RadialDisk'
    assert src['SourceType'] == 'DiffuseSource'

    src_dict = {'SpatialModel': 'RadialGaussian', 'RA': ra, 'DEC': dec}
    src = Source('testsrc', src_dict)
    src.set_spatial_model(
        'RadialDisk', {'ra': 2.0, 'dec': 3.0, 'SpatialWidth': 2.0})

    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['RA']['value'], 2.0)
        assert_allclose(src.spatial_pars['DEC']['value'], 3.0)
    assert_allclose(src['ra'], 2.0)
    assert_allclose(src['dec'], 3.0)
    assert_allclose(src['SpatialWidth'], 2.0)
    assert src['SpatialModel'] == 'RadialDisk'
    assert src['SourceType'] == 'DiffuseSource'
Example #14
0
def test_create_source_from_dict(tmppath):
    ra = 252.367
    dec = 52.6356

    src = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'PointSource',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3,
                                   'Prefactor': 1.3E-9,
                                   'ra': ra, 'dec': dec},
                                  rescale=True)

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src.spectral_pars['Prefactor']['value'], 1.3)
    assert_allclose(src.spectral_pars['Prefactor']['scale'], 1E-9)
    assert_allclose(src.spectral_pars['Index']['value'], 2.3)
    assert src['SpatialModel'] == 'PointSource'
    assert src['SpatialType'] == 'SkyDirFunction'
    assert src['SourceType'] == 'PointSource'
    assert src.extended is False
    assert 'Prefactor' not in src.data
    assert 'Index' not in src.data

    src = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'PointSource',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3,
                                   'Prefactor': {'value': 1.3, 'scale': 1E-8,
                                                 'min': 0.15, 'max': 10.0,
                                                 'free': False},
                                   'ra': ra, 'dec': dec},
                                  rescale=True)

    assert_allclose(src.spectral_pars['Prefactor']['value'], 1.3)
    assert_allclose(src.spectral_pars['Prefactor']['scale'], 1E-8)
    assert_allclose(src.spectral_pars['Prefactor']['min'], 0.15)
    assert_allclose(src.spectral_pars['Prefactor']['max'], 10.0)
    assert src.spectral_pars['Prefactor']['free'] is False

    src = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'RadialGaussian',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3,
                                   'ra': ra, 'dec': dec})

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert src['SpatialModel'] == 'RadialGaussian'
    assert (src['SpatialType'] == 'SpatialMap') or (
        src['SpatialType'] == 'RadialGaussian')
    assert src['SourceType'] == 'DiffuseSource'
    assert src.extended is True

    # src = Source.create_from_dict({'name': 'testsrc',
    #                               'SpatialModel': 'RadialGaussian',
    #                               'SpectrumType': 'PowerLaw',
    #                               'Index': 2.3, 'Sigma': 0.5,
    #                               'ra': ra, 'dec': dec})

    #assert_allclose(src['ra'], ra)
    #assert_allclose(src['dec'], dec)
    #assert_allclose(src['SpatialWidth'], 0.5*1.5095921854516636)
    # if src['SpatialType'] == 'RadialGaussian':
    #    assert_allclose(src.spatial_pars['Sigma']['value'], 0.5)

    src = Source.create_from_dict({'name': 'testsrc',
                                   'SpatialModel': 'RadialDisk',
                                   'SpectrumType': 'PowerLaw',
                                   'Index': 2.3, 'SpatialWidth': 0.5,
                                   'ra': ra, 'dec': dec})

    assert_allclose(src['ra'], ra)
    assert_allclose(src['dec'], dec)
    assert_allclose(src['SpatialWidth'], 0.5)
    if src['SpatialType'] == 'RadialDisk':
        assert_allclose(src.spatial_pars['Radius']['value'],
                        0.5 / 0.8246211251235321)