Empirical
histogram.h
Go to the documentation of this file.
1 #include "d3_init.h"
2 #include "dataset.h"
3 #include "base/vector.h"
4 #include "tools/tuple_struct.h"
5 #include "../js_utils.h"
6 
7 namespace D3 {
8 
9  struct HistogramBin {
10  EMP_BUILD_INTROSPECTIVE_TUPLE( int, x0,
11  int, x1,
12  int, length
13 
14  )
15  };
16 
17  class Histogram : public D3::D3_Base {
18  protected:
19  uint32_t value_fun_id = -1;
20  public:
22  EM_ASM_ARGS({js.objects[$0] = d3.histogram();}, this->id);
23  };
24 
26  emp::JSDelete(value_fun_id);
27  }
28 
29  Histogram& SetDomain(double x, double y) {
30  EM_ASM_ARGS({js.objects[$0].domain([$1, $2]);}, this->id, x, y);
31  return (*this);
32  }
33 
34  Histogram& Domain(double x, double y) {
35  return SetDomain(x, y);
36  }
37 
39  EM_ASM_ARGS({emp_i.__outgoing_array = js.objects[$0].domain();}
40  , this->id);
41  emp::array<double, 2> domain;
42  emp::pass_array_to_cpp(domain);
43  return domain;
44  }
45 
47  return GetDomain();
48  }
49 
50  Histogram& SetThresholds(int count) {
51  EM_ASM_ARGS({js.objects[$0].thresholds($1);}, this->id, count);
52  return (*this);
53  }
54 
55  Histogram& SetThresholds(std::string threshold_generator) {
56  EM_ASM_ARGS({
57  js.objects[$0].thresholds(Pointer_stringify($1));
58  }, this->id, threshold_generator.c_str());
59  return (*this);
60  }
61 
62  template <typename T>
63  Histogram& Thresholds(T thresh) {
64  return SetThresholds(thresh);
65  }
66 
67 
68  template <typename DATA_TYPE>
71  Dataset bins = Dataset();
72  EM_ASM_ARGS({
73  console.log(emp_i.__incoming_array);
74  js.objects[$1] = js.objects[$0](emp_i.__incoming_array);
75  console.log(js.objects[$1]);
76  }, this->id, bins.GetID());
77 
78  return bins;
79  }
80 
81  template <typename DATA_TYPE>
83  return Call(data);
84  }
85 
87  template <typename T>
89  SetValueAccessor(T func) {
90  value_fun_id = JSWrap(func, emp::to_string(id)+"_return_value");
91  EM_ASM_ARGS({
92  js.objects[$0].value(window["emp"][$0+"_return_value"]);
93  }, this->id);
94  return (*this);
95  }
97 
98  Histogram& SetValueAccessor(std::string func) {
99  D3_CALLBACK_METHOD_1_ARG(value, func.c_str());
100  return (*this);
101  }
102 
103  template <typename T>
104  Histogram& Value(T func) {
105  return SetValueAccessor(func);
106  }
107  };
108 }
Definition: array.h:42
Dataset operator()(emp::vector< DATA_TYPE > data)
Definition: histogram.h:82
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
REAL_TYPE sfinae_decoy
Definition: meta.h:93
Definition: dataset.h:23
Histogram & SetThresholds(std::string threshold_generator)
Definition: histogram.h:55
Definition: d3_init.h:43
Histogram()
Definition: histogram.h:21
~Histogram()
Definition: histogram.h:25
Histogram & Thresholds(T thresh)
Definition: histogram.h:63
Histogram & Value(T func)
Definition: histogram.h:104
data
A set of modifiers are available do describe DataNode.
Definition: DataNode.h:38
void pass_array_to_cpp(emp::array< T, SIZE > &arr, bool recurse=false)
Definition: js_utils.h:299
Definition: histogram.h:17
emp::array< double, 2 > GetDomain()
Definition: histogram.h:38
Histogram & SetThresholds(int count)
Definition: histogram.h:50
Definition: histogram.h:9
A drop-in wrapper for std::vector; adds on bounds checking in debug mode.
Build a debug wrapper emp::vector around std::vector.
Definition: vector.h:42
emp::array< double, 2 > Domain()
Definition: histogram.h:46
Definition: axis.h:20
#define D3_CALLBACK_METHOD_1_ARG(FUNC, ARG1)
Definition: utils.h:115
Histogram & SetDomain(double x, double y)
Definition: histogram.h:29
Dataset Call(emp::vector< DATA_TYPE > data)
Definition: histogram.h:69
Tools to maintain data in D3.
int GetID() const
Definition: d3_init.h:96
Histogram & Domain(double x, double y)
Definition: histogram.h:34
void pass_array_to_javascript(C values)
Definition: js_utils.h:212
Histogram & SetValueAccessor(std::string func)
Definition: histogram.h:98