Fix some lint warnings

This commit is contained in:
Ricardo Montañana Gómez 2023-07-29 20:20:38 +02:00
parent 9a0449c12d
commit 5efa3beaee
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
7 changed files with 33 additions and 40 deletions

View File

@ -20,7 +20,7 @@ namespace bayesnet {
{ {
return samples; return samples;
} }
void Network::addNode(string name, int numStates) void Network::addNode(const string& name, int numStates)
{ {
if (find(features.begin(), features.end(), name) == features.end()) { if (find(features.begin(), features.end(), name) == features.end()) {
features.push_back(name); features.push_back(name);
@ -69,7 +69,7 @@ namespace bayesnet {
recStack.erase(nodeId); // remove node from recursion stack before function ends recStack.erase(nodeId); // remove node from recursion stack before function ends
return false; return false;
} }
void Network::addEdge(const string parent, const string child) void Network::addEdge(const string& parent, const string& child)
{ {
if (nodes.find(parent) == nodes.end()) { if (nodes.find(parent) == nodes.end()) {
throw invalid_argument("Parent node " + parent + " does not exist"); throw invalid_argument("Parent node " + parent + " does not exist");
@ -105,8 +105,8 @@ namespace bayesnet {
for (int i = 0; i < featureNames.size(); ++i) { for (int i = 0; i < featureNames.size(); ++i) {
auto column = torch::flatten(X.index({ "...", i })); auto column = torch::flatten(X.index({ "...", i }));
auto k = vector<int>(); auto k = vector<int>();
for (auto i = 0; i < X.size(0); ++i) { for (auto z = 0; z < X.size(0); ++z) {
k.push_back(column[i].item<int>()); k.push_back(column[z].item<int>());
} }
dataset[featureNames[i]] = k; dataset[featureNames[i]] = k;
} }
@ -280,7 +280,7 @@ namespace bayesnet {
} }
return result; return result;
} }
vector<string> Network::graph(string title) vector<string> Network::graph(const string& title)
{ {
auto output = vector<string>(); auto output = vector<string>();
auto prefix = "digraph BayesNet {\nlabel=<BayesNet "; auto prefix = "digraph BayesNet {\nlabel=<BayesNet ";

View File

@ -32,8 +32,8 @@ namespace bayesnet {
explicit Network(Network&); explicit Network(Network&);
torch::Tensor& getSamples(); torch::Tensor& getSamples();
float getmaxThreads(); float getmaxThreads();
void addNode(string, int); void addNode(const string&, int);
void addEdge(const string, const string); void addEdge(const string&, const string&);
map<string, std::unique_ptr<Node>>& getNodes(); map<string, std::unique_ptr<Node>>& getNodes();
vector<string> getFeatures(); vector<string> getFeatures();
int getStates(); int getStates();
@ -48,7 +48,7 @@ namespace bayesnet {
vector<vector<double>> predict_proba(const vector<vector<int>>&); vector<vector<double>> predict_proba(const vector<vector<int>>&);
double score(const vector<vector<int>>&, const vector<int>&); double score(const vector<vector<int>>&, const vector<int>&);
vector<string> show(); vector<string> show();
vector<string> graph(string title); // Returns a vector of strings representing the graph in graphviz format vector<string> graph(const string& title); // Returns a vector of strings representing the graph in graphviz format
inline string version() { return "0.1.0"; } inline string version() { return "0.1.0"; }
}; };
} }

View File

@ -52,9 +52,9 @@ namespace platform {
seeds_str = trim(seeds_str); seeds_str = trim(seeds_str);
seeds_str = seeds_str.substr(1, seeds_str.size() - 2); seeds_str = seeds_str.substr(1, seeds_str.size() - 2);
auto seeds_str_split = split(seeds_str, ','); auto seeds_str_split = split(seeds_str, ',');
for (auto seed_str : seeds_str_split) { transform(seeds_str_split.begin(), seeds_str_split.end(), back_inserter(seeds), [](const std::string& str) {
seeds.push_back(stoi(seed_str)); return stoi(str);
} });
return seeds; return seeds;
} }
}; };

View File

@ -40,7 +40,7 @@ namespace platform {
string Models::toString() string Models::toString()
{ {
string result = ""; string result = "";
for (auto& pair : functionRegistry) { for (const auto& pair : functionRegistry) {
result += pair.first + ", "; result += pair.first + ", ";
} }
return "{" + result.substr(0, result.size() - 2) + "}"; return "{" + result.substr(0, result.size() - 2) + "}";

View File

@ -49,22 +49,17 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
}}); }});
auto seed_values = env.getSeeds(); auto seed_values = env.getSeeds();
program.add_argument("-s", "--seeds").nargs(1, 10).help("Random seeds. Set to -1 to have pseudo random").scan<'i', int>().default_value(seed_values); program.add_argument("-s", "--seeds").nargs(1, 10).help("Random seeds. Set to -1 to have pseudo random").scan<'i', int>().default_value(seed_values);
bool class_last, discretize_dataset, stratified;
int n_folds;
vector<int> seeds;
string model_name, file_name, path, complete_file_name, title;
try { try {
program.parse_args(argc, argv); program.parse_args(argc, argv);
file_name = program.get<string>("dataset"); auto file_name = program.get<string>("dataset");
path = program.get<string>("path"); auto path = program.get<string>("path");
model_name = program.get<string>("model"); auto model_name = program.get<string>("model");
discretize_dataset = program.get<bool>("discretize"); auto discretize_dataset = program.get<bool>("discretize");
stratified = program.get<bool>("stratified"); auto stratified = program.get<bool>("stratified");
n_folds = program.get<int>("folds"); auto n_folds = program.get<int>("folds");
seeds = program.get<vector<int>>("seeds"); auto seeds = program.get<vector<int>>("seeds");
complete_file_name = path + file_name + ".arff"; auto complete_file_name = path + file_name + ".arff";
class_last = false;//datasets[file_name]; auto title = program.get<string>("title");
title = program.get<string>("title");
if (title == "" && file_name == "") { if (title == "" && file_name == "") {
throw runtime_error("title is mandatory if dataset is not provided"); throw runtime_error("title is mandatory if dataset is not provided");
} }

View File

@ -2,7 +2,7 @@
using namespace torch; using namespace torch;
vector<string> split(string text, char delimiter) vector<string> split(const string& text, char delimiter)
{ {
vector<string> result; vector<string> result;
stringstream ss(text); stringstream ss(text);
@ -39,7 +39,7 @@ vector<mdlp::labels_t> discretizeDataset(vector<mdlp::samples_t>& X, mdlp::label
return Xd; return Xd;
} }
bool file_exists(const std::string& name) bool file_exists(const string& name)
{ {
if (FILE* file = fopen(name.c_str(), "r")) { if (FILE* file = fopen(name.c_str(), "r")) {
fclose(file); fclose(file);
@ -49,7 +49,7 @@ bool file_exists(const std::string& name)
} }
} }
tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(string path, string name, bool class_last, bool discretize_dataset) tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(const string& path, const string& name, bool class_last, bool discretize_dataset)
{ {
auto handler = ArffFiles(); auto handler = ArffFiles();
handler.load(path + static_cast<string>(name) + ".arff", class_last); handler.load(path + static_cast<string>(name) + ".arff", class_last);
@ -59,9 +59,8 @@ tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadData
// Get className & Features // Get className & Features
auto className = handler.getClassName(); auto className = handler.getClassName();
vector<string> features; vector<string> features;
for (auto feature : handler.getAttributes()) { auto attributes = handler.getAttributes();
features.push_back(feature.first); transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
}
Tensor Xd; Tensor Xd;
auto states = map<string, vector<int>>(); auto states = map<string, vector<int>>();
if (discretize_dataset) { if (discretize_dataset) {
@ -83,7 +82,7 @@ tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadData
return { Xd, torch::tensor(y, torch::kInt32), features, className, states }; return { Xd, torch::tensor(y, torch::kInt32), features, className, states };
} }
tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(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(PATH + static_cast<string>(name) + ".arff");
@ -93,9 +92,8 @@ tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vect
// Get className & Features // Get className & Features
auto className = handler.getClassName(); auto className = handler.getClassName();
vector<string> features; vector<string> features;
for (auto feature : handler.getAttributes()) { auto attributes = handler.getAttributes();
features.push_back(feature.first); transform(attributes.begin(), attributes.end(), back_inserter(features), [](const auto& pair) { return pair.first; });
}
// Discretize Dataset // Discretize Dataset
vector<mdlp::labels_t> Xd; vector<mdlp::labels_t> Xd;
map<string, int> maxes; map<string, int> maxes;

View File

@ -11,11 +11,11 @@ using namespace std;
const string PATH = "../../data/"; const string PATH = "../../data/";
bool file_exists(const std::string& name); bool file_exists(const std::string& name);
vector<string> split(string text, char delimiter); vector<string> split(const string& text, char delimiter);
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features); pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features);
vector<mdlp::labels_t> discretizeDataset(vector<mdlp::samples_t>& X, mdlp::labels_t& y); vector<mdlp::labels_t> discretizeDataset(vector<mdlp::samples_t>& X, mdlp::labels_t& y);
pair<torch::Tensor, map<string, vector<int>>> discretizeTorch(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className); pair<torch::Tensor, map<string, vector<int>>> discretizeTorch(torch::Tensor& X, torch::Tensor& y, vector<string>& features, const string& className);
tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(string name); tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(const string& name);
tuple<torch::Tensor, torch::Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(string path, string name, bool class_last, bool discretize_dataset); tuple<torch::Tensor, torch::Tensor, vector<string>, string, map<string, vector<int>>> loadDataset(const string& path, const string& name, bool class_last, bool discretize_dataset);
map<string, vector<int>> get_states(vector<string>& features, string className, map<string, int>& maxes); map<string, vector<int>> get_states(vector<string>& features, string className, map<string, int>& maxes);
#endif //PLATFORM_UTILS_H #endif //PLATFORM_UTILS_H