def _fit_extension(self, name, **kwargs): spatial_model = kwargs.get('spatial_model', 'RadialGaussian') optimizer = kwargs.get('optimizer', {}) width_min = kwargs.get('width_min', 10**-2.0) width_max = kwargs.get('width_max', 10**0.5) width_nstep = kwargs.get('width_nstep', 21) fit_position = kwargs.get('fit_position', False) skydir = kwargs.get('skydir', self.roi[name].skydir) psf_scale_fn = kwargs.get('psf_scale_fn', None) reoptimize = kwargs.get('reoptimize', True) src = self.roi.copy_source(name) # If the source is extended split the likelihood scan into two # parts centered on the best-fit value -- this ensures better # fit stability if (src['SpatialModel'] in ['RadialGaussian', 'RadialDisk'] and src['SpatialWidth'] > width_min): width_lo = np.logspace(np.log10(width_min), np.log10(src['SpatialWidth']), width_nstep // 2 + (width_nstep % 2 > 0)) width_hi = np.logspace(np.log10(src['SpatialWidth']), np.log10(width_max), width_nstep // 2 + 1) loglike_lo = self._scan_extension(name, spatial_model=spatial_model, width=width_lo[::-1], optimizer=optimizer, skydir=skydir, psf_scale_fn=psf_scale_fn, reoptimize=reoptimize)[::-1] loglike_hi = self._scan_extension(name, spatial_model=spatial_model, width=width_hi, optimizer=optimizer, skydir=skydir, psf_scale_fn=psf_scale_fn, reoptimize=reoptimize) width = np.concatenate((width_lo, width_hi[1:])) loglike = np.concatenate((loglike_lo, loglike_hi[1:])) else: width = np.logspace(np.log10(width_min), np.log10(width_max), width_nstep) width = np.concatenate(([0.0], width)) loglike = self._scan_extension(name, spatial_model=spatial_model, width=width, optimizer=optimizer, skydir=skydir, psf_scale_fn=psf_scale_fn, reoptimize=reoptimize) ul_data = utils.get_parameter_limits(width, loglike, bounds=[10**-3.0, width_max]) if not np.isfinite(ul_data['err']): ul_data['x0'] = width[np.argmax(loglike)] ul_data['err'] = ul_data['x0'] ul_data['err_lo'] = ul_data['x0'] ul_data['err_hi'] = ul_data['x0'] imax = np.argmax(loglike) err = max(10**-2.0, ul_data['err']) lolim = max(min(ul_data['x0'], width[imax]) - 2.0 * err, 0) if np.isfinite(ul_data['ul']): hilim = 1.5 * ul_data['ul'] else: hilim = ul_data['x0'] + 2.0 * err nstep = max(width_nstep, int((hilim - lolim) / err)) width2 = np.linspace(lolim, hilim, nstep) loglike2 = self._scan_extension(name, spatial_model=spatial_model, width=width2, optimizer=optimizer, skydir=skydir, psf_scale_fn=psf_scale_fn, reoptimize=reoptimize) ul_data2 = utils.get_parameter_limits(width2, loglike2, bounds=[10**-3.0, width_max]) self.logger.debug('width: %s', width) self.logger.debug('loglike: %s', loglike - np.max(loglike)) self.logger.debug('ul_data:\n %s', pprint.pformat(ul_data)) self.logger.debug('width2: %s', width2) self.logger.debug('loglike2: %s', loglike2 - np.max(loglike2)) self.logger.debug('ul_data2:\n %s', pprint.pformat(ul_data2)) return MutableNamedTuple(ext=max(ul_data2['x0'], 10**-2.5), ext_ul95=ul_data2['ul'], ext_err_lo=ul_data2['err_lo'], ext_err_hi=ul_data2['err_hi'], ext_err=ul_data2['err'], loglike_ext=ul_data2['lnlmax'], ra=skydir.ra.deg, dec=skydir.dec.deg, glon=skydir.galactic.l.deg, glat=skydir.galactic.b.deg, pos_offset=0.0)
def _fit_extension_full(self, name, **kwargs): skydir = self.roi[name].skydir.copy() src = self.roi.copy_source(name) spatial_model = kwargs.get('spatial_model', 'RadialGaussian') loglike = -self.like() o = MutableNamedTuple(ext=np.nan, ext_ul95=np.nan, ext_err_lo=np.nan, ext_err_hi=np.nan, ext_err=np.nan, loglike_ext=np.nan, ra=np.nan, dec=np.nan, glon=np.nan, glat=np.nan, ra_err=np.nan, dec_err=np.nan, glon_err=np.nan, glat_err=np.nan, pos_err=np.nan, pos_r68=np.nan, pos_r95=np.nan, pos_r99=np.nan, pos_err_semimajor=np.nan, pos_err_semiminor=np.nan, pos_angle=np.nan, pos_offset=np.nan) t0 = Timer() t1 = Timer() nstep = 7 for i in range(4): t0.start() fit_ext = self._fit_extension(name, skydir=skydir, **kwargs) o.update(fit_ext) self.set_source_morphology( name, spatial_model=spatial_model, spatial_pars={'SpatialWidth': max(o.ext, 0.00316)}, use_pylike=False) t0.stop() t1.start() if i == 0 or not np.isfinite(o.pos_r99): dtheta_max = max(0.5, 1.5 * o.ext) else: dtheta_max = max(0.5, o.pos_r99) fit_pos0, fit_pos1 = self._fit_position(name, nstep=nstep, dtheta_max=dtheta_max, zmin=-3.0, use_pylike=False) o.update(fit_pos0) t1.stop() self.set_source_morphology(name, spatial_model=spatial_model, spatial_pars={ 'RA': o['ra'], 'DEC': o['dec'] }, use_pylike=False) self.logger.debug('Elapsed Time: %.2f %.2f', t0.elapsed_time, t1.elapsed_time) fit_output = self._fit(loglevel=logging.DEBUG, **kwargs['optimizer']) skydir = fit_pos0['skydir'] o.pos_offset = skydir.separation(src.skydir).deg o.loglike_ext = -self.like() dloglike = o.loglike_ext - loglike self.logger.info( 'Iter %i R68 = %8.3f Offset = %8.3f ' 'LogLikelihood = %10.2f Delta-LogLikelihood = %8.2f', i, o.ext, o.pos_offset, o.loglike_ext, dloglike) if i > 0 and dloglike < 0.1: break loglike = o.loglike_ext self.set_source_morphology(name, spatial_pars=src.spatial_pars, use_pylike=False) return o
def _fit_extension_full(self, name, **kwargs): skydir = self.roi[name].skydir.copy() src = self.roi.copy_source(name) spatial_model = kwargs.get('spatial_model', 'RadialGaussian') loglike = -self.like() o = MutableNamedTuple( ext=np.nan, ext_ul95=np.nan, ext_err_lo=np.nan, ext_err_hi=np.nan, ext_err=np.nan, loglike_ext=np.nan, ra=np.nan, dec=np.nan, glon=np.nan, glat=np.nan, ra_err=np.nan, dec_err=np.nan, glon_err=np.nan, glat_err=np.nan, pos_err=np.nan, pos_r68=np.nan, pos_r95=np.nan, pos_r99=np.nan, pos_err_semimajor=np.nan, pos_err_semiminor=np.nan, pos_angle=np.nan, pos_offset=np.nan) t0 = Timer() t1 = Timer() nstep = 7 for i in range(4): t0.start() fit_ext = self._fit_extension(name, skydir=skydir, **kwargs) o.update(fit_ext) self.set_source_morphology(name, spatial_model=spatial_model, spatial_pars={ 'SpatialWidth': max(o.ext, 0.00316)}, use_pylike=False) t0.stop() t1.start() if i == 0 or not np.isfinite(o.pos_r99): dtheta_max = max(0.5, 1.5 * o.ext) else: dtheta_max = max(0.5, o.pos_r99) fit_pos0, fit_pos1 = self._fit_position(name, nstep=nstep, dtheta_max=dtheta_max, zmin=-3.0, use_pylike=False) o.update(fit_pos0) t1.stop() self.set_source_morphology(name, spatial_model=spatial_model, spatial_pars={'RA': o['ra'], 'DEC': o['dec']}, use_pylike=False) self.logger.debug('Elapsed Time: %.2f %.2f', t0.elapsed_time, t1.elapsed_time) fit_output = self._fit( loglevel=logging.DEBUG, **kwargs['optimizer']) skydir = fit_pos0['skydir'] o.pos_offset = skydir.separation(src.skydir).deg o.loglike_ext = -self.like() dloglike = o.loglike_ext - loglike self.logger.info('Iter %i R68 = %8.3f Offset = %8.3f ' 'LogLikelihood = %10.2f Delta-LogLikelihood = %8.2f', i, o.ext, o.pos_offset, o.loglike_ext, dloglike) if i > 0 and dloglike < 0.1: break loglike = o.loglike_ext self.set_source_morphology(name, spatial_pars=src.spatial_pars, use_pylike=False) return o