C++ API#

Generated from the headers with Doxygen and rendered with breathe.

Values#

Scalar values, column types and timestamp helpers.

namespace data_cube#

Typedefs

using Value = std::variant<std::monostate, int64_t, double, std::string, Timestamp>#

A scalar value. std::monostate stands for a missing value (None or NaN in Python).

Enums

enum class ColumnType : uint8_t#

Physical type of a column.

Values:

enumerator Int64#

signed 64-bit integers with a validity mask

enumerator Float64#

doubles, missing values are NaN

enumerator String#

UTF-8 strings with a validity mask.

enumerator DateTime#

timestamps (nanoseconds) with a validity mask

Functions

const char *column_type_name(ColumnType type)#

Returns the name of a column type (int64, float64, object, datetime64[ns]).

bool is_null(const Value &value)#

Tells if a value is missing (monostate or NaN).

bool is_number(const Value &value)#

Tells if a value is a number (int64 or double, NaN included).

double value_as_double(const Value &value)#

Converts a numerical value into a double, NaN if the value is not a number.

int compare_values(const Value &a, const Value &b)#

Compares two values. Missing values are greater than anything else (they are sorted last), numbers are compared by value whatever their type, values of different kinds are ordered by kind (numbers, strings, timestamps).

Returns:

negative, zero or positive integer

bool values_equal(const Value &a, const Value &b)#

Equality based on compare_values, two missing values are equal.

std::string value_to_string(const Value &value)#

Converts a value into a string, missing values become an empty string.

std::string format_double(double value)#

Formats a double with the shortest representation which round-trips.

bool parse_timestamp(std::string_view text, int64_t *ns)#

Parses a timestamp. Supported formats are YYYY-MM-DD, YYYY/MM/DD, M/D/YYYY optionally followed by HH:MM[:SS[.fffffffff]] (or T as a separator) and an optional Z or +HH:MM offset (the timestamp is then converted into UTC).

Parameters:
  • text – string to parse

  • ns – receives the number of nanoseconds since the epoch

Returns:

true if the string was parsed

std::string format_timestamp(int64_t ns)#

Formats a timestamp as YYYY-MM-DD HH:MM:SS[.fffffffff].

bool parse_double(std::string_view text, double *value)#

Parses a number. Leading and trailing spaces are ignored.

Parameters:
  • text – string to parse

  • value – receives the parsed value

Returns:

true if the full string is a number

bool parse_int64(std::string_view text, int64_t *value)#

Parses an integer, returns true if the full string is an integer.

struct Timestamp#
#include <value.h>

A naive point in time stored as a number of nanoseconds since 1970-01-01 00:00:00 (no time zone, same convention as numpy.datetime64[ns]).

Public Functions

auto operator<=>(const Timestamp&) const = default#

Usual comparison operators.

Public Members

int64_t ns = 0#

Nanoseconds since the epoch.

Column#

class Column#

A typed column. Floats use NaN to represent missing values, the other types store a validity mask (1 for a valid value, 0 for a missing one). Appending a value which does not fit into the current type promotes the column (int64 + float64 -> float64, anything + string -> string).

Public Functions

Column() = default#

Creates an empty float column.

explicit Column(ColumnType type, size_t n = 0)#

Creates a column of n missing values.

inline ColumnType type() const#

Returns the column type.

inline size_t size() const#

Returns the number of rows.

inline bool is_numeric() const#

Tells if the column stores numbers (int64 or float64).

bool is_null(size_t i) const#

Tells if a value is missing.

size_t null_count() const#

Returns the number of missing values.

Value get(size_t i) const#

Returns a value.

double as_double(size_t i) const#

Returns a value as a double, NaN if missing or not a number.

inline const std::string &string_at(size_t i) const#

Returns the string stored at position i (type must be String).

inline int64_t int_at(size_t i) const#

Returns the integer stored at position i (type must be Int64 or DateTime).

inline double double_at(size_t i) const#

Returns the double stored at position i (type must be Float64).

void push_back(const Value &value)#

Appends a value, the column is promoted if the value does not fit.

void set(size_t i, const Value &value)#

Replaces a value, the column is promoted if the value does not fit.

void append(const Column &other)#

Appends another column, the column is promoted if needed.

Column take(const std::vector<size_t> &rows) const#

Returns a column with the selected rows.

Column cast(ColumnType type) const#

Casts a column into another type, throws if a value cannot be converted.

int compare(size_t i, size_t j) const#

Compares two rows of the column, missing values are last.

size_t factorize(std::vector<int64_t> *codes) const#

Factorizes the column. Missing values get their own code.

Parameters:

codes – receives one code per row, codes follow the order of appearance

