forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFilter.cpp
171 lines (153 loc) · 5.78 KB
/
LogFilter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidKernel/LogFilter.h"
#include "MantidKernel/TimeSeriesProperty.h"
namespace Mantid {
namespace Kernel {
/**
* Constructor taking a reference to a filter. Note that constructing a
* LogFilter this
* way only allows filters to be combined. They will not affect a property
* @param filter :: A reference to a TimeSeriesProperty<bool> that will act as a
* filter
*/
LogFilter::LogFilter(const TimeSeriesProperty<bool> &filter) : m_prop(), m_filter() { addFilter(filter); }
/**
* Constructor
* @param prop :: Pointer to property to be filtered. Its actual type must be
* TimeSeriesProperty<double>
*/
LogFilter::LogFilter(const Property *prop) : m_prop(), m_filter() { m_prop.reset(convertToTimeSeriesOfDouble(prop)); }
/**
* / Constructor from a TimeSeriesProperty<double> object to avoid overhead of
* casts
*/
LogFilter::LogFilter(const TimeSeriesProperty<double> *timeSeries) : m_prop(), m_filter() {
m_prop.reset(timeSeries->clone());
}
/**
* Filter using a TimeSeriesProperty<bool>. True values mark the allowed time
* intervals.
* The object is cloned
* @param filter :: Filtering mask
*/
void LogFilter::addFilter(const TimeSeriesProperty<bool> &filter) {
if (filter.size() == 0)
return;
if (!m_filter || m_filter->size() == 0)
m_filter.reset(filter.clone());
else {
auto filterProperty = std::make_unique<TimeSeriesProperty<bool>>("tmp");
TimeSeriesProperty<bool> *filter1 = m_filter.get();
auto filter2 = std::unique_ptr<TimeSeriesProperty<bool>>(filter.clone());
TimeInterval time1 = filter1->nthInterval(filter1->size() - 1);
TimeInterval time2 = filter2->nthInterval(filter2->size() - 1);
if (time1.begin() < time2.begin()) {
filter1->addValue(time2.begin(),
true); // should be f1->lastValue, but it doesnt
// matter for boolean AND
} else if (time2.begin() < time1.begin()) {
filter2->addValue(time1.begin(), true);
}
int i = 0;
int j = 0;
time1 = filter1->nthInterval(i);
time2 = filter2->nthInterval(j);
// Make the two filters start at the same time. An entry is added at the
// beginning
// of the filter that starts later to equalise their staring times. The new
// interval will have
// value opposite to the one it started with originally.
if (time1.begin() > time2.begin()) {
filter1->addValue(time2.begin(), !filter1->nthValue(i));
time1 = filter1->nthInterval(i);
} else if (time2.begin() > time1.begin()) {
filter2->addValue(time1.begin(), !filter2->nthValue(j));
time2 = filter2->nthInterval(j);
}
for (;;) {
TimeInterval time3;
time3 = time1.intersection(time2);
if (time3.isValid()) {
filterProperty->addValue(time3.begin(), (filter1->nthValue(i) && filter2->nthValue(j)));
}
if (time1.end() < time2.end()) {
i++;
} else if (time2.end() < time1.end()) {
j++;
} else {
i++;
j++;
}
if (i == filter1->size() || j == filter2->size())
break;
time1 = filter1->nthInterval(i);
time2 = filter2->nthInterval(j);
}
filterProperty->clearFilter();
m_filter = std::move(filterProperty);
}
if (m_prop) {
m_prop->clearFilter();
m_prop->filterWith(m_filter.get());
}
}
//-------------------------------------------------------------------------------------------------
/// Clears filters
void LogFilter::clear() {
if (m_prop)
m_prop->clearFilter();
m_filter.reset();
}
//--------------------------------------------------------------------------------------------------
// Private methods
//--------------------------------------------------------------------------------------------------
namespace {
template <typename SrcType> struct ConvertToTimeSeriesDouble {
static TimeSeriesProperty<double> *apply(const Property *prop) {
auto srcTypeSeries = dynamic_cast<const TimeSeriesProperty<SrcType> *>(prop);
if (!srcTypeSeries)
return nullptr;
auto converted = new TimeSeriesProperty<double>(prop->name());
auto pmap = srcTypeSeries->valueAsMap();
for (auto it = pmap.begin(); it != pmap.end(); ++it) {
converted->addValue(it->first, double(it->second));
}
return converted;
}
};
/// Specialization for a double so that it just clones the input
template <> struct ConvertToTimeSeriesDouble<double> {
static TimeSeriesProperty<double> *apply(const Property *prop) {
auto doubleSeries = dynamic_cast<const TimeSeriesProperty<double> *>(prop);
if (!doubleSeries)
return nullptr;
return doubleSeries->clone();
}
};
} // namespace
/**
* Converts the given property to a TimeSeriesProperty<double>, throws if
* invalid.
* @param prop :: A pointer to a property
* @return A new TimeSeriesProperty<double> object converted from the input.
* Throws std::invalid_argument if not possible
*/
TimeSeriesProperty<double> *LogFilter::convertToTimeSeriesOfDouble(const Property *prop) {
if (auto doubleSeries = ConvertToTimeSeriesDouble<double>::apply(prop)) {
return doubleSeries;
} else if (auto doubleSeries = ConvertToTimeSeriesDouble<int>::apply(prop)) {
return doubleSeries;
} else if (auto doubleSeries = ConvertToTimeSeriesDouble<bool>::apply(prop)) {
return doubleSeries;
} else {
throw std::invalid_argument("LogFilter::convertToTimeSeriesOfDouble - Cannot convert property, \"" + prop->name() +
"\", to double series.");
}
}
} // namespace Kernel
} // namespace Mantid