예제 #1
0
 def postprocess(self, kwargs=dict()):
     # force nfreq=1
     self._nfreq = 1
     self._background_obj._nfreq = 1
     # assemble fiducial+abs
     abs_bp = self._absrslt.reshape(self._noise_nsamp, self._ntarget,
                                    self._estimator.nmode, 1, 1)
     abs_fid = self._fiducial_bp[:, :, :, 0,
                                 0].reshape(self._fiducial_nsamp,
                                            self._ntarget,
                                            self._estimator.nmode, 1, 1)
     # empirical cov
     xbp = gvec(abs_bp)
     xfid = gvec(abs_fid)
     self.covmat = 2. * empcov(xbp) + empcov(xfid)
     # null noise
     null_noise = np.zeros((self._ntarget, self._estimator.nmode, 1, 1),
                           dtype=np.float64)
     if (self._likelihood == 'gauss'):
         self._engine = gaussfit(np.mean(abs_bp, axis=0),
                                 np.mean(abs_fid,
                                         axis=0), null_noise, self._covmat,
                                 self._background_obj, None, self._solver)
     elif (self._likelihood == 'hl'):
         self._engine = hlfit(np.mean(abs_bp, axis=0),
                              np.mean(abs_fid,
                                      axis=0), null_noise, self._covmat,
                              self._background_obj, None, self._solver)
     if (len(self._paramrange)):
         self._engine.rerange(self._paramrange)
     rslt = self._engine.run(kwargs)
     self._paramlist = sorted(self._engine.activelist)
     return rslt
예제 #2
0
 def lsq(self, predicted):
     assert (predicted.shape == self._data.shape)
     diff = gvec(predicted + self._noise - self._data)
     if (np.isnan(diff).any()):
         raise ValueError('encounter nan')
     chi = np.vdot(diff,
                   np.linalg.lstsq(self._covariance, diff, rcond=None)[0])
     if np.isnan(chi):
         return np.nan_to_num(np.inf)
     return chi
