Page MenuHomec4science

DataLayer.cpp
No OneTemporary

File Metadata

Created
Mon, May 6, 04:18

DataLayer.cpp

#include <DataLayer.hpp>
#include <stdexcept> // std::invalid_argument
#include <cmath> // log()
#include <Matrix2D.hpp>
#include <Matrix3D.hpp>
#include <Matrix4D.hpp>
#include <ThreadPool.hpp>
DataLayer::DataLayer()
{}
DataLayer::DataLayer(const Matrix2D<int>& data,
size_t n_class,
size_t n_shift,
bool flip,
bool last_class_cst)
:data(data),
flip(flip),
n_row(this->data.get_nrow()),
n_col(this->data.get_ncol()),
n_class(n_class),
l_model(n_col - n_shift + 1),
n_shift(n_shift),
n_flip(flip + 1),
last_class_cst(last_class_cst)
{ // models cannot be initialise here
// as the number of categories depend
// on the exact class
}
DataLayer::DataLayer(Matrix2D<int>&& data,
size_t n_class,
size_t n_shift,
bool flip,
bool last_class_cst)
:data(data),
flip(flip),
n_row(this->data.get_nrow()),
n_col(this->data.get_ncol()),
n_class(n_class),
l_model(n_col - n_shift + 1),
n_shift(n_shift),
n_flip(flip + 1),
last_class_cst(last_class_cst)
{ // models cannot be initialise here
// as the number of categories depend
// on the exact class
}
DataLayer::DataLayer(const Matrix2D<int>& data,
const Matrix3D<double>& model,
bool flip,
bool last_class_cst)
: data(data),
model(model),
flip(flip),
n_row(this->data.get_nrow()),
n_col(this->data.get_ncol()),
n_class(this->model.get_dim()[0]),
l_model(this->model.get_dim()[1]),
n_category(this->model.get_dim()[2]),
n_shift(n_col - l_model + 1),
n_flip(flip + 1),
last_class_cst(last_class_cst)
{ // check if model is not too long
if(this->n_col < this->l_model)
{ char msg[4096] ;
sprintf(msg,
"Error! model is longer than data : %zu / %zu",
this->l_model, this->n_col) ;
throw std::invalid_argument(msg) ;
}
this->n_shift = this->n_col - this->l_model + 1 ;
}
DataLayer::DataLayer(Matrix2D<int>&& data,
Matrix3D<double>&& model,
bool flip,
bool last_class_cst)
: data(data),
model(model),
flip(flip),
n_row(this->data.get_nrow()),
n_col(this->data.get_ncol()),
n_class(this->model.get_dim()[0]),
l_model(this->model.get_dim()[1]),
n_category(this->model.get_dim()[2]),
n_shift(n_col - l_model + 1),
n_flip(flip + 1),
last_class_cst(last_class_cst)
{ // check if model is not too long
if(this->n_col < this->l_model)
{ char msg[4096] ;
sprintf(msg,
"Error! model is longer than data : %zu / %zu",
this->l_model, this->n_col) ;
throw std::invalid_argument(msg) ;
}
this->n_shift = this->n_col - this->l_model + 1 ;
}
DataLayer::~DataLayer()
{}
void DataLayer::set_class(size_t i, const Matrix2D<double>& class_model)
{ // check dimensions
if(class_model.get_nrow() != this->n_category)
{ char msg[4096] ;
sprintf(msg, "Error! the given class model is incompatible "
"with the model (%zu rows instead of %zu)",
class_model.get_nrow(), this->n_category) ;
throw std::invalid_argument(msg) ;
}
else if(class_model.get_ncol() != this->l_model)
{ char msg[4096] ;
sprintf(msg, "Error! the given class model is incompatible "
"with the model (%zu columns instead of %zu)",
class_model.get_ncol(), this->l_model) ;
throw std::invalid_argument(msg) ;
}
for(size_t j=0; j<class_model.get_ncol(); j++)
{ for(size_t k=0; k<class_model.get_nrow(); k++)
{ this->model(i,j,k) = class_model(k,j) ; }
}
}
Matrix3D<double> DataLayer::get_model() const
{ return this->model ; }
void DataLayer::check_loglikelihood_dim(const Matrix4D<double>& loglikelihood) const
{ if(loglikelihood.get_dim()[0] != this->n_row)
{ char msg[4096] ;
sprintf(msg,
"Error! loglikelihood matrix 1st dimension is not "
"equal to data row number : %zu / %zu",
loglikelihood.get_dim()[0], this->n_row) ;
throw std::invalid_argument(msg) ;
}
else if(loglikelihood.get_dim()[1] != this->n_class)
{ char msg[4096] ;
sprintf(msg,
"Error! loglikelihood matrix 2nd dimension is not "
"equal to model class number : %zu / %zu",
loglikelihood.get_dim()[1], this->n_class) ;
throw std::invalid_argument(msg) ;
}
else if(loglikelihood.get_dim()[2] != this->n_shift)
{ char msg[4096] ;
sprintf(msg,
"Error! loglikelihood matrix 3rd dimension is not "
"equal to model shift state number : %zu / %zu",
loglikelihood.get_dim()[2], this->n_shift) ;
throw std::invalid_argument(msg) ;
}
else if(loglikelihood.get_dim()[3] != this->n_flip)
{ char msg[4096] ;
sprintf(msg,
"Error! loglikelihood matrix 4th dimension is not "
"equal to model flip state number : %zu / %zu",
loglikelihood.get_dim()[3], this->n_flip) ;
throw std::invalid_argument(msg) ;
}
}
void DataLayer::check_loglikelihood_max_dim(const vector_d& loglikelihood_max) const
{ if(loglikelihood_max.size() != this->n_row)
{ char msg[4096] ;
sprintf(msg,
"Error! loglikelihood_max length is not "
"equal to data row number : %zu / %zu",
loglikelihood_max.size(), this->n_flip) ;
throw std::invalid_argument(msg) ;
}
}
void DataLayer::check_posterior_prob_dim(const Matrix4D<double>& posterior_prob) const
{ if(posterior_prob.get_dim()[0] != this->n_row)
{ char msg[4096] ;
sprintf(msg,
"Error! posterior_prob matrix 1st dimension is not "
"equal to data row number : %zu / %zu",
posterior_prob.get_dim()[0], this->n_row) ;
throw std::invalid_argument(msg) ;
}
else if(posterior_prob.get_dim()[1] != this->n_class)
{ char msg[4096] ;
sprintf(msg,
"Error! posterior_prob matrix 2nd dimension is not "
"equal to model class number : %zu / %zu",
posterior_prob.get_dim()[1], this->n_class) ;
throw std::invalid_argument(msg) ;
}
else if(posterior_prob.get_dim()[2] != this->n_shift)
{ char msg[4096] ;
sprintf(msg,
"Error! posterior_prob matrix 3rd dimension is not "
"equal to model shift state number : %zu / %zu",
posterior_prob.get_dim()[2], this->n_shift) ;
throw std::invalid_argument(msg) ;
}
else if(posterior_prob.get_dim()[3] != this->n_flip)
{ char msg[4096] ;
sprintf(msg,
"Error! posterior_prob matrix 4th dimension is not "
"equal to model flip state number : %zu / %zu",
posterior_prob.get_dim()[3], this->n_flip) ;
throw std::invalid_argument(msg) ;
}
}
const double DataLayer::p_min = 1e-100 ;
const double DataLayer::p_min_log = log(DataLayer::p_min) ;

Event Timeline