Turi Create  4.0
heatmap.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 
7 #ifndef __TC_VIS_HEATMAP
8 #define __TC_VIS_HEATMAP
9 
10 #include <core/data/sframe/gl_sframe.hpp>
11 
12 #include "extrema.hpp"
13 #include "groupby.hpp"
14 #include <visualization/server/plot.hpp>
15 
16 namespace turi {
17 namespace visualization {
18 
19  class heatmap_result: public transformation_output,
20  public group_aggregate_value {
21  private:
22  std::vector<std::vector<flex_int>> bins; // row-major order
23 
24  // heatmap methods
25  void widen_x(double value);
26  void widen_y(double value);
27 
28  public:
29  bounding_box<double> extrema;
30 
31  heatmap_result();
32  void init(double xMin, double xMax, double yMin, double yMax);
33 
34  // group_aggregate_value methods
35  virtual group_aggregate_value* new_instance() const override;
36  virtual void add_element_simple(const flexible_type& flex) override;
37  virtual void combine(const group_aggregate_value& other) override;
38  virtual flexible_type emit() const override;
39  virtual bool support_type(flex_type_enum type) const override;
40  virtual std::string name() const override;
41  virtual void save(oarchive& oarc) const override;
42  virtual void load(iarchive& iarc) override;
43 
44  // transformation_output methods
45  virtual std::string vega_column_data(bool) const override;
46  };
47 
48  /*
49  * heatmap()
50  * Uses an optimal streaming histogram in 2-d to avoid
51  * the need for restarting when re-binning. Bins into a potentially wider
52  * range than the data (not to exceed 2x the range), and re-bins into the
53  * data range on get().
54  *
55  * dtype of sarrays can be flex_int or flex_float
56  * Heatmap always gives bins as flex_ints (bin counts are positive integers).
57  */
58  class heatmap : public groupby<heatmap_result> {
59  public:
60  virtual void init(const gl_sframe& source, size_t batch_size) override;
61  virtual std::vector<heatmap_result> split_input(size_t num_threads) override;
62  };
63 
64  std::shared_ptr<Plot> plot_heatmap(
65  const gl_sarray& x,
66  const gl_sarray& y,
67  const flexible_type& xlabel,
68  const flexible_type& ylabel,
69  const flexible_type& title);
70 
71 }}
72 
73 #endif