예제 #3
0
파일: pipeline.py 프로젝트: afra-dev/afra
    def preprocess(self, aposcale, psbin, lmin=None, lmax=None):
        """
        preprocess routine, converts maps into band-powers.

        Parameters
        ----------

        aposcale : float
            Apodization scale in deg.

        psbin : integer
            Number of bins,
            for conducting pseudo-PS estimation.

        lmin/lmax : integer
            Lower/Upper multipole limit.
        """
        assert isinstance(aposcale, float)
        assert isinstance(psbin, int)
        assert (psbin > 0)
        assert (aposcale > 0)
        # STEP I
        # init PS estimator
        self.estimator = pstimator(nside=self._nside,
                                   mask=self._mask,
                                   aposcale=aposcale,
                                   psbin=psbin,
                                   lmin=lmin,
                                   lmax=lmax,
                                   targets=self._targets,
                                   filt=self._filt)
        # STEP II
        # template PS estimations (auto corr. only)
        if self._template_flag:
            self.template_bp = dict()
            for i in range(self._template_nfreq):
                # allocate for template
                data_bp = np.zeros((self._ntarget, self._estimator.nmode),
                                   dtype=np.float64)
                noise_bp = np.zeros(
                    (self._noise_nsamp, self._ntarget, self._estimator.nmode),
                    dtype=np.float64)
                _fi = self._template_freqlist[i]
                # template workspace
                twsp = self._estimator.autoWSP(self._templates[_fi],
                                               beams=self._template_beams[_fi])
                # template auto-corr.
                stmp = self._estimator.autoBP(self._templates[_fi],
                                              wsp=twsp,
                                              beams=self._template_beams[_fi])
                data_bp = np.array(stmp[1:1 + self._ntarget])
                # template noise auto corr.
                for s in range(self._template_nsamp):
                    ntmp = self._estimator.autoBP(
                        self._template_noises[_fi][s],
                        wsp=twsp,
                        beams=self._template_beams[_fi])
                    noise_bp[s] = np.array(ntmp[1:1 + self._ntarget])
                # mean noise subtraction
                self._template_bp[_fi] = data_bp - np.mean(noise_bp, axis=0)
        # STEP III
        # prepare model, parameter list generated during init models
        if self._background is not None:
            self._background_obj = self._background(self._freqlist,
                                                    self._estimator)
        if self._foreground is not None:
            self._foreground_obj = self._foreground(self._freqlist,
                                                    self._estimator,
                                                    self._template_bp)
        # STEP IV-A
        # data PS estimations (with workspace)
        # allocate
        wsp_dict = dict()  # data nmt workspace
        fwsp_dict = dict()  # fiducial nmt workspace
        self.data_bp = np.zeros(
            (self._ntarget, self._estimator.nmode, self._nfreq, self._nfreq),
            dtype=np.float64)
        for i in range(self._nfreq):
            _fi = self._freqlist[i]
            # auto corr.
            wsp_dict[(i, i)] = self._estimator.autoWSP(self._data[_fi],
                                                       beams=self._beams[_fi])
            stmp = self._estimator.autoBP(self._data[_fi],
                                          wsp=wsp_dict[(i, i)],
                                          beams=self._beams[_fi])
            self._data_bp[:, :, i, i] = np.array(stmp[1:1 + self._ntarget])
            for j in range(i + 1, self._nfreq):
                _fj = self._freqlist[j]
                # cross corr.
                wsp_dict[(i, j)] = self._estimator.crosWSP(
                    np.r_[self._data[_fi], self._data[_fj]],
                    beams=[self._beams[_fi], self._beams[_fj]])
                stmp = self._estimator.crosBP(
                    np.r_[self._data[_fi], self._data[_fj]],
                    wsp=wsp_dict[(i, j)],
                    beams=[self._beams[_fi], self._beams[_fj]])
                self._data_bp[:, :, i, j] = np.array(stmp[1:1 + self._ntarget])
                self._data_bp[:, :, j, i] = np.array(stmp[1:1 + self._ntarget])
        # STEP IV-B
        # fiducial PS estimation
        if self._fiducial_flag:
            # allocate
            self.fiducial_bp = np.zeros(
                (self._fiducial_nsamp, self._ntarget, self._estimator.nmode,
                 self._nfreq, self._nfreq),
                dtype=np.float64)
            for i in range(self._nfreq):
                _fi = self._freqlist[i]
                # auto corr.
                fwsp_dict[(i, i)] = self._estimator.autoWSP(
                    self._fiducials[_fi][0], beams=self._fiducial_beams[_fi])
                for s in range(self._fiducial_nsamp):
                    # auto corr.
                    ftmp = self._estimator.autoBP(
                        self._fiducials[_fi][s],
                        wsp=fwsp_dict[(i, i)],
                        beams=self._fiducial_beams[_fi])
                    self._fiducial_bp[s, :, :, i,
                                      i] = np.array(ftmp[1:1 + self._ntarget])
                for j in range(i + 1, self._nfreq):
                    _fj = self._freqlist[j]
                    fwsp_dict[(i, j)] = self._estimator.crosWSP(
                        np.r_[self._fiducials[_fi][0],
                              self._fiducials[_fj][0]],
                        beams=[
                            self._fiducial_beams[_fi],
                            self._fiducial_beams[_fj]
                        ])
                    for s in range(self._fiducial_nsamp):
                        # cross corr.
                        ftmp = self._estimator.crosBP(
                            np.r_[self._fiducials[_fi][s],
                                  self._fiducials[_fj][s]],
                            wsp=fwsp_dict[(i, j)],
                            beams=[
                                self._fiducial_beams[_fi],
                                self._fiducial_beams[_fj]
                            ])
                        self._fiducial_bp[s, :, :, i, j] = np.array(
                            ftmp[1:1 + self._ntarget])
                        self._fiducial_bp[s, :, :, j, i] = np.array(
                            ftmp[1:1 + self._ntarget])
        # STEP IV-C
        # noise PS estimations
        if self._noise_flag:
            # allocate
            self.noise_bp = np.zeros(
                (self._noise_nsamp, self._ntarget, self._estimator.nmode,
                 self._nfreq, self._nfreq),
                dtype=np.float64)
            for s in range(self._noise_nsamp):
                for i in range(self._nfreq):
                    _fi = self._freqlist[i]
                    # auto corr.
                    ntmp = self._estimator.autoBP(self._noises[_fi][s],
                                                  wsp=wsp_dict[(i, i)],
                                                  beams=self._beams[_fi])
                    self._noise_bp[s, :, :, i,
                                   i] = np.array(ntmp[1:1 + self._ntarget])
                    for j in range(i + 1, self._nfreq):
                        _fj = self._freqlist[j]
                        # cross corr.
                        ntmp = self._estimator.crosBP(
                            np.r_[self._noises[_fi][s], self._noises[_fj][s]],
                            wsp=wsp_dict[(i, j)],
                            beams=[self._beams[_fi], self._beams[_fj]])
                        self._noise_bp[s, :, :, i,
                                       j] = np.array(ntmp[1:1 + self._ntarget])
                        self._noise_bp[s, :, :, j,
                                       i] = np.array(ntmp[1:1 + self._ntarget])
        # STEP V
        # fiducial+noise PS covariance matrix
        # fiducial+noise has to be processed in the pixel doamin, in order to yield a proper cov matrix
        if self._fiducial_flag and self._noise_flag:
            ncom = min(self._noise_nsamp, self._fiducial_nsamp)
            # allocate
            nfid = np.zeros((ncom, self._ntarget, self._estimator.nmode,
                             self._nfreq, self._nfreq),
                            dtype=np.float64)
            for s in range(ncom):
                for i in range(self._nfreq):
                    _fi = self._freqlist[i]
                    # auto corr.
                    ntmp = self._estimator.autoBP(
                        self._fiducials[_fi][s] + self._noises[_fi][s],
                        wsp=fwsp_dict[(i, i)],
                        beams=self._fiducial_beams[_fi])
                    nfid[s, :, :, i, i] = np.array(ntmp[1:1 + self._ntarget])
                    for j in range(i + 1, self._nfreq):
                        _fj = self._freqlist[j]
                        # cross corr.
                        ntmp = self._estimator.crosBP(
                            np.r_[self._fiducials[_fi][s] +
                                  self._noises[_fi][s],
                                  self._fiducials[_fj][s] +
                                  self._noises[_fj][s]],
                            wsp=fwsp_dict[(i, j)],
                            beams=[
                                self._fiducial_beams[_fi],
                                self._fiducial_beams[_fj]
                            ])
                        nfid[s, :, :, i,
                             j] = np.array(ntmp[1:1 + self._ntarget])
                        nfid[s, :, :, j,
                             i] = np.array(ntmp[1:1 + self._ntarget])
            # full cov
            self.covmat = empcov(gvec(nfid))