Skip to content
Snippets Groups Projects
Commit 6a3c464d authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH: DataSet has a new method for saving data to text files.

parent b601c67b
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
// //
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <boost/serialization/export.hpp> #include <boost/serialization/export.hpp>
#include "DataSetSerialization.h" #include "DataSetSerialization.h"
#include "../exceptions.h" #include "exceptions.h"
BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::DataSet); BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::DataSet);
...@@ -25,7 +26,7 @@ namespace lib4neuro { ...@@ -25,7 +26,7 @@ namespace lib4neuro {
ia >> *this; ia >> *this;
ifs.close(); ifs.close();
} else { } else {
THROW_RUNTIME_ERROR("File couldn't be open!"); THROW_RUNTIME_ERROR("File " + file_path + " couldn't be open!");
} }
} }
...@@ -182,12 +183,38 @@ namespace lib4neuro { ...@@ -182,12 +183,38 @@ namespace lib4neuro {
} }
} }
void DataSet::store_text(std::string &file_path) { void DataSet::store_text(std::string file_path) {
//TODO check if stream was successfully opened
std::ofstream ofs(file_path); std::ofstream ofs(file_path);
boost::archive::text_oarchive oa(ofs);
oa << *this; if(!ofs.is_open()) {
ofs.close(); THROW_RUNTIME_ERROR("File " + file_path + " couldn't be open!");
} else {
boost::archive::text_oarchive oa(ofs);
oa << *this;
ofs.close();
}
}
void DataSet::store_data_text(std::string file_path) {
std::ofstream ofs(file_path);
if(!ofs.is_open()) {
THROW_RUNTIME_ERROR("File " + file_path + " couldn't be open!");
} else {
for (auto e : this->data) {
/* First part of the pair */
for (unsigned int i = 0; i < e.first.size() - 1; i++) {
ofs << e.first.at(i) << ",";
}
ofs << e.first.back() << " ";
/* Second part of the pair */
for (unsigned int i = 0; i < e.second.size() - 1; i++) {
ofs << e.second.at(i) << ",";
}
ofs << e.second.back() << std::endl;
}
}
} }
template<class T> template<class T>
...@@ -316,27 +343,26 @@ namespace lib4neuro { ...@@ -316,27 +343,26 @@ namespace lib4neuro {
std::vector<std::pair<std::vector<double>, std::vector<double>>> DataSet::get_random_data_batch(size_t max) { std::vector<std::pair<std::vector<double>, std::vector<double>>> DataSet::get_random_data_batch(size_t max) {
if (max <= 0) { if (max <= 0) {
return this->data; return this->data;
} else { } else {
std::vector<std::pair<std::vector<double>, std::vector<double>>> newData; std::vector<std::pair<std::vector<double>, std::vector<double>>> newData;
srand(time(NULL)); //TODO use Mersen twister from Boost srand(time(NULL)); //TODO use Mersen twister from Boost
size_t n_chosen = rand() % std::min(max, this->data.size())+1; size_t n_chosen = rand() % std::min(max, this->data.size())+1;
std::vector<size_t> chosens; std::vector<size_t> chosens;
size_t chosen; size_t chosen;
for (int i = 0; i < n_chosen; i++) { for (int i = 0; i < n_chosen; i++) {
chosen = rand() % this->data.size(); chosen = rand() % this->data.size();
auto it = std::find(chosens.begin(), chosens.end(), chosen); auto it = std::find(chosens.begin(), chosens.end(), chosen);
if (it != chosens.end()) { if (it != chosens.end()) {
i--; i--;
} else { } else {
newData.push_back(this->data.at(chosen)); newData.push_back(this->data.at(chosen));
} }
} }
return newData;
}
}
return newData;
}
}
} }
...@@ -211,8 +211,16 @@ namespace lib4neuro { ...@@ -211,8 +211,16 @@ namespace lib4neuro {
/** /**
* Stores the DataSet object to the binary file * Stores the DataSet object to the binary file
*
*/
LIB4NEURO_API void store_text(std::string file_path);
/**
* Stores the data to the text file in a human readable format
*
* @param file_path
*/ */
LIB4NEURO_API void store_text(std::string &file_path); LIB4NEURO_API void store_data_text(std::string file_path);
/** /**
* Normalizes the data set * Normalizes the data set
......
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment