Mantid
Loading...
Searching...
No Matches
TimeSeriesProperty.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2007 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9//----------------------------------------------------------------------
10// Includes
11//----------------------------------------------------------------------
13#include "MantidKernel/DllConfig.h"
16
18#include <cstdint>
19#include <utility>
20
21namespace Mantid {
22namespace Kernel {
23class DataItem;
24class SplittingInterval;
25
27
28//=========================================================================
32struct MANTID_KERNEL_DLL TimeSeriesPropertyStatistics {
34 double minimum;
36 double maximum;
38 double mean;
40 double median;
44 double time_mean;
48 double duration;
49
51
58 constexpr double nan{std::numeric_limits<double>::quiet_NaN()};
59 minimum = stats.minimum;
60 maximum = stats.maximum;
61 median = stats.median;
62 mean = stats.mean;
63 standard_deviation = stats.standard_deviation;
64 time_mean = nan;
65 time_standard_deviation = nan;
66 duration = nan;
67 }
68
75 minimum = value;
76 maximum = value;
77 mean = value;
78 median = value;
79 standard_deviation = 0.0;
80 time_mean = value;
81 time_standard_deviation = 0.0;
82 duration = std::numeric_limits<double>::quiet_NaN();
83 }
84
85 void setAllToNan() {
86 constexpr double nan{std::numeric_limits<double>::quiet_NaN()};
87 minimum = nan;
88 maximum = nan;
89 mean = nan;
90 median = nan;
91 standard_deviation = nan;
92 time_mean = nan;
93 time_standard_deviation = nan;
94 duration = nan;
95 }
96};
97
98//================================================================================================
102template <class TYPE> class TimeValueUnit {
103private:
104 Types::Core::DateAndTime m_time;
106
107public:
108 TimeValueUnit(const Types::Core::DateAndTime &time, const TYPE &value) : m_time(time), m_value(value) {}
109
110 ~TimeValueUnit() = default;
111
112 bool operator>(const TimeValueUnit &rhs) { return (m_time > rhs.m_time); }
113
114 friend bool operator>(const TimeValueUnit &lhs, const TimeValueUnit &rhs) { return (lhs.m_time > rhs.m_time); }
115
116 bool operator==(const TimeValueUnit &rhs) { return (m_time == rhs.m_time); }
117
118 friend bool operator==(const TimeValueUnit &lhs, const TimeValueUnit &rhs) { return (lhs.m_time == rhs.m_time); }
119
120 bool operator<(const TimeValueUnit &rhs) { return (m_time < rhs.m_time); }
121
122 friend bool operator<(const TimeValueUnit &lhs, const TimeValueUnit &rhs) { return (lhs.m_time < rhs.m_time); }
123
124 Types::Core::DateAndTime time() const { return m_time; }
125
126 void setTime(Types::Core::DateAndTime newtime) { m_time = newtime; }
127
128 const TYPE &value() const { return m_value; }
129
130 static bool valueCmp(const TimeValueUnit &lhs, const TimeValueUnit &rhs) { return (lhs.m_value < rhs.m_value); }
131};
132//========================================================================================================
133
137template <typename TYPE> class DLLExport TimeSeriesProperty : public Property, public ITimeSeriesProperty {
138public:
140 explicit TimeSeriesProperty(const std::string &name);
141 TimeSeriesProperty(const std::string &name, const std::vector<Types::Core::DateAndTime> &times,
142 const std::vector<TYPE> &values);
143
146
147private:
149 TimeSeriesProperty(const Property *const p);
150
151public:
153 TimeSeriesProperty<TYPE> *clone() const override;
154
156 Property *cloneInTimeROI(const TimeROI &timeROI) const override;
157 //
160 std::unique_ptr<TimeSeriesProperty<double>> getDerivative() const;
161
162 void saveProperty(Nexus::File *file) override;
163 Json::Value valueAsJson() const override;
164
166 Property *cloneWithTimeShift(const double timeShift) const override;
168 size_t getMemorySize() const override;
170 TimeSeriesProperty<TYPE> &merge(Property *rhs) override;
171
172 //--------------------------------------------------------------------------------------
176 virtual bool operator==(const TimeSeriesProperty<TYPE> &right) const;
178 virtual bool operator!=(const TimeSeriesProperty<TYPE> &right) const;
180 virtual bool operator==(const Property &right) const;
182 virtual bool operator!=(const Property &right) const;
183
185 void setName(const std::string &name);
186
187 // Remove time series values according to TimeROI
188 void removeDataOutsideTimeROI(const TimeROI &timeRoi) override;
189
191 void makeFilterByValue(std::vector<SplittingInterval> &split, double min, double max, double TimeTolerance = 0.0,
192 bool centre = false) const override;
194 TimeROI makeFilterByValue(double min, double max, bool expand = false,
195 const TimeInterval &expandRange = TimeInterval(0, 1), double TimeTolerance = 0.0,
196 bool centre = false, const TimeROI *existingROI = nullptr) const override;
198 void expandFilterToRange(std::vector<SplittingInterval> &split, double min, double max,
199 const TimeInterval &range) const override;
203 std::pair<double, double> timeAverageValueAndStdDev(const Kernel::TimeROI *timeRoi = nullptr) const override;
205 double timeAverageValue(const TimeROI *timeRoi = nullptr) const override;
207 void histogramData(const Types::Core::DateAndTime &tMin, const Types::Core::DateAndTime &tMax,
208 std::vector<double> &counts) const;
209
212 std::map<Types::Core::DateAndTime, TYPE> valueAsCorrectMap() const;
214 std::vector<TYPE> valuesAsVector() const;
217 std::multimap<Types::Core::DateAndTime, TYPE> valueAsMultiMap() const;
219 virtual std::vector<TYPE> filteredValuesAsVector(const Kernel::TimeROI *roi) const;
220 // overload method rather than default value so python bindings work
221 virtual std::vector<TYPE> filteredValuesAsVector() const;
222
224 std::vector<Types::Core::DateAndTime> timesAsVector() const override;
225
227 std::vector<double> timesAsVectorSeconds() const;
229 std::vector<double> timesAsVectorSeconds(Types::Core::DateAndTime start) const;
230
232 virtual std::vector<Types::Core::DateAndTime> filteredTimesAsVector(const Kernel::TimeROI *roi) const;
233 // overload method rather than default value so python bindings work
234 virtual std::vector<Types::Core::DateAndTime> filteredTimesAsVector() const;
236 void addValue(const Types::Core::DateAndTime &time, const TYPE &value);
238 void addValue(const std::string &time, const TYPE &value);
240 void addValue(const std::time_t &time, const TYPE &value);
243 void addValues(const std::vector<Types::Core::DateAndTime> &times, const std::vector<TYPE> &values);
245 void replaceValues(const std::vector<Types::Core::DateAndTime> &times, const std::vector<TYPE> &values);
246
248 Types::Core::DateAndTime lastTime() const;
250 TYPE firstValue() const;
252 TYPE firstValue(const Kernel::TimeROI &roi) const;
254 Types::Core::DateAndTime firstTime() const;
256 TYPE lastValue() const;
258 TYPE lastValue(const Kernel::TimeROI &roi) const;
260 double durationInSeconds(const Kernel::TimeROI *roi = nullptr) const;
261
263 TYPE minValue() const;
265 TYPE maxValue() const;
267 double mean() const;
268
270 int size() const override;
272 int realSize() const override;
273
274 // ==== The following functions are specific to the odd mechanism of
275 // FilterByLogValue =========
277 std::string value() const override;
279 std::vector<std::string> time_tValue() const;
281 std::map<Types::Core::DateAndTime, TYPE> valueAsMap() const;
282 // ============================================================================================
283
285 std::string setValue(const std::string &) override;
287 std::string setValueFromJson(const Json::Value &) override;
289 std::string setDataItem(const std::shared_ptr<DataItem> &) override;
290
292 void clear() override;
294 void clearOutdated() override;
296 void create(const Types::Core::DateAndTime &start_time, const std::vector<double> &time_sec,
297 const std::vector<TYPE> &new_values);
299 void create(const std::vector<Types::Core::DateAndTime> &new_times, const std::vector<TYPE> &new_values);
300
302 TYPE getSingleValue(const Types::Core::DateAndTime &t) const;
304 TYPE getSingleValue(const Types::Core::DateAndTime &t, int &index) const;
305
307 virtual TimeInterval nthInterval(int n) const;
309 virtual TYPE nthValue(int n) const;
311 virtual Types::Core::DateAndTime nthTime(int n) const;
312
313 // Returns whether the time series has been filtered
314 bool isFiltered() const override { return false; }
315
317 void countSize() const;
318
320 static bool isTimeString(const std::string &str);
321
323 std::string isValid() const override;
325 std::string getDefault() const override;
327 bool isDefault() const override;
328
330 TimeSeriesPropertyStatistics getStatistics(const Kernel::TimeROI *roi = nullptr) const override;
331
337 double extractStatistic(Math::StatisticType selection, const TimeROI *roi = nullptr) const override;
338
341 void eliminateDuplicates();
342
344 std::string toString() const;
345
349 void reserve(size_t size) { m_values.reserve(size); };
350
352 virtual std::vector<Mantid::Kernel::TimeInterval> getTimeIntervals() const;
353
354private:
356 double averageValueInFilter(const std::vector<TimeInterval> &filter) const;
358 std::pair<double, double> averageAndStdDevInFilter(const std::vector<TimeInterval> &intervals) const;
359 void createFilteredData(const TimeROI &timeROI, std::vector<TimeValueUnit<TYPE>> &filteredData) const;
360
361protected:
362 //----------------------------------------------------------------------------------------------
364 void saveTimeVector(Nexus::File *file);
366 void sortIfNecessary() const;
368 int findIndex(Types::Core::DateAndTime t) const;
370 int upperBound(Types::Core::DateAndTime t, int istart, int iend) const;
375 Types::Core::DateAndTime getFakeEndTime() const;
376
378 std::string setValueFromProperty(const Property &right) override;
379
381 mutable std::vector<TimeValueUnit<TYPE>> m_values;
382
385 mutable int m_size;
386
389};
390
391} // namespace Kernel
392} // namespace Mantid
std::string name
Definition Run.cpp:60
const std::vector< double > & rhs
double value
The value of the point.
Definition FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
double right
MANTID_NEXUS_DLL bool operator!=(std::string const &s, Mantid::Nexus::NexusAddress const &p)
MANTID_NEXUS_DLL bool operator==(std::string const &s, Mantid::Nexus::NexusAddress const &p)
boost::python::list getTimeIntervals(const TimeROI &self)
Definition TimeROI.cpp:19
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
Definition System.h:37
A non-templated interface to a TimeSeriesProperty.
Base class for properties.
Definition Property.h:94
Represents a time interval.
Definition DateAndTime.h:25
TimeROI : Object that holds information about when the time measurement was active.
Definition TimeROI.h:18
A specialised Property class for holding a series of time-value pairs.
TimeSeriesSortStatus m_propSortedFlag
Flag to state whether mP is sorted or not.
~TimeSeriesProperty() override
Virtual destructor.
int m_size
The number of values (or time intervals) in the time series.
std::vector< TimeValueUnit< TYPE > > m_values
Holds the time series data.
void reserve(size_t size)
Reserve memory for efficient adding values to existing property makes sense only when you have reason...
Class to hold unit value (DateAndTime, T)
bool operator>(const TimeValueUnit &rhs)
TimeValueUnit(const Types::Core::DateAndTime &time, const TYPE &value)
friend bool operator==(const TimeValueUnit &lhs, const TimeValueUnit &rhs)
friend bool operator<(const TimeValueUnit &lhs, const TimeValueUnit &rhs)
void setTime(Types::Core::DateAndTime newtime)
bool operator==(const TimeValueUnit &rhs)
Types::Core::DateAndTime time() const
friend bool operator>(const TimeValueUnit &lhs, const TimeValueUnit &rhs)
static bool valueCmp(const TimeValueUnit &lhs, const TimeValueUnit &rhs)
bool operator<(const TimeValueUnit &rhs)
Types::Core::DateAndTime m_time
MatrixWorkspace_sptr MANTID_API_DLL operator+=(const MatrixWorkspace_sptr &lhs, const MatrixWorkspace_sptr &rhs)
Adds two workspaces.
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
StatisticType
Maps a "statistic" to a number.
Definition Statistics.h:18
std::string toString(const T &value)
Convert values to strings.
Statistics getStatistics(const std::vector< TYPE > &data, const unsigned int flags=StatOptions::AllStats)
Return a statistics object for the given data set.
Helper class which provides the Collimation Length for SANS instruments.
Simple struct to store statistics.
Definition Statistics.h:35
double mean
Mean value.
Definition Statistics.h:41
double median
Median value.
Definition Statistics.h:43
double minimum
Minimum value.
Definition Statistics.h:37
double maximum
Maximum value.
Definition Statistics.h:39
double standard_deviation
standard_deviation of the values
Definition Statistics.h:45
Struct holding some useful statistics for a TimeSeriesProperty.
double time_standard_deviation
time weighted standard deviation
double standard_deviation
standard_deviation of the values
TimeSeriesPropertyStatistics(const Statistics &stats)
Initialize only the statistics that are time-weighted independent.
TimeSeriesPropertyStatistics(double value)
Statistics of a value that is constant throughout time.