Skip to content

DataMap

cgnitash edited this page Apr 24, 2018 · 9 revisions

DataMap is a utility type within MABE. DataMaps may be used to store arbitrary data, do basic computations on this data, and trivially write the data to file. Several modules in MABE use DataMap, e.g. every organism in MABE has a DataMap associated with it, and whenever an organism is archived, its data is written to file.

Overview

A DataMap is basically a map of keys to values. These values can be type bool, int, double, string, or a std::vector of these basic types. In addition, every key has a set of associated output behaviours. The behaviours specify the types of computations that must be done on the data when written to file. Some of these computations can be done at run time as well.
A DataMap object is created by saying,

 DataMap my_data;

Member functions

  1. set(string s, value v)

This member function sets the key s to have the value v. Once a key is set to a value of a particular type, only values of that type, or vectors of that type can be assigned to the key.

Usage

  DataMap d;
  d.set("id", 5);        // assign int
  d.set("name", "bob");  // assign const char * (implicitly converted to
                         // and stored as std::string)
  d.set("happy", true);  // assign bool
  d.set("weight", 42.5); // assign double

  d.set("weight",42); //error: "weight" is already of type double
  d.set("id","bob");  //error: "id" is already of type int

  std::vector v = {1, 2, 3, 4};
  d.set("all_ids", v); // assign vector\

  d.set("all_ids", 8); // fine: we can assign T to key with type vector\

  std::vector x{3.1, 2.4, 5.6};
  d.set("weight", x); // fine: we can assign vector\ to key with type T

, Get() and, Append() methods write values into and read values from a data Map.

  • Set(key,value) - set a value in a data map associated with key.
  • Append(key,value) - add a value to a list of values associated with key.
  • Clear(key) - clear entry for key from a dataMap
  • ClearMap() - clear all elements from dataMap
  • fieldExists(key) - return true if this key is in map
  • GetBoolVector(key),GetIntVector(key),GetDoubleVector(key),GetStringVector(key) - get a vector of the given type associated with key.
  • GetStringOfVector(key) - convert the values associated key to a string
  • GetAverage(key) - retuns a double, which is the average of the values associated with key. If key is associated with string values, this will result in an error message.
  • writeToFile(fileName, keys, aveOnly) - save the contents of this DataMap to file 'fileName'. If keys are provided (a vector of string) then only write these keys data. If aveOnly (bool, default false), than only save average values (not lists, etc.)
  • setOutputBehavior(key, outputBehavior) - define how data associated with key will be saved to file when saveToFile() is called. outputBehavior options are: LIST, AVE, FIRST, VAR(variance) (more will be implimented in the future). If more then one output behavior is needed, list them sperated with '|' (e.g. setOutputBehavior("score", DataMap::AVE | DataMap::LIST) )
  • Merge(otherDataMap, replace) - merge contents of two data maps - if common keys are found, replace 1 = overwrite, replace 0 (default) = append.

    Under the hood

    The data is stored in a map, that is an unordered list of key-value pairs. Data can be added to and removed from the DataMap. The values in data maps can be bool, int, double or string. DataMaps also include functions to save their contents to files.

Clone this wiki locally