Mantid
Loading...
Searching...
No Matches
LogFilter.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
9
10namespace Mantid::Kernel {
18LogFilter::LogFilter(const TimeSeriesProperty<bool> &filter) : m_prop(), m_filter() { addFilter(filter); }
19
25LogFilter::LogFilter(const Property *prop) : m_prop(), m_filter() { m_prop.reset(convertToTimeSeriesOfDouble(prop)); }
26
31LogFilter::LogFilter(const TimeSeriesProperty<double> *timeSeries) : m_prop(), m_filter() {
32 m_prop.reset(timeSeries->clone());
33}
34
42 if (filter.size() == 0)
43 return;
44 if (!m_filter || m_filter->size() == 0)
45 m_filter.reset(filter.clone());
46 else {
47 auto filterProperty = std::make_unique<TimeSeriesProperty<bool>>("tmp");
48
49 TimeSeriesProperty<bool> *filter1 = m_filter.get();
50 auto filter2 = std::unique_ptr<TimeSeriesProperty<bool>>(filter.clone());
51
52 TimeInterval time1 = filter1->nthInterval(filter1->size() - 1);
53 TimeInterval time2 = filter2->nthInterval(filter2->size() - 1);
54
55 if (time1.begin() < time2.begin()) {
56 filter1->addValue(time2.begin(),
57 true); // should be f1->lastValue, but it doesnt
58 // matter for boolean AND
59 } else if (time2.begin() < time1.begin()) {
60 filter2->addValue(time1.begin(), true);
61 }
62
63 int i = 0;
64 int j = 0;
65
66 time1 = filter1->nthInterval(i);
67 time2 = filter2->nthInterval(j);
68
69 // Make the two filters start at the same time. An entry is added at the
70 // beginning
71 // of the filter that starts later to equalise their staring times. The new
72 // interval will have
73 // value opposite to the one it started with originally.
74 if (time1.begin() > time2.begin()) {
75 filter1->addValue(time2.begin(), !filter1->nthValue(i));
76 time1 = filter1->nthInterval(i);
77 } else if (time2.begin() > time1.begin()) {
78 filter2->addValue(time1.begin(), !filter2->nthValue(j));
79 time2 = filter2->nthInterval(j);
80 }
81
82 for (;;) {
83 TimeInterval time3;
84 time3 = time1.intersection(time2);
85 if (time3.isValid()) {
86 filterProperty->addValue(time3.begin(), (filter1->nthValue(i) && filter2->nthValue(j)));
87 }
88
89 if (time1.end() < time2.end()) {
90 i++;
91 } else if (time2.end() < time1.end()) {
92 j++;
93 } else {
94 i++;
95 j++;
96 }
97
98 if (i == filter1->size() || j == filter2->size())
99 break;
100 time1 = filter1->nthInterval(i);
101 time2 = filter2->nthInterval(j);
102 }
103 filterProperty->clearFilter();
104 m_filter = std::move(filterProperty);
105 }
106 if (m_prop) {
107 m_prop->clearFilter();
108 m_prop->filterWith(m_filter.get());
109 }
110}
111
112//-------------------------------------------------------------------------------------------------
115 if (m_prop)
116 m_prop->clearFilter();
117 m_filter.reset();
118}
119
120//--------------------------------------------------------------------------------------------------
121// Private methods
122//--------------------------------------------------------------------------------------------------
123namespace {
124template <typename SrcType> struct ConvertToTimeSeriesDouble {
125 static TimeSeriesProperty<double> *apply(const Property *prop) {
126 auto srcTypeSeries = dynamic_cast<const TimeSeriesProperty<SrcType> *>(prop);
127 if (!srcTypeSeries)
128 return nullptr;
129 auto converted = new TimeSeriesProperty<double>(prop->name());
130 auto pmap = srcTypeSeries->valueAsMap();
131 for (auto it = pmap.begin(); it != pmap.end(); ++it) {
132 converted->addValue(it->first, double(it->second));
133 }
134 return converted;
135 }
136};
137
139template <> struct ConvertToTimeSeriesDouble<double> {
140 static TimeSeriesProperty<double> *apply(const Property *prop) {
141 auto doubleSeries = dynamic_cast<const TimeSeriesProperty<double> *>(prop);
142 if (!doubleSeries)
143 return nullptr;
144 return doubleSeries->clone();
145 }
146};
147} // namespace
148
157 if (auto doubleSeries = ConvertToTimeSeriesDouble<double>::apply(prop)) {
158 return doubleSeries;
159 } else if (auto doubleSeries = ConvertToTimeSeriesDouble<int>::apply(prop)) {
160 return doubleSeries;
161 } else if (auto doubleSeries = ConvertToTimeSeriesDouble<bool>::apply(prop)) {
162 return doubleSeries;
163 } else {
164 throw std::invalid_argument("LogFilter::convertToTimeSeriesOfDouble - Cannot convert property, \"" + prop->name() +
165 "\", to double series.");
166 }
167}
168
169} // namespace Mantid::Kernel
void clear()
Clears filters.
Definition: LogFilter.cpp:114
const TimeSeriesProperty< bool > * filter() const
Returns a reference to the filter.
Definition: LogFilter.h:52
TimeSeriesProperty< double > * convertToTimeSeriesOfDouble(const Property *prop)
Converts the given property to a TimeSeriesProperty<double>, throws if invalid.
Definition: LogFilter.cpp:156
void addFilter(const TimeSeriesProperty< bool > &filter)
Adds a filter using boolean AND.
Definition: LogFilter.cpp:41
std::unique_ptr< TimeSeriesProperty< double > > m_prop
Owned pointer to the filtered property.
Definition: LogFilter.h:62
std::unique_ptr< TimeSeriesProperty< bool > > m_filter
Owned pointer to the filter mask.
Definition: LogFilter.h:64
LogFilter()=delete
Disable default constructor.
Base class for properties.
Definition: Property.h:94
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
Represents a time interval.
Definition: DateAndTime.h:25
Types::Core::DateAndTime begin() const
Beginning of the interval.
Definition: DateAndTime.h:32
bool isValid() const
True if the interval is not empty.
Definition: DateAndTime.h:36
Types::Core::DateAndTime end() const
End of the interval.
Definition: DateAndTime.h:34
TimeInterval intersection(const TimeInterval &ti) const
Returns an intersection of two intervals.
Definition: DateAndTime.cpp:36
A specialised Property class for holding a series of time-value pairs.
TimeSeriesProperty< TYPE > * clone() const override
"Virtual" copy constructor
int size() const override
Returns the number of values at UNIQUE time intervals in the time series.
Generate a tableworkspace to store the calibration results.