Commit b29c370b authored by Alessio Netti's avatar Alessio Netti
Browse files

Analytics: fixing covariance computation and general I/O

parent 0daba507
......@@ -82,7 +82,7 @@ void CSOperator::execOnInit() {
_trainingUnit = _units[0]->getName();
_actualBlocks = _units[0]->getInputs().size() < _numBlocks ? _units[0]->getInputs().size() : _numBlocks;
if(_actualBlocks!=_numBlocks)
LOG(warning) << "Operator " + _name + ": cannot enforce " + _numBlocks + ", using " + _actualBlocks + " instead.";
LOG(warning) << "Operator " << _name << ": cannot enforce " << _numBlocks << ", using " << _actualBlocks << " instead.";
} else {
_actualBlocks = _numBlocks;
}
......@@ -162,14 +162,14 @@ bool CSOperator::dumpToFile(std::string &path) {
return false;
// Saving CS data in terms of permutation index, minimum and maximum for each input sensor
for(size_t idx=0; idx<_actualBlocks; idx++) {
for(size_t idx=0; idx<_permVector.size(); idx++) {
boost::property_tree::ptree group;
group.push_back(boost::property_tree::ptree::value_type("idx", std::to_string(_permVector[idx])));
group.push_back(boost::property_tree::ptree::value_type("min", std::to_string(_min[idx])));
group.push_back(boost::property_tree::ptree::value_type("max", std::to_string(_max[idx])));
blocks.add_child(std::to_string(idx), group);
}
root.add_child(std::to_string(_actualBlocks), blocks);
root.add_child(std::to_string(_permVector.size()), blocks);
try {
std::ofstream outFile(path);
......@@ -185,21 +185,21 @@ bool CSOperator::readFromFile(std::string &path) {
boost::property_tree::read_json(path, config);
} catch(const std::exception &e) { return false; }
// The root JSON node encodes the number of blocks and has to match with that of the operator
std::string blockString = std::to_string(_actualBlocks);
if(config.find(blockString) == config.not_found())
// The root JSON node encodes the number of sensors with which the model was trained
if(config.begin() == config.end() || stoull(config.begin()->first) < _actualBlocks)
return false;
std::vector<size_t> newPermVector(_actualBlocks);
std::vector<int64_t> newMin(_actualBlocks);
std::vector<int64_t> newMax(_actualBlocks);
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config.get_child(blockString)) {
uint64_t numSensors = stoull(config.begin()->first);
std::vector<size_t> newPermVector(numSensors);
std::vector<int64_t> newMin(numSensors);
std::vector<int64_t> newMax(numSensors);
BOOST_FOREACH(boost::property_tree::iptree::value_type &val, config.begin()->second) {
size_t blockID = std::stoull(val.first);
boost::property_tree::iptree &blk = val.second;
if(blk.find("idx")==blk.not_found() || blk.find("min")==blk.not_found() || blk.find("max")==blk.not_found())
return false;
if(blockID>=_actualBlocks)
if(blockID>=numSensors)
return false;
BOOST_FOREACH(boost::property_tree::iptree::value_type &val2, blk) {
......@@ -262,13 +262,13 @@ void CSOperator::computePermutation(std::vector<std::vector<reading_t>>& v) {
boost::math::cubic_b_spline<double> spline(sValues.begin(), sValues.end(), startInterp, stepInterp);
// Evaluating in the interpolated points and storing in the matrix
for(size_t idx2=0; idx2<_trainingSamples; idx2++)
sensorMatrix.at<double>(idx, idx2) = spline(stepEval*idx2);
sensorMatrix.at<double>(idx2, idx) = spline(stepEval*idx2);
sValues.clear();
}
// Calculating covariance matrix
cv::Mat covMatrix, meanMatrix;
cv::calcCovarMatrix(sensorMatrix, covMatrix, meanMatrix, cv::COVAR_COLS + cv::COVAR_SCALE + cv::COVAR_NORMAL, CV_64F);
cv::calcCovarMatrix(sensorMatrix, covMatrix, meanMatrix, cv::COVAR_ROWS + cv::COVAR_SCALE + cv::COVAR_NORMAL, CV_64F);
sensorMatrix.release();
meanMatrix.release();
// Transforming the matrix
......
......@@ -111,7 +111,7 @@ protected:
// CS data
size_t _actualBlocks;
unsigned long long _actualBlocks;
float _blockLen;
std::vector<size_t> _permVector;
std::vector<int64_t> _min;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment