CEDRIC  Revision_backup-2009-02
EventMap.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef EVENTMAP_H
10 #define EVENTMAP_H
11 
12 #include <Redukt/CMDS.h>
14 
15 #include <Util/ProgressIndicator.h>
16 
17 #include <boost/numeric/ublas/matrix.hpp>
18 #include <boost/numeric/ublas/vector.hpp>
19 
20 #include <map>
21 #include <vector>
22 
23 #include <iomanip>
24 #include <iostream>
25 #include <fstream>
26 
27 #include "EventGroundDistances.h"
28 
29 
30 
31 namespace ublas = boost::numeric::ublas;
32 
33 
34 class EventMap;
37 
38 
39 
48 class EventMap
49 {
50 public:
51  typedef ublas::matrix<double> Matrix;
52  typedef ublas::vector<double> Vector;
54 
56  {
59  virtual void operator () ( EventSignatureSet& in, EventSignatureSet& out )=0;
60  };
61 
63  struct Coordinate
64  {
66  Coordinate( double x_, double y_ ): x(x_), y(y_) {}
67  double x, y;
68  };
69 
70  enum Progress {
74  STEP_MDS = 95,
75  STEP_STRESS = 100,
76  NUM_STEPS = 100
77  };
78 
79  void print( Matrix A, std::ostream& os = std::cout )
80  {
81  unsigned int n = (unsigned int)A.size1(),
82  m = (unsigned int)A.size2();
83  for( unsigned int i=0; i < n; ++i )
84  {
85  for( unsigned int j=0; j < m; ++j )
86  {
87  os << std::setw(12) << A(i,j);
88  }
89  os << std::endl;
90  }
91  }
92 
93 public:
95  : m_grounddistance( gd ),
96  m_progressIndicator( 0 )
97  {}
98 
100  EventMap( EventMap& other );
101 
103  void create( EventSignatureSet& signatures, SignatureSampler& sel );
104 
107  {
108  m_progressIndicator = pi;
109  }
110 
112  bool wasCanceled() const
113  {
114  if( m_progressIndicator )
115  return m_progressIndicator->wasCanceled();
116  return false;
117  }
118 
120  int numCoordinates() const
121  {
122  return m_points.size1();
123  }
124 
127  int phast2mapIndex( unsigned int phastIndex ) const
128  {
129  //### replace with std::find()
130  for( unsigned int i=0; i < m_indexmap.size(); ++i )
131  if( m_indexmap[i] == (int)phastIndex )
132  return i;
133 
134  return -1; // not found
135  }
136 
138  int getIndex( unsigned int i ) const
139  {
140  assert( i < m_indexmap.size() );
141  return m_indexmap[i];
142  }
143 
145  Coordinate getCoordinate( unsigned int i ) const
146  {
147  assert( i < m_points.size1() );
148 
149  const Vector& row = ublas::row( m_points, i );
150 
151  return Coordinate(row[0],row[1]);
152  }
153 
154 
156  double getDissimilarity( unsigned int i, unsigned int j ) const
157  {
158  assert( (i < m_delta.size1()) && (j < m_delta.size2()) );
159 
160  return m_delta(i,j);
161  }
162 
164  double getRawStress( unsigned int i )
165  {
166  assert( (int)i < numCoordinates() );
167  return m_rawStress[i];
168  }
169 
170  double getKruskalPointStress( unsigned int i )
171  {
172  assert( (int)i < numCoordinates() );
173  return m_kruskalPointStress[i];
174  }
175 
176  double stressMean() const { return m_stressMean; }
177  double stressVar() const { return m_stressVar; }
178  double stressNorm() const { return m_stressNorm; }
179 
180  double stressKruskal() const { return m_kruskalStress; }
181 
183  double minRawStress();
185  double maxRawStress();
186 
187  /*### OBSOLETE ?
188  double getNormStress(int index)
189  {
190  assert(index < numCoordinates());
191  return m_normStress[index];
192  }
193  */
194 
195  //const Matrix& distanceMatrix() const { return m_delta; }
196  Matrix& distanceMatrix() { return m_delta; }
197 
199  double dist(Coordinate c1, Coordinate c2) const
200  {
201  return sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y));
202  }
203 
204 
205 protected:
206 
208 
209  bool progress( int step );
210 
211  void calculateStress();
212 
214  double calculateKruskalStress() const;
215 
217  double calculatePointStress(int index,PointStressType type) const;
218 
219 private:
220  // copy assignment not possible because const referenced members exist
221  EventMap& operator = ( EventMap& ) { return *this; }
222 
223  std::vector<int> m_indexmap;
224  EventSignatureSet m_subset;
225  Matrix m_delta;
226  Matrix m_points;
227 
228  //~ enum PointStressType { PointStressKruskal, PointStressBort };
229  //~ std::map<PointStressType,std::vector<double> > m_pointStress;
230 
231  std::vector<double> m_rawStress;
232  std::vector<double> m_kruskalPointStress;
233 
234  double m_kruskalStress;
235 
236  double m_stressNorm;
237  double m_stressMean;
238  double m_stressVar;
239 
240  //### OBSOLETE ?
241  //std::vector<double> m_normStress; ///> unit variance normalized stress per point
242  //double m_stressVar; ///> for stress normalization
243 
244  const GroundDistance<EventFeature>& m_grounddistance;
245 
246  ProgressIndicator* m_progressIndicator;
247 };
248 
249 
258 {
259 public:
260  SignatureRandomSample( unsigned int sampleSize ): m_sampleSize(sampleSize) {}
261 
263 
264 private:
265  unsigned int m_sampleSize;
266 };
267 
268 
269 
278 {
279 public:
280  SignaturePivotSample( unsigned int k, double radius=0.0 )
281  : m_k(k),
282  m_radius(radius),
283  m_sizeOut(-1),
284  m_sizeIn(-1)
285  {}
286 
288 
289  float fractionVisible() const { return m_fractionVisible; }
290 
291  double radius() const { return m_radius; }
292 
293  int sizeOut() const { return m_sizeOut; }
294  int sizeIn() const { return m_sizeIn; }
295 
296 private:
297  unsigned int m_k;
298  double m_radius;
299  float m_fractionVisible;
300  int m_sizeOut, m_sizeIn;
301 };
302 
303 #endif // EVENTMAP_H
Definition: EventMap.h:257
double maxRawStress()
Return maximum stress.
Definition: EventMap.cpp:356
ublas::matrix< double > Matrix
Definition: EventMap.h:51
double stressVar() const
Definition: EventMap.h:177
Definition: EventMap.h:73
double y
Definition: EventMap.h:67
C++ Template Wrapper for original Ansi C code from Yossi Rubner.
Definition: EarthMoversDistance.h:69
Coordinate()
Definition: EventMap.h:65
double minRawStress()
Return minimum stress.
Definition: EventMap.cpp:362
void create(EventSignatureSet &signatures, SignatureSampler &sel)
Create map for subset of signatures computed with the SignatureSampler sel.
Definition: EventMap.cpp:72
virtual void operator()(EventSignatureSet &in, EventSignatureSet &out)=0
Make a subselection of "in" in "out".
Coordinate(double x_, double y_)
Definition: EventMap.h:66
double x
Definition: EventMap.h:67
EarthMoversDistance< EventSignature, EventFeature > EMD
Definition: EventMap.h:53
void operator()(EventSignatureSet &in, EventSignatureSet &out)
Make a subselection of "in" in "out".
Definition: EventMap.cpp:375
int getIndex(unsigned int i) const
Return the Phast index of co-ordinate i.
Definition: EventMap.h:138
SignatureRandomSample(unsigned int sampleSize)
Definition: EventMap.h:260
Definition: EventMap.h:277
bool wasCanceled() const
Return true if create() was cancelled via the progress indicator.
Definition: EventMap.h:112
void setProgressIndicator(ProgressIndicator *pi)
Specify the progress indicator to use.
Definition: EventMap.h:106
int phast2mapIndex(unsigned int phastIndex) const
Definition: EventMap.h:127
double stressKruskal() const
Definition: EventMap.h:180
double getDissimilarity(unsigned int i, unsigned int j) const
Return dissimilarity from point i to point j.
Definition: EventMap.h:156
int sizeIn() const
Definition: EventMap.h:294
double getRawStress(unsigned int i)
Return stress for coordinate i.
Definition: EventMap.h:164
PointStressType
Definition: EventMap.h:207
SignaturePivotSample(unsigned int k, double radius=0.0)
Definition: EventMap.h:280
void calculateStress()
Definition: EventMap.cpp:234
Definition: EventMap.h:48
double dist(Coordinate c1, Coordinate c2) const
Euclidean distance (needed for stress calculation)
Definition: EventMap.h:199
double getKruskalPointStress(unsigned int i)
Definition: EventMap.h:170
int numCoordinates() const
Return the number of co-ordinates in map.
Definition: EventMap.h:120
double radius() const
Definition: EventMap.h:291
bool progress(int step)
Definition: EventMap.cpp:39
void operator()(EventSignatureSet &in, EventSignatureSet &out)
Make a subselection of "in" in "out".
Definition: EventMap.cpp:416
void print(Matrix A, std::ostream &os=std::cout)
Definition: EventMap.h:79
Matrix & distanceMatrix()
Definition: EventMap.h:196
int sizeOut() const
Definition: EventMap.h:293
Definition: EventMap.h:75
A co-ordinate is represented as 2D point (x,y) on the map.
Definition: EventMap.h:63
Definition: EventMap.h:76
Interface for distance functions to use with EarthMoversDistance.
Definition: EarthMoversDistance.h:14
std::vector< EventSignature > EventSignatureSet
Definition: EventSignature.h:21
Definition: EventMap.h:207
Definition: EventMap.h:72
bool wasCanceled() const
Definition: ProgressIndicator.h:108
double calculatePointStress(int index, PointStressType type) const
Calculate raw point stress.
Definition: EventMap.cpp:310
float fractionVisible() const
Definition: EventMap.h:289
double calculateKruskalStress() const
Calculate overall Kruskal stress.
Definition: EventMap.cpp:291
Coordinate getCoordinate(unsigned int i) const
Return Coordinate i.
Definition: EventMap.h:145
Definition: EventMap.h:71
Progress
Definition: EventMap.h:70
Definition: EventMap.h:55
ublas::vector< double > Vector
Definition: EventMap.h:52
double stressMean() const
Definition: EventMap.h:176
double stressNorm() const
Definition: EventMap.h:178
Interface for a polling-based progress indicator (e.g. progress bar)
Definition: ProgressIndicator.h:37
EventMap(const GroundDistance< EventFeature > &gd)
Definition: EventMap.h:94
Definition: EventMap.h:74
Definition: EventMap.h:207