heatmap_data.h
Go to the documentation of this file.
1 
8 #pragma once
9 
10 #include "interop/util/assert.h"
11 #include "interop/util/exception.h"
16 
17 namespace illumina { namespace interop { namespace model { namespace plot
18 {
19 
22  class heatmap_data : public chart_data
23  {
24 
25  public:
27  heatmap_data() : m_data(0), m_num_columns(0), m_num_rows(0), m_free(false)
28  { }
30  virtual ~heatmap_data()
31  {
32  clear();
33  }
34 
35  public:
54  float at(const size_t row, const size_t col) const INTEROP_THROW_SPEC((model::index_out_of_bounds_exception))
55  {
56  INTEROP_BOUNDS_CHECK(row, m_num_rows, "Row Index out of bounds");
57  INTEROP_BOUNDS_CHECK(col, m_num_columns, "Column Index out of bounds");
58  const size_t idx = index_of(row, col);
59  INTEROP_ASSERT(idx < m_num_rows*m_num_columns);
60  INTEROP_ASSERT(m_data != 0);
61  return m_data[idx];
62  }
63 
71  {
72  INTEROP_BOUNDS_CHECK(idx, length(), "Index out of bounds");
73  INTEROP_ASSERT(m_data != 0);
74  return m_data[idx];
75  }
82  {
83  INTEROP_BOUNDS_CHECK(idx, length(), "Index out of bounds");
84  INTEROP_ASSERT(m_data != 0);
85  return m_data[idx];
86  }
87 
96  float operator()(const size_t row, const size_t col) const INTEROP_THROW_SPEC((model::index_out_of_bounds_exception))
97  {
98  INTEROP_BOUNDS_CHECK(row, m_num_rows, "Row Index out of bounds");
99  INTEROP_BOUNDS_CHECK(col, m_num_columns, "Column Index out of bounds");
100  const size_t idx = index_of(row, col);
101  INTEROP_ASSERT(idx < m_num_rows*m_num_columns);
102  INTEROP_ASSERT(m_data != 0);
103  return m_data[idx];
104  }
105 
114  float &operator()(const size_t row, const size_t col) INTEROP_THROW_SPEC((model::index_out_of_bounds_exception))
115  {
116  INTEROP_BOUNDS_CHECK(row, m_num_rows, "Row Index out of bounds");
117  INTEROP_BOUNDS_CHECK(col, m_num_columns, "Column Index out of bounds");
118  const size_t idx = index_of(row, col);
119  INTEROP_ASSERT(idx < m_num_rows*m_num_columns);
120  INTEROP_ASSERT(m_data != 0);
121  return m_data[idx];
122  }
123 
128  size_t row_count() const
129  {
130  return m_num_rows;
131  }
132 
137  size_t column_count() const
138  {
139  return m_num_columns;
140  }
141 
146  size_t length() const
147  {
148  return m_num_columns * m_num_rows;
149  }
154  bool empty()const
155  {
156  return length()==0;
157  }
160  public:
166  {
167  if(m_free) INTEROP_THROW( invalid_parameter, "Cannot use internal buffer map with external buffer");
168  if(empty()) INTEROP_THROW( invalid_parameter, "Cannot set external buffer to empty map");
169  m_data = data;
170  }
178  void set_buffer(float* data,
179  const size_t rows,
180  const size_t cols,
181  const float default_val=std::numeric_limits<float>::quiet_NaN())
182  {
183  if(m_free) delete[] m_data;
184  m_data = data;
185  m_num_columns = cols;
186  m_num_rows = rows;
187  m_free=false;
188  std::fill(data, data+length(), default_val);
189  }
196  void resize(const size_t rows, const size_t cols,
197  const float default_val=std::numeric_limits<float>::quiet_NaN())
198  {
199  if(rows != m_num_rows && cols != m_num_columns)
200  {
201  if (m_free) delete[] m_data;
202  m_data = new float[cols * rows];
203  m_num_columns = cols;
204  m_num_rows = rows;
205  m_free = true;
206  std::fill(m_data, m_data+length(), default_val);
207  }
208  }
209 
212  void clear()
213  {
214  if(m_free)
215  {
216  delete[] m_data;
217  m_data=0;
218  m_free = false;
219  }
220  m_num_columns = 0;
221  m_num_rows = 0;
223  }
230  size_t index_of(const size_t row, const size_t col) const
231  {
232  return row * m_num_columns + col;
233  }
234  friend std::ostream& operator<<(std::ostream& out, const heatmap_data& data)
235  {
236  out << static_cast<const chart_data&>(data);
237  out << data.m_num_columns << ",";
238  out << data.m_num_rows << ",";
239  io::table::write_csv(out, data.m_data, data.m_data+data.length(), ',');
240  return out;
241  }
242  friend std::istream& operator>>(std::istream& in, heatmap_data& data)
243  {
244  in >> static_cast<chart_data&>(data);
245  std::string tmp;
246  size_t col_count, row_count;
247  io::table::read_value<size_t>(in, col_count, tmp);
248  io::table::read_value<size_t>(in, row_count, tmp);
249  data.resize(row_count, col_count);
250  io::table::read_csv(in, data.m_data, data.m_data+data.length());
251  return in;
252  }
253 
254  private:
255  float* m_data;
256  size_t m_num_columns;
257  size_t m_num_rows;
258  bool m_free;
259  };
260 
261 }}}}
262 
float operator()(const size_t row, const size_t col) const INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:96
void set_buffer(float *data) INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:165
Definition: enum_description.h:15
void clear()
Definition: chart_data.h:110
#define INTEROP_THROW_SPEC(SPEC)
Definition: exception_specification.h:15
size_t column_count() const
Definition: heatmap_data.h:137
float operator[](const size_t idx) const INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:81
#define INTEROP_THROW(EXCEPTION, MESSAGE)
Definition: exception.h:18
void write_csv(std::ostream &out, I beg, I end, const char eol, const size_t precision=10)
Definition: csv_format.h:117
size_t index_of(const size_t row, const size_t col) const
Definition: heatmap_data.h:230
float & operator()(const size_t row, const size_t col) INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:114
void resize(const size_t rows, const size_t cols, const float default_val=std::numeric_limits< float >::quiet_NaN())
Definition: heatmap_data.h:196
#define INTEROP_ASSERT(TST)
Definition: assert.h:21
float at(const size_t row, const size_t col) const INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:54
void clear()
Definition: heatmap_data.h:212
bool empty() const
Definition: heatmap_data.h:154
size_t length() const
Definition: heatmap_data.h:146
heatmap_data()
Definition: heatmap_data.h:27
friend std::ostream & operator<<(std::ostream &out, const heatmap_data &data)
Definition: heatmap_data.h:234
float at(const size_t idx) const INTEROP_THROW_SPEC((model
Definition: heatmap_data.h:70
Definition: model_exceptions.h:50
void set_buffer(float *data, const size_t rows, const size_t cols, const float default_val=std::numeric_limits< float >::quiet_NaN())
Definition: heatmap_data.h:178
#define INTEROP_BOUNDS_CHECK(VALUE, RANGE, MESSAGE)
Definition: exception.h:24
virtual ~heatmap_data()
Definition: heatmap_data.h:30
void read_csv(std::istream &in, I beg, I end, const char delim=',')
Definition: csv_format.h:69
size_t row_count() const
Definition: heatmap_data.h:128
friend std::istream & operator>>(std::istream &in, heatmap_data &data)
Definition: heatmap_data.h:242