KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mcmcda_data.h
Go to the documentation of this file.
1 /* =========================================================================== *
2  |
3  | Copyright (c) 1994-2011 by Kobus Barnard (author)
4  |
5  | Personal and educational use of this code is granted, provided that this
6  | header is kept intact, and that the authorship is not misrepresented, that
7  | its use is acknowledged in publications, and relevant papers are cited.
8  |
9  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
10  |
11  | Please note that the code in this file has not necessarily been adequately
12  | tested. Naturally, there is no guarantee of performance, support, or fitness
13  | for any particular task. Nonetheless, I am interested in hearing about
14  | problems that you encounter.
15  |
16  | Author: Ernesto Brau
17  * =========================================================================== */
18 
19 #ifndef MCMCDA_DATA_H_INCLUDED
20 #define MCMCDA_DATA_H_INCLUDED
21 
22 #include <vector>
23 #include <set>
24 #include <string>
25 #include <algorithm>
26 #include <iterator>
27 #include <cmath>
28 #include <iostream>
29 #include <functional>
30 #include <l_cpp/l_exception.h>
31 #include <m_cpp/m_vector.h>
32 #include <m_cpp/m_matrix.h>
33 #include <boost/function.hpp>
34 #include <boost/bind.hpp>
35 #include <boost/ref.hpp>
36 #include <fstream>
37 
38 namespace kjb {
39 namespace mcmcda {
40 
49 template<class Element>
50 class Data : private std::vector<std::set<Element> >
51 {
52 public:
53  typedef boost::function1<Vector, const Element&> Convert;
54 
55 private:
56  typedef std::set<Element> E_set;
57  typedef std::vector<E_set> Parent;
58 
59 public:
61  Data() {}
62 
68  template<class Iterator>
69  Data(Iterator first, Iterator last) : Parent(first, last) {}
70 
71  // make these public for use
72  using Parent::empty;
73  using Parent::begin;
74  using Parent::end;
75  using Parent::clear;
76  using Parent::size;
77  using Parent::resize;
78  using Parent::reserve;
79  using Parent::push_back;
80  using Parent::operator[];
81  using Parent::operator=;
82  typedef typename Parent::iterator iterator;
83  typedef typename Parent::const_iterator const_iterator;
84 
88  void read(const std::vector<std::string>& filenames)
89  {
90  IFT(filenames.size() > 1, Illegal_argument,
91  "Read data from file: must have at least two time steps.");
92 
93  clear();
94  resize(filenames.size());
95  std::transform(filenames.begin(), filenames.end(), begin(),
96  boost::bind(&Data<Element>::read_single_time, this, _1));
97  }
98 
102  void write(const std::vector<std::string>& filenames) const;
103 
108  virtual E_set read_single_time(const std::string& /*filename*/) const
109  {
110  KJB_THROW_2(Not_implemented, "read_single_time: not defined in general");
111  return E_set();
112  }
113 
118  virtual void write_single_time(const E_set&, const std::string&) const
119  {
121  "write_single_time: not defined in general");
122  }
123 
128  bool is_completely_empty() const
129  {
130  using namespace std;
131 
132  E_set empty_set;
133  const_iterator eset_p = find_if(
134  begin(),
135  end(),
136  boost::bind(
137  not_equal_to<E_set>(),
138  _1,
139  boost::cref(empty_set)));
140 
141  return eset_p == end();
142  }
143 
148  std::set<const Element*> neighborhood
149  (
150  const Element& y,
151  int t,
152  int d,
153  int d_bar,
154  double v_bar,
155  double sg,
156  const Convert& to_vector
157  ) const;
158 
165  (
166  const Element& y,
167  int t,
168  int d,
169  int d_bar,
170  double v_bar,
171  double sg,
172  const Convert& to_vector
173  ) const;
174 };
175 
180 template<class Element>
181 inline bool is_neighbor
182 (
183  const Element& y,
184  const Element& y_p,
185  int d,
186  int d_bar,
187  double v_bar,
188  double sg,
189  const typename Data<Element>::Convert& to_vector
190 )
191 {
192  if(std::abs(d) > d_bar)
193  {
194  return false;
195  }
196 
197  if(vector_distance(to_vector(y), to_vector(y_p))
198  <= std::abs(d)*v_bar + 2*sqrt(sg))
199  {
200  return true;
201  }
202 
203  return false;
204 }
205 
206 /*============================================================================*
207  * MEMBER FUNCTION DEFINITIONS *
208  *----------------------------------------------------------------------------*/
209 
210 template<class Element>
211 void Data<Element>::write(const std::vector<std::string>& filenames) const
212 {
213  const size_t sz = size();
214  IFT(filenames.size() == sz, Illegal_argument,
215  "Write data to file: wrong number of files.");
216 
217  for(size_t t = 0; t < sz; t++)
218  {
219  write_single_time((*this)[t], filenames[t]);
220  }
221 }
222 
223 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
224 
225 template<class Element>
226 std::set<const Element*> Data<Element>::neighborhood
227 (
228  const Element& y,
229  int t,
230  int d,
231  int d_bar,
232  double v_bar,
233  double sg,
234  const Convert& to_vector
235 ) const
236 {
237  std::set<const Element*> hood;
238 
239  IFTD(t >= 0, Illegal_argument,
240  "neighborhood: t=%d cannot be negative.", (t));
241 
242  if(t + d >= static_cast<int>(size()) || t + d < 1 || std::abs(d) > d_bar)
243  {
244  return hood;
245  }
246  const E_set& Y_t = (*this)[t - 1 + d];
247  for(typename E_set::const_iterator p = Y_t.begin();
248  p != Y_t.end();
249  p++)
250  {
251  if(is_neighbor(y, *p, d, d_bar, v_bar, sg, to_vector))
252  {
253  hood.insert(&(*p));
254  }
255  }
256 
257  return hood;
258 }
259 
260 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
261 
262 template<class Element>
264 (
265  const Element& y,
266  int t,
267  int d,
268  int d_bar,
269  double v_bar,
270  double sg,
271  const Convert& to_vector
272 ) const
273 {
274  int hood_size = 0;
275 
276  IFTD(t >= 0, Illegal_argument,
277  "neighborhood: t=%d cannot be negative.", (t));
278 
279  if(t + d >= static_cast<int>(size()) || t + d < 0 || std::abs(d) > d_bar)
280  {
281  return hood_size;
282  }
283 
284  const E_set& Y_t = (*this)[t - 1 + d];
285  for(typename E_set::const_iterator p = Y_t.begin();
286  p != Y_t.end();
287  p++)
288  {
289  if(is_neighbor(y, *p, d, d_bar, v_bar, sg, to_vector))
290  {
291  hood_size++;
292  }
293  }
294 
295  return hood_size;
296 }
297 
298 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
299 
301 template<>
302 inline
303 std::set<Vector> Data<Vector>::read_single_time(const std::string& filename) const
304 {
305  std::set<Vector> data_t;
306  Matrix M(filename);
307  M.get_all_rows(std::inserter(data_t, data_t.begin()));
308 
309  return data_t;
310 }
311 
312 /* \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ */
313 
315 template<>
316 inline
318 (
319  const E_set& data_t,
320  const std::string& filename
321 ) const
322 {
323  std::ofstream ofs(filename.c_str());
324  if(ofs.fail())
325  {
326  KJB_THROW_3(IO_error, "can't open file %s", (filename.c_str()));
327  }
328 
329  std::copy(data_t.begin(), data_t.end(),
330  std::ostream_iterator<Vector>(ofs, "\n"));
331 }
332 
333 }} //namespace kjb::mcmcda
334 
335 #endif /*MCMCDA_DATA_H_INCLUDED */
336 
double vector_distance(const Int_vector &op1, const Int_vector &op2)
Compute the Euclidian distance between two vectors.
Definition: l_int_vector.h:1569
virtual E_set read_single_time(const std::string &) const
Read data elements from a single time-step, contained in a single file.
Definition: mcmcda_data.h:108
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
A class that holds data for the tracking problem.
Definition: mcmcda_data.h:50
int neighborhood_size(const Element &y, int t, int d, int d_bar, double v_bar, double sg, const Convert &to_vector) const
Computes the size of the neighborhood of a point. This should be faster than computing the neighborho...
Definition: mcmcda_data.h:264
Parent::iterator iterator
Definition: mcmcda_data.h:82
#define IFT(a, ex, msg)
Definition: l_exception.h:101
virtual void write_single_time(const E_set &, const std::string &) const
Read data elements from a single time-step, contained in a single file.
Definition: mcmcda_data.h:118
kjb_c::Pixel abs(const kjb_c::Pixel &p)
Take the channel-wise absolute value of a kjb_c::Pixel.
Definition: i_pixel.h:354
std::set< const Element * > neighborhood(const Element &y, int t, int d, int d_bar, double v_bar, double sg, const Convert &to_vector) const
Computes the neighborhood of a point. See MCMCDA paper for details.
Definition: mcmcda_data.h:227
boost::function1< Vector, const Element & > Convert
Definition: mcmcda_data.h:53
Data(Iterator first, Iterator last)
Constructor from sequence.
Definition: mcmcda_data.h:69
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void read(const std::vector< std::string > &filenames)
Reads data from files with given names.
Definition: mcmcda_data.h:88
#define KJB_THROW_3(ex, fmt, params)
Definition: l_exception.h:56
Parent::const_iterator const_iterator
Definition: mcmcda_data.h:83
bool is_neighbor(const Element &y, const Element &y_p, int d, int d_bar, double v_bar, double sg, const typename Data< Element >::Convert &to_vector)
Returns true if given point is a neighbor of second given point.
Definition: mcmcda_data.h:182
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
Object thrown when attempting to use unimplemented functionality.
Definition: l_exception.h:281
bool is_completely_empty() const
Checks whether this data set is completely empty.
Definition: mcmcda_data.h:128
Object thrown when input or output fails.
Definition: l_exception.h:496
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Support for error handling exception classes in libKJB.
#define IFTD(a, ex, msg, params)
Definition: l_exception.h:112
OutputIterator get_all_rows(OutputIterator result) const
Return all rows of this matrix into the provided iterator.
Definition: m_matrix.h:1656
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void write(const std::vector< std::string > &filenames) const
Write data to files with given names.
Definition: mcmcda_data.h:211
Data()
Empty constructor.
Definition: mcmcda_data.h:61