Esempio n. 1
0
    def _scan_extension_fast(self, name, **kwargs):

        state = SourceMapState(self.like, [name])

        self.free_norm(name)

        optimizer = kwargs.get('optimizer', {})
        width = kwargs.get('width')
        spatial_model = kwargs.get('spatial_model')
        skydir = kwargs.pop('skydir', self.roi[name].skydir)
        psf_scale_fn = kwargs.pop('psf_scale_fn', None)
        reoptimize = kwargs.pop('reoptimize', True)

        src = self.roi.copy_source(name)
        spatial_pars = {'ra': skydir.ra.deg, 'dec': skydir.dec.deg}

        loglike = []
        for i, w in enumerate(width):

            spatial_pars['SpatialWidth'] = max(w, 0.00316)
            self.set_source_morphology(name,
                                       spatial_model=spatial_model,
                                       spatial_pars=spatial_pars,
                                       use_pylike=False,
                                       psf_scale_fn=psf_scale_fn)
            if reoptimize:
                fit_output = self._fit(loglevel=logging.DEBUG, **optimizer)
                loglike += [fit_output['loglike']]
            else:
                loglike += [-self.like()]

        state.restore()

        return np.array(loglike)
Esempio n. 2
0
    def _scan_extension_fast(self, name, **kwargs):

        state = SourceMapState(self.like, [name])

        self.free_norm(name)

        optimizer = kwargs.get('optimizer', {})
        width = kwargs.get('width')
        spatial_model = kwargs.get('spatial_model')
        skydir = kwargs.pop('skydir', self.roi[name].skydir)
        psf_scale_fn = kwargs.pop('psf_scale_fn', None)
        reoptimize = kwargs.pop('reoptimize', True)

        src = self.roi.copy_source(name)
        spatial_pars = {'ra': skydir.ra.deg, 'dec': skydir.dec.deg}

        loglike = []
        for i, w in enumerate(width):

            spatial_pars['SpatialWidth'] = max(w, 0.00316)
            self.set_source_morphology(name,
                                       spatial_model=spatial_model,
                                       spatial_pars=spatial_pars,
                                       use_pylike=False,
                                       psf_scale_fn=psf_scale_fn)
            if reoptimize:
                fit_output = self._fit(loglevel=logging.DEBUG, **optimizer)
                loglike += [fit_output['loglike']]
            else:
                loglike += [-self.like()]

        state.restore()

        return np.array(loglike)
Esempio n. 3
0
    def _scan_extension_fast_ebin(self, name, **kwargs):

        state = SourceMapState(self.like, [name])

        self.free_norm(name)
        optimizer = kwargs.get('optimizer', {})
        width = kwargs.get('width')
        spatial_model = kwargs.get('spatial_model')
        skydir = kwargs.pop('skydir', self.roi[name].skydir)
        psf_scale_fn = kwargs.pop('psf_scale_fn', None)
        reoptimize = kwargs.pop('reoptimize', True)

        src = self.roi.copy_source(name)
        spatial_pars = {'ra': skydir.ra.deg, 'dec': skydir.dec.deg}

        loglike = np.ones((self.enumbins, len(width)))
        for i, w in enumerate(width):

            spatial_pars['SpatialWidth'] = max(w, 0.00316)
            self.set_source_morphology(name,
                                       spatial_model=spatial_model,
                                       spatial_pars=spatial_pars,
                                       use_pylike=False,
                                       psf_scale_fn=psf_scale_fn)

            for j, (logemin, logemax) in enumerate(
                    zip(self.log_energies[:-1], self.log_energies[1:])):
                self.set_energy_range(logemin, logemax)
                if reoptimize:
                    fit_output = self._fit(loglevel=logging.DEBUG, **optimizer)
                    loglike[j, i] = fit_output['loglike']
                else:
                    loglike[j, i] = -self.like()
            self.set_energy_range(self.log_energies[0], self.log_energies[-1])

        state.restore()
        return loglike
Esempio n. 4
0
    def _scan_extension_fast_ebin(self, name, **kwargs):

        state = SourceMapState(self.like, [name])

        self.free_norm(name)
        optimizer = kwargs.get('optimizer', {})
        width = kwargs.get('width')
        spatial_model = kwargs.get('spatial_model')
        skydir = kwargs.pop('skydir', self.roi[name].skydir)
        psf_scale_fn = kwargs.pop('psf_scale_fn', None)
        reoptimize = kwargs.pop('reoptimize', True)

        src = self.roi.copy_source(name)
        spatial_pars = {'ra': skydir.ra.deg, 'dec': skydir.dec.deg}

        loglike = np.ones((self.enumbins, len(width)))
        for i, w in enumerate(width):

            spatial_pars['SpatialWidth'] = max(w, 0.00316)
            self.set_source_morphology(name,
                                       spatial_model=spatial_model,
                                       spatial_pars=spatial_pars,
                                       use_pylike=False,
                                       psf_scale_fn=psf_scale_fn)

            for j, (logemin, logemax) in enumerate(zip(self.log_energies[:-1],
                                                       self.log_energies[1:])):
                self.set_energy_range(logemin, logemax)
                if reoptimize:
                    fit_output = self._fit(loglevel=logging.DEBUG, **optimizer)
                    loglike[j, i] = fit_output['loglike']
                else:
                    loglike[j, i] = -self.like()
            self.set_energy_range(self.log_energies[0], self.log_energies[-1])

        state.restore()
        return loglike
