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 <RobotAPI/interface/observers/ObserverFilters.h>
26 
30 
31 #include <algorithm>
32 
33 namespace armarx::filters
34 {
35 
37  public MatrixMaxFilterBase,
38  public DatafieldFilter
39  {
40  public:
42  {
43  this->windowFilterSize = 1;
44  }
45 
46  VariantBasePtr 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  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
60  {
61  ParameterTypeList result;
62  result.push_back(VariantType::MatrixFloat);
63  return result;
64  }
65  };
66 
68  public MatrixMinFilterBase,
69  public DatafieldFilter
70  {
71  public:
73  {
74  this->windowFilterSize = 1;
75  }
76 
77  VariantBasePtr calculate(const Ice::Current&) const override
78  {
79  std::unique_lock lock(historyMutex);
80 
81  if (dataHistory.size() == 0)
82  {
83  return new Variant(new MatrixFloat(1, 1));
84  }
85 
86  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
87  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
88  return new Variant(matrix->toEigen().minCoeff());
89  }
90  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
91  {
92  ParameterTypeList result;
93  result.push_back(VariantType::MatrixFloat);
94  return result;
95  }
96  };
97 
99  public MatrixAvgFilterBase,
100  public DatafieldFilter
101  {
102  public:
104  {
105  this->windowFilterSize = 1;
106  }
107 
108  VariantBasePtr calculate(const Ice::Current&) const override
109  {
110  std::unique_lock lock(historyMutex);
111 
112  if (dataHistory.size() == 0)
113  {
114  return new Variant(new MatrixFloat(1, 1));
115  }
116 
117  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
118  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
119  return new Variant(matrix->toEigen().mean());
120  }
121  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
122  {
123  ParameterTypeList result;
124  result.push_back(VariantType::MatrixFloat);
125  return result;
126  }
127  };
128 
130  public MatrixPercentileFilterBase,
131  public DatafieldFilter
132  {
133  public:
135  {
136  this->windowFilterSize = 1;
137  }
138  MatrixPercentileFilter(float percentile)
139  {
140  this->percentile = percentile;
141  this->windowFilterSize = 1;
142  }
143 
144  VariantBasePtr calculate(const Ice::Current&) const override
145  {
146  std::unique_lock lock(historyMutex);
147 
148  if (dataHistory.size() == 0)
149  {
150  return new Variant(new MatrixFloat(1, 1));
151  }
152 
153  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
154  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
155  std::vector<float> vector = matrix->toVector();
156  std::sort(vector.begin(), vector.end());
157  return new Variant(GetPercentile(vector, percentile));
158  }
159  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
160  {
161  ParameterTypeList result;
162  result.push_back(VariantType::MatrixFloat);
163  return result;
164  }
165 
166  static float GetPercentile(const std::vector<float>& sortedData, float percentile)
167  {
168  if (sortedData.size() == 0)
169  {
170  throw LocalException("GetPercentile not possible for empty vector");
171  }
172 
173  float indexf = (sortedData.size() - 1) * percentile;
174  indexf = std::max(0.f, std::min(sortedData.size() - 1.f, indexf));
175  int index = (int)indexf;
176  float f = indexf - index;
177 
178  if (index == (int)sortedData.size() - 1)
179  {
180  return sortedData.at(sortedData.size() - 1);
181  }
182 
183  return sortedData.at(index) * (1 - f) + sortedData.at(index + 1) * f;
184  }
185  };
186 
188  public MatrixPercentilesFilterBase,
189  public DatafieldFilter
190  {
191  public:
193  {
194  this->windowFilterSize = 1;
195  this->percentiles = 10;
196  }
197  MatrixPercentilesFilter(int percentiles)
198  {
199  this->percentiles = percentiles;
200  this->windowFilterSize = 1;
201  }
202 
203  VariantBasePtr calculate(const Ice::Current&) const override
204  {
205  std::unique_lock lock(historyMutex);
206 
207  if (dataHistory.size() == 0)
208  {
209  ARMARX_IMPORTANT_S << "no data";
210  return new Variant(new MatrixFloat(1, 1));
211  }
212 
213  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
214  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
215  std::vector<float> vector = matrix->toVector();
216  std::sort(vector.begin(), vector.end());
217  std::vector<float> result;
218  result.push_back(vector.at(0));
219 
220  for (int i = 1; i < percentiles; i++)
221  {
222  result.push_back(MatrixPercentileFilter::GetPercentile(vector, 1.f / percentiles * i));
223  }
224 
225  result.push_back(vector.at(vector.size() - 1));
226  return new Variant(new MatrixFloat(1, result.size(), result));
227  }
228  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
229  {
230  ParameterTypeList result;
231  result.push_back(VariantType::MatrixFloat);
232  return result;
233  }
234  };
235 
237  public MatrixCumulativeFrequencyFilterBase,
238  public DatafieldFilter
239  {
240  public:
242  {
243  this->windowFilterSize = 1;
244  }
245  MatrixCumulativeFrequencyFilter(float min, float max, int bins)
246  {
247  this->min = min;
248  this->max = max;
249  this->bins = bins;
250  this->windowFilterSize = 1;
251  }
252  VariantBasePtr calculate(const Ice::Current&) const override
253  {
254  std::unique_lock lock(historyMutex);
255 
256  if (dataHistory.size() == 0)
257  {
258  return new Variant(new MatrixFloat(1, 1));
259  }
260 
261  VariantPtr currentValue = VariantPtr::dynamicCast(dataHistory.rbegin()->second);
262  MatrixFloatPtr matrix = MatrixFloatPtr::dynamicCast(currentValue->get<MatrixFloat>());
263  std::vector<float> vector = matrix->toVector();
264  std::sort(vector.begin(), vector.end());
265  std::vector<int> result = Calculate(vector, min, max, bins);
266  std::vector<float> resultF;
267 
268  for (int v : result)
269  {
270  resultF.push_back(v);
271  }
272 
273  return new Variant(new MatrixFloat(1, resultF.size(), resultF));
274  }
275  ParameterTypeList getSupportedTypes(const Ice::Current&) const override
276  {
277  ParameterTypeList result;
278  result.push_back(VariantType::MatrixFloat);
279  return result;
280  }
281  static std::vector<int> Calculate(const std::vector<float>& sortedData, float min, float max, int bins)
282  {
283  std::vector<int> result;
284  float val = min;
285  int nr = 0;
286  int lastCount = 0;
287 
288  for (size_t i = 0; i < sortedData.size(); i++)
289  {
290  if (sortedData.at(i) > val && nr < bins)
291  {
292  result.push_back(i);
293  nr++;
294  val = min + (max - min) * nr / bins;
295  lastCount = i;
296  }
297  }
298 
299  while ((int)result.size() < bins)
300  {
301  result.push_back(lastCount);
302  }
303 
304  return result;
305  }
306 
307  };
308 }
309 
310 
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
ARMARX_IMPORTANT_S
#define ARMARX_IMPORTANT_S
Definition: Logging.h:203
armarx::DatafieldFilter
The DatafieldFilter class is the base class for all filters and filter implementation should derive f...
Definition: DatafieldFilter.h:45
armarx::filters::MatrixPercentileFilter::MatrixPercentileFilter
MatrixPercentileFilter(float percentile)
Definition: MatrixFilters.h:138
armarx::filters::MatrixPercentilesFilter::MatrixPercentilesFilter
MatrixPercentilesFilter()
Definition: MatrixFilters.h:192
armarx::filters::MatrixPercentileFilter
Definition: MatrixFilters.h:129
index
uint8_t index
Definition: EtherCATFrame.h:59
MatrixVariant.h
armarx::filters::MatrixMaxFilter
Definition: MatrixFilters.h:36
armarx::filters::MatrixPercentileFilter::GetPercentile
static float GetPercentile(const std::vector< float > &sortedData, float percentile)
Definition: MatrixFilters.h:166
armarx::filters::MatrixMaxFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:59
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
armarx::filters::MatrixMaxFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:46
armarx::filters::MatrixCumulativeFrequencyFilter
Definition: MatrixFilters.h:236
armarx::filters::MatrixCumulativeFrequencyFilter::Calculate
static std::vector< int > Calculate(const std::vector< float > &sortedData, float min, float max, int bins)
Definition: MatrixFilters.h:281
armarx::filters::MatrixCumulativeFrequencyFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:275
armarx::filters::MatrixPercentileFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:144
armarx::filters::MatrixAvgFilter::MatrixAvgFilter
MatrixAvgFilter()
Definition: MatrixFilters.h:103
armarx::filters::MatrixPercentileFilter::MatrixPercentileFilter
MatrixPercentileFilter()
Definition: MatrixFilters.h:134
armarx::filters::MatrixPercentileFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:159
armarx::filters::MatrixCumulativeFrequencyFilter::MatrixCumulativeFrequencyFilter
MatrixCumulativeFrequencyFilter(float min, float max, int bins)
Definition: MatrixFilters.h:245
armarx::filters::MatrixPercentilesFilter
Definition: MatrixFilters.h:187
IceInternal::Handle<::armarx::VariantBase >
armarx::filters
Definition: AverageFilter.h:31
armarx::filters::MatrixPercentilesFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:203
armarx::filters::MatrixAvgFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:121
armarx::filters::MatrixMinFilter::MatrixMinFilter
MatrixMinFilter()
Definition: MatrixFilters.h:72
DatafieldFilter.h
armarx::MatrixFloat
The MatrixFloat class.
Definition: MatrixVariant.h:48
armarx::filters::MatrixMinFilter
Definition: MatrixFilters.h:67
armarx::filters::MatrixCumulativeFrequencyFilter::MatrixCumulativeFrequencyFilter
MatrixCumulativeFrequencyFilter()
Definition: MatrixFilters.h:241
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::filters::MatrixPercentilesFilter::MatrixPercentilesFilter
MatrixPercentilesFilter(int percentiles)
Definition: MatrixFilters.h:197
armarx::filters::MatrixCumulativeFrequencyFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:252
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::filters::MatrixMaxFilter::MatrixMaxFilter
MatrixMaxFilter()
Definition: MatrixFilters.h:41
armarx::filters::MatrixAvgFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:108
armarx::DatafieldFilter::dataHistory
TimeVariantBaseMap dataHistory
Definition: DatafieldFilter.h:79
armarx::VariantType::MatrixFloat
const VariantTypeId MatrixFloat
Definition: MatrixVariant.h:36
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::DatafieldFilter::historyMutex
std::mutex historyMutex
Definition: DatafieldFilter.h:80
armarx::filters::MatrixPercentilesFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:228
armarx::filters::MatrixAvgFilter
Definition: MatrixFilters.h:98
armarx::filters::MatrixMinFilter::getSupportedTypes
ParameterTypeList getSupportedTypes(const Ice::Current &) const override
Definition: MatrixFilters.h:90
Logging.h
min
T min(T t1, T t2)
Definition: gdiam.h:42
armarx::filters::MatrixMinFilter::calculate
VariantBasePtr calculate(const Ice::Current &) const override
Definition: MatrixFilters.h:77