creativity  v1.3.0
Agent-based model of creativity and piracy
Classes | Public Member Functions | List of all members
creativity::data::TreatmentFilter Class Reference

Extension to Treatment that applies a filter to an existing Treatment variable, resulting in a new Treatment object that only contains the row sets for matched data observations. More...

#include <creativity/data/TreatmentFilter.hpp>

Classes

struct  Properties
 Data properties for rows filtered by TreatmentFilter() More...
 
class  StageProperties
 Provides a simple interface to accessing the value of a field for a single observation. More...
 

Public Member Functions

 TreatmentFilter (const Treatment &source, std::function< bool(const Properties &)> filter, std::function< bool(bool pre, bool piracy, bool policy, bool short_run)> stage_filter=[](bool, bool, bool, bool) { return true;})
 Creates a TreatmentFilter object by copying data matching a particular criterion from another TreatmentFilter object. More...
 
- Public Member Functions inherited from creativity::data::Treatment
 Treatment ()=default
 Default constructor.
 
 Treatment (CSVParser &&csv)
 Takes over the given CSVParser and reads the data from it.
 
 Treatment (const std::string &filename)
 Creates a new CSVParser to read the given filename.
 
void readCSV (CSVParser &&csv)
 Reads the data from the given CSVParser.
 
void requirePiracy (bool require=true)
 Requires that the parsed data contain piracy observations. More...
 
void requirePolicy (bool require=true)
 Like requirePiracy(), but for policy data.
 
void requirePre (bool require=true)
 Like requirePiracy(), but for pre-piracy data.
 
void requireSR (bool require=true)
 Requires short-run data for long-run piracy/policy data that exists in the source data. More...
 
const bool & hasPre () const
 True if this treatment data pre-piracy observations.
 
const bool & hasPiracy () const
 True if the source data has piracy data.
 
const bool & hasPolicy () const
 True if the source data has policy data.
 
const bool & hasPiracySR () const
 True if the source data has short-run piracy data.
 
const bool & hasPolicySR () const
 True if the source data has short-run policy data.
 
bool hasShortrun () const
 True if the source data has short-run data for each type of associated long-run data. More...
 
const unsigned int & rowsPerSimulation () const
 The number of data rows per simulation input. This equals 1 plus the number of treatments.
 
unsigned int simulations () const
 The number of simulation files contributing to this data: this is simply an alias for obj.data().rows() / obj.rowsPerSimulation()
 
const Eigen::MatrixXd & data () const
 The full set of data. More...
 
const std::vector< std::string > & sourceFiles () const
 The simulation source values. More...
 
const std::string & sourceFile (unsigned r) const
 Return the source file associated with row r. More...
 
const std::unordered_map< std::string, unsigned > & columns () const
 Accesses the map of field name to column indices. More...
 
std::shared_ptr< const SimpleVariablevariable (const std::string &name) const
 Returns a shared_ptr to a SimpleVariable containing a copy of the requested column. More...
 
std::shared_ptr< const SimpleVariableoperator[] (const std::string &name) const
 Alias for variable()
 

Additional Inherited Members

- Protected Member Functions inherited from creativity::data::Treatment
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. More...
 
- Protected Attributes inherited from creativity::data::Treatment
Eigen::MatrixXd data_
 The data matrix.
 
std::vector< std::string > source_
 In-order source values for data.
 
bool have_data_ {false}
 True once we've parsed a data file.
 
bool has_pre_ {false}
 True if the data contains pre-piracy, non-treatment rows.
 
bool has_piracy_ {false}
 True if the data contains LR piracy treatment rows.
 
bool has_piracy_sr_ {false}
 True if the data contains SR piracy treatment rows.
 
bool has_policy_ {false}
 True if the data contains LR policy treatment rows.
 
bool has_policy_sr_ {false}
 True if the data contains SR policy treatment rows.
 
bool require_pre_ {false}
 True if pre data is required.
 
bool require_piracy_ {false}
 True if piracy data is required.
 
bool require_policy_ {false}
 True if policy data is required.
 
bool require_sr_ {false}
 True if short-run data is required for each type of long-run data.
 
unsigned int rows_per_sim_ {0}
 The number of treatment row observations per source data rows (i.e. per simulation)
 
std::unordered_map< std::string, unsigned > data_column_
 Field name to column map.
 
std::unordered_map< std::string, std::shared_ptr< SimpleVariable > > var_cache_
 Cache of weak pointers to returned SimpleVariables.
 

Detailed Description

Extension to Treatment that applies a filter to an existing Treatment variable, resulting in a new Treatment object that only contains the row sets for matched data observations.

For example, the following:

Treatment data("some.csv");
TreatmentFilter data_filtered(data, [](const TreatmentFilter::Properties &p) {
    return p.piracy and p.piracy->value("books_written_pc") >= 0.2;
});

constructs a filter that only includes data observations that have piracy data with at a piracy books_written_pc value of at least 0.2.

The filter is applied on a simulation-by-simulation row, which typically corresponds to multiple treatment rows at once.

The filter can also be applied to select only particular types of rows: for example, the following constructs a filter that only includes "pre" and "piracy" rows, but omits short-run piracy, and public rows:

Treatment data("some.csv");
TreatmentFilter pre_and_piracy(
    data,
    [](const TreatmentFilter::Properties&) { return true; } // No per-simulation filtering
    [](bool pre, bool piracy, bool policy, bool short_run) {
        return pre or (piracy and not short_run);
    }
);

Constructor & Destructor Documentation

§ TreatmentFilter()

creativity::data::TreatmentFilter::TreatmentFilter ( const Treatment source,
std::function< bool(const Properties &)>  filter,
std::function< bool(bool pre, bool piracy, bool policy, bool short_run)>  stage_filter = [](bool, bool, bool, bool) { return true;} 
)

Creates a TreatmentFilter object by copying data matching a particular criterion from another TreatmentFilter object.

Parameters
sourcethe source Treatment object.
filterthe filter to apply. This object should return true for any data row sets that should be included in the filtered data.
stage_filterthis filter is called to determine which stages should be considered. It is called multiple times before looping through rows to determine which data source row sets should be passed to filter.
Exceptions
std::logic_errorif the stage filter doesn't admit any stages in source
Inheritance diagram for creativity::data::TreatmentFilter:
[legend]
Collaboration diagram for creativity::data::TreatmentFilter:
[legend]

The documentation for this class was generated from the following file: