trase
Data.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018, University of Oxford.
3 All rights reserved.
4 
5 University of Oxford means the Chancellor, Masters and Scholars of the
6 University of Oxford, having an administrative office at Wellington
7 Square, Oxford OX1 2JD, UK.
8 
9 This file is part of trase.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice, this
14  list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 * Neither the name of the copyright holder nor the names of its
19  contributors may be used to endorse or promote products derived from
20  this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
36 
37 #ifndef DATA_H_
38 #define DATA_H_
39 
40 #include <cassert>
41 #include <functional>
42 #include <map>
43 #include <memory>
44 #include <set>
45 #include <unordered_map>
46 #include <vector>
47 
48 #include "util/BBox.hpp"
49 #include "util/Colors.hpp"
50 #include "util/ColumnIterator.hpp"
51 #include "util/Exception.hpp"
52 
53 namespace trase {
54 
56 class RawData {
57  // raw data set, in row major order
58  std::vector<float> m_matrix;
59 
60  // sets for non-numeric string data
61  std::vector<std::set<std::string>> m_string_data;
62 
64  std::vector<float> m_tmp;
65 
66  int m_rows{0};
67  int m_cols{0};
68 
69 public:
71  int cols() const { return m_cols; };
72 
74  int rows() const { return m_rows; };
75 
78  template <typename T> void add_column(T new_col_begin, T new_col_end);
79 
82  template <typename T> void add_row(T new_row_begin, T new_row_end);
83 
86  template <typename T> void add_column(const std::vector<T> &new_col);
87 
90  template <typename T> void add_row(const std::vector<T> &new_row);
91 
94  template <typename T> void set_column(int i, const std::vector<T> &new_col);
95 
97  ColumnIterator begin(int i) const;
98 
100  ColumnIterator end(int i) const;
101 
105  const std::set<std::string> &string_data(int i) const;
106 
112  template <typename T>
113  std::map<T, std::shared_ptr<RawData>> facet(const std::vector<T> &data) const;
114 
120  template <typename T1, typename T2>
121  std::map<std::pair<T1, T2>, std::shared_ptr<RawData>>
122  facet(const std::vector<T1> &data1, const std::vector<T2> &data2) const;
123 };
124 
130 struct Aesthetic {
131  // total number of Aesthetics
132  static const int N = 9;
133 
136  using Limits = bbox<float, N - 4>;
137 
139  struct x {
140  static const int index = 0;
141  static const char *name;
142  static float to_display(float data, const Limits &data_lim,
143  const bfloat2_t &display_lim);
144  static float from_display(float display, const Limits &data_lim,
145  const bfloat2_t &display_lim);
146  };
147 
149  struct y {
150  static const int index = 1;
151  static const char *name;
152  static float to_display(float data, const Limits &data_lim,
153  const bfloat2_t &display_lim);
154  static float from_display(float display, const Limits &data_lim,
155  const bfloat2_t &display_lim);
156  };
157 
159  struct color {
160  static const int index = 2;
161  static const char *name;
162 
163  static float to_display(float data, const Limits &data_lim,
164  const bfloat2_t &display_lim);
165  static float from_display(float display, const Limits &data_lim,
166  const bfloat2_t &display_lim);
167  };
168 
171  struct size {
172  static const int index = 3;
173  static const char *name;
174 
175  static float to_display(float data, const Limits &data_lim,
176  const bfloat2_t &display_lim);
177  static float from_display(float display, const Limits &data_lim,
178  const bfloat2_t &display_lim);
179  };
180 
182  struct fill {
183  static const int index = 4;
184  static const char *name;
185 
186  static float to_display(float data, const Limits &data_lim,
187  const bfloat2_t &display_lim);
188  static float from_display(float display, const Limits &data_lim,
189  const bfloat2_t &display_lim);
190  };
191 
192  // NOTE: xmin,ymin,xmax,ymax need to go at end so that the indices for
193  // Limits work out
194 
196  struct xmin {
197  static const int index = 5;
198  static const char *name;
199  static float to_display(float data, const Limits &data_lim,
200  const bfloat2_t &display_lim);
201  static float from_display(float display, const Limits &data_lim,
202  const bfloat2_t &display_lim);
203  };
204 
206  struct ymin {
207  static const int index = 6;
208  static const char *name;
209  static float to_display(float data, const Limits &data_lim,
210  const bfloat2_t &display_lim);
211  static float from_display(float display, const Limits &data_lim,
212  const bfloat2_t &display_lim);
213  };
214 
216  struct xmax {
217  static const int index = 7;
218  static const char *name;
219  static float to_display(float data, const Limits &data_lim,
220  const bfloat2_t &display_lim);
221  static float from_display(float display, const Limits &data_lim,
222  const bfloat2_t &display_lim);
223  };
224 
226  struct ymax {
227  static const int index = 8;
228  static const char *name;
229  static float to_display(float data, const Limits &data_lim,
230  const bfloat2_t &display_lim);
231  static float from_display(float display, const Limits &data_lim,
232  const bfloat2_t &display_lim);
233  };
234 };
235 
239 
245  std::shared_ptr<RawData> m_data;
246 
248  std::unordered_map<int, int> m_map;
249 
251  Limits m_limits;
252 
253 public:
254  DataWithAesthetic() : m_data(std::make_shared<RawData>()) {}
255 
256  explicit DataWithAesthetic(std::shared_ptr<RawData> data)
257  : m_data(std::move(data)) {}
258 
259  DataWithAesthetic(std::shared_ptr<RawData> data,
260  const std::unordered_map<int, int> &map,
261  const Limits &limits)
262  : m_data(std::move(data)), m_map(map), m_limits(limits) {}
263 
266  template <typename Aesthetic> ColumnIterator begin() const;
267 
270  template <typename Aesthetic> ColumnIterator end() const;
271 
276  template <typename Aesthetic, typename T>
277  void set(const std::vector<T> &data);
278 
283  template <typename Aesthetic> void set(float min, float max);
284 
286  template <typename Aesthetic> bool has() const;
287 
289  int rows() const;
290 
292  int cols() const;
293 
295  const Limits &limits() const;
296 
297  template <typename T> DataWithAesthetic &x(const std::vector<T> &data);
298  DataWithAesthetic &x(float min, float max);
299 
300  template <typename T> DataWithAesthetic &y(const std::vector<T> &data);
301  DataWithAesthetic &y(float min, float max);
302 
303  template <typename T> DataWithAesthetic &color(const std::vector<T> &data);
304  DataWithAesthetic &color(float min, float max);
305 
306  template <typename T> DataWithAesthetic &size(const std::vector<T> &data);
307  DataWithAesthetic &size(float min, float max);
308 
309  template <typename T> DataWithAesthetic &fill(const std::vector<T> &data);
310  DataWithAesthetic &fill(float min, float max);
311 
312  template <typename T> DataWithAesthetic &xmin(const std::vector<T> &data);
313  DataWithAesthetic &xmin(float min, float max);
314 
315  template <typename T> DataWithAesthetic &ymin(const std::vector<T> &data);
316  DataWithAesthetic &ymin(float min, float max);
317 
318  template <typename T> DataWithAesthetic &xmax(const std::vector<T> &data);
319  DataWithAesthetic &xmax(float min, float max);
320 
321  template <typename T> DataWithAesthetic &ymax(const std::vector<T> &data);
322  DataWithAesthetic &ymax(float min, float max);
323 
329  template <typename T>
330  std::map<T, DataWithAesthetic> facet(const std::vector<T> &data) const;
331 
337  template <typename T1, typename T2>
338  std::map<std::pair<T1, T2>, DataWithAesthetic>
339  facet(const std::vector<T1> &data1, const std::vector<T2> &data2) const;
340 
341 private:
342  template <typename Aesthetic, typename T>
343  void calculate_limits(T begin, T end);
344 };
345 
348 DataWithAesthetic create_data();
349 
350 } // namespace trase
351 
352 #include "Data.tcc"
353 
354 #endif // DATA_H_
int cols() const
return the number of columns
Definition: Data.hpp:71
Contains the minimum and maximum extents of a hypercube in D dimensional space.
Definition: BBox.hpp:54
the color of each plotting element, scaled from 0 -> 1
Definition: Data.hpp:159
void add_column(T new_col_begin, T new_col_end)
add a new column to the matrix using begin/end iterators.
ColumnIterator end(int i) const
return a ColumnIterator to the end of column i
Definition: Data.cpp:57
bbox< float, N - 4 > Limits
all aethetics except for xmin,ymin,xmax,ymax have their own min/max bounds
Definition: Data.hpp:136
ColumnIterator begin(int i) const
return a ColumnIterator to the beginning of column i
Definition: Data.cpp:50
Aesthetics are a collection of tag classes that represent each aesthetic Each aesthetic has a name...
Definition: Data.hpp:130
Combination of the RawData class and Aesthetics, this class points to a RawData object, and contains a mapping from aesthetics to RawData column numbers.
Definition: Data.hpp:243
const std::set< std::string > & string_data(int i) const
return the set of strings for column i
Definition: Data.cpp:64
the fill color of each plotting element, scaled from 0 -> 1
Definition: Data.hpp:182
the maximum y coordinate of the data
Definition: Data.hpp:226
the maximum x coordinate of the data
Definition: Data.hpp:216
the size of each plotting element, scaled from 1 pixel to 1/20 size of y-axis
Definition: Data.hpp:171
void add_row(T new_row_begin, T new_row_end)
add a new row to the matrix using begin/end iterators.
the minimum y coordinate of the data
Definition: Data.hpp:206
std::map< T, std::shared_ptr< RawData > > facet(const std::vector< T > &data) const
facets the data based on the input data column
the minimum x coordinate of the data
Definition: Data.hpp:196
void set_column(int i, const std::vector< T > &new_col)
set a column in the matrix.
the data to display on the y-axis of the plot
Definition: Data.hpp:149
Raw data class, impliments a matrix with row major order.
Definition: Data.hpp:56
A const iterator that iterates through a single column of the raw data class Impliments an random acc...
Definition: ColumnIterator.hpp:45
Definition: Backend.cpp:39
the data to display on the x-axis of the plot
Definition: Data.hpp:139
int rows() const
return the number of rows
Definition: Data.hpp:74