Esempio n. 5
0
    def _fit_position_opt(self, name, use_cache=True):

        state = SourceMapState(self.like, [name])

        src = self.roi.copy_source(name)

        if use_cache:
            self._create_srcmap_cache(src.name, src)

        loglike = []
        skydir = src.skydir
        skywcs = self.geom.wcs
        src_pix = skydir.to_pixel(skywcs)

        c = skydir.transform_to('icrs')
        src.set_radec(c.ra.deg, c.dec.deg)
        self._update_srcmap(src.name, src)

        print(src_pix, self.like())

        import time

        def fit_fn(params):

            t0 = time.time()

            c = SkyCoord.from_pixel(params[0], params[1], self.geom.wcs)
            c = c.transform_to('icrs')
            src.set_radec(c.ra.deg, c.dec.deg)

            t1 = time.time()

            self._update_srcmap(src.name, src)

            t2 = time.time()

            val = self.like()

            t3 = time.time()

            print(params, val)
            # print(t1-t0,t2-t1,t3-t2)

            return val

        #lnl0 = fit_fn(src_pix[0],src_pix[1])
        #lnl1 = fit_fn(src_pix[0]+0.1,src_pix[1])
        # print(lnl0,lnl1)

        import scipy

        #src_pix[1] += 3.0
        p0 = [src_pix[0], src_pix[1]]

        #p0 = np.array([14.665692574327048, 16.004594098101926])
        #delta = np.array([0.3,-0.4])
        #p0 = [14.665692574327048, 16.004594098101926]

        o = scipy.optimize.minimize(fit_fn, p0,
                                    bounds=[(0.0, 39.0),
                                            (0.0, 39.0)],
                                    # method='L-BFGS-B',
                                    method='SLSQP',
                                    tol=1e-6)

        print('fit 2')

        o = scipy.optimize.minimize(fit_fn, o.x,
                                    bounds=[(0.0, 39.0),
                                            (0.0, 39.0)],
                                    # method='L-BFGS-B',
                                    method='SLSQP',
                                    tol=1e-6)
        print(o)

        print(fit_fn(p0))
        print(fit_fn(o.x))
        print(fit_fn(o.x + np.array([0.02, 0.02])))
        print(fit_fn(o.x + np.array([0.02, -0.02])))
        print(fit_fn(o.x + np.array([-0.02, 0.02])))
        print(fit_fn(o.x + np.array([-0.02, -0.02])))

        state.restore()

        return o
Esempio n. 6
0
    def _fit_position_opt(self, name, use_cache=True):

        state = SourceMapState(self.like, [name])

        src = self.roi.copy_source(name)

        if use_cache:
            self._create_srcmap_cache(src.name, src)

        loglike = []
        skydir = src.skydir
        skywcs = self._skywcs
        src_pix = skydir.to_pixel(skywcs)

        c = skydir.transform_to('icrs')
        src.set_radec(c.ra.deg, c.dec.deg)
        self._update_srcmap(src.name, src)

        print(src_pix, self.like())

        import time

        def fit_fn(params):

            t0 = time.time()

            c = SkyCoord.from_pixel(params[0], params[1], self._skywcs)
            c = c.transform_to('icrs')
            src.set_radec(c.ra.deg, c.dec.deg)

            t1 = time.time()

            self._update_srcmap(src.name, src)

            t2 = time.time()

            val = self.like()

            t3 = time.time()

            print(params, val)
            # print(t1-t0,t2-t1,t3-t2)

            return val

        #lnl0 = fit_fn(src_pix[0],src_pix[1])
        #lnl1 = fit_fn(src_pix[0]+0.1,src_pix[1])
        # print(lnl0,lnl1)

        import scipy

        #src_pix[1] += 3.0
        p0 = [src_pix[0], src_pix[1]]

        #p0 = np.array([14.665692574327048, 16.004594098101926])
        #delta = np.array([0.3,-0.4])
        #p0 = [14.665692574327048, 16.004594098101926]

        o = scipy.optimize.minimize(fit_fn, p0,
                                    bounds=[(0.0, 39.0),
                                            (0.0, 39.0)],
                                    # method='L-BFGS-B',
                                    method='SLSQP',
                                    tol=1e-6)

        print ('fit 2')

        o = scipy.optimize.minimize(fit_fn, o.x,
                                    bounds=[(0.0, 39.0),
                                            (0.0, 39.0)],
                                    # method='L-BFGS-B',
                                    method='SLSQP',
                                    tol=1e-6)
        print(o)

        print(fit_fn(p0))
        print(fit_fn(o.x))
        print(fit_fn(o.x + np.array([0.02, 0.02])))
        print(fit_fn(o.x + np.array([0.02, -0.02])))
        print(fit_fn(o.x + np.array([-0.02, 0.02])))
        print(fit_fn(o.x + np.array([-0.02, -0.02])))

        state.restore()

        return o