MatrixFilters.h
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Simon Ottenhaus ( simon.ottenhaus at kit dot edu)
18* @date 2015
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#pragma once
24
25#include <algorithm>
26
31
32#include <RobotAPI/interface/observers/ObserverFilters.h>
33
34namespace armarx::filters
35{
36
37 class MatrixMaxFilter : public MatrixMaxFilterBase, public DatafieldFilter
38 {
39 public:
41 {
42 this->windowFilterSize = 1;
43 }
44
46 calculate(const Ice::Current&) const override
47 {
48 std::unique_lock lock(historyMutex);
49
50 if (dataHistory.size() == 0)
51 {
52 return new Variant(new MatrixFloat(1, 1));
53 }
54
55 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
56 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
57 return new Variant(matrix->toEigen().maxCoeff());
58 }
59
60 ParameterTypeList
61 getSupportedTypes(const Ice::Current&) const override
62 {
63 ParameterTypeList result;
64 result.push_back(VariantType::MatrixFloat);
65 return result;
66 }
67 };
68
69 class MatrixMinFilter : public MatrixMinFilterBase, public DatafieldFilter
70 {
71 public:
73 {
74 this->windowFilterSize = 1;
75 }
76
78 calculate(const Ice::Current&) const override
79 {
80 std::unique_lock lock(historyMutex);
81
82 if (dataHistory.size() == 0)
83 {
84 return new Variant(new MatrixFloat(1, 1));
85 }
86
87 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
88 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
89 return new Variant(matrix->toEigen().minCoeff());
90 }
91
92 ParameterTypeList
93 getSupportedTypes(const Ice::Current&) const override
94 {
95 ParameterTypeList result;
96 result.push_back(VariantType::MatrixFloat);
97 return result;
98 }
99 };
100
101 class MatrixAvgFilter : public MatrixAvgFilterBase, public DatafieldFilter
102 {
103 public:
105 {
106 this->windowFilterSize = 1;
107 }
108
110 calculate(const Ice::Current&) const override
111 {
112 std::unique_lock lock(historyMutex);
113
114 if (dataHistory.size() == 0)
115 {
116 return new Variant(new MatrixFloat(1, 1));
117 }
118
119 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
120 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
121 return new Variant(matrix->toEigen().mean());
122 }
123
124 ParameterTypeList
125 getSupportedTypes(const Ice::Current&) const override
126 {
127 ParameterTypeList result;
128 result.push_back(VariantType::MatrixFloat);
129 return result;
130 }
131 };
132
133 class MatrixPercentileFilter : public MatrixPercentileFilterBase, public DatafieldFilter
134 {
135 public:
137 {
138 this->windowFilterSize = 1;
139 }
140
141 MatrixPercentileFilter(float percentile)
142 {
143 this->percentile = percentile;
144 this->windowFilterSize = 1;
145 }
146
148 calculate(const Ice::Current&) const override
149 {
150 std::unique_lock lock(historyMutex);
151
152 if (dataHistory.size() == 0)
153 {
154 return new Variant(new MatrixFloat(1, 1));
155 }
156
157 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
158 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
159 std::vector<float> vector = matrix->toVector();
160 std::sort(vector.begin(), vector.end());
161 return new Variant(GetPercentile(vector, percentile));
162 }
163
164 ParameterTypeList
165 getSupportedTypes(const Ice::Current&) const override
166 {
167 ParameterTypeList result;
168 result.push_back(VariantType::MatrixFloat);
169 return result;
170 }
171
172 static float
173 GetPercentile(const std::vector<float>& sortedData, float percentile)
174 {
175 if (sortedData.size() == 0)
176 {
177 throw LocalException("GetPercentile not possible for empty vector");
178 }
179
180 float indexf = (sortedData.size() - 1) * percentile;
181 indexf = std::max(0.f, std::min(sortedData.size() - 1.f, indexf));
182 int index = (int)indexf;
183 float f = indexf - index;
184
185 if (index == (int)sortedData.size() - 1)
186 {
187 return sortedData.at(sortedData.size() - 1);
188 }
189
190 return sortedData.at(index) * (1 - f) + sortedData.at(index + 1) * f;
191 }
192 };
193
194 class MatrixPercentilesFilter : public MatrixPercentilesFilterBase, public DatafieldFilter
195 {
196 public:
198 {
199 this->windowFilterSize = 1;
200 this->percentiles = 10;
201 }
202
203 MatrixPercentilesFilter(int percentiles)
204 {
205 this->percentiles = percentiles;
206 this->windowFilterSize = 1;
207 }
208
210 calculate(const Ice::Current&) const override
211 {
212 std::unique_lock lock(historyMutex);
213
214 if (dataHistory.size() == 0)
215 {
216 ARMARX_IMPORTANT_S << "no data";
217 return new Variant(new MatrixFloat(1, 1));
218 }
219
220 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
221 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
222 std::vector<float> vector = matrix->toVector();
223 std::sort(vector.begin(), vector.end());
224 std::vector<float> result;
225 result.push_back(vector.at(0));
226
227 for (int i = 1; i < percentiles; i++)
228 {
229 result.push_back(
230 MatrixPercentileFilter::GetPercentile(vector, 1.f / percentiles * i));
231 }
232
233 result.push_back(vector.at(vector.size() - 1));
234 return new Variant(new MatrixFloat(1, result.size(), result));
235 }
236
237 ParameterTypeList
238 getSupportedTypes(const Ice::Current&) const override
239 {
240 ParameterTypeList result;
241 result.push_back(VariantType::MatrixFloat);
242 return result;
243 }
244 };
245
247 public MatrixCumulativeFrequencyFilterBase,
248 public DatafieldFilter
249 {
250 public:
252 {
253 this->windowFilterSize = 1;
254 }
255
257 {
258 this->min = min;
259 this->max = max;
260 this->bins = bins;
261 this->windowFilterSize = 1;
262 }
263
265 calculate(const Ice::Current&) const override
266 {
267 std::unique_lock lock(historyMutex);
268
269 if (dataHistory.size() == 0)
270 {
271 return new Variant(new MatrixFloat(1, 1));
272 }
273
274 VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
275 MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
276 std::vector<float> vector = matrix->toVector();
277 std::sort(vector.begin(), vector.end());
278 std::vector<int> result = Calculate(vector, min, max, bins);
279 std::vector<float> resultF;
280
281 for (int v : result)
282 {
283 resultF.push_back(v);
284 }
285
286 return new Variant(new MatrixFloat(1, resultF.size(), resultF));
287 }
288
289 ParameterTypeList
290 getSupportedTypes(const Ice::Current&) const override
291 {
292 ParameterTypeList result;
293 result.push_back(VariantType::MatrixFloat);
294 return result;
295 }
296
297 static std::vector<int>
298 Calculate(const std::vector<float>& sortedData, float min, float max, int bins)
299 {
300 std::vector<int> result;
301 float val = min;
302 int nr = 0;
303 int lastCount = 0;
304
305 for (size_t i = 0; i < sortedData.size(); i++)
306 {
307 if (sortedData.at(i) > val && nr < bins)
308 {
309 result.push_back(i);
310 nr++;
311 val = min + (max - min) * nr / bins;
312 lastCount = i;
313 }
314 }
315
316 while ((int)result.size() < bins)
317 {
318 result.push_back(lastCount);
319 }
320
321 return result;
322 }
323 };
324} // namespace armarx::filters
uint8_t index
TimeVariantBaseMap dataHistory
The MatrixFloat class.
The Variant class is described here: Variants.
Definition Variant.h:224
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
static std::vector< int > Calculate(const std::vector< float > &sortedData, float min, float max, int bins)
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
MatrixCumulativeFrequencyFilter(float min, float max, int bins)
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
static float GetPercentile(const std::vector< float > &sortedData, float percentile)
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
VariantBasePtr calculate(const Ice::Current &) const override
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:210
const VariantTypeId MatrixFloat
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< MatrixFloat > MatrixFloatPtr
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)