#ifndef EXPERIMENTS_HPP #define EXPERIMENTS_HPP #include #include #include #include #include #include #include "../typesFImdlp.h" class Experiment { public: Experiment(float from_, float to_, float step_, int n_bins, std::string strategy, std::vector data_discretized, std::vector cutpoints) : from_{ from_ }, to_{ to_ }, step_{ step_ }, n_bins_{ n_bins }, strategy_{ strategy }, discretized_data_{ data_discretized }, cutpoints_{ cutpoints } { if (strategy != "Q" && strategy != "U") { throw std::invalid_argument("Invalid strategy " + strategy); } } float from_; float to_; float step_; int n_bins_; std::string strategy_; std::vector discretized_data_; std::vector cutpoints_; }; class Experiments { public: Experiments(const std::string filename) : filename{ filename } { test_file.open(filename); if (!test_file.is_open()) { throw std::runtime_error("File " + filename + " not found"); } exp_end = false; } ~Experiments() { test_file.close(); } bool end() const { return exp_end; } bool is_next() { while (std::getline(test_file, line) && line[0] == '#'); if (test_file.eof()) { exp_end = true; return false; } return true; } Experiment next() { return parse_experiment(line); } private: std::tuple parse_header(const std::string& line) { std::istringstream iss(line); std::string from_, to_, step_, n_bins, strategy; iss >> from_ >> to_ >> step_ >> n_bins >> strategy; return { std::stof(from_), std::stof(to_), std::stof(step_), std::stoi(n_bins), strategy }; } template std::vector parse_vector(const std::string& line) { std::istringstream iss(line); std::vector data; std::string d; while (iss >> d) { data.push_back(std::is_same::value ? std::stof(d) : std::stoi(d)); } return data; } Experiment parse_experiment(std::string& line) { auto [from_, to_, step_, n_bins, strategy] = parse_header(line); std::getline(test_file, line); auto data_discretized = parse_vector(line); std::getline(test_file, line); auto cutpoints = parse_vector(line); return Experiment{ from_, to_, step_, n_bins, strategy, data_discretized, cutpoints }; } std::ifstream test_file; std::string filename; std::string line; bool exp_end; }; template void show_vector(const std::vector& data, std::string title) { std::cout << title << ": "; std::string sep = ""; for (const auto& d : data) { std::cout << sep << d; sep = ", "; } std::cout << std::endl; } #endif