10 #ifndef EMP_DATA_FILE_H 11 #define EMP_DATA_FILE_H 18 #include "../base/assert.h" 19 #include "../base/vector.h" 20 #include "../meta/type_traits.h" 21 #include "../tools/FunctionSet.h" 22 #include "../tools/string_utils.h" 34 using fun_t = void(std::ostream &);
50 const std::string & b=
"",
const std::string & s=
",",
const std::string & e=
"\n")
51 : filename(in_filename), os(new
std::ofstream(in_filename)), funs(), keys(), descs()
52 , timing_fun([](size_t){
return true;})
55 const std::string & b=
"",
const std::string & s=
",",
const std::string & e=
"\n")
56 : filename(), os(&in_os), funs(), keys(), descs(), timing_fun([](size_t){
return true;})
83 timing_fun = [print_time](
size_t update) {
return update == print_time; };
89 timing_fun = [step](
size_t update) {
return update % step == 0; };
96 timing_fun = [first,step,last](
size_t update) {
97 if (update < first || update > last)
return false;
98 return ((update - first) % step) == 0;
105 void SetSpacer(
const std::string & _in) { line_spacer = _in; }
109 void SetupLine(
const std::string & b,
const std::string & s,
const std::string & e) {
118 for (
size_t i = 0; i < keys.
size(); i++) {
128 for (
size_t i = 0; i < keys.
size(); i++) {
129 *os << cstart << i <<
": " << descs[i] <<
" (" << keys[i] <<
")" <<
std::endl;
137 for (
size_t i = 0; i < funs.size(); i++) {
153 size_t Add(
const std::function<
void(std::ostream &)> & fun,
const std::string & key,
const std::string & desc) {
154 size_t id = funs.GetSize();
162 template <
typename T>
163 size_t AddFun(
const std::function<T()> & fun,
const std::string & key=
"",
const std::string & desc=
"") {
164 std::function<fun_t> in_fun = [fun](std::ostream &
os){ os << fun(); };
165 return Add(in_fun, key, desc);
169 template <
typename T>
170 size_t AddVar(
const T & var,
const std::string & key=
"",
const std::string & desc=
"") {
171 std::function<fun_t> in_fun = [&var](std::ostream &
os){ os << var; };
172 return Add(in_fun, key, desc);
179 template <
typename VAL_TYPE,
emp::data... MODS>
181 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
183 os << node.GetCurrent();
184 if (reset) node.
Reset();
186 return Add(in_fun, key, desc);
194 template <
typename VAL_TYPE,
emp::data... MODS>
196 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
198 os << node.GetMean();
199 if (reset) node.
Reset();
201 return Add(in_fun, key, desc);
209 template <
typename VAL_TYPE,
emp::data... MODS>
211 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
213 os << node.GetTotal();
214 if (reset) node.
Reset();
216 return Add(in_fun, key, desc);
223 template <
typename VAL_TYPE,
emp::data... MODS>
225 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
228 if (reset) node.
Reset();
230 return Add(in_fun, key, desc);
237 template <
typename VAL_TYPE,
emp::data... MODS>
239 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
242 if (reset) node.
Reset();
244 return Add(in_fun, key, desc);
249 template <
typename VAL_TYPE,
emp::data... MODS>
251 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
253 os << node.GetVariance();
254 if (reset) node.
Reset();
256 return Add(in_fun, key, desc);
261 template <
typename VAL_TYPE,
emp::data... MODS>
263 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
265 os << node.GetStandardDeviation();
266 if (reset) node.
Reset();
268 return Add(in_fun, key, desc);
273 template <
typename VAL_TYPE,
emp::data... MODS>
275 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
277 os << node.GetSkew();
278 if (reset) node.
Reset();
280 return Add(in_fun, key, desc);
285 template <
typename VAL_TYPE,
emp::data... MODS>
287 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
289 os << node.GetKurtosis();
290 if (reset) node.
Reset();
292 return Add(in_fun, key, desc);
300 template <
typename VAL_TYPE,
emp::data... MODS>
302 AddMean(node,
"mean_" + key,
"mean of " + desc, reset, pull);
303 AddMin(node,
"min_" + key,
"min of " + desc, reset, pull);
304 AddMax(node,
"max_" + key,
"max of " + desc, reset, pull);
305 AddVariance(node,
"variance_" + key,
"variance of " + desc, reset, pull);
314 template <
typename VAL_TYPE,
emp::data... MODS>
316 AddStats(node, key, desc, reset, pull);
317 AddSkew(node,
"skew_" + key,
"skew of " + desc, reset, pull);
318 AddKurtosis(node,
"kurtosis_" + key,
"kurtosis of " + desc, reset, pull);
326 template <
typename VAL_TYPE,
emp::data... MODS>
328 std::function<fun_t> in_fun =
329 [&node,bin_id,reset, pull](std::ostream &
os){
331 os << node.GetHistCount(bin_id);
332 if (reset) node.
Reset();
334 return Add(in_fun, key, desc);
341 template <
typename VAL_TYPE,
emp::data... MODS>
343 std::function<fun_t> in_fun = [&node, reset, pull](std::ostream &
os){
345 VAL_TYPE inf = (node.GetMax() == 0) ? 0 : (node.GetMean() / node.GetMax());
347 if (reset) node.
Reset();
349 return Add(in_fun, key, desc);
354 template <
typename container_t>
360 template <
typename container_t>
363 using data_t =
typename container_t::value_type;
370 template <
typename container_t>
375 for (
const data_t & d : *(df->GetCurrentRows())) {
381 template <
typename container_t>
401 template <
typename CONTAINER>
405 using container_t =
typename std::remove_reference<CONTAINER>::type;
407 using data_t =
typename raw_container_t::value_type;
421 const std::string & b=
"",
const std::string & s=
",",
const std::string & e=
"\n")
422 :
DataFile(filename, b, s, e), update_container_fun(), current_rows() {;}
429 update_container_fun = fun;
435 for (
size_t i = 0; i < keys.
size(); i++) {
439 for (
size_t i = 0; i < container_keys.
size(); i++) {
440 if (i > 0 || keys.
size() > 0) *os << line_spacer;
441 *os << container_keys[i];
449 for (
size_t i = 0; i < keys.
size(); i++) {
450 *os << cstart << i <<
": " << descs[i] <<
" (" << keys[i] <<
")" <<
std::endl;
452 for (
size_t i = 0; i < container_keys.
size(); i++) {
453 *os << cstart << i+keys.
size() <<
": " << container_descs[i] <<
" (" << container_keys[i] <<
")" <<
std::endl;
463 for (
size_t i = 0; i < funs.size(); i++) {
468 for (
size_t i = 0; i < container_funs.size(); i++) {
469 if (i > 0 || keys.
size() > 0) *os << line_spacer;
470 container_funs[i](*
os, d);
478 current_rows = update_container_fun();
491 size_t Add(
const std::function<
void(std::ostream &,
data_t)> & fun,
const std::string & key,
const std::string & desc) {
492 size_t id = container_funs.GetSize();
493 container_funs.Add(fun);
500 template <
typename T>
501 size_t AddContainerFun(
const std::function<T(
const data_t)> & fun,
const std::string & key=
"",
const std::string & desc=
"") {
502 std::function<container_fun_t> in_fun = [fun](std::ostream &
os,
const data_t data){ os << fun(
data); };
503 return Add(in_fun, key, desc);
511 template <
typename CONTAINER>
513 const std::string & filename,
514 const std::string & b=
"",
515 const std::string & s=
",",
516 const std::string & e=
"\n") {
size_t AddTotal(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:210
virtual void Update()
Run all of the functions and print the results as a new line in the file.
Definition: DataFile.h:135
void SetTimingRepeat(size_t step)
Setup this file to print every 'step' updates.
Definition: DataFile.h:87
std::function< container_t(void)> fun_update_container_t
Definition: DataFile.h:409
size_t AddKurtosis(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:286
DataNode objects track a specific type of data over the course of a run.
Definition: DataFile.h:355
size_t AddHistBin(DataNode< VAL_TYPE, MODS... > &node, size_t bin_id, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:327
void Update(size_t update)
Definition: DataFile.h:147
void OutputLine(const data_t d)
Definition: DataFile.h:461
std::function< bool(size_t)> time_fun_t
Definition: DataFile.h:35
virtual void PrintHeaderComment(const std::string &cstart="# ")
Print a header containing comments describing all of the columns.
Definition: DataFile.h:127
std::string filename
Name of the file that we are printing to (if one exists)
Definition: DataFile.h:37
std::string line_begin
What should we print at the start of each line?
Definition: DataFile.h:44
DataFile(std::ostream &in_os, const std::string &b="", const std::string &s=",", const std::string &e="\n")
Definition: DataFile.h:54
ContainerDataFile< CONTAINER > MakeContainerDataFile(std::function< CONTAINER(void)> fun, const std::string &filename, const std::string &b="", const std::string &s=",", const std::string &e="\n")
Definition: DataFile.h:512
void AddStats(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:301
virtual void PrintHeaderKeys()
Print a header containing the name of each column.
Definition: DataFile.h:116
DataFile(const std::string &in_filename, const std::string &b="", const std::string &s=",", const std::string &e="\n")
Definition: DataFile.h:49
void Update(ContainerDataFile< container_t * > *df)
Definition: DataFile.h:383
void(std::ostream &) fun_t
Definition: DataFile.h:34
void SetTimingOnce(size_t print_time)
Definition: DataFile.h:82
const std::string & GetLineEnd() const
Returns the string that is printed at the end of each line.
Definition: DataFile.h:74
Definition: BitVector.h:785
void SetSpacer(const std::string &_in)
Print.
Definition: DataFile.h:105
typename raw_container_t::value_type data_t
Definition: DataFile.h:407
void Update(ContainerDataFile< Ptr< container_t >> *df)
Definition: DataFile.h:372
void AddAllStats(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:315
typename std::remove_reference< CONTAINER >::type container_t
Definition: DataFile.h:405
size_t Add(const std::function< void(std::ostream &, data_t)> &fun, const std::string &key, const std::string &desc)
Definition: DataFile.h:491
size_t AddSkew(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:274
typename remove_ptr_type< container_t >::type raw_container_t
Definition: DataFile.h:406
data
A set of modifiers are available do describe DataNode.
Definition: DataNode.h:38
size_t size() const
Definition: vector.h:151
Definition: DataFile.h:361
void emplace_back(ARGS &&...args)
Definition: vector.h:219
Definition: DataNode.h:648
~ContainerDataFile()
Definition: DataFile.h:424
emp::vector< std::string > descs
Full description for each column.
Definition: DataFile.h:41
size_t AddMin(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:224
void SetTiming(time_fun_t fun)
Definition: DataFile.h:78
emp::vector< std::string > container_descs
Definition: DataFile.h:416
const std::string & GetLineBegin() const
Returns the string that is printed at the beginning of each line.
Definition: DataFile.h:70
size_t AddCurrent(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:180
void SetupLine(const std::string &b, const std::string &s, const std::string &e)
Set line begin character (.
Definition: DataFile.h:109
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
time_fun_t timing_fun
Function to determine updates to print on (default: all)
Definition: DataFile.h:42
container_t current_rows
Definition: DataFile.h:413
ContainerDataFile(const std::string &filename, const std::string &b="", const std::string &s=",", const std::string &e="\n")
Definition: DataFile.h:420
std::string line_spacer
What should we print between entries?
Definition: DataFile.h:45
void PullData()
Method to retrieve new data.
Definition: DataNode.h:664
FunctionSet< fun_t > funs
Set of functions to call, one per column in the file.
Definition: DataFile.h:39
size_t AddInferiority(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:342
T type
Definition: type_traits.h:30
emp::vector< std::string > keys
Keywords associated with each column.
Definition: DataFile.h:40
size_t AddMax(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:238
std::ostream * os
Stream to print to.
Definition: DataFile.h:38
size_t Add(const std::function< void(std::ostream &)> &fun, const std::string &key, const std::string &desc)
Definition: DataFile.h:153
If we are in emscripten, make sure to include the header.
Definition: array.h:37
FunctionSet< container_fun_t > container_funs
Definition: DataFile.h:414
void PrintHeaderKeys()
Print a header containing the name of each column.
Definition: DataFile.h:433
size_t AddVariance(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:250
size_t AddFun(const std::function< T()> &fun, const std::string &key="", const std::string &desc="")
Add a function that returns a value to be printed to the file.
Definition: DataFile.h:163
void Reset()
Methods to reset data.
Definition: DataNode.h:670
#define emp_assert(...)
Definition: assert.h:199
virtual ~DataFile()
Definition: DataFile.h:61
size_t AddMean(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:195
void SetLineBegin(const std::string &_in)
Print.
Definition: DataFile.h:103
emp::vector< std::string > container_keys
Definition: DataFile.h:415
void Update(size_t update)
Update the file with an additional set of lines.
Definition: DataFile.h:484
void SetLineEnd(const std::string &_in)
Print.
Definition: DataFile.h:107
void(std::ostream &, data_t) container_fun_t
Definition: DataFile.h:408
DataFile & operator=(const DataFile &)=default
Definition: type_traits.h:30
const container_t GetCurrentRows() const
Definition: DataFile.h:459
const std::string & GetFilename() const
Get the filename used for this file.
Definition: DataFile.h:67
const std::string & GetSpacer() const
Returns the string that is printed between elements on each line (i.e. the delimeter).
Definition: DataFile.h:72
Definition: DataFile.h:32
void SetTimingRange(size_t first, size_t step, size_t last)
Setup this file to print only in a specified time range, and a given frequency (step).
Definition: DataFile.h:93
size_t AddVar(const T &var, const std::string &key="", const std::string &desc="")
Add a function that always prints the current value of.
Definition: DataFile.h:170
void SetUpdateContainerFun(const fun_update_container_t fun)
Definition: DataFile.h:428
void Update(ContainerDataFile< container_t > *df)
Definition: DataFile.h:362
void Update() override
Run all of the functions and print the results as a new line in the file.
Definition: DataFile.h:476
size_t AddStandardDeviation(DataNode< VAL_TYPE, MODS... > &node, const std::string &key="", const std::string &desc="", const bool &reset=false, const bool &pull=false)
Definition: DataFile.h:262
void PrintHeaderComment(const std::string &cstart="# ")
Print a header containing comments describing all of the columns.
Definition: DataFile.h:448
fun_update_container_t update_container_fun
Definition: DataFile.h:411
std::string line_end
What should we print at the end of each line?
Definition: DataFile.h:46
size_t AddContainerFun(const std::function< T(const data_t)> &fun, const std::string &key="", const std::string &desc="")
Add a function that returns a value to be printed to the file.
Definition: DataFile.h:501