Returns:

number of distinct codes

bool strings_are_numbers() const#

Tells if every non missing string can be parsed as a double.

inline const std::vector<double> &doubles() const#

Raw float storage.

inline const std::vector<int64_t> &ints() const#

Raw integer storage (Int64, DateTime).

inline const std::vector<std::string> &strings() const#

Raw string storage.

inline const std::vector<uint8_t> &valid() const#

Validity mask (empty for Float64).

Public Static Functions

static Column from_doubles(std::vector<double> values)#

Creates a float column.

static Column from_int64(std::vector<int64_t> values, std::vector<uint8_t> valid = {})#

Creates an integer column, an empty mask means every value is valid.

static Column from_strings(std::vector<std::string> values, std::vector<uint8_t> valid = {})#

Creates a string column, an empty mask means every value is valid.

static Column from_timestamps(std::vector<int64_t> ns, std::vector<uint8_t> valid = {})#

Creates a timestamp column (nanoseconds), an empty mask means every value is valid.

static Column from_values(const std::vector<Value> &values)#

Creates a column from values, the type is inferred (int64 if the vector is empty or null).

static Column full(size_t n, const Value &value)#

Creates a column of n identical values.

static ColumnType common_type(ColumnType a, ColumnType b)#

Returns the type able to store both types.

static ColumnType type_of(const Value &value)#

Returns the type needed to store a value.

Table#

class Table#

A list of named columns of the same length.

Public Functions

Table() = default#

Creates an empty table.

inline size_t num_rows() const#

Returns the number of rows.

inline size_t num_columns() const#

Returns the number of columns.

inline const std::vector<std::string> &names() const#

Returns the column names.

bool has_column(const std::string &name) const#

Tells if a column exists.

size_t index_of(const std::string &name) const#

Returns the position of a column, throws if it does not exist.

const Column &column(const std::string &name) const#

Returns a column by name, throws if it does not exist.

Column &column(const std::string &name)#

Returns a column by name, throws if it does not exist.

inline const Column &column(size_t i) const#

Returns a column by position.

inline Column &column(size_t i)#

Returns a column by position.

void add_column(const std::string &name, Column column)#

Adds a column or replaces an existing one. The first column defines the number of rows.

void drop_column(const std::string &name)#

Removes a column, throws if it does not exist.

Table select(const std::vector<std::string> &names) const#

Returns a table with a subset of columns in the given order.

Table take(const std::vector<size_t> &rows) const#

Returns a table with the selected rows.

inline Value get(size_t row, const std::string &name) const#

Returns a value.

std::string to_string(size_t max_rows = 20) const#

Returns a text representation of the first rows.

std::vector<size_t> group_by(const std::vector<std::string> &keys, std::vector<size_t> *codes) const#

Groups rows by the values of several columns. Missing values are considered as a regular value (dropna=False in pandas).

Parameters:
  • keys – columns to group by

  • codes – receives the group index of every row (groups follow the order of appearance)

Returns:

the first row of every group

Public Static Functions

static Table concat(const std::vector<Table> &tables)#

Concatenates tables. The result contains the union of all columns (in order of appearance), missing columns are filled with missing values.

static Table from_records(const std::vector<std::vector<std::pair<std::string, Value>>> &rows)#

Builds a table from rows (one map per row, like a list of dictionaries in Python).

Table data_cube::read_csv(const std::string &filename, char separator = ',')#

Reads a CSV file, see read_csv_string.

Table data_cube::read_csv_string(const std::string &content, char separator = ',')#

Parses a CSV content. The first line is the header. Every column is converted into int64 if all values are integers, into float64 if all values are numbers, into strings otherwise. Empty fields and usual markers (NA, NaN, null, …) are missing values.

Parameters:
  • content – CSV content

  • separator – field separator

Returns:

the table

Frame#

class Frame#

Two dimensional data with multi-level indices on rows and columns, equivalent to a pandas DataFrame with a MultiIndex on both axes. Every level is stored as a column. Data are stored column by column. An empty level name stands for an unnamed level (None in pandas).

Public Functions

size_t num_rows() const#

Returns the number of rows.

size_t num_columns() const#

Returns the number of columns.

inline Value cell(size_t row, size_t col) const#

Returns a cell.

std::vector<Value> row_key(size_t row) const#

Returns the index values of a row.

std::vector<Value> column_key(size_t col) const#

Returns the column index values of a column.

int index_level(const std::string &name) const#

Returns the position of a row index level or -1.

int column_level(const std::string &name) const#

Returns the position of a column index level or -1.

int find_column(const std::vector<Value> &key) const#

Returns the position of a column given its key or -1.

void check() const#

