casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StatWtTVI.h
Go to the documentation of this file.
1 //# CASA - Common Astronomy Software Applications (http://casa.nrao.edu/)
2 //# Copyright (C) Associated Universities, Inc. Washington DC, USA 2011, All rights reserved.
3 //# Copyright (C) European Southern Observatory, 2011, All rights reserved.
4 //#
5 //# This library is free software; you can redistribute it and/or
6 //# modify it under the terms of the GNU Lesser General Public
7 //# License as published by the Free software Foundation; either
8 //# version 2.1 of the License, or (at your option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful,
11 //# but WITHOUT ANY WARRANTY, without even the implied warranty of
12 //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 //# Lesser General Public License for more details.
14 //#
15 //# You should have received a copy of the GNU Lesser General Public
16 //# License along with this library; if not, write to the Free Software
17 //# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 //# MA 02111-1307 USA
19 
20 #ifndef STATWTTVI_H_
21 #define STATWTTVI_H_
22 
24 
28 
29 #include <msvis/MSVis/VisBuffer2.h>
32 #include <stdcasa/variant.h>
34 
35 namespace casa {
36 
37 namespace vi {
38 
39 class StatWtTVI : public TransformingVi2 {
40 
41 public:
42 
43  static const casacore::String CHANBIN;
44 
45  // The following fields are supported in the input configuration record
46  // combine String, if contains "corr", data will be aggregated
47  // across correlations.
48  // value in CHANBIN: Int or Quantity String, describes channel bin widths
49  // in which to aggregate data within spectral windows
50  // (spw boundaries are not crossed). If not supplied,
51  // data for all channels in each spectral window are
52  // aggregated.
53  // minsamp: Int, minimum number of samples required in an
54  // aggregated set, if less than that, stats are not
55  // computed and the data in the sample are flagged. If
56  // not supplied, 2 is used.
57  // statalg String representing what statistics algorithm to use.
58  // "cl", "ch", "f", "h".
59  // maxiter Int max number of iterations for Chauvenet algorithm
60  // zscore Double zscore for Chauvenet algorithm
61  // center String center for FitToHalf algorithm, "mean",
62  // "median", or "zero"
63  // lside Bool side to use for FitToHalf algorithm, True means
64  // <= center side.
65  // fence Double fence value for HingesFences algorithm
66  // wtrange Zero or two element Array<Double>. Specifies the range
67  // of "good"
68  // weight values. Data with weights computed to be outside
69  // this range will be flagged. Both elements must be
70  // non-negative. If zero length, all weights are
71  // acceptable.
72  // fitspw String. MSSelection string representing channels to
73  // exclude from weight computation.
74  // datacolumn String. Data column to use for computing weights.
75  // Supports 'data' or 'corrected'. Minimum match, case
76  // insensitive. If not provided. 'corrected' is used.
77  // slidetimebin Bool. If true, use a sliding window for binning in
78  // time.
79  // timebin Double. Width of sliding time window. Not used if
80  // doslidetime is not supplied or if doslidetime = false;
81  StatWtTVI(
82  ViImplementation2* inputVii, const casacore::Record &configuration
83  );
84 
85  virtual ~StatWtTVI();
86 
87  virtual casacore::String ViiType() const {
88  return casacore::String("StatWt( ") + getVii()->ViiType() + " )";
89  };
90 
92 
94 
95  void next();
96 
97  void origin();
98 
99  virtual void weightSpectrum(casacore::Cube<casacore::Float>& wtsp) const;
100 
101  virtual void sigmaSpectrum(casacore::Cube<casacore::Float>& sigmaSp) const;
102 
103  virtual void weight(casacore::Matrix<casacore::Float> & wtmat) const;
104 
105  virtual void sigma(casacore::Matrix<casacore::Float> & sigmaMat) const;
106 
107  virtual void flag(casacore::Cube<casacore::Bool>& flagCube) const;
108 
109  virtual void flagRow (casacore::Vector<casacore::Bool> & flagRow) const;
110 
111  void summarizeFlagging() const;
112 
113  void summarizeStats(
115  ) const;
116 
117  // Override unimplemented TransformingVi2 version
118  void writeBackChanges(VisBuffer2* vb);
119 
120  // these are public so that class StatWt can call them. In general, other
121  // clients shouldn't call them.
123  const casacore::Quantity& binWidth
124  );
125 
126  static void checkTimeBinWidth(casacore::Double binWidth);
127 
130  );
131 
132 protected:
133 
134  void originChunks(casacore::Bool forceRewind);
135 
136  void nextChunk();
137 
138 private:
139 
140  using Baseline = std::pair<casacore::uInt, casacore::uInt>;
141 
142  struct ChanBin {
145 
146  bool operator<(const ChanBin& other) const {
147  if (start < other.start) {
148  return true;
149  }
150  if (start == other.start && end < other.end) {
151  return true;
152  }
153  return false;
154  }
155  };
156 
158  Baseline baseline = std::make_pair(0, 0);
161  bool operator<(const BaselineChanBin& other) const {
162  if (baseline < other.baseline) {
163  return true;
164  }
165  if (baseline == other.baseline && spw < other.spw) {
166  return true;
167  }
168  return baseline == other.baseline && spw == other.spw
169  && chanBin < other.chanBin;
170  };
171  };
172 
173  enum Column {
174  // column(s) to use
175  // DATA
177  // CORRECTED_DATA
179  // CORRECTED_DATA - MODEL_DATA
181  // DATA - MODEL_DATA
183  };
184 
186  mutable std::unique_ptr<casacore::Bool> _mustComputeWtSp {};
191  // the vector represents separate correlations, there will be
192  // only one element in the vector if _combineCorr is true
193  mutable std::map<BaselineChanBin, std::vector<casacore::Double>>
195  // The key refers to the spw, the value vector refers to the
196  // channel numbers within that spw that are the first, last channel pair
197  // in their respective bins
198  std::map<casacore::Int, std::vector<ChanBin>> _chanBins {};
205  > _statAlg {} ;
206  std::unique_ptr<std::pair<casacore::Double, casacore::Double>> _wtrange {};
207  // The _chanSelFlags key is the spw. The value is a Cube for convenience
208  // for subchunk computations that require the same shaped cube of flags to
209  // be applied. The dimension that counts is the second (zero-based 1) as it
210  // has length equal to the number of channels in the spw. A value of True
211  // indicates that the channel is "flagged", ie should not be used.
212  std::map<casacore::uInt, casacore::Cube<casacore::Bool>> _chanSelFlags {};
213 
214  mutable size_t _nTotalPts = 0;
215  mutable size_t _nNewFlaggedPts = 0;
216  mutable size_t _nOrigFlaggedPts = 0;
218  mutable std::map<casacore::uInt, std::pair<casacore::uInt, casacore::uInt>>
220  mutable std::set<casacore::uInt> _processedRowIDs {};
221  mutable std::vector<std::vector<casacore::Double>> _timeWindowWts {};
223  // if False, the a sliding time window is being used
225  // for running time window, for each subchunk, map the rowID (in the MS)
226  // to the row index in the chunk
227  mutable std::map<casacore::uInt, casacore::uInt>
230 
234 
235  std::shared_ptr<
239  > _wtStats {};
240 
241  // returns True if this chunk has already been processed. This can happen
242  // for the last chunk.
244  casacore::Int& spw, casacore::Bool& firstTime,
245  const VisBuffer2 * const vb
246  ) const;
247 
248  void _computeWeightSpectrumAndFlags() const;
249 
251  const VisBuffer2 *const vb
252  ) const;
253 
254  void _gatherAndComputeWeights() const;
255 
257 
259 
260  // combines the flag cube with the channel selection flags (if any)
262  casacore::Cube<casacore::Bool>& chanSelFlagTemplate,
263  casacore::Cube<casacore::Bool>& chanSelFlags,
264  casacore::Bool& initChanSelFlags, casacore::Int spw,
265  const casacore::Cube<casacore::Bool>& flagCube
266  ) const;
267 
268  // CAS-12358
269  void _logUsedChannels() const;
270 
271  // multi-threaded case
275  casacore::uInt spw
276  ) const;
277 
279  const std::map<BaselineChanBin, casacore::Cube<casacore::Complex>>& data,
280  const std::map<BaselineChanBin, casacore::Cube<casacore::Bool>>& flags
281  ) const;
282 
286  const std::vector<std::set<casacore::uInt>>& rowMap,
287  casacore::uInt spw
288  ) const;
289 
291 
292  //void _initialize();
293 
294  // swaps ant1/ant2 if necessary
295  static Baseline _baseline(casacore::uInt ant1, casacore::uInt ant2);
296 
297  std::pair<
299  > _getLowerLayerWtSpFlags(size_t& nOrigFlagged) const;
300 
301  void _setChanBinMap(casacore::Int binWidth);
302 
303  void _setChanBinMap(const casacore::Quantity& binWidth);
304 
305  void _setDefaultChanBinMap();
306 
307  void _clearCache();
308 
309  void _updateWtSpFlags(
312  const casacore::Slicer& slice, casacore::Float wt
313  ) const;
314 
315  void _configureStatAlg(const casacore::Record& config);
316 
319  casacore::Cube<casacore::Bool>& flagCube, casacore::Bool& checkFlags
320  ) const;
321 
324  casacore::Cube<casacore::Bool>& flagCube, casacore::Bool& checkFlags
325  ) const;
326 
329  ) const;
330 
333  ) const;
334 
335 };
336 
337 }
338 
339 }
340 
341 #endif
342 
casacore::Matrix< casacore::Float > _newWt
Definition: StatWtTVI.h:188
void _configureStatAlg(const casacore::Record &config)
casacore::CountedPtr< casacore::StatisticsAlgorithm< casacore::Double, casacore::Array< casacore::Float >::const_iterator, casacore::Array< casacore::Bool >::const_iterator > > _statAlg
Definition: StatWtTVI.h:205
void _setChanBinMap(casacore::Int binWidth)
void origin()
+==================================+ | | | Iteration Control and Monitoring | | | +==================...
casacore::Int _minSamp
Definition: StatWtTVI.h:199
void _weightSingleChanBinSlidingTimeWindow(casacore::Matrix< casacore::Float > &wtmat, casacore::Int nrows) const
int Int
Definition: aipstype.h:50
void _weightSingleChanBinBlockTimeProcessing(casacore::Matrix< casacore::Float > &wtmat, casacore::Int nrows) const
std::vector< std::vector< casacore::Double > > _timeWindowWts
Definition: StatWtTVI.h:221
void _weightSpectrumFlagsSlidingTimeWindow(casacore::Cube< casacore::Float > &wtsp, casacore::Cube< casacore::Bool > &flagCube, casacore::Bool &checkFlags) const
void initSigmaSpectrum(const casacore::Cube< casacore::Float > &sigspec)
Initialize the sigmaSpectrum.
virtual void flagRow(casacore::Vector< casacore::Bool > &flagRow) const
Return row flag.
void _weightSpectrumFlagsTimeBlockProcessing(casacore::Cube< casacore::Float > &wtsp, casacore::Cube< casacore::Bool > &flagCube, casacore::Bool &checkFlags) const
size_t _nNewFlaggedPts
Definition: StatWtTVI.h:215
std::map< casacore::uInt, casacore::Cube< casacore::Bool > > _chanSelFlags
The _chanSelFlags key is the spw.
Definition: StatWtTVI.h:212
virtual void sigma(casacore::Matrix< casacore::Float > &sigmaMat) const
Return sigma.
void _gatherAndComputeWeightsSlidingTimeWindow() const
casacore::Bool _noModel
Definition: StatWtTVI.h:233
casacore::Cube< casacore::Float > _newWtSp
Definition: StatWtTVI.h:187
casacore::Bool _mustComputeSigma
Definition: StatWtTVI.h:231
size_t _nOrigFlaggedPts
Definition: StatWtTVI.h:216
std::map< BaselineChanBin, std::vector< casacore::Double > > _weights
the vector represents separate correlations, there will be only one element in the vector if _combine...
Definition: StatWtTVI.h:194
static casacore::Double getTimeBinWidthInSec(const casacore::Quantity &binWidth)
these are public so that class StatWt can call them.
void _updateWtSpFlags(casacore::Cube< casacore::Float > &wtsp, casacore::Cube< casacore::Bool > &flags, casacore::Bool &checkFlags, const casacore::Slicer &slice, casacore::Float wt) const
static Baseline _baseline(casacore::uInt ant1, casacore::uInt ant2)
void _initialize();
Class to calculate statistics in a &quot;classical&quot; sense, ie using accumulators with no special filtering...
virtual casacore::String ViiType() const
Report the the ViImplementation type (should be specialized in child classes)
Definition: StatWtTVI.h:87
casacore::Bool _combineCorr
Definition: StatWtTVI.h:200
virtual void flag(casacore::Cube< casacore::Bool > &flagCube) const
Return flag for each polarization, channel and row.
void _computeWeightSpectrumAndFlags() const
const casacore::Cube< casacore::Complex > _dataCube(const VisBuffer2 *const vb) const
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
Definition: PlotData.h:48
bool operator<(const ChanBin &other) const
Definition: StatWtTVI.h:146
void _gatherAndComputeWeightsTimeBlockProcessing() const
casacore::Bool _checkFirsSubChunk(casacore::Int &spw, casacore::Bool &firstTime, const VisBuffer2 *const vb) const
returns True if this chunk has already been processed.
virtual void weight(casacore::Matrix< casacore::Float > &wtmat) const
Return weight.
Referenced counted pointer for constant data.
Definition: VisModelData.h:42
std::set< casacore::uInt > _processedRowIDs
Definition: StatWtTVI.h:220
std::map< casacore::uInt, casacore::uInt > _rowIDInMSTorowIndexInChunk
for running time window, for each subchunk, map the rowID (in the MS) to the row index in the chunk ...
Definition: StatWtTVI.h:228
static void checkTimeBinWidth(casacore::Double binWidth)
casacore::Cube< casacore::Double > _slidingTimeWindowWeights
Definition: StatWtTVI.h:222
double Double
Definition: aipstype.h:55
bool operator<(const BaselineChanBin &other) const
Definition: StatWtTVI.h:161
casacore::Bool _updateWeight
Definition: StatWtTVI.h:232
CORRECTED_DATA - MODEL_DATA.
Definition: StatWtTVI.h:180
void _setDefaultChanBinMap()
void summarizeFlagging() const
std::map< casacore::uInt, std::pair< casacore::uInt, casacore::uInt > > _samples
Definition: StatWtTVI.h:219
std::unique_ptr< casacore::Bool > _mustComputeWtSp
Definition: StatWtTVI.h:186
column(s) to use DATA
Definition: StatWtTVI.h:176
virtual void weightSpectrum(casacore::Cube< casacore::Float > &wtsp) const
Return weightspectrum (a weight for each channel)
A hierarchical collection of named fields of various types.
Definition: Record.h:180
ViImplementation2 * getVii() const
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
void originChunks(casacore::Bool forceRewind)
Methods to control chunk iterator.
casacore::Bool _parseConfiguration(const casacore::Record &configuration)
void _logUsedChannels() const
CAS-12358.
VisibilityIterator2 iterates through one or more readonly MeasurementSets.
void _gatherAndComputeWeights() const
float Float
Definition: aipstype.h:54
const Bool False
Definition: aipstype.h:44
casacore::Cube< casacore::Bool > _newFlag
Definition: StatWtTVI.h:189
std::shared_ptr< casacore::ClassicalStatistics< casacore::Double, casacore::Array< casacore::Float >::const_iterator, casacore::Array< casacore::Bool >::const_iterator > > _wtStats
Definition: StatWtTVI.h:239
A Table intended to hold astronomical data (a set of Measurements).
casacore::Bool _timeBlockProcessing
if False, the a sliding time window is being used
Definition: StatWtTVI.h:224
Specify which elements to extract from an n-dimensional array.
Definition: Slicer.h:289
void writeBackChanges(VisBuffer2 *vb)
Override unimplemented TransformingVi2 version.
StatWtTVI(ViImplementation2 *inputVii, const casacore::Record &configuration)
The following fields are supported in the input configuration record combine String, if contains &quot;corr&quot;, data will be aggregated across correlations.
VisBuffer2s encapsulate one chunk of visibility data for processing.
Definition: VisBuffer2.h:141
void summarizeStats(casacore::Double &mean, casacore::Double &variance) const
LatticeExprNode mean(const LatticeExprNode &expr)
VisibilityIterator2 iterates through one or more readonly MeasurementSets.
std::unique_ptr< std::pair< casacore::Double, casacore::Double > > _wtrange
Definition: StatWtTVI.h:206
std::map< casacore::Int, std::vector< ChanBin > > _chanBins
The key refers to the spw, the value vector refers to the channel numbers within that spw that are th...
Definition: StatWtTVI.h:198
static const casacore::String CHANBIN
Definition: StatWtTVI.h:43
virtual casacore::String ViiType() const =0
Report the the ViImplementation type.
String: the storage and methods of handling collections of characters.
Definition: String.h:223
casacore::Bool _weightsComputed
Definition: StatWtTVI.h:185
void initWeightSpectrum(const casacore::Cube< casacore::Float > &wtspec)
Initialize the weightSpectrum.
casacore::Cube< casacore::Bool > _getResultantFlags(casacore::Cube< casacore::Bool > &chanSelFlagTemplate, casacore::Cube< casacore::Bool > &chanSelFlags, casacore::Bool &initChanSelFlags, casacore::Int spw, const casacore::Cube< casacore::Bool > &flagCube) const
combines the flag cube with the channel selection flags (if any)
std::pair< casacore::Cube< casacore::Float >, casacore::Cube< casacore::Bool > > _getLowerLayerWtSpFlags(size_t &nOrigFlagged) const
casacore::Double _computeWeight(const casacore::Cube< casacore::Complex > &data, const casacore::Cube< casacore::Bool > &flags, casacore::uInt spw) const
multi-threaded case
virtual void sigmaSpectrum(casacore::Cube< casacore::Float > &sigmaSp) const
Return sgimaspectrum (a sigma for each channel)
casacore::Double _slidingTimeWindowWidth
Definition: StatWtTVI.h:229
void _computeWeightsSlidingTimeWindow(const casacore::Cube< casacore::Complex > &data, const casacore::Cube< casacore::Bool > &flags, const std::vector< std::set< casacore::uInt >> &rowMap, casacore::uInt spw) const
LatticeExprNode variance(const LatticeExprNode &expr)
Base class of statistics algorithm class hierarchy.
static casacore::Double getTimeBinWidthUsingInterval(const casacore::MeasurementSet *const ms, casacore::Int n)
void _computeWeightsTimeBlockProcessing(const std::map< BaselineChanBin, casacore::Cube< casacore::Complex >> &data, const std::map< BaselineChanBin, casacore::Cube< casacore::Bool >> &flags) const
casacore::Vector< casacore::Bool > _newFlagRow
Definition: StatWtTVI.h:190
const Bool True
Definition: aipstype.h:43
virtual const casacore::MeasurementSet & ms() const override
unsigned int uInt
Definition: aipstype.h:51
std::pair< casacore::uInt, casacore::uInt > Baseline
Definition: StatWtTVI.h:140