diff --git a/src/DataSet/DataSet.cpp b/src/DataSet/DataSet.cpp index cfb4c7ad1d3453dec3ed8908e2729afa7e3a7e47..98579370bd17aaf70dc1343c6a2673ce0049b4de 100644 --- a/src/DataSet/DataSet.cpp +++ b/src/DataSet/DataSet.cpp @@ -3,10 +3,11 @@ // #include <algorithm> +#include <filesystem> #include <boost/serialization/export.hpp> #include "DataSetSerialization.h" -#include "../exceptions.h" +#include "exceptions.h" BOOST_CLASS_EXPORT_IMPLEMENT(lib4neuro::DataSet); @@ -25,7 +26,7 @@ namespace lib4neuro { ia >> *this; ifs.close(); } 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 { } } - void DataSet::store_text(std::string &file_path) { - //TODO check if stream was successfully opened + void DataSet::store_text(std::string file_path) { std::ofstream ofs(file_path); - boost::archive::text_oarchive oa(ofs); - oa << *this; - ofs.close(); + + if(!ofs.is_open()) { + 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> @@ -316,27 +343,26 @@ namespace lib4neuro { std::vector<std::pair<std::vector<double>, std::vector<double>>> DataSet::get_random_data_batch(size_t max) { if (max <= 0) { return this->data; - } else { - std::vector<std::pair<std::vector<double>, std::vector<double>>> newData; - srand(time(NULL)); //TODO use Mersen twister from Boost - - size_t n_chosen = rand() % std::min(max, this->data.size())+1; - std::vector<size_t> chosens; - size_t chosen; - + } else { + std::vector<std::pair<std::vector<double>, std::vector<double>>> newData; + srand(time(NULL)); //TODO use Mersen twister from Boost + + size_t n_chosen = rand() % std::min(max, this->data.size())+1; + std::vector<size_t> chosens; + size_t chosen; + for (int i = 0; i < n_chosen; i++) { - chosen = rand() % this->data.size(); - auto it = std::find(chosens.begin(), chosens.end(), chosen); - + chosen = rand() % this->data.size(); + auto it = std::find(chosens.begin(), chosens.end(), chosen); + if (it != chosens.end()) { - i--; - } else { - newData.push_back(this->data.at(chosen)); - } - } - - return newData; - } - } + i--; + } else { + newData.push_back(this->data.at(chosen)); + } + } + return newData; + } + } } diff --git a/src/DataSet/DataSet.h b/src/DataSet/DataSet.h index 018ac196bc3164fa24a2813f8e52ca24d178e51a..e1ff9bb1109b1fe26376d39db93f9ea1c85be22f 100644 --- a/src/DataSet/DataSet.h +++ b/src/DataSet/DataSet.h @@ -211,8 +211,16 @@ namespace lib4neuro { /** * 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 diff --git a/exceptions.h b/src/exceptions.h similarity index 100% rename from exceptions.h rename to src/exceptions.h