Checks the frame is consistent, throws an exception otherwise.

void sort_index()#

Sorts the rows by index (missing values last).

void sort_columns()#

Sorts the columns by column index (missing values last).

void reorder_column_levels(const std::vector<std::string> &names)#

Reorders the column levels.

void reorder_index_levels(const std::vector<std::string> &names)#

Reorders the row index levels.

Frame transpose() const#

Returns the transposed frame.

Frame stack(const std::string &level) const#

Moves a column level into the last level of the row index (DataFrame.stack(level, future_stack=True)). If no column level remains, the frame gets a single column named VALUE.

Frame reset_index() const#

Moves the row index into the columns and replaces it by a range.

Frame select_columns(const std::vector<size_t> &cols) const#

Returns a frame with a subset of columns.

void append_columns(const Frame &other)#

Appends the columns of another frame sharing the same rows.

Frame group_by_index_level(const std::string &level, const std::vector<std::string> &aggregations) const#

Groups rows by one level of the row index and aggregates every column.

Parameters:
  • level – index level

  • aggregations – one aggregation per column (see aggregate)

Returns:

frame indexed by the level

std::string to_string(size_t max_rows = 30) const#

Returns a text representation.

Public Members

std::vector<std::string> index_names#

Names of the row index levels.

std::vector<Column> index#

Row index levels, every level has num_rows() values.

std::vector<std::string> column_names#

Names of the column index levels.

std::vector<Column> columns#

Column index levels, every level has num_columns() values.

std::vector<Column> data#

Data, one Column per column, every one has num_rows() values.

Value data_cube::aggregate(const Column &column, const std::vector<size_t> &rows, const std::string &aggregation)#

Aggregates a subset of a column. Supported aggregations: sum, mean, median, min, max, count, first, last, std, var, nunique, size. Missing values are skipped, sum of an empty set is 0, sum of strings is the concatenation.

Parameters:
  • column – column

  • rows – rows to aggregate

  • aggregation – aggregation name

Returns:

aggregated value

DataCube#

struct ViewDef#

