creativity  v1.3.0
Agent-based model of creativity and piracy
Treatment.hpp
1 #pragma once
2 #include <creativity/data/CSVParser.hpp>
3 #include <creativity/data/Variable.hpp>
4 #include <Eigen/Core>
5 #include <memory>
6 
7 namespace creativity { namespace data {
8 
14 class Treatment {
15  public:
17  Treatment() = default;
18 
20  explicit Treatment(CSVParser &&csv);
21 
23  explicit Treatment(const std::string &filename);
24 
26  void readCSV(CSVParser &&csv);
27 
36  void requirePiracy(bool require = true);
37 
39  void requirePolicy(bool require = true);
40 
42  void requirePre(bool require = true);
43 
55  void requireSR(bool require = true);
56 
58  const bool& hasPre() const { return has_pre_; }
59 
61  const bool& hasPiracy() const { return has_piracy_; }
62 
64  const bool& hasPolicy() const { return has_policy_; }
65 
67  const bool& hasPiracySR() const { return has_piracy_sr_; }
68 
70  const bool& hasPolicySR() const { return has_policy_sr_; }
71 
76  bool hasShortrun() const { return (hasPolicySR() or not hasPolicy()) and (hasPiracySR() or not hasPiracy()); }
77 
79  const unsigned int& rowsPerSimulation() const { return rows_per_sim_; }
80 
84  unsigned int simulations() const { return data().rows() / rowsPerSimulation(); }
85 
90  const Eigen::MatrixXd& data() const { return data_; }
91 
95  const std::vector<std::string>& sourceFiles() const { return source_; }
96 
100  const std::string& sourceFile(unsigned r) const { return sourceFiles().at(r / rowsPerSimulation()); }
101 
106  const std::unordered_map<std::string, unsigned>& columns() const;
107 
116  std::shared_ptr<const SimpleVariable> variable(const std::string &name) const;
117 
119  std::shared_ptr<const SimpleVariable> operator[](const std::string &name) const { return variable(name); }
120 
121  protected:
122  Eigen::MatrixXd data_;
123  std::vector<std::string> source_;
124 
125  bool have_data_{false},
126  has_pre_{false},
127  has_piracy_{false},
128  has_piracy_sr_{false},
129  has_policy_{false},
130  has_policy_sr_{false},
131  require_pre_{false},
134  require_sr_{false};
135  unsigned int rows_per_sim_{0};
137 
139  std::unordered_map<std::string, unsigned> data_column_;
140 
142  mutable std::unordered_map<std::string, std::shared_ptr<SimpleVariable>> var_cache_;
143 
151  void generateRow(const CSVParser &csv,
152  Eigen::Ref<Eigen::RowVectorXd, 0, Eigen::InnerStride<>> newrow,
153  const std::string &prefix) const;
154 
155 };
156 
157 }}
const Eigen::MatrixXd & data() const
The full set of data.
Definition: Treatment.hpp:90
std::shared_ptr< const SimpleVariable > variable(const std::string &name) const
Returns a shared_ptr to a SimpleVariable containing a copy of the requested column.
bool require_piracy_
True if piracy data is required.
Definition: Treatment.hpp:132
Primary namespace for all Creativity library code.
Definition: config.hpp:4
unsigned int rows_per_sim_
The number of treatment row observations per source data rows (i.e. per simulation) ...
Definition: Treatment.hpp:136
const bool & hasPiracySR() const
True if the source data has short-run piracy data.
Definition: Treatment.hpp:67
const bool & hasPolicySR() const
True if the source data has short-run policy data.
Definition: Treatment.hpp:70
const bool & hasPre() const
True if this treatment data pre-piracy observations.
Definition: Treatment.hpp:58
const std::unordered_map< std::string, unsigned > & columns() const
Accesses the map of field name to column indices.
bool have_data_
True once we&#39;ve parsed a data file.
Definition: Treatment.hpp:125
bool has_pre_
True if the data contains pre-piracy, non-treatment rows.
Definition: Treatment.hpp:126
void requireSR(bool require=true)
Requires short-run data for long-run piracy/policy data that exists in the source data...
void generateRow(const CSVParser &csv, Eigen::Ref< Eigen::RowVectorXd, 0, Eigen::InnerStride<>> newrow, const std::string &prefix) const
Generates a row of data from the CSVParser at its current position.
Treatment()=default
Default constructor.
void requirePolicy(bool require=true)
Like requirePiracy(), but for policy data.
bool require_policy_
True if policy data is required.
Definition: Treatment.hpp:133
std::shared_ptr< const SimpleVariable > operator[](const std::string &name) const
Alias for variable()
Definition: Treatment.hpp:119
bool has_policy_sr_
True if the data contains SR policy treatment rows.
Definition: Treatment.hpp:130
bool has_piracy_sr_
True if the data contains SR piracy treatment rows.
Definition: Treatment.hpp:128
void readCSV(CSVParser &&csv)
Reads the data from the given CSVParser.
const std::vector< std::string > & sourceFiles() const
The simulation source values.
Definition: Treatment.hpp:95
This class converts raw simulation data into multiple data rows, with piracy/policy, SR/LR as treatment effects on the base (pre-piracy, pre-policy) row.
Definition: Treatment.hpp:14
Eigen::MatrixXd data_
The data matrix.
Definition: Treatment.hpp:122
const bool & hasPolicy() const
True if the source data has policy data.
Definition: Treatment.hpp:64
unsigned int simulations() const
The number of simulation files contributing to this data: this is simply an alias for obj...
Definition: Treatment.hpp:84
bool require_sr_
True if short-run data is required for each type of long-run data.
Definition: Treatment.hpp:134
const unsigned int & rowsPerSimulation() const
The number of data rows per simulation input. This equals 1 plus the number of treatments.
Definition: Treatment.hpp:79
const std::string & sourceFile(unsigned r) const
Return the source file associated with row r.
Definition: Treatment.hpp:100
std::unordered_map< std::string, std::shared_ptr< SimpleVariable > > var_cache_
Cache of weak pointers to returned SimpleVariables.
Definition: Treatment.hpp:142
void requirePre(bool require=true)
Like requirePiracy(), but for pre-piracy data.
std::vector< std::string > source_
In-order source values for data.
Definition: Treatment.hpp:123
const bool & hasPiracy() const
True if the source data has piracy data.
Definition: Treatment.hpp:61
bool has_policy_
True if the data contains LR policy treatment rows.
Definition: Treatment.hpp:129
bool has_piracy_
True if the data contains LR piracy treatment rows.
Definition: Treatment.hpp:127
bool hasShortrun() const
True if the source data has short-run data for each type of associated long-run data.
Definition: Treatment.hpp:76
Primitive comma-separated-value file parser.
Definition: CSVParser.hpp:32
std::unordered_map< std::string, unsigned > data_column_
Field name to column map.
Definition: Treatment.hpp:139
bool require_pre_
True if pre data is required.
Definition: Treatment.hpp:131
void requirePiracy(bool require=true)
Requires that the parsed data contain piracy observations.