Add class Paths and enhance input

This commit is contained in:
Ricardo Montañana Gómez 2023-08-14 00:40:31 +02:00
parent 3691cb4a61
commit 55d21294d5
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
6 changed files with 98 additions and 60 deletions

10
src/Platform/Paths.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef PATHS_H
#define PATHS_H
namespace platform {
class Paths {
public:
static std::string datasets() { return "datasets/"; }
static std::string results() { return "results/"; }
};
}
#endif

View File

@ -22,7 +22,7 @@ namespace platform {
duration = data["duration"]; duration = data["duration"];
model = data["model"]; model = data["model"];
} }
json Result::load() json Result::load() const
{ {
ifstream resultData(path + "/" + filename); ifstream resultData(path + "/" + filename);
if (resultData.is_open()) { if (resultData.is_open()) {
@ -70,7 +70,6 @@ namespace platform {
if (i == max && max != 0) { if (i == max && max != 0) {
break; break;
} }
} }
} }
int Results::getIndex(const string& intent) const int Results::getIndex(const string& intent) const
@ -81,70 +80,98 @@ namespace platform {
if (index >= 0 && index < files.size()) { if (index >= 0 && index < files.size()) {
return index; return index;
} }
cout << "Invalid index" << endl; cout << "Invalid index" << endl;
return -1; return -1;
} }
void Results::report(const int index) const
{
cout << "Reporting " << files.at(index).getFilename() << endl;
auto data = files.at(index).load();
Report report(data);
report.show();
}
void Results::menu() void Results::menu()
{ {
cout << "Choose option (quit='q', list='l', delete='d', hide='h', sort='s', report='r'): ";
char option; char option;
int index; int index;
string filename; bool finished = false;
cin >> option; string filename, line, options = "qldhsr";
switch (option) { while (!finished) {
case 'q': cout << "Choose option (quit='q', list='l', delete='d', hide='h', sort='s', report='r'): ";
exit(0); getline(cin, line);
case 'l': if (line.size() == 0)
show(); continue;
menu(); if (options.find(line[0]) != string::npos) {
break; if (line.size() > 1) {
case 'd': cout << "Invalid option" << endl;
index = getIndex("delete"); continue;
if (index == -1) }
option = line[0];
} else {
index = stoi(line);
if (index >= 0 && index < files.size()) {
report(index);
} else {
cout << "Invalid option" << endl;
}
continue;
}
switch (option) {
case 'q':
finished = true;
break; break;
filename = files[index].getFilename(); case 'l':
cout << "Deleting " << filename << endl; show();
remove((path + "/" + filename).c_str());
files.erase(files.begin() + index);
show();
menu();
break;
case 'h':
index = getIndex("hide");
if (index == -1)
break; break;
filename = files[index].getFilename(); case 'd':
cout << "Hiding " << filename << endl; index = getIndex("delete");
rename((path + "/" + filename).c_str(), (path + "/." + filename).c_str()); if (index == -1)
files.erase(files.begin() + index); break;
show(); filename = files[index].getFilename();
menu(); cout << "Deleting " << filename << endl;
break; remove((path + "/" + filename).c_str());
case 's': files.erase(files.begin() + index);
sortList(); show();
show();
menu();
break;
case 'r':
index = getIndex("report");
if (index == -1)
break; break;
filename = files[index].getFilename(); case 'h':
cout << "Reporting " << filename << endl; index = getIndex("hide");
auto data = files[index].load(); if (index == -1)
Report report(data); break;
report.show(); filename = files[index].getFilename();
menu(); cout << "Hiding " << filename << endl;
break; rename((path + "/" + filename).c_str(), (path + "/." + filename).c_str());
files.erase(files.begin() + index);
show();
menu();
break;
case 's':
sortList();
show();
break;
case 'r':
index = getIndex("report");
if (index == -1)
break;
report(index);
break;
default:
cout << "Invalid option" << endl;
}
} }
} }
void Results::sortList() void Results::sortList()
{ {
cout << "Choose sorting field (date='d', score='s', duration='u', model='m'): "; cout << "Choose sorting field (date='d', score='s', duration='u', model='m'): ";
string line;
char option; char option;
cin >> option; getline(cin, line);
if (line.size() == 0)
return;
if (line.size() > 1) {
cout << "Invalid option" << endl;
return;
}
option = line[0];
switch (option) { switch (option) {
case 'd': case 'd':
sortDate(); sortDate();
@ -161,7 +188,6 @@ namespace platform {
default: default:
cout << "Invalid option" << endl; cout << "Invalid option" << endl;
} }
} }
void Results::sortDate() void Results::sortDate()
{ {
@ -195,6 +221,7 @@ namespace platform {
} }
show(); show();
menu(); menu();
cout << "Done!" << endl;
} }
} }

View File

@ -11,7 +11,7 @@ namespace platform {
class Result { class Result {
public: public:
Result(const string& path, const string& filename); Result(const string& path, const string& filename);
json load(); json load() const;
string to_string() const; string to_string() const;
string getFilename() const { return filename; }; string getFilename() const { return filename; };
string getDate() const { return date; }; string getDate() const { return date; };
@ -42,6 +42,7 @@ namespace platform {
vector<Result> files; vector<Result> files;
void load(); // Loads the list of results void load(); // Loads the list of results
void show() const; void show() const;
void report(const int index) const;
int getIndex(const string& intent) const; int getIndex(const string& intent) const;
void menu(); void menu();
void sortList(); void sortList();

View File

@ -6,10 +6,10 @@
#include "DotEnv.h" #include "DotEnv.h"
#include "Models.h" #include "Models.h"
#include "modelRegister.h" #include "modelRegister.h"
#include "Paths.h"
using namespace std; using namespace std;
const string PATH_RESULTS = "results";
const string PATH_DATASETS = "datasets";
argparse::ArgumentParser manageArguments(int argc, char** argv) argparse::ArgumentParser manageArguments(int argc, char** argv)
{ {
@ -18,8 +18,7 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
program.add_argument("-d", "--dataset").default_value("").help("Dataset file name"); program.add_argument("-d", "--dataset").default_value("").help("Dataset file name");
program.add_argument("-p", "--path") program.add_argument("-p", "--path")
.help("folder where the data files are located, default") .help("folder where the data files are located, default")
.default_value(string{ PATH_DATASETS } .default_value(string{ platform::Paths::datasets() });
);
program.add_argument("-m", "--model") program.add_argument("-m", "--model")
.help("Model to use " + platform::Models::instance()->toString()) .help("Model to use " + platform::Models::instance()->toString())
.action([](const std::string& value) { .action([](const std::string& value) {
@ -115,7 +114,7 @@ int main(int argc, char** argv)
experiment.go(filesToTest, path); experiment.go(filesToTest, path);
experiment.setDuration(timer.getDuration()); experiment.setDuration(timer.getDuration());
if (saveResults) if (saveResults)
experiment.save(PATH_RESULTS); experiment.save(platform::Paths::results());
else else
experiment.report(); experiment.report();
cout << "Done!" << endl; cout << "Done!" << endl;

View File

@ -1,10 +1,10 @@
#include <iostream> #include <iostream>
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include "platformUtils.h" #include "platformUtils.h"
#include "Paths.h"
#include "Results.h" #include "Results.h"
using namespace std; using namespace std;
const string PATH_RESULTS = "results";
argparse::ArgumentParser manageArguments(int argc, char** argv) argparse::ArgumentParser manageArguments(int argc, char** argv)
{ {
@ -35,7 +35,7 @@ int main(int argc, char** argv)
auto number = program.get<int>("number"); auto number = program.get<int>("number");
auto model = program.get<string>("model"); auto model = program.get<string>("model");
auto score = program.get<string>("score"); auto score = program.get<string>("score");
auto results = platform::Results(PATH_RESULTS, number, model, score); auto results = platform::Results(platform::Paths::results(), number, model, score);
results.manage(); results.manage();
return 0; return 0;
} }

View File

@ -1,4 +1,5 @@
#include "platformUtils.h" #include "platformUtils.h"
#include "Paths.h"
using namespace torch; using namespace torch;
@ -85,7 +86,7 @@ tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadData
tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(const string& name) tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(const string& name)
{ {
auto handler = ArffFiles(); auto handler = ArffFiles();
handler.load(PATH + static_cast<string>(name) + ".arff"); handler.load(platform::Paths::datasets() + static_cast<string>(name) + ".arff");
// Get Dataset X, y // Get Dataset X, y
vector<mdlp::samples_t>& X = handler.getX(); vector<mdlp::samples_t>& X = handler.getX();
mdlp::labels_t& y = handler.getY(); mdlp::labels_t& y = handler.getY();