Defines how to compute a view. Every list of columns may contain regular expressions (a filter is a regular expression if it contains one of "^.*+{}).

Public Types

using MultiAggregation = std::function<double(const Table&)>#

Function aggregating a group of rows (only the value columns) into a single number.

Public Members

std::vector<std::string> key_index#

Keys to put in the row index.

std::vector<std::string> values#

Values to show.

bool ignore_unique = true#

Ignores keys with a unique value.

std::vector<std::string> order#

Reorders keys in the column index.

std::vector<std::string> key_agg#

Aggregates over these keys before creating the view.

std::string agg = "sum"#

Default aggregation (see aggregate).

std::map<std::string, std::string> agg_by_column#

Aggregation by value column, it overwrites agg.

std::vector<std::pair<std::string, MultiAggregation>> agg_multi#

Aggregations over multiple columns, the name becomes a new value.

std::vector<std::string> ignore_columns#

Ignores these columns.

std::vector<std::string> keep_columns_in_index#

Keeps these columns even if they have a unique value.

bool dropna = true#

Drops rows with only missing values.

bool transpose = false#

Transposes the result.

std::string name#

Name of the view.

bool no_index = false#

Moves the row index into the columns.

bool plots = false#

Adds plots when the view is exported into Excel (Python only).

std::vector<std::string> fix_aggregation_change#

Keys whose values changed over time, they are merged before aggregation.

struct SideBySide#

Result of DataCube::sbs.

Public Members

Frame raw#

Raw data with comparison columns, the metric is the first level of the index.

Frame agg#

Aggregated data, indexed by time and metric.

Frame col#

Raw data, one row per key.

class DataCube#

Processes logs coming from experiments. A cube is basically a database with certain columns playing specific roles.

  • time: only one column, it is not mandatory but it is recommended to have one

  • keys: they are somehow coordinates, they cannot be aggregated, they are not numbers, more like categories, (time, *keys) identifies an element of the database in an unique way, there cannot be more than one row sharing the same key and time values

  • values: they are not necessary numerical, but if they are, they can be aggregated

Every other column is ignored. More columns can be added by using formulas.

Public Types

using Formula = std::function<Column(const Table&)>#

A formula computes a new column from the data.

Public Functions

DataCube(std::vector<Table> data, std::string time = "date", std::vector<std::string> keys = {"version_.*", "model_.*"}, std::vector<std::string> values = {"time_.*", "disc_.*"}, std::vector<std::string> ignored = {}, bool recent = false, std::vector<std::pair<std::string, Formula>> formulas = {}, std::vector<std::pair<std::string, Value>> fill_missing = {}, bool keep_last_date = false)#

Constructor.

Parameters:
  • data – pieces of raw data, they are concatenated

  • time – the time column

  • keys – the keys, can include regular expressions

  • values – the values, can include regular expressions

  • ignored – ignores some columns, acts as negative regular expressions for the other two

  • recent – if more than one row share the same keys, the cube only keeps the most recent one

  • formulas – columns to add, defined with formulas

  • fill_missing – defines values replacing missing columns in every piece

  • keep_last_date – overwrites all the times with the most recent one, it makes things easier for timeseries

DataCube &load(int verbose = 0, const Logger &log = default_logger)#

Loads and preprocesses the data. Returns *this.

inline bool is_loaded() const#

Tells if load was called.

const Table &data() const#

Processed data.

std::pair<size_t, size_t> shape() const#

Shape of the processed data.

const std::vector<std::string> &columns() const#

Columns of the processed data.

inline const std::string &time() const#

Time column.

inline const std::vector<std::string> &keys_no_time() const#

Keys without time.

inline const std::vector<std::string> &keys_time() const#

Time and keys.

inline const std::vector<std::string> &values() const#

Values.

inline const std::vector<std::string> &ignored() const#

Ignored columns.

inline const std::vector<std::string> &ignored_keys() const#

Keys ignored.

inline const std::vector<std::string> &ignored_values() const#

Values ignored.

inline const std::vector<std::string> &dropped() const#

Columns not used (neither time, key, value or ignored).

inline const std::vector<std::string> &keys_with_nans() const#

Keys with missing values.

inline bool recent() const#

Tells if more than one row may share the same keys.

inline bool keep_last_date() const#

Tells if all dates are replaced by the last one.

inline const std::map<std::string, size_t> &n_unique_for_key() const#

Number of distinct values (missing value included except for time) for every key.

ViewDef make_view_def(const std::string &name) const#

Returns a default view definition for a value.

Frame view(const ViewDef &view_def, int verbose = 0, const Logger &log = default_logger) const#

Returns a pivot view. key_index determines the index, the other key columns determines the columns. If ignore_unique is true, every column with a unique value is removed.

Frame view(const std::string &value_name, int verbose = 0, const Logger &log = default_logger) const#

Returns the default view for a value.

Frame describe() const#

Basic description of all variables.

DataCube clone(std::optional<Table> data = std::nullopt, std::optional<std::vector<std::string>> keys = std::nullopt) const#

Makes a copy of the cube, it copies the processed data not the original one.

Parameters:
  • data – new data or the current one if empty

  • keys – new keys or the current ones if empty

DataCube cube_time(bool fill_other_dates = false, double threshold = 1.2) const#

Aggregates the data over time to detect changes on the last value. If fill_other_dates is true, all dates are kept, but values are filled with 0. threshold determines the bandwidth within the values are expected, it is a factor of the standard deviation.

SideBySide sbs(const std::vector<std::pair<std::string, std::vector<std::pair<std::string, Value>>>> &configs, const std::string &column_name = "CONF") const#

Creates a side-by-side for two configurations. Every configuration is a list of (column, value) filtering in the rows to keep. Every configuration is given a name, it is added in column column_name.

Table fix_aggregation_change(const Table &data, const std::vector<std::string> &columns_to_fix) const#

Fixes columns used to aggregate values because their meaning changed over time. The values of a column are replaced by the concatenation of all the values found for the other keys.

std::string to_string() const#

Returns a text representation of the data.

Public Static Functions

static std::vector<std::string> filter_column(const std::vector<std::string> &filters, const std::vector<std::string> &columns, bool can_be_empty = false)#

Selects the columns matching filters. A filter is a regular expression if it contains one of "^.*+{}, otherwise it must be equal to a column name.

Parameters:
  • filters – filters

  • columns – available columns

  • can_be_empty – throws an exception if false and no column is found

Returns:

sorted list of columns

Statistics#

Statistical tests on time series.

namespace data_cube

Functions

std::pair<double, double> breaking_last_point(const std::vector<double> &series, double threshold = 1.2)#

Assuming a time series is constant, checks the last value is not an outlier.

Parameters:
  • series – values in chronological order

  • threshold – number of standard deviations beyond the mean required for the last point to be considered as a significant change

Returns:

significant change (-1, 0, +1) and the test value

std::pair<double, double> mann_kendall(const std::vector<double> &series, double threshold = 0.5)#

Computes the test of Mann-Kendall.

Parameters:
  • series – values in chronological order

  • threshold – 1.96 is the usual value, 0.5 means a short time series (0, 1, 2, 3, 4) has a significant trend

Returns:

trend (-1, 0, +1) and the test value