Exemplo n.º 1
0
 def _update_mask(self):
     # Min and Max for new mask
     xstart = self.rect['world'][0]
     xend = self.rect['world'][2]
     if xstart <= xend: newlist=[xstart,xend]
     else: newlist = [xend,xstart]
     # Mask or unmask
     invmask = None
     if self.rect['button'] == 1:
         invmask = False
         mflg = 'Mask'
     elif self.rect['button'] == 3:
         invmask = True
         mflg = 'UNmask'
     asaplog.push(mflg+': '+str(newlist))
     asaplog.post()
     newmask = self.scan.create_mask(newlist,invert=invmask)
     # Logic operation to update mask
     if invmask:
         self.mask = mask_and(self.mask,newmask)
     else:
         self.mask = mask_or(self.mask,newmask)
     # Plot masked regions
     #if self.showmask or not self.once: self._plot_mask()
     if self.showmask: self._plot_mask()
Exemplo n.º 2
0
    def auto_fit(self, insitu=None, plot=False):
        """
        Return a scan where the function is applied to all rows for
        all Beams/IFs/Pols.

        """
        from asap import scantable

        if not isinstance(self.data, scantable):
            msg = "Data is not a scantable"
            raise TypeError(msg)
        if insitu is None:
            insitu = rcParams["insitu"]
        if not insitu:
            scan = self.data.copy()
        else:
            scan = self.data
        rows = xrange(scan.nrow())
        # Save parameters of baseline fits as a class attribute.
        # NOTICE: This does not reflect changes in scantable!
        if len(rows) > 0:
            self.blpars = []
        asaplog.push("Fitting:")
        for r in rows:
            out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (
                scan.getscan(r),
                scan.getbeam(r),
                scan.getif(r),
                scan.getpol(r),
                scan.getcycle(r),
            )
            asaplog.push(out, False)
            self.x = scan._getabcissa(r)
            self.y = scan._getspectrum(r)
            # self.mask = mask_and(self.mask, scan._getmask(r))
            if len(self.x) == len(self.mask):
                self.mask = mask_and(self.mask, self.data._getmask(row))
            else:
                asaplog.push("lengths of data and mask are not the same. " "preset mask will be ignored")
                asaplog.post("WARN", "asapfit.fit")
                self.mask = self.data._getmask(row)
            self.data = None
            self.fit()
            x = self.get_parameters()
            fpar = self.get_parameters()
            if plot:
                self.plot(residual=True)
                x = raw_input("Accept fit ([y]/n): ")
                if x.upper() == "N":
                    self.blpars.append(None)
                    continue
            scan._setspectrum(self.fitter.getresidual(), r)
            self.blpars.append(fpar)
        if plot:
            self._p.quit()
            del self._p
            self._p = None
        return scan
Exemplo n.º 3
0
    def auto_fit(self, insitu=None, plot=False):
        """
        Return a scan where the function is applied to all rows for
        all Beams/IFs/Pols.

        """
        from asap import scantable
        if not isinstance(self.data, scantable):
            msg = "Data is not a scantable"
            raise TypeError(msg)
        if insitu is None: insitu = rcParams['insitu']
        if not insitu:
            scan = self.data.copy()
        else:
            scan = self.data
        rows = xrange(scan.nrow())
        # Save parameters of baseline fits as a class attribute.
        # NOTICE: This does not reflect changes in scantable!
        if len(rows) > 0: self.blpars = []
        asaplog.push("Fitting:")
        for r in rows:
            out = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (
                scan.getscan(r), scan.getbeam(r), scan.getif(r),
                scan.getpol(r), scan.getcycle(r))
            asaplog.push(out, False)
            self.x = scan._getabcissa(r)
            self.y = scan._getspectrum(r)
            #self.mask = mask_and(self.mask, scan._getmask(r))
            if len(self.x) == len(self.mask):
                self.mask = mask_and(self.mask, self.data._getmask(row))
            else:
                asaplog.push('lengths of data and mask are not the same. '
                             'preset mask will be ignored')
                asaplog.post('WARN', 'asapfit.fit')
                self.mask = self.data._getmask(row)
            self.data = None
            self.fit()
            x = self.get_parameters()
            fpar = self.get_parameters()
            if plot:
                self.plot(residual=True)
                x = raw_input("Accept fit ([y]/n): ")
                if x.upper() == 'N':
                    self.blpars.append(None)
                    continue
            scan._setspectrum(self.fitter.getresidual(), r)
            self.blpars.append(fpar)
        if plot:
            self._p.quit()
            del self._p
            self._p = None
        return scan
Exemplo n.º 4
0
    def fit(self, row=0, estimate=False):
        """
        Execute the actual fitting process. All the state has to be set.
        Parameters:
            row:        specify the row in the scantable
            estimate:   auto-compute an initial parameter set (default False)
                        This can be used to compute estimates even if fit was
                        called before.
        Example:
            s = scantable('myscan.asap')
            s.set_cursor(thepol=1)        # select second pol
            f = fitter()
            f.set_scan(s)
            f.set_function(poly=0)
            f.fit(row=0)                  # fit first row
        """
        if ((self.x is None or self.y is None) and self.data is None) or self.fitfunc is None:
            msg = "Fitter not yet initialised. Please set data & fit function"
            raise RuntimeError(msg)

        if self.data is not None:
            if self.data._getflagrow(row):
                raise RuntimeError, "Can not fit flagged row."
            self.x = self.data._getabcissa(row)
            self.y = self.data._getspectrum(row)
            # self.mask = mask_and(self.mask, self.data._getmask(row))
            if len(self.x) == len(self.mask):
                self.mask = mask_and(self.mask, self.data._getmask(row))
            else:
                asaplog.push("lengths of data and mask are not the same. " "preset mask will be ignored")
                asaplog.post("WARN", "asapfit.fit")
                self.mask = self.data._getmask(row)
            asaplog.push("Fitting:")
            i = row
            out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (
                self.data.getscan(i),
                self.data.getbeam(i),
                self.data.getif(i),
                self.data.getpol(i),
                self.data.getcycle(i),
            )

            asaplog.push(out, False)

        self.fitter.setdata(self.x, self.y, self.mask)
        if self.fitfunc == "gauss" or self.fitfunc == "lorentz":
            ps = self.fitter.getparameters()
            if len(ps) == 0 or estimate:
                self.fitter.estimate()
        fxdpar = list(self.fitter.getfixedparameters())
        if len(fxdpar) and fxdpar.count(0) == 0:
            raise RuntimeError, "No point fitting, if all parameters are fixed."
        if self._constraints:
            for c in self._constraints:
                self.fitter.addconstraint(c[0] + [c[-1]])
        if self.uselinear:
            converged = self.fitter.lfit()
        else:
            converged = self.fitter.fit()
        if not converged:
            raise RuntimeError, "Fit didn't converge."
        self._fittedrow = row
        self.fitted = True
        return
Exemplo n.º 5
0
    def fit(self, row=0, estimate=False):
        """
        Execute the actual fitting process. All the state has to be set.
        Parameters:
            row:        specify the row in the scantable
            estimate:   auto-compute an initial parameter set (default False)
                        This can be used to compute estimates even if fit was
                        called before.
        Example:
            s = scantable('myscan.asap')
            s.set_cursor(thepol=1)        # select second pol
            f = fitter()
            f.set_scan(s)
            f.set_function(poly=0)
            f.fit(row=0)                  # fit first row
        """
        if ((self.x is None or self.y is None) and self.data is None) \
               or self.fitfunc is None:
            msg = "Fitter not yet initialised. Please set data & fit function"
            raise RuntimeError(msg)

        if self.data is not None:
            if self.data._getflagrow(row):
                raise RuntimeError, "Can not fit flagged row."
            self.x = self.data._getabcissa(row)
            self.y = self.data._getspectrum(row)
            #self.mask = mask_and(self.mask, self.data._getmask(row))
            if len(self.x) == len(self.mask):
                self.mask = mask_and(self.mask, self.data._getmask(row))
            else:
                asaplog.push('lengths of data and mask are not the same. '
                             'preset mask will be ignored')
                asaplog.post('WARN', 'asapfit.fit')
                self.mask = self.data._getmask(row)
            asaplog.push("Fitting:")
            i = row
            out = "Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % (
                self.data.getscan(i), self.data.getbeam(i), self.data.getif(i),
                self.data.getpol(i), self.data.getcycle(i))

            asaplog.push(out, False)

        self.fitter.setdata(self.x, self.y, self.mask)
        if self.fitfunc == 'gauss' or self.fitfunc == 'lorentz':
            ps = self.fitter.getparameters()
            if len(ps) == 0 or estimate:
                self.fitter.estimate()
        fxdpar = list(self.fitter.getfixedparameters())
        if len(fxdpar) and fxdpar.count(0) == 0:
            raise RuntimeError, "No point fitting, if all parameters are fixed."
        if self._constraints:
            for c in self._constraints:
                self.fitter.addconstraint(c[0] + [c[-1]])
        if self.uselinear:
            converged = self.fitter.lfit()
        else:
            converged = self.fitter.fit()
        if not converged:
            raise RuntimeError, "Fit didn't converge."
        self._fittedrow = row
        self.fitted = True
        return