Esempio n. 1
0
    def localize(self, name, **kwargs):
        """Find the best-fit position of a source.  Localization is
        performed in two steps.  First a TS map is computed centered
        on the source with half-width set by ``dtheta_max``.  A fit is
        then performed to the maximum TS peak in this map.  The source
        position is then further refined by scanning the likelihood in
        the vicinity of the peak found in the first step.  The size of
        the scan region is set to encompass the 99% positional
        uncertainty contour as determined from the peak fit.

        Parameters
        ----------
        name : str
            Source name.

        {options}

        optimizer : dict
            Dictionary that overrides the default optimizer settings.

        Returns
        -------
        localize : dict
            Dictionary containing results of the localization
            analysis.

        """
        name = self.roi.get_source_by_name(name).name

        schema = ConfigSchema(self.defaults['localize'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('use_cache', True)
        schema.add_option('prefix', '')
        config = utils.create_dict(self.config['localize'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Running localization for %s' % name)

        free_state = FreeParameterState(self)
        loc = self._localize(name, **config)
        free_state.restore()

        self.logger.info('Finished localization.')

        if config['make_plots']:
            self._plotter.make_localization_plots(loc, self.roi,
                                                  prefix=config['prefix'])

        outfile = \
            utils.format_filename(self.workdir, 'loc',
                                  prefix=[config['prefix'],
                                          name.lower().replace(' ', '_')])

        if config['write_fits']:
            loc['file'] = os.path.basename(outfile) + '.fits'
            self._make_localize_fits(loc, outfile + '.fits',
                                     **config)

        if config['write_npy']:
            np.save(outfile + '.npy', dict(loc))

        return loc
Esempio n. 2
0
    def localize(self, name, **kwargs):
        """Find the best-fit position of a source.  Localization is
        performed in two steps.  First a TS map is computed centered
        on the source with half-width set by ``dtheta_max``.  A fit is
        then performed to the maximum TS peak in this map.  The source
        position is then further refined by scanning the likelihood in
        the vicinity of the peak found in the first step.  The size of
        the scan region is set to encompass the 99% positional
        uncertainty contour as determined from the peak fit.

        Parameters
        ----------
        name : str
            Source name.

        {options}

        optimizer : dict
            Dictionary that overrides the default optimizer settings.

        Returns
        -------
        localize : dict
            Dictionary containing results of the localization
            analysis.

        """
        timer = Timer.create(start=True)
        name = self.roi.get_source_by_name(name).name

        schema = ConfigSchema(self.defaults['localize'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('use_cache', True)
        schema.add_option('prefix', '')
        config = utils.create_dict(self.config['localize'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Running localization for %s' % name)

        free_state = FreeParameterState(self)
        loc = self._localize(name, **config)
        free_state.restore()

        self.logger.info('Finished localization.')

        if config['make_plots']:
            self._plotter.make_localization_plots(loc, self.roi,
                                                  prefix=config['prefix'])

        outfile = \
            utils.format_filename(self.workdir, 'loc',
                                  prefix=[config['prefix'],
                                          name.lower().replace(' ', '_')])

        if config['write_fits']:
            loc['file'] = os.path.basename(outfile) + '.fits'
            self._make_localize_fits(loc, outfile + '.fits',
                                     **config)

        if config['write_npy']:
            np.save(outfile + '.npy', dict(loc))

        self.logger.info('Execution time: %.2f s', timer.elapsed_time)
        return loc
Esempio n. 3
0
    def extension(self, name, **kwargs):
        """Test this source for spatial extension with the likelihood
        ratio method (TS_ext).  This method will substitute an
        extended spatial model for the given source and perform a
        one-dimensional scan of the spatial extension parameter over
        the range specified with the width parameters.  The 1-D
        profile likelihood is then used to compute the best-fit value,
        upper limit, and TS for extension.  The nuisance parameters
        that will be simultaneously fit when performing the spatial
        scan can be controlled with the ``fix_shape``,
        ``free_background``, and ``free_radius`` options.  By default
        the position of the source will be fixed to its current
        position.  A simultaneous fit to position and extension can be
        performed by setting ``fit_position`` to True.

        Parameters
        ----------
        name : str
            Source name.

        {options}

        optimizer : dict
            Dictionary that overrides the default optimizer settings.

        Returns
        -------
        extension : dict
            Dictionary containing results of the extension analysis.  The same
            dictionary is also saved to the dictionary of this source under
            'extension'.
        """
        timer = Timer.create(start=True)
        name = self.roi.get_source_by_name(name).name

        schema = ConfigSchema(self.defaults['extension'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('prefix', '')
        schema.add_option('outfile', None, '', str)
        config = utils.create_dict(self.config['extension'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Running extension fit for %s', name)

        free_state = FreeParameterState(self)
        ext = self._extension(name, **config)
        free_state.restore()

        self.logger.info('Finished extension fit.')

        if config['make_plots']:
            self._plotter.make_extension_plots(ext,
                                               self.roi,
                                               prefix=config['prefix'])

        outfile = config.get('outfile', None)
        if outfile is None:
            outfile = utils.format_filename(
                self.workdir,
                'ext',
                prefix=[config['prefix'],
                        name.lower().replace(' ', '_')])
        else:
            outfile = os.path.join(self.workdir, os.path.splitext(outfile)[0])

        if config['write_fits']:
            self._make_extension_fits(ext, outfile + '.fits')

        if config['write_npy']:
            o_copy = dict(ext)
            self.logger.warning(
                'Saving maps in .npy files is disabled b/c of incompatibilities in python3, remove the maps from the %s.npy'
                % outfile)
            print(o_copy)
            for xrm in ['tsmap']:
                o_copy.pop(xrm)
            np.save(outfile + '.npy', o_copy)

        self.logger.info('Execution time: %.2f s', timer.elapsed_time)
        return ext
Esempio n. 4
0
    def extension(self, name, **kwargs):
        """Test this source for spatial extension with the likelihood
        ratio method (TS_ext).  This method will substitute an
        extended spatial model for the given source and perform a
        one-dimensional scan of the spatial extension parameter over
        the range specified with the width parameters.  The 1-D
        profile likelihood is then used to compute the best-fit value,
        upper limit, and TS for extension.  The nuisance parameters
        that will be simultaneously fit when performing the spatial
        scan can be controlled with the ``fix_shape``,
        ``free_background``, and ``free_radius`` options.  By default
        the position of the source will be fixed to its current
        position.  A simultaneous fit to position and extension can be
        performed by setting ``fit_position`` to True.

        Parameters
        ----------
        name : str
            Source name.

        {options}

        optimizer : dict
            Dictionary that overrides the default optimizer settings.

        Returns
        -------
        extension : dict
            Dictionary containing results of the extension analysis.  The same
            dictionary is also saved to the dictionary of this source under
            'extension'.
        """
        timer = Timer.create(start=True)
        name = self.roi.get_source_by_name(name).name

        schema = ConfigSchema(self.defaults['extension'],
                              optimizer=self.defaults['optimizer'])
        schema.add_option('prefix', '')
        schema.add_option('outfile', None, '', str)
        config = utils.create_dict(self.config['extension'],
                                   optimizer=self.config['optimizer'])
        config = schema.create_config(config, **kwargs)

        self.logger.info('Running extension fit for %s', name)

        free_state = FreeParameterState(self)
        ext = self._extension(name, **config)
        free_state.restore()

        self.logger.info('Finished extension fit.')

        if config['make_plots']:
            self._plotter.make_extension_plots(ext, self.roi,
                                               prefix=config['prefix'])

        outfile = config.get('outfile', None)
        if outfile is None:
            outfile = utils.format_filename(self.workdir, 'ext',
                                            prefix=[config['prefix'],
                                                    name.lower().replace(' ', '_')])
        else:
            outfile = os.path.join(self.workdir,
                                   os.path.splitext(outfile)[0])

        if config['write_fits']:
            self._make_extension_fits(ext, outfile + '.fits')

        if config['write_npy']:
            np.save(outfile + '.npy', dict(ext))

        self.logger.info('Execution time: %.2f s', timer.elapsed_time)
        return ext
Esempio n. 5
0
def _fit_lc(gta, name, **kwargs):

    # lightcurve fitting routine-
    # 1.) start by freeing target and provided list of
    # sources, fix all else- if fit fails, fix all pars
    # except norm and try again
    # 2.) if that fails to converge then try fixing low TS
    #  (<4) sources and then refit
    # 3.) if that fails to converge then try fixing low-moderate TS (<9) sources and then refit
    # 4.) if that fails then fix sources out to 1dg away from center of ROI
    # 5.) if that fails set values to 0 in output and print warning message

    free_sources = kwargs.get('free_sources', [])
    free_background = kwargs.get('free_background', False)
    free_params = kwargs.get('free_params', None)
    shape_ts_threshold = kwargs.get('shape_ts_threshold', 16)
    max_free_sources = kwargs.get('max_free_sources', 5)

    if name in free_sources:
        free_sources.remove(name)

    free_state = FreeParameterState(gta)
    gta.free_sources(free=False)
    gta.free_sources_by_name(free_sources + [name], pars='norm')
    gta.fit()

    free_sources = sorted(free_sources,
                          key=lambda t: gta.roi[t]['ts']
                          if np.isfinite(gta.roi[t]['ts']) else -100.,
                          reverse=True)
    free_sources = free_sources[:max_free_sources]

    free_sources_norm = free_sources + [name]
    free_sources_shape = []
    for t in free_sources_norm:
        if gta.roi[t]['ts'] > shape_ts_threshold:
            free_sources_shape += [t]

    gta.free_sources(free=False)

    gta.logger.debug('Free Sources Norm: %s', free_sources_norm)
    gta.logger.debug('Free Sources Shape: %s', free_sources_shape)

    for niter in range(5):

        if free_background:
            free_state.restore()

        if free_params:
            gta.free_source(name, pars=free_params)

        if niter == 0:
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources_by_name(free_sources_shape, pars='shape')
        elif niter == 1:
            gta.logger.info('Fit Failed. Retrying with free '
                            'normalizations.')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
        elif niter == 2:
            gta.logger.info('Fit Failed with User Supplied List of '
                            'Free/Fixed Sources.....Lets try '
                            'fixing TS<4 sources')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources(minmax_ts=[None, 4], free=False, exclude=[name])
        elif niter == 3:
            gta.logger.info('Fit Failed with User Supplied List of '
                            'Free/Fixed Sources.....Lets try '
                            'fixing TS<9 sources')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources(minmax_ts=[None, 9], free=False, exclude=[name])
        elif niter == 4:
            gta.logger.info('Fit still did not converge, lets try fixing the '
                            'sources up to 1dg out from ROI')
            gta.free_sources_by_name(free_sources, False)
            for s in free_sources:
                src = gta.roi.get_source_by_name(s)
                if src['offset'] < 1.0:
                    gta.free_source(s, pars='norm')
            gta.free_sources(minmax_ts=[None, 9], free=False, exclude=[name])
        else:
            gta.logger.error('Fit still didnt converge.....please examine this data '
                             'point, setting output to 0')
            break

        fit_results = gta.fit()

        if fit_results['fit_success'] is True:
            break

    return fit_results
Esempio n. 6
0
    def _fit_lc(self, gta, name, **kwargs):

        # lightcurve fitting routine-
        # 1.) start by freeing target and provided list of
        # sources, fix all else- if fit fails, fix all pars
        # except norm and try again
        # 2.) if that fails to converge then try fixing low TS
        #  (<4) sources and then refit
        # 3.) if that fails to converge then try fixing low-moderate TS (<9) sources and then refit
        # 4.) if that fails then fix sources out to 1dg away from center of ROI
        # 5.) if that fails set values to 0 in output and print warning message

        free_sources = kwargs.get('free_sources', [])
        free_background = kwargs.get('free_background', False)
        free_params = kwargs.get('free_params', None)

        if name in free_sources:
            free_sources.remove(name)

        free_state = FreeParameterState(gta)
        gta.free_sources(free=False)

        for niter in range(5):

            if free_background:
                free_state.restore()
            gta.free_source(name, pars=free_params)

            if niter == 0:
                gta.free_sources_by_name(free_sources)
            elif niter == 1:
                self.logger.info('Fit Failed. Retrying with free '
                                 'normalizations.')
                gta.free_sources_by_name(free_sources, False)
                gta.free_sources_by_name(free_sources, pars='norm')
            elif niter == 2:
                self.logger.info('Fit Failed with User Supplied List of '
                                 'Free/Fixed Sources.....Lets try '
                                 'fixing TS<4 sources')
                gta.free_sources_by_name(free_sources, False)
                gta.free_sources_by_name(free_sources, pars='norm')
                gta.free_sources(minmax_ts=[0, 4], free=False, exclude=[name])
            elif niter == 3:
                self.logger.info('Fit Failed with User Supplied List of '
                                 'Free/Fixed Sources.....Lets try '
                                 'fixing TS<9 sources')
                gta.free_sources_by_name(free_sources, False)
                gta.free_sources_by_name(free_sources, pars='norm')
                gta.free_sources(minmax_ts=[0, 9], free=False, exclude=[name])
            elif niter == 4:
                self.logger.info('Fit still did not converge, lets try fixing the '
                                 'sources up to 1dg out from ROI')
                gta.free_sources_by_name(free_sources, False)
                for s in free_sources:
                    src = self.roi.get_source_by_name(s)
                    if src['offset'] < 1.0:
                        gta.free_source(s, pars='norm')
                gta.free_sources(minmax_ts=[0, 9], free=False, exclude=[name])
            else:
                self.logger.error('Fit still didnt converge.....please examine this data '
                                  'point, setting output to 0')
                break

            fit_results = gta.fit()
            if fit_results['fit_success'] is True:
                break

        return fit_results
Esempio n. 7
0
def _fit_lc(gta, name, **kwargs):

    # lightcurve fitting routine-
    # 1.) start by freeing target and provided list of
    # sources, fix all else- if fit fails, fix all pars
    # except norm and try again
    # 2.) if that fails to converge then try fixing low TS
    #  (<4) sources and then refit
    # 3.) if that fails to converge then try fixing low-moderate TS (<9) sources and then refit
    # 4.) if that fails then fix sources out to 1dg away from center of ROI
    # 5.) if that fails set values to 0 in output and print warning message

    free_sources = kwargs.get('free_sources', [])
    free_background = kwargs.get('free_background', False)
    free_params = kwargs.get('free_params', None)
    shape_ts_threshold = kwargs.get('shape_ts_threshold', 16)
    max_free_sources = kwargs.get('max_free_sources', 5)

    if name in free_sources:
        free_sources.remove(name)

    free_state = FreeParameterState(gta)
    gta.free_sources(free=False)
    gta.free_sources_by_name(free_sources + [name], pars='norm')
    gta.fit()

    free_sources = sorted(free_sources,
                          key=lambda t: gta.roi[t]['ts']
                          if np.isfinite(gta.roi[t]['ts']) else -100.,
                          reverse=True)
    free_sources = free_sources[:max_free_sources]

    free_sources_norm = free_sources + [name]
    free_sources_shape = []
    for t in free_sources_norm:
        if gta.roi[t]['ts'] > shape_ts_threshold:
            free_sources_shape += [t]

    gta.free_sources(free=False)

    gta.logger.debug('Free Sources Norm: %s', free_sources_norm)
    gta.logger.debug('Free Sources Shape: %s', free_sources_shape)

    for niter in range(5):

        if free_background:
            free_state.restore()

        if free_params:
            gta.free_source(name, pars=free_params)

        if niter == 0:
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources_by_name(free_sources_shape, pars='shape')
        elif niter == 1:
            gta.logger.info('Fit Failed. Retrying with free '
                            'normalizations.')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
        elif niter == 2:
            gta.logger.info('Fit Failed with User Supplied List of '
                            'Free/Fixed Sources.....Lets try '
                            'fixing TS<4 sources')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources(minmax_ts=[None, 4], free=False, exclude=[name])
        elif niter == 3:
            gta.logger.info('Fit Failed with User Supplied List of '
                            'Free/Fixed Sources.....Lets try '
                            'fixing TS<9 sources')
            gta.free_sources_by_name(free_sources, False)
            gta.free_sources_by_name(free_sources_norm, pars='norm')
            gta.free_sources(minmax_ts=[None, 9], free=False, exclude=[name])
        elif niter == 4:
            gta.logger.info('Fit still did not converge, lets try fixing the '
                            'sources up to 1dg out from ROI')
            gta.free_sources_by_name(free_sources, False)
            for s in free_sources:
                src = gta.roi.get_source_by_name(s)
                if src['offset'] < 1.0:
                    gta.free_source(s, pars='norm')
            gta.free_sources(minmax_ts=[None, 9], free=False, exclude=[name])
        else:
            gta.logger.error('Fit still didnt converge.....please examine this data '
                             'point, setting output to 0')
            break

        fit_results = gta.fit()

        if fit_results['fit_success'] is True:
            break

    return fit_results