Brush C++ API
A flexible interpretable machine learning framework
Loading...
Searching...
No Matches
Brush::Util Namespace Reference

namespace containing various utility functions More...

Classes

struct  Log_Stats
 
class  Logger
 Defines a multi level static logger. More...
 
struct  Normalizer
 normalizes a matrix to unit variance, 0 mean centered. More...
 
class  Rnd
 Defines a multi-core random number generator and its operators. More...
 
class  Timer
 class for timing things. More...
 

Typedefs

template<typename T >
using TypeMap = std::map<std::type_index, T>
 
typedef struct Log_Stats Log_stats
 

Functions

void HandleErrorThrow (string err, const char *file, int line)
 prints error and throws an exception
 
void HandleErrorNoThrow (string err, const char *file, int line)
 prints error to stderr and returns
 
void clean (ArrayXf &x)
 limits node output to be between MIN_FLT and MAX_FLT
 
std::string ltrim (std::string str, const std::string &chars)
 
std::string rtrim (std::string str, const std::string &chars)
 
std::string trim (std::string str, const std::string &chars)
 
vector< type_index > get_dtypes (MatrixXf &X)
 calculates data types for each column of X
 
float condition_number (const MatrixXf &X)
 returns true for elements of x that are infinite
 
MatrixXf corrcoef (const MatrixXf &X)
 returns the pearson correlation coefficients of matrix.
 
float mean_square_corrcoef (const MatrixXf &X)
 
int argmiddle (vector< float > &v)
 returns the (first) index of the element with the middlest value in v
 
float variance (const ArrayXf &v)
 calculate variance
 
float skew (const ArrayXf &v)
 calculate skew
 
float kurtosis (const ArrayXf &v)
 calculate kurtosis
 
float covariance (const ArrayXf &x, const ArrayXf &y)
 covariance of x and y
 
float slope (const ArrayXf &x, const ArrayXf &y)
 slope of x/y
 
float pearson_correlation (const ArrayXf &x, const ArrayXf &y)
 the normalized covariance of x and y
 
float mad (const ArrayXf &x)
 median absolute deviation
 
std::string ReplaceString (std::string subject, const std::string &search, const std::string &replace)
 find and replace string
 
void ReplaceStringInPlace (std::string &subject, const std::string &search, const std::string &replace)
 string find and replace in place
 
vector< size_tmask_to_index (const ArrayXb &mask)
 convert a boolean mask to an index array
 
tuple< vector< size_t >, vector< size_t > > mask_to_indices (const ArrayXb &mask)
 returns 2 indices: first where mask is true, and second where mask is false.
 
template<typename V , typename T >
bool in (const V &v, const T &i)
 check if element is in vector.
 
template<typename T , typename Scalar = T::Scalar>
Scalar median (const T &v)
 calculate median
 
float variance (const ArrayXf &v, float mean)
 calculate variance when mean provided
 
template<typename T >
vector< size_targsort (const vector< T > &v, bool ascending=true)
 return indices that sort a vector
 
template<typename T >
vector< T > softmax (const vector< T > &w)
 return the softmax transformation of a vector.
 
template<typename T >
vector< T > unique (vector< T > w)
 returns unique elements in vector
 
template<typename T >
vector< T > unique (Matrix< T, -1, -1 > w)
 returns unique elements in Eigen matrix of variable rows/cols
 
template<typename T >
vector< T > unique (Matrix< T, -1, 1 > w)
 returns unique elements in Eigen vector
 
template<typename T >
vector< T > unique (Array< T, -1, 1 > w)
 returns unique elements in 1d Eigen array
 
template<typename T >
string to_string (const T &value)
 template function to convert objects to string for logging
 
 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE (Log_Stats, generation, time, best_score, best_score_v, med_score, med_score_v, med_size, med_complexity, max_size, max_complexity)
 
template<typename T >
std::enable_if_t< std::is_scalar_v< T >, T > limited (T x)
 limits the output to finite real numbers
 
template<typename T >
std::enable_if_t< std::is_base_of_v< Eigen::ArrayBase< T >, T >, T > limited (T x)
 
template<typename T >
void reorder (vector< T > &v, vector< int > const &order)
 
template<typename T >
array< Array< T,-1, 1 >, 2 > split (const Array< T,-1, 1 > &v, const ArrayXb &mask)
 split Eigen matrix or array into two by mask
 
template<std::ranges::range T>
void print (T t)
 prints comma delimited container contents.
 
template<class Vector , class T >
void unique_insert (Vector &v, const T &t)
 unique insertion into a vector. allows a vector to be used like a set. source: http://www.lafstern.org/matt/col1.pdf
 
template<typename T , std::size_t... Indices>
auto vectorToTupleHelper (const std::vector< T > &v, std::index_sequence< Indices... >)
 
template<std::size_t N, typename T >
auto vectorToTuple (const std::vector< T > &v)
 
template<typename R , typename Arg , typename... Args>
R apply (const std::function< R(Args...)> &f, const vector< Arg > &inputs)
 
template<class T , class U >
std::vector< T > slice (const vector< T > &v, const U &idx)
 
template<typename K , typename V >
static map< V, K > reverse_map (const map< K, V > &m)
 Given a map from keys to values, creates a new map from values to keys.
 
template<typename T >
ostream & operator<< (ostream &os, const vector< T > &v)
 

Variables

static Loggerlogger = *Logger::initLogger()
 
static Rndr = *Rnd::initRand()
 
string PBSTR = "===================="
 
int PBWIDTH = 20
 
TypeMap< std::string > type_names
 

Detailed Description

namespace containing various utility functions

Typedef Documentation

◆ Log_stats

◆ TypeMap

template<typename T >
using Brush::Util::TypeMap = std::map<std::type_index, T>

Definition at line 175 of file utils.h.

Function Documentation

◆ apply()

template<typename R , typename Arg , typename... Args>
R Brush::Util::apply ( const std::function< R(Args...)> & f,
const vector< Arg > & inputs )

Definition at line 691 of file utils.h.

Here is the call graph for this function:

◆ argmiddle()

int Brush::Util::argmiddle ( vector< float > & v)

returns the (first) index of the element with the middlest value in v

Definition at line 300 of file utils.cpp.

Here is the call graph for this function:

◆ argsort()

template<typename T >
vector< size_t > Brush::Util::argsort ( const vector< T > & v,
bool ascending = true )

return indices that sort a vector

Definition at line 247 of file utils.h.

Here is the call graph for this function:

◆ clean()

void Brush::Util::clean ( ArrayXf & x)

limits node output to be between MIN_FLT and MAX_FLT

Definition at line 17 of file utils.cpp.

Here is the call graph for this function:

◆ condition_number()

float Brush::Util::condition_number ( const MatrixXf & X)

returns true for elements of x that are infinite

returns the condition number of a matrix.

Definition at line 172 of file utils.cpp.

Here is the call graph for this function:

◆ corrcoef()

MatrixXf Brush::Util::corrcoef ( const MatrixXf & X)

returns the pearson correlation coefficients of matrix.

Definition at line 200 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ covariance()

float Brush::Util::covariance ( const ArrayXf & x,
const ArrayXf & y )

covariance of x and y

Definition at line 346 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_dtypes()

vector< type_index > Brush::Util::get_dtypes ( MatrixXf & X)

calculates data types for each column of X

Definition at line 41 of file utils.cpp.

Here is the call graph for this function:

◆ HandleErrorNoThrow()

void Brush::Util::HandleErrorNoThrow ( string err,
const char * file,
int line )

prints error to stderr and returns

Definition at line 25 of file error.cpp.

Here is the call graph for this function:

◆ HandleErrorThrow()

void Brush::Util::HandleErrorThrow ( string err,
const char * file,
int line )

prints error and throws an exception

Definition at line 13 of file error.cpp.

Here is the call graph for this function:

◆ in()

template<typename V , typename T >
bool Brush::Util::in ( const V & v,
const T & i )

check if element is in vector.

Definition at line 192 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kurtosis()

float Brush::Util::kurtosis ( const ArrayXf & v)

calculate kurtosis

Definition at line 335 of file utils.cpp.

Here is the call graph for this function:

◆ limited() [1/2]

template<typename T >
std::enable_if_t< std::is_scalar_v< T >, T > Brush::Util::limited ( T x)

limits the output to finite real numbers

Definition at line 447 of file utils.h.

Here is the call graph for this function:

◆ limited() [2/2]

template<typename T >
std::enable_if_t< std::is_base_of_v< Eigen::ArrayBase< T >, T >, T > Brush::Util::limited ( T x)

Definition at line 461 of file utils.h.

Here is the call graph for this function:

◆ ltrim()

std::string Brush::Util::ltrim ( std::string str,
const std::string & chars )

Definition at line 24 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mad()

float Brush::Util::mad ( const ArrayXf & x)

median absolute deviation

Definition at line 373 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mask_to_index()

vector< size_t > Brush::Util::mask_to_index ( const ArrayXb & mask)

convert a boolean mask to an index array

Definition at line 409 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mask_to_indices()

tuple< vector< size_t >, vector< size_t > > Brush::Util::mask_to_indices ( const ArrayXb & mask)

returns 2 indices: first where mask is true, and second where mask is false.

convert a boolean mask into true and false index arrays

Definition at line 421 of file utils.cpp.

Here is the call graph for this function:

◆ mean_square_corrcoef()

float Brush::Util::mean_square_corrcoef ( const MatrixXf & X)

Definition at line 219 of file utils.cpp.

Here is the call graph for this function:

◆ median()

template<typename T , typename Scalar = T::Scalar>
Scalar Brush::Util::median ( const T & v)

calculate median

calculate median

Definition at line 202 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE()

Brush::Util::NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE ( Log_Stats ,
generation ,
time ,
best_score ,
best_score_v ,
med_score ,
med_score_v ,
med_size ,
med_complexity ,
max_size ,
max_complexity  )

◆ operator<<()

template<typename T >
ostream & Brush::Util::operator<< ( ostream & os,
const vector< T > & v )

Definition at line 746 of file utils.h.

Here is the call graph for this function:

◆ pearson_correlation()

float Brush::Util::pearson_correlation ( const ArrayXf & x,
const ArrayXf & y )

the normalized covariance of x and y

Definition at line 366 of file utils.cpp.

Here is the call graph for this function:

◆ print()

template<std::ranges::range T>
void Brush::Util::print ( T t)

prints comma delimited container contents.

Definition at line 523 of file utils.h.

Here is the call graph for this function:

◆ reorder()

template<typename T >
void Brush::Util::reorder ( vector< T > & v,
vector< int > const & order )

Definition at line 470 of file utils.h.

Here is the call graph for this function:

◆ ReplaceString()

std::string Brush::Util::ReplaceString ( std::string subject,
const std::string & search,
const std::string & replace )

find and replace string

Definition at line 387 of file utils.cpp.

Here is the call graph for this function:

◆ ReplaceStringInPlace()

void Brush::Util::ReplaceStringInPlace ( std::string & subject,
const std::string & search,
const std::string & replace )

string find and replace in place

Definition at line 398 of file utils.cpp.

Here is the call graph for this function:

◆ reverse_map()

template<typename K , typename V >
static map< V, K > Brush::Util::reverse_map ( const map< K, V > & m)
static

Given a map from keys to values, creates a new map from values to keys.

Definition at line 738 of file utils.h.

Here is the call graph for this function:

◆ rtrim()

std::string Brush::Util::rtrim ( std::string str,
const std::string & chars )

Definition at line 30 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ skew()

float Brush::Util::skew ( const ArrayXf & v)

calculate skew

Definition at line 323 of file utils.cpp.

Here is the call graph for this function:

◆ slice()

template<class T , class U >
std::vector< T > Brush::Util::slice ( const vector< T > & v,
const U & idx )

Definition at line 726 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ slope()

float Brush::Util::slope ( const ArrayXf & x,
const ArrayXf & y )

slope of x/y

Definition at line 359 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ softmax()

template<typename T >
vector< T > Brush::Util::softmax ( const vector< T > & w)

return the softmax transformation of a vector.

Definition at line 296 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ split()

template<typename T >
array< Array< T,-1, 1 >, 2 > Brush::Util::split ( const Array< T,-1, 1 > & v,
const ArrayXb & mask )

split Eigen matrix or array into two by mask

Definition at line 496 of file utils.h.

Here is the call graph for this function:

◆ to_string()

template<typename T >
string Brush::Util::to_string ( const T & value)

template function to convert objects to string for logging

Definition at line 369 of file utils.h.

Here is the caller graph for this function:

◆ trim()

std::string Brush::Util::trim ( std::string str,
const std::string & chars )

Definition at line 36 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unique() [1/4]

template<typename T >
vector< T > Brush::Util::unique ( Array< T, -1, 1 > w)

returns unique elements in 1d Eigen array

Definition at line 361 of file utils.h.

Here is the call graph for this function:

◆ unique() [2/4]

template<typename T >
vector< T > Brush::Util::unique ( Matrix< T, -1, -1 > w)

returns unique elements in Eigen matrix of variable rows/cols

Definition at line 345 of file utils.h.

Here is the call graph for this function:

◆ unique() [3/4]

template<typename T >
vector< T > Brush::Util::unique ( Matrix< T, -1, 1 > w)

returns unique elements in Eigen vector

Definition at line 353 of file utils.h.

Here is the call graph for this function:

◆ unique() [4/4]

template<typename T >
vector< T > Brush::Util::unique ( vector< T > w)

returns unique elements in vector

Definition at line 334 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unique_insert()

template<class Vector , class T >
void Brush::Util::unique_insert ( Vector & v,
const T & t )

unique insertion into a vector. allows a vector to be used like a set. source: http://www.lafstern.org/matt/col1.pdf

Definition at line 670 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ variance() [1/2]

float Brush::Util::variance ( const ArrayXf & v)

calculate variance

Definition at line 317 of file utils.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ variance() [2/2]

float Brush::Util::variance ( const ArrayXf & v,
float mean )

calculate variance when mean provided

◆ vectorToTuple()

template<std::size_t N, typename T >
auto Brush::Util::vectorToTuple ( const std::vector< T > & v)

Definition at line 685 of file utils.h.

Here is the call graph for this function:

◆ vectorToTupleHelper()

template<typename T , std::size_t... Indices>
auto Brush::Util::vectorToTupleHelper ( const std::vector< T > & v,
std::index_sequence< Indices... >  )

Definition at line 680 of file utils.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ logger

Logger& Brush::Util::logger = *Logger::initLogger()
static

Definition at line 60 of file logger.h.

◆ PBSTR

string Brush::Util::PBSTR = "===================="

Definition at line 13 of file utils.cpp.

◆ PBWIDTH

int Brush::Util::PBWIDTH = 20

Definition at line 14 of file utils.cpp.

◆ r

Rnd& Brush::Util::r = *Rnd::initRand()
static

Definition at line 174 of file rnd.h.

◆ type_names

TypeMap< std::string > Brush::Util::type_names
Initial value:
= {
{ typeid(int) , "int" },
{ typeid(float) , "float" },
{ typeid(bool) , "bool" },
{ typeid(ArrayXf) , "ArrayXf" },
{ typeid(ArrayXi) , "ArrayXi" },
{ typeid(ArrayXb) , "ArrayXb" }
}
void bind_engine(py::module &m, string name)
Eigen::Array< bool, Eigen::Dynamic, 1 > ArrayXb
Definition functions.h:25

Definition at line 281 of file utils.cpp.