Commit 63d8b367 authored by Martin Beseda's avatar Martin Beseda

NEW: New class DataSet created

parent b5ecfb4c
......@@ -2,3 +2,62 @@
// Created by martin on 7/13/18.
//
#include <iostream>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "DataSet.h"
DataSet::DataSet(std::string file_path) {
std::ifstream ifs (file_path);
boost::archive::text_iarchive ia(ifs);
ia >> *this;
ifs.close();
}
DataSet::DataSet(std::vector<std::pair<std::vector<double>, std::vector<double>>> *data_ptr) {
this->n_elements = data_ptr->size();
this->data = *data_ptr;
}
void DataSet::add_data_pair(std::vector<double> inputs, std::vector<double> outputs) {
this->n_elements++;
this->data.push_back(std::make_pair(inputs, outputs));
}
std::vector<std::pair<std::vector<double>, std::vector<double>>>* DataSet::get_data() {
return &(this->data);
}
size_t DataSet::get_n_elements() {
return this->n_elements;
}
void DataSet::print_data() {
if (n_elements) {
for (auto p : this->data) {
/* INPUT */
for (auto v : std::get<0>(p)) {
std::cout << v << " ";
}
std::cout << "-> ";
/* OUTPUT */
for (auto v : std::get<1>(p)) {
std::cout << v << " ";
}
std::cout << std::endl;
}
}
}
void DataSet::store_text(std::string file_path) {
//TODO check if stream was successfully opened
std::ofstream ofs(file_path);
boost::archive::text_oarchive oa(ofs);
oa << *this;
ofs.close();
}
\ No newline at end of file
......@@ -5,4 +5,134 @@
#ifndef INC_4NEURO_DATASET_H
#define INC_4NEURO_DATASET_H
#include <utility>
#include <vector>
#include <boost/serialization/base_object.hpp>
#include <boost/range/size_type.hpp>
/**
* Class representing data, which can be used for training
* and testing purposes.
*/
class DataSet {
friend class boost::serialization::access;
private:
/**
* Number of elements in the data set
*/
size_t n_elements;
/**
* Stored data in the format of pairs of corresponding
* input and output vectors
*/
std::vector<std::pair<std::vector<double>, std::vector<double>>> data;
protected:
/**
* Serialization function
* @tparam Archive Boost library template
* @param ar Boost parameter - filled automatically during serialization!
* @param version Boost parameter - filled automatically during serialization!
*/
template<class Archive>
void serialize(Archive & ar, const unsigned int version){
if(Archive::is_loading::value) {
/* LOADING data */
ar & this->n_elements;
std::vector<std::pair<std::vector<double>, std::vector<double>>> data_tmp;
unsigned int v_size; // Size of the vector which is going to be loaded
double tmp;
for(unsigned int i=0; i < this->n_elements; i++) {
/* INPUT vector */;
ar & v_size;
std::vector<double> inputs;
for(unsigned int i=0; i < v_size; i++) {
ar & tmp;
inputs.push_back(tmp);
}
/* OUTPUT vector */
ar & v_size;
std::vector<double> outputs;
for(unsigned int i=0; i < v_size; i++) {
ar & tmp;
outputs.push_back(tmp);
}
/* Append to the data vector */
data_tmp.emplace_back(std::make_pair(inputs, outputs));
}
this->data = data_tmp;
} else {
/* STORING data */
ar & this->n_elements;
size_t size;
for(const auto p : this->data) {
/* Input vector */
size = std::get<0>(p).size();
ar & size;
for(auto val : std::get<0>(p)) {
ar & val;
}
/* Output vector */
size = std::get<1>(p).size();
ar & size;
for(auto val : std::get<1>(p)) {
ar & val;
}
}
}
};
public:
/**
* Constructor reading data from the file
* @param file_path Path to the file with stored data set
*/
DataSet(std::string file_path);
/**
* Constructor accepting data vector
* @param data_ptr Pointer to the vector containing data
*/
DataSet(std::vector<std::pair<std::vector<double>, std::vector<double>>>* data_ptr);
/**
* Getter for number of elements
* @return Number of elements in the data set
*/
size_t get_n_elements();
/**
* Getter for the data structure
* @return Vector of data
*/
std::vector<std::pair<std::vector<double>, std::vector<double>>>* get_data();
/**
* Adds a new pair of data to the data set
* @param inputs Vector of input data
* @param outputs Vector of output data corresponding to the input data
*/
void add_data_pair(std::vector<double> inputs, std::vector<double> outputs);
/**
* Prints the data set
*/
void print_data();
/**
* Stores the DataSet object to the binary file
*/
void store_text(std::string file_path);
};
#endif //INC_4NEURO_DATASET_H
Markdown is supported
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