Changeset 1907
- Timestamp:
- 08/26/10 19:46:26 (14 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/scantable.py
r1904 r1907 16 16 from asap.coordinate import coordinate 17 17 from asap.utils import _n_bools, mask_not, mask_and, mask_or, page 18 from asap.asapfitter import fitter 18 19 19 20 … … 486 487 487 488 @asaplog_post_dec 488 def stats(self, stat='stddev', mask=None, form='3.3f' ):489 def stats(self, stat='stddev', mask=None, form='3.3f', row=None): 489 490 """\ 490 491 Determine the specified statistic of the current beam/if/pol … … 502 503 form: format string to print statistic values 503 504 504 Example:: 505 505 row: row number of spectrum to process. 506 (default is None: for all rows) 507 508 Example: 506 509 scan.set_unit('channel') 507 510 msk = scan.create_mask([100, 200], [500, 600]) … … 521 524 getchan = True 522 525 statvals = [] 523 if not rtnabc: statvals = self._math._stats(self, mask, stat) 526 if not rtnabc: 527 if row == None: 528 statvals = self._math._stats(self, mask, stat) 529 else: 530 statvals = self._math._statsrow(self, mask, stat, int(row)) 524 531 525 532 #def cb(i): … … 533 540 #outvec = [] 534 541 sep = '-'*50 535 for i in range(self.nrow()): 542 543 if row == None: 544 rows = xrange(self.nrow()) 545 elif isinstance(row, int): 546 rows = [ row ] 547 548 for i in rows: 536 549 refstr = '' 537 550 statunit= '' … … 549 562 out += 'Scan[%d] (%s) ' % (self.getscan(i), src) 550 563 out += 'Time[%s]:\n' % (tm) 551 if self.nbeam(-1) > 1: 552 out += ' Beam[%d] ' % (self.getbeam(i)) 553 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) 554 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) 564 if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (self.getbeam(i)) 565 if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) 566 if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) 555 567 #outvec.append(callback(i)) 556 #out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 557 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 568 if len(rows) > 1: 569 # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' 570 out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' 571 else: 572 # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n' 573 out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n' 558 574 out += sep+"\n" 559 560 575 561 576 import os … … 1837 1852 1838 1853 @asaplog_post_dec 1839 def poly_baseline(self, mask=None, order=0, plot=False, uselin=False, 1840 insitu=None): 1854 def old_poly_baseline(self, mask=None, order=0, plot=False, uselin=False, insitu=None, rows=None): 1841 1855 """\ 1842 1856 Return a scan which has been baselined (all rows) by a polynomial. 1843 1857 1844 1858 Parameters: 1845 1859 … … 1858 1872 The default is taken from .asaprc (False) 1859 1873 1860 Example:: 1861 1874 rows: row numbers of spectra to be processed. 1875 (default is None: for all rows) 1876 1877 Example: 1862 1878 # return a scan baselined by a third order polynomial, 1863 1879 # not using a mask … … 1872 1888 varlist = vars() 1873 1889 if mask is None: 1874 mask = [True for i in xrange(self.nchan(-1))] 1875 1876 from asap.asapfitter import fitter 1890 mask = [True for i in xrange(self.nchan())] 1891 1877 1892 try: 1878 1893 f = fitter() … … 1882 1897 f.set_function(poly=order) 1883 1898 1884 rows = range(workscan.nrow()) 1899 if rows == None: 1900 rows = xrange(workscan.nrow()) 1901 elif isinstance(rows, int): 1902 rows = [ rows ] 1903 1885 1904 if len(rows) > 0: 1886 1905 self.blpars = [] 1887 1906 self.masklists = [] 1907 self.actualmask = [] 1908 1888 1909 for r in rows: 1889 # take into account flagtra info (CAS-1434)1890 flagtra = workscan._getmask(r)1891 actualmask = mask[:]1892 if len(actualmask) == 0:1893 actualmask = list(flagtra[:])1894 else:1895 if len(actualmask) != len(flagtra):1896 raise RuntimeError, "Mask and flagtra have different length"1897 else:1898 for i in range(0, len(actualmask)):1899 actualmask[i] = actualmask[i] and flagtra[i]1900 f.set_scan(workscan, actualmask)1901 1910 f.x = workscan._getabcissa(r) 1902 1911 f.y = workscan._getspectrum(r) 1912 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 1903 1913 f.data = None 1904 1914 f.fit() … … 1908 1918 if x.upper() == 'N': 1909 1919 self.blpars.append(None) 1920 self.masklists.append(None) 1921 self.actualmask.append(None) 1910 1922 continue 1911 1923 workscan._setspectrum(f.fitter.getresidual(), r) 1912 1924 self.blpars.append(f.get_parameters()) 1925 self.masklists.append(workscan.get_masklist(f.mask, row=r)) 1926 self.actualmask.append(f.mask) 1913 1927 1914 1928 if plot: … … 1925 1939 1926 1940 1927 def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0, 1941 def poly_baseline(self, mask=None, order=0, plot=False, batch=False, insitu=None, rows=None): 1942 """\ 1943 Return a scan which has been baselined (all rows) by a polynomial. 1944 Parameters: 1945 mask: an optional mask 1946 order: the order of the polynomial (default is 0) 1947 plot: plot the fit and the residual. In this each 1948 indivual fit has to be approved, by typing 'y' 1949 or 'n'. Ignored if batch = True. 1950 batch: if True a faster algorithm is used and logs 1951 including the fit results are not output 1952 (default is False) 1953 insitu: if False a new scantable is returned. 1954 Otherwise, the scaling is done in-situ 1955 The default is taken from .asaprc (False) 1956 rows: row numbers of spectra to be processed. 1957 (default is None: for all rows) 1958 Example: 1959 # return a scan baselined by a third order polynomial, 1960 # not using a mask 1961 bscan = scan.poly_baseline(order=3) 1962 """ 1963 if insitu is None: insitu = rcParams["insitu"] 1964 if insitu: 1965 workscan = self 1966 else: 1967 workscan = self.copy() 1968 1969 varlist = vars() 1970 nchan = workscan.nchan() 1971 1972 if mask is None: 1973 mask = [True for i in xrange(nchan)] 1974 1975 try: 1976 if rows == None: 1977 rows = xrange(workscan.nrow()) 1978 elif isinstance(rows, int): 1979 rows = [ rows ] 1980 1981 if len(rows) > 0: 1982 self.blpars = [] 1983 self.masklists = [] 1984 self.actualmask = [] 1985 1986 if batch: 1987 for r in rows: 1988 workscan._poly_baseline(mask, order, r) 1989 elif plot: 1990 f = fitter() 1991 f.set_function(lpoly=order) 1992 for r in rows: 1993 f.x = workscan._getabcissa(r) 1994 f.y = workscan._getspectrum(r) 1995 f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 1996 f.data = None 1997 f.fit() 1998 1999 f.plot(residual=True) 2000 accept_fit = raw_input("Accept fit ( [y]/n ): ") 2001 if accept_fit.upper() == "N": 2002 self.blpars.append(None) 2003 self.masklists.append(None) 2004 self.actualmask.append(None) 2005 continue 2006 workscan._setspectrum(f.fitter.getresidual(), r) 2007 self.blpars.append(f.get_parameters()) 2008 self.masklists.append(workscan.get_masklist(f.mask, row=r)) 2009 self.actualmask.append(f.mask) 2010 2011 f._p.unmap() 2012 f._p = None 2013 else: 2014 import array 2015 for r in rows: 2016 pars = array.array("f", [0.0 for i in range(order+1)]) 2017 pars_adr = pars.buffer_info()[0] 2018 pars_len = pars.buffer_info()[1] 2019 2020 errs = array.array("f", [0.0 for i in range(order+1)]) 2021 errs_adr = errs.buffer_info()[0] 2022 errs_len = errs.buffer_info()[1] 2023 2024 fmsk = array.array("i", [1 for i in range(nchan)]) 2025 fmsk_adr = fmsk.buffer_info()[0] 2026 fmsk_len = fmsk.buffer_info()[1] 2027 2028 workscan._poly_baseline(mask, order, r, pars_adr, pars_len, errs_adr, errs_len, fmsk_adr, fmsk_len) 2029 2030 params = pars.tolist() 2031 fmtd = "" 2032 for i in xrange(len(params)): fmtd += " p%d= %3.6f," % (i, params[i]) 2033 fmtd = fmtd[:-1] # remove trailing "," 2034 errors = errs.tolist() 2035 fmask = fmsk.tolist() 2036 for i in xrange(len(fmask)): fmask[i] = (fmask[i] > 0) # transform (1/0) -> (True/False) 2037 2038 self.blpars.append({"params":params, "fixed":[], "formatted":fmtd, "errors":errors}) 2039 self.masklists.append(workscan.get_masklist(fmask, r)) 2040 self.actualmask.append(fmask) 2041 2042 asaplog.push(str(fmtd)) 2043 2044 workscan._add_history("poly_baseline", varlist) 2045 print_log() 2046 2047 if insitu: 2048 self._assign(workscan) 2049 else: 2050 return workscan 2051 2052 except RuntimeError: 2053 msg = "The fit failed, possibly because it didn't converge." 2054 if rcParams["verbose"]: 2055 print_log() 2056 asaplog.push(str(msg)) 2057 print_log("ERROR") 2058 return 2059 else: 2060 raise RuntimeError(msg) 2061 2062 2063 def auto_poly_baseline(self, mask=None, edge=(0, 0), order=0, 1928 2064 threshold=3, chan_avg_limit=1, plot=False, 1929 insitu=None ):2065 insitu=None, rows=None): 1930 2066 """\ 1931 2067 Return a scan which has been baselined (all rows) by a polynomial. … … 1968 2104 Otherwise, the scaling is done in-situ 1969 2105 The default is taken from .asaprc (False) 2106 rows: row numbers of spectra to be processed. 2107 (default is None: for all rows) 1970 2108 1971 2109 … … 1977 2115 if insitu is None: insitu = rcParams['insitu'] 1978 2116 varlist = vars() 1979 from asap.asapfitter import fitter1980 2117 from asap.asaplinefind import linefinder 1981 2118 from asap import _is_sequence_or_number as _is_valid … … 2001 2138 curedge = edge; 2002 2139 2140 if not insitu: 2141 workscan = self.copy() 2142 else: 2143 workscan = self 2144 2003 2145 # setup fitter 2004 2146 f = fitter() 2005 f.set_function( poly=order)2147 f.set_function(lpoly=order) 2006 2148 2007 2149 # setup line finder … … 2009 2151 fl.set_options(threshold=threshold,avg_limit=chan_avg_limit) 2010 2152 2011 if not insitu:2012 workscan = self.copy()2013 else:2014 workscan = self2015 2016 2153 fl.set_scan(workscan) 2017 2154 2018 rows = range(workscan.nrow()) 2155 if mask is None: 2156 mask = _n_bools(workscan.nchan(), True) 2157 2158 if rows is None: 2159 rows = xrange(workscan.nrow()) 2160 elif isinstance(rows, int): 2161 rows = [ rows ] 2162 2019 2163 # Save parameters of baseline fits & masklists as a class attribute. 2020 2164 # NOTICE: It does not reflect changes in scantable! … … 2022 2166 self.blpars=[] 2023 2167 self.masklists=[] 2168 self.actualmask=[] 2024 2169 asaplog.push("Processing:") 2025 2170 for r in rows: … … 2036 2181 curedge = edge[workscan.getif(r)] 2037 2182 2038 # take into account flagtra info (CAS-1434) 2039 flagtra = workscan._getmask(r) 2040 actualmask = mask[:] 2041 if len(actualmask) == 0: 2042 actualmask = list(flagtra[:]) 2043 else: 2044 if len(actualmask) != len(flagtra): 2045 raise RuntimeError, "Mask and flagtra have different length" 2046 else: 2047 for i in range(0, len(actualmask)): 2048 actualmask[i] = actualmask[i] and flagtra[i] 2183 actualmask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) 2049 2184 2050 2185 # setup line finder 2051 2186 fl.find_lines(r, actualmask, curedge) 2052 outmask=fl.get_mask() 2053 f.set_scan(workscan, fl.get_mask()) 2187 2054 2188 f.x = workscan._getabcissa(r) 2055 2189 f.y = workscan._getspectrum(r) 2190 f.mask = fl.get_mask() 2056 2191 f.data = None 2057 2192 f.fit() 2058 2193 2059 2194 # Show mask list 2060 masklist=workscan.get_masklist(f l.get_mask(),row=r)2195 masklist=workscan.get_masklist(f.mask, row=r) 2061 2196 msg = "mask range: "+str(masklist) 2062 2197 asaplog.push(msg, False) … … 2068 2203 self.blpars.append(None) 2069 2204 self.masklists.append(None) 2205 self.actualmask.append(None) 2070 2206 continue 2071 2207 … … 2073 2209 self.blpars.append(f.get_parameters()) 2074 2210 self.masklists.append(masklist) 2211 self.actualmask.append(f.mask) 2075 2212 if plot: 2076 2213 f._p.unmap() -
trunk/src/STMath.cpp
r1819 r1907 1796 1796 out.push_back(outstat); 1797 1797 } 1798 return out; 1799 } 1800 1801 std::vector< float > STMath::statisticRow( const CountedPtr< Scantable > & in, 1802 const std::vector< bool > & mask, 1803 const std::string& which, 1804 int row ) 1805 { 1806 1807 Vector<Bool> m(mask); 1808 const Table& tab = in->table(); 1809 ROArrayColumn<Float> specCol(tab, "SPECTRA"); 1810 ROArrayColumn<uChar> flagCol(tab, "FLAGTRA"); 1811 std::vector<float> out; 1812 1813 Vector<Float> spec; specCol.get(row, spec); 1814 Vector<uChar> flag; flagCol.get(row, flag); 1815 MaskedArray<Float> ma = maskedArray(spec, flag); 1816 float outstat = 0.0; 1817 if ( spec.nelements() == m.nelements() ) { 1818 outstat = mathutil::statistics(which, ma(m)); 1819 } else { 1820 outstat = mathutil::statistics(which, ma); 1821 } 1822 out.push_back(outstat); 1823 1798 1824 return out; 1799 1825 } -
trunk/src/STMath.h
r1819 r1907 277 277 const std::vector<bool>& mask, 278 278 const std::string& which); 279 280 std::vector<float> statisticRow(const casa::CountedPtr<Scantable>& in, 281 const std::vector<bool>& mask, 282 const std::string& which, 283 int row); 279 284 280 285 std::vector< int > minMaxChan(const casa::CountedPtr<Scantable>& in, -
trunk/src/STMathWrapper.h
r1819 r1907 132 132 { return STMath::statistic(in.getCP(), mask, which); } 133 133 134 std::vector<float> statisticRow(const ScantableWrapper& in, 135 const std::vector<bool>& mask, 136 const std::string& which, 137 int row) 138 { return STMath::statisticRow(in.getCP(), mask, which, row); } 139 134 140 std::vector<int> minMaxChan(const ScantableWrapper& in, 135 141 const std::vector<bool>& mask, -
trunk/src/Scantable.cpp
r1881 r1907 1712 1712 } 1713 1713 1714 bool Scantable::getFlagtraFast(int whichrow) 1715 { 1716 uChar flag; 1717 Vector<uChar> flags; 1718 flagsCol_.get(uInt(whichrow), flags); 1719 for (int i = 0; i < flags.size(); i++) { 1720 if (i==0) { 1721 flag = flags[i]; 1722 } 1723 else { 1724 flag &= flags[i]; 1725 } 1726 return ((flag >> 7) == 1); 1727 } 1728 } 1729 1730 void Scantable::doPolyBaseline(const std::vector<bool>& mask, int order, int rowno, Fitter& fitter) 1731 { 1732 fitter.setExpression("poly", order); 1733 1734 std::vector<double> abcsd = getAbcissa(rowno); 1735 std::vector<float> abcs; 1736 for (int i = 0; i < abcsd.size(); i++) { 1737 abcs.push_back((float)abcsd[i]); 1738 } 1739 std::vector<float> spec = getSpectrum(rowno); 1740 std::vector<bool> fmask = getMask(rowno); 1741 for (int i = 0; i < fmask.size(); i++) { 1742 fmask[i] = fmask[i] && mask[i]; 1743 } 1744 fitter.setData(abcs, spec, fmask); 1745 1746 fitter.lfit(); 1747 } 1748 1749 void Scantable::polyBaseline(const std::vector<bool>& mask, int order, int rowno) 1750 { 1751 Fitter fitter = Fitter(); 1752 doPolyBaseline(mask, order, rowno, fitter); 1753 setSpectrum(fitter.getResidual(), rowno); 1754 } 1755 1756 void Scantable::polyBaseline(const std::vector<bool>& mask, int order, int rowno, int pars_ptr, int pars_size, int errs_ptr, int errs_size, int fmask_ptr, int fmask_size) 1757 { 1758 Fitter fitter = Fitter(); 1759 doPolyBaseline(mask, order, rowno, fitter); 1760 setSpectrum(fitter.getResidual(), rowno); 1761 1762 std::vector<float> pars = fitter.getParameters(); 1763 if (pars_size != pars.size()) { 1764 throw(AipsError("wrong pars size")); 1765 } 1766 float *ppars = reinterpret_cast<float*>(pars_ptr); 1767 for (int i = 0; i < pars_size; i++) { 1768 ppars[i] = pars[i]; 1769 } 1770 1771 std::vector<float> errs = fitter.getErrors(); 1772 if (errs_size != errs.size()) { 1773 throw(AipsError("wrong errors size")); 1774 } 1775 float *perrs = reinterpret_cast<float*>(errs_ptr); 1776 for (int i = 0; i < errs_size; i++) { 1777 perrs[i] = errs[i]; 1778 } 1779 1780 if (fmask_size != fmask.size()) { 1781 throw(AipsError("wrong fmask size")); 1782 } 1783 int *pfmask = reinterpret_cast<int*>(fmask_ptr); 1784 for (int i = 0; i < fmask_size; i++) { 1785 pfmask[i] = (fmask[i] ? 1 : 0); 1786 } 1787 } 1788 1714 1789 } 1715 1790 //namespace asap -
trunk/src/Scantable.h
r1819 r1907 47 47 #include "STFit.h" 48 48 #include "STFitEntry.h" 49 #include "STFitter.h" 49 50 50 51 namespace asap { … … 486 487 void regridChannel( int nchan, double dnu, int irow ) ; 487 488 489 bool getFlagtraFast(int whichrow); 490 491 void polyBaseline(const std::vector<bool>& mask, int order, int rowno); 492 void polyBaseline(const std::vector<bool>& mask, int order, int rowno, int pars_ptr, int pars_size, int errs_ptr, int errs_size, int fmask_ptr, int fmask_size); 493 488 494 489 495 private: … … 506 512 */ 507 513 std::string formatDirection(const casa::MDirection& md) const; 508 509 514 510 515 /** … … 598 603 const casa::String&, 599 604 const casa::Array<T2>&); 605 606 void doPolyBaseline(const std::vector<bool>& mask, int order, int rowno, Fitter& fitter); 600 607 }; 601 608 -
trunk/src/ScantableWrapper.h
r1819 r1907 250 250 { table_->reshapeSpectrum( nmin, nmax ); } 251 251 252 void polyBaseline(const std::vector<bool>& mask, int order, int rowno, int pars_ptr, int pars_size, int errs_ptr, int errs_size, int fmask_ptr, int fmask_size) 253 { table_->polyBaseline(mask, order, rowno, pars_ptr, pars_size, errs_ptr, errs_size, fmask_ptr, fmask_size); } 254 255 void polyBaseline(const std::vector<bool>& mask, int order, int rowno) 256 { table_->polyBaseline(mask, order, rowno); } 257 258 bool getFlagtraFast(int whichrow=0) const 259 { return table_->getFlagtraFast(whichrow); } 260 261 252 262 private: 253 263 casa::CountedPtr<Scantable> table_; -
trunk/src/python_STMath.cpp
r1819 r1907 59 59 .def("_dofs", &STMathWrapper::dofs) 60 60 .def("_stats", &STMathWrapper::statistic) 61 .def("_statsrow", &STMathWrapper::statisticRow) 61 62 .def("_minmaxchan", &STMathWrapper::minMaxChan) 62 63 .def("_freqswitch", &STMathWrapper::freqSwitch) -
trunk/src/python_Scantable.cpp
r1819 r1907 139 139 (boost::python::arg("nmin")=-1, 140 140 boost::python::arg("nmax")=-1) ) 141 .def("_poly_baseline", &ScantableWrapper::polyBaseline) 142 .def("_getflagtrafast", &ScantableWrapper::getFlagtraFast, 143 (boost::python::arg("whichrow")=0) ) 141 144 ; 142 145 };
Note:
See TracChangeset
for help on using the changeset viewer.