source: trunk/src/STLineFinder.h@ 909

Last change on this file since 909 was 907, checked in by vor010, 19 years ago

LineFinder & auto_poly_baseline: a support of
new scantable format has been added. Now findLines accept a mask and edge
parameters, which can therefore be different for different rows. Constructor
need now just a scan table. Boost types removed from STLineFinder. auto_poly_baseline can accept a nested tuple of edges, which would be interpreted as
different edge parameters for different IFs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.4 KB
Line 
1//#---------------------------------------------------------------------------
2//# STLineFinder.h: A class for automated spectral line search
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# ATNF
6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//# Internet email: Malte.Marquarding@csiro.au
23//# Postal address: Malte Marquarding,
24//# Australia Telescope National Facility,
25//# P.O. Box 76,
26//# Epping, NSW, 2121,
27//# AUSTRALIA
28//#
29//# $Id: STLineFinder.h 907 2006-03-20 04:17:06Z vor010 $
30//#---------------------------------------------------------------------------
31#ifndef STLINEFINDER_H
32#define STLINEFINDER_H
33
34// STL
35#include <vector>
36#include <list>
37#include <utility>
38#include <exception>
39
40// boost
41#include <boost/python.hpp>
42
43// AIPS++
44#include <casa/aips.h>
45#include <casa/Exceptions/Error.h>
46#include <casa/Arrays/Vector.h>
47#include <casa/Utilities/Assert.h>
48#include <casa/Utilities/CountedPtr.h>
49
50// ASAP
51#include "ScantableWrapper.h"
52#include "Scantable.h"
53
54namespace asap {
55
56///////////////////////////////////////////////////////////////////////////////
57//
58// LFLineListOperations - a class incapsulating operations with line lists
59// The LF prefix stands for Line Finder
60//
61
62struct LFLineListOperations {
63 // concatenate two lists preserving the order. If two lines appear to
64 // be adjacent or have a non-void intersection, they are joined into
65 // the new line
66 static void addNewSearchResult(const std::list<std::pair<int, int> >
67 &newlines, std::list<std::pair<int, int> > &lines_list)
68 throw(casa::AipsError);
69
70 // extend all line ranges to the point where a value stored in the
71 // specified vector changes (e.g. value-mean change its sign)
72 // This operation is necessary to include line wings, which are below
73 // the detection threshold. If lines becomes adjacent, they are
74 // merged together. Any masked channel stops the extension
75 static void searchForWings(std::list<std::pair<int, int> > &newlines,
76 const casa::Vector<casa::Int> &signs,
77 const casa::Vector<casa::Bool> &mask,
78 const std::pair<int,int> &edge)
79 throw(casa::AipsError);
80protected:
81
82 // An auxiliary object function to test whether two lines have a non-void
83 // intersection
84 class IntersectsWith : public std::unary_function<pair<int,int>, bool> {
85 std::pair<int,int> line1; // range of the first line
86 // start channel and stop+1
87 public:
88 IntersectsWith(const std::pair<int,int> &in_line1);
89 // return true if line2 intersects with line1 with at least one
90 // common channel, and false otherwise
91 bool operator()(const std::pair<int,int> &line2) const throw();
92 };
93
94 // An auxiliary object function to build a union of several lines
95 // to account for a possibility of merging the nearby lines
96 class BuildUnion {
97 std::pair<int,int> temp_line; // range of the first line
98 // start channel and stop+1
99 public:
100 BuildUnion(const std::pair<int,int> &line1);
101 // update temp_line with a union of temp_line and new_line
102 // provided there is no gap between the lines
103 void operator()(const std::pair<int,int> &new_line) throw();
104 // return the result (temp_line)
105 const std::pair<int,int>& result() const throw();
106 };
107
108 // An auxiliary object function to test whether a specified line
109 // is at lower spectral channels (to preserve the order in the line list)
110 class LaterThan : public std::unary_function<pair<int,int>, bool> {
111 std::pair<int,int> line1; // range of the first line
112 // start channel and stop+1
113 public:
114 LaterThan(const std::pair<int,int> &in_line1);
115
116 // return true if line2 should be placed later than line1
117 // in the ordered list (so, it is at greater channel numbers)
118 bool operator()(const std::pair<int,int> &line2) const throw();
119 };
120
121
122};
123
124//
125///////////////////////////////////////////////////////////////////////////////
126
127///////////////////////////////////////////////////////////////////////////////
128//
129// STLineFinder - a class for automated spectral line search
130//
131//
132
133struct STLineFinder : protected LFLineListOperations {
134 STLineFinder() throw();
135 virtual ~STLineFinder() throw(casa::AipsError);
136
137 // set the parameters controlling algorithm
138 // in_threshold a single channel threshold default is sqrt(3), which
139 // means together with 3 minimum channels at least 3 sigma
140 // detection criterion
141 // For bad baseline shape, in_threshold may need to be
142 // increased
143 // in_min_nchan minimum number of channels above the threshold to report
144 // a detection, default is 3
145 // in_avg_limit perform the averaging of no more than in_avg_limit
146 // adjacent channels to search for broad lines
147 // Default is 8, but for a bad baseline shape this
148 // parameter should be decreased (may be even down to a
149 // minimum of 1 to disable this option) to avoid
150 // confusing of baseline undulations with a real line.
151 // Setting a very large value doesn't usually provide
152 // valid detections.
153 // in_box_size the box size for running mean calculation. Default is
154 // 1./5. of the whole spectrum size
155 void setOptions(const casa::Float &in_threshold=sqrt(3.),
156 const casa::Int &in_min_nchan=3,
157 const casa::Int &in_avg_limit=8,
158 const casa::Float &in_box_size=0.2) throw();
159
160 // set the scan to work with (in_scan parameter)
161 void setScan(const ScantableWrapper &in_scan) throw(casa::AipsError);
162
163 // search for spectral lines in a row specified by whichRow
164 // in_mask and in_edge parameters control channel rejection
165 // if in_edge has zero length, all channels chosen by mask will be used
166 // if in_edge has one element only, it represents the number of
167 // channels to drop from both sides of the spectrum
168 // in_edge is introduced for convinience, although all functionality
169 // can be achieved using a spectrum mask only
170 // Number of lines found is returned
171 int findLines(const std::vector<bool> &in_mask,
172 const std::vector<int> &in_edge = std::vector<int>(),
173 const casa::uInt &whichRow = 0) throw(casa::AipsError);
174
175 // get the mask to mask out all lines that have been found (default)
176 // if invert=true, only channels belong to lines will be unmasked
177 // Note: all channels originally masked by the input mask (in_mask
178 // in setScan) or dropped out by the edge parameter (in_edge
179 // in setScan) are still excluded regardless on the invert option
180 std::vector<bool> getMask(bool invert=false) const throw(casa::AipsError);
181
182 // get range for all lines found. The same units as used in the scan
183 // will be returned (e.g. velocity instead of channels).
184 std::vector<double> getLineRanges() const throw(casa::AipsError);
185 // The same as getLineRanges, but channels are always used to specify
186 // the range
187 std::vector<int> getLineRangesInChannels() const throw(casa::AipsError);
188protected:
189 // auxiliary function to average adjacent channels and update the mask
190 // if at least one channel involved in summation is masked, all
191 // output channels will be masked. This function works with the
192 // spectrum and edge fields of this class, but updates the mask
193 // array specified, rather than the field of this class
194 // boxsize - a number of adjacent channels to average
195 void averageAdjacentChannels(casa::Vector<casa::Bool> &mask2update,
196 const casa::Int &boxsize)
197 throw(casa::AipsError);
198
199 // auxiliary function to fit and subtract a polynomial from the current
200 // spectrum. It uses the Fitter class. This action is required before
201 // reducing the spectral resolution if the baseline shape is bad
202 void subtractBaseline(const casa::Vector<casa::Bool> &temp_mask,
203 const casa::Int &order) throw(casa::AipsError);
204
205 // an auxiliary function to remove all lines from the list, except the
206 // strongest one (by absolute value). If the lines removed are real,
207 // they will be find again at the next iteration. This approach
208 // increases the number of iterations required, but is able to remove
209 // the sidelobes likely to occur near strong lines.
210 // Later a better criterion may be implemented, e.g.
211 // taking into consideration the brightness of different lines. Now
212 // use the simplest solution
213 // temp_mask - mask to work with (may be different from original mask as
214 // the lines previously found may be masked)
215 // lines2update - a list of lines to work with
216 // nothing will be done if it is empty
217 // max_box_nchan - channels in the running box for baseline filtering
218 void keepStrongestOnly(const casa::Vector<casa::Bool> &temp_mask,
219 std::list<std::pair<int, int> > &lines2update,
220 int max_box_nchan)
221 throw (casa::AipsError);
222private:
223 casa::CountedConstPtr<Scantable> scan; // the scan to work with
224 casa::Vector<casa::Bool> mask; // associated mask
225 std::pair<int,int> edge; // start and stop+1 channels
226 // to work with
227 casa::Float threshold; // detection threshold - the
228 // minimal signal to noise ratio
229 casa::Double box_size; // size of the box for running
230 // mean calculations, specified as
231 // a fraction of the whole spectrum
232 int min_nchan; // A minimum number of consequtive
233 // channels, which should satisfy
234 // the detection criterion, to be
235 // a detection
236 casa::Int avg_limit; // perform the averaging of no
237 // more than in_avg_limit
238 // adjacent channels to search
239 // for broad lines. see setOptions
240 casa::uInt last_row_used; // the Row number specified
241 // during the last findLines call
242 std::list<std::pair<int, int> > lines; // container of start and stop+1
243 // channels of the spectral lines
244 // a buffer for the spectrum
245 mutable casa::Vector<casa::Float> spectrum;
246};
247
248//
249///////////////////////////////////////////////////////////////////////////////
250
251} // namespace asap
252#endif // #ifndef STLINEFINDER_H
Note: See TracBrowser for help on using the repository browser.