casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StatisticsDataset.h
Go to the documentation of this file.
1 //# Copyright (C) 2000,2001
2 //# Associated Universities, Inc. Washington DC, USA.
3 //#
4 //# This library is free software; you can redistribute it and/or modify it
5 //# under the terms of the GNU Library General Public License as published by
6 //# the Free Software Foundation; either version 2 of the License, or (at your
7 //# option) any later version.
8 //#
9 //# This library is distributed in the hope that it will be useful, but WITHOUT
10 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 //# License for more details.
13 //#
14 //# You should have received a copy of the GNU Library General Public License
15 //# along with this library; if not, write to the Free Software Foundation,
16 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
17 //#
18 //# Correspondence concerning AIPS++ should be addressed as follows:
19 //# Internet email: aips2-request@nrao.edu.
20 //# Postal address: AIPS++ Project Office
21 //# National Radio Astronomy Observatory
22 //# 520 Edgemont Road
23 //# Charlottesville, VA 22903-2475 USA
24 //#
25 
26 #ifndef SCIMATH_STATISTICSDATASET_H
27 #define SCIMATH_STATISTICSDATASET_H
28 
29 #include <casacore/casa/aips.h>
30 
32 
33 namespace casacore {
34 
35 template <class T> class PtrHolder;
36 
37 // Representation of a statistics dataset used in statistics framework
38 // calculatations.
39 //
40 // This class is used internally by StatisticsAlgorithm and its derived classes.
41 // There should be no need for an API developer to make direct use of this
42 // class. It encapsulates the data-related portions of StatisticsAlgorithm and
43 // derived classes. To add and set data or to set a data provider, one should
44 // call the relevant methods in StatisticsAlgorithm which have been left
45 // unchanged for the convenience of the API developer. Those methods call the
46 // analogous methods in this class (and the methods in StatisticsAlgorithm also
47 // do necessary bookkeeping for the StatisticsAlgorithm and derived objects).
48 
49 template <
50  class AccumType, class DataIterator, class MaskIterator=const Bool *,
51  class WeightsIterator=DataIterator
52 >
54 
55 public:
56 
57  // holds information about a data chunk. A data chunk is either an
58  // individual underlying dataset (if no data provider), or a chunk of data
59  // served by the data provider if it exists.
60  struct ChunkData {
61  // start of data
62  DataIterator data;
63  // total number of points
65  // data stride
67  // associated ranges. If nullptr, then there are none. If not, the
68  // second member of the pair indicates if they are include ranges.
70  // associated mask. If nullptr, then there is no mask.
71  // If there is a mask, the second member is the mask stride.
73  // associated weights. If nullptr, then there are no weights.
75  };
76 
78 
80 
82 
83  // use copy semantics, except for the data provider which uses reference
84  // semantics
87  );
88 
89  // <group>
90  // Add a dataset to an existing set of datasets on which statistics are to
91  // be calculated. nr is the number of points to be considered. If
92  // <src>dataStride</src> is greater than 1, when
93  // <src>nrAccountsForStride</src>=True indicates that the stride has been
94  // taken into account in the value of <src>nr</src>. Otherwise, it has not
95  // so that the actual number of points to include is nr/dataStride if
96  // nr % dataStride == 0 or (int)(nr/dataStride) + 1 otherwise. If one calls
97  // this method after a data provider has been set, an exception will be
98  // thrown. In this case, one should call setData(), rather than addData(),
99  // to indicate that the underlying data provider should be removed.
100  // <src>dataRanges</src> provide the ranges of data to include if
101  // <src>isInclude</src> is True, or ranges of data to exclude if
102  // <src>isInclude</src> is False. If a datum equals the end point of a data
103  // range, it is considered good (included) if <src>isInclude</src> is True,
104  // and it is considered bad (excluded) if <src>isInclude</src> is False.
105 
106  void addData(
107  const DataIterator& first, uInt nr, uInt dataStride=1,
108  Bool nrAccountsForStride=False
109  );
110 
111  void addData(
112  const DataIterator& first, uInt nr, const DataRanges& dataRanges,
113  Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False
114  );
115 
116  void addData(
117  const DataIterator& first, const MaskIterator& maskFirst, uInt nr,
118  uInt dataStride=1, Bool nrAccountsForStride=False, uInt maskStride=1
119  );
120 
121  void addData(
122  const DataIterator& first, const MaskIterator& maskFirst, uInt nr,
123  const DataRanges& dataRanges, Bool isInclude=True, uInt dataStride=1,
124  Bool nrAccountsForStride=False, uInt maskStride=1
125  );
126 
127  void addData(
128  const DataIterator& first, const WeightsIterator& weightFirst,
129  uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False
130  );
131 
132  void addData(
133  const DataIterator& first, const WeightsIterator& weightFirst,
134  uInt nr, const DataRanges& dataRanges, Bool isInclude=True,
135  uInt dataStride=1, Bool nrAccountsForStride=False
136  );
137 
138  void addData(
139  const DataIterator& first, const WeightsIterator& weightFirst,
140  const MaskIterator& maskFirst, uInt nr, uInt dataStride=1,
141  Bool nrAccountsForStride=False, uInt maskStride=1
142  );
143 
144  void addData(
145  const DataIterator& first, const WeightsIterator& weightFirst,
146  const MaskIterator& maskFirst, uInt nr, const DataRanges& dataRanges,
147  Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
148  uInt maskStride=1
149  );
150  // </group>
151 
152  // returns ! dataProvider && _data.empty()
153  Bool empty() const;
154 
155  // get data counts associated with the underlying data sets
156  const std::vector<Int64>& getCounts() const { return _counts; }
157 
159  return _dataProvider;
160  }
161 
163  return _dataProvider;
164  }
165 
166  Int64 iDataset() const { return _idataset; }
167 
168  Bool increment(Bool includeIDataset);
169 
171  DataIterator& dataIter, MaskIterator& maskIter,
172  WeightsIterator& weightsIter, uInt64& offset, uInt nthreads
173  ) const;
174 
175  void initIterators();
176 
177  // used for threaded methods
178  void initLoopVars(
179  uInt64& chunkCount, uInt& chunkStride, Bool& chunkHasRanges,
180  DataRanges& chunkRanges, Bool& chunkIsIncludeRanges,
181  Bool& chunkHasMask, uInt& chunkMaskStride, Bool& chunkHasWeights
182  );
183 
184  // used for unthreaded methods
185  void initLoopVars(
186  DataIterator& chunkData, uInt64& chunkCount, uInt& chunkStride,
187  Bool& chunkHasRanges, DataRanges& chunkRanges,
188  Bool& chunkIsIncludeRanges, Bool& chunkHasMask, MaskIterator& chunkMask,
189  uInt& chunkMaskStride, Bool& chunkHasWeights,
190  WeightsIterator& chunkWeights
191  );
192 
193  const ChunkData& initLoopVars();
194 
195  void initThreadVars(
196  uInt& nBlocks, uInt64& extra, uInt& nthreads,
197  std::unique_ptr<DataIterator[]>& dataIter, std::unique_ptr<MaskIterator[]>& maskIter,
198  std::unique_ptr<WeightsIterator[]>& weightsIter, std::unique_ptr<uInt64[]>& offset,
199  uInt nThreadsMax
200  ) const;
201 
202  void reset();
203 
204  void resetIDataset() { _idataset = 0; }
205 
206  // <group>
207  // setdata() clears any current datasets or data provider and then adds the
208  // specified data set as the first dataset in the (possibly new) set of data
209  // sets for which statistics are to be calculated. See addData() for
210  // parameter meanings.
211  void setData(
212  const DataIterator& first, uInt nr, uInt dataStride=1,
213  Bool nrAccountsForStride=False
214  );
215 
216  void setData(
217  const DataIterator& first, uInt nr, const DataRanges& dataRanges,
218  Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False
219  );
220 
221  void setData(
222  const DataIterator& first, const MaskIterator& maskFirst, uInt nr,
223  uInt dataStride=1, Bool nrAccountsForStride=False, uInt maskStride=1
224  );
225 
226  void setData(
227  const DataIterator& first, const MaskIterator& maskFirst,
228  uInt nr, const DataRanges& dataRanges, Bool isInclude=True,
229  uInt dataStride=1, Bool nrAccountsForStride=False, uInt maskStride=1
230  );
231 
232  void setData(
233  const DataIterator& first, const WeightsIterator& weightFirst,
234  uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False
235  );
236 
237  void setData(
238  const DataIterator& first, const WeightsIterator& weightFirst,
239  uInt nr, const DataRanges& dataRanges, Bool isInclude=True,
240  uInt dataStride=1, Bool nrAccountsForStride=False
241  );
242 
243  void setData(
244  const DataIterator& first, const WeightsIterator& weightFirst,
245  const MaskIterator& maskFirst, uInt nr, uInt dataStride=1,
246  Bool nrAccountsForStride=False, uInt maskStride=1
247  );
248 
249  void setData(
250  const DataIterator& first, const WeightsIterator& weightFirst,
251  const MaskIterator& maskFirst, uInt nr, const DataRanges& dataRanges,
252  Bool isInclude=True, uInt dataStride=1, Bool nrAccountsForStride=False,
253  uInt maskStride=1
254  );
255  // </group>
256 
257  // instead of setting and adding data "by hand", set the data provider that
258  // will provide all the data sets. Calling this method will clear any other
259  // data sets that have previously been set or added.
261 
262 private:
263  std::vector<DataIterator> _data{};
264  // maps data to weights. maps are used rather than vectors because only some
265  // (or none) of the data sets in the _data vector may have associated
266  // weights, masks, and/or ranges.
267  std::map<uInt, WeightsIterator> _weights{};
268  // maps data to masks
269  std::map<uInt, MaskIterator> _masks{};
270  std::vector<Int64> _counts{};
271  std::vector<uInt> _dataStrides{};
272  std::map<uInt, uInt> _maskStrides{};
273  std::map<uInt, Bool> _isIncludeRanges{};
274  std::map<uInt, DataRanges> _dataRanges{};
276 
278  typename std::vector<DataIterator>::const_iterator _dend{}, _diter{};
279  std::vector<Int64>::const_iterator _citer{};
280  std::vector<uInt>::const_iterator _dsiter{};
282  ChunkData _chunk;
283 
284  void _throwIfDataProviderDefined() const;
285 };
286 
287 }
288 
289 #ifndef CASACORE_NO_AUTO_TEMPLATES
290 #include <casacore/scimath/StatsFramework/StatisticsDataset.tcc>
291 #endif
292 
293 #endif
void initThreadVars(uInt &nBlocks, uInt64 &extra, uInt &nthreads, std::unique_ptr< DataIterator[]> &dataIter, std::unique_ptr< MaskIterator[]> &maskIter, std::unique_ptr< WeightsIterator[]> &weightsIter, std::unique_ptr< uInt64[]> &offset, uInt nThreadsMax) const
std::vector< DataIterator > _data
std::vector< DataIterator >::const_iterator _dend
const ChunkData & initLoopVars()
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
std::vector< DataIterator >::const_iterator _diter
void addData(const DataIterator &first, uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False)
Add a dataset to an existing set of datasets on which statistics are to be calculated.
const std::vector< Int64 > & getCounts() const
get data counts associated with the underlying data sets
Representation of a statistics dataset used in statistics framework calculatations.
const StatsDataProvider< CASA_STATP > * getDataProvider() const
StatisticsDataset< CASA_STATP > & operator=(const StatisticsDataset< CASA_STATP > &other)
use copy semantics, except for the data provider which uses reference semantics
void incrementThreadIters(DataIterator &dataIter, MaskIterator &maskIter, WeightsIterator &weightsIter, uInt64 &offset, uInt nthreads) const
PtrHolder< WeightsIterator > weights
associated weights.
struct Node * first
Definition: malloc.h:330
void setData(const DataIterator &first, uInt nr, uInt dataStride=1, Bool nrAccountsForStride=False)
setdata() clears any current datasets or data provider and then adds the specified data set as the fi...
unsigned long long uInt64
Definition: aipsxtype.h:39
PtrHolder(const PtrHolder< T > &other)
std::map< uInt, DataRanges > _dataRanges
std::vector< uInt > _dataStrides
Hold and delete pointers not deleted by object destructors.
Definition: PtrHolder.h:81
std::vector< Int64 > _counts
Bool empty() const
returns ! dataProvider &amp;&amp; _data.empty()
std::map< uInt, MaskIterator > _masks
maps data to masks
holds information about a data chunk.
PtrHolder< std::pair< MaskIterator, uInt > > mask
associated mask.
#define DataRanges
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
StatsDataProvider< CASA_STATP > * _dataProvider
std::map< uInt, uInt > _maskStrides
uInt64 count
total number of points
const Bool False
Definition: aipstype.h:44
std::map< uInt, WeightsIterator > _weights
maps data to weights.
void _throwIfDataProviderDefined() const
std::vector< uInt >::const_iterator _dsiter
void setDataProvider(StatsDataProvider< CASA_STATP > *dataProvider)
instead of setting and adding data &quot;by hand&quot;, set the data provider that will provide all the data se...
std::vector< Int64 >::const_iterator _citer
Bool increment(Bool includeIDataset)
std::map< uInt, Bool > _isIncludeRanges
PtrHolder< std::pair< DataRanges, Bool > > ranges
associated ranges.
StatsDataProvider< CASA_STATP > * getDataProvider()
const Bool True
Definition: aipstype.h:43
unsigned int uInt
Definition: aipstype.h:51