diff --git a/common/inc/scaler.h b/common/inc/scaler.h index 4ff435af6b9db9fcdf1638ca085ddfe2df3f1e44..082789c6156537a137d028924464e00612116ffb 100644 --- a/common/inc/scaler.h +++ b/common/inc/scaler.h @@ -15,15 +15,13 @@ #pragma once -#include <vector> // std::vector -#include <string> // std::string -#include <memory> // std::unique_ptr, std::make_unique +#include <vector> // std::vector +#include <string> // std::string +#include <memory> // std::unique_ptr, std::make_unique +#include <unordered_map> // std::unordered_map #include "exceptions.h" -#include <nlohmann/json.hpp> -using json = nlohmann::json; - namespace melon { /* @@ -54,7 +52,8 @@ namespace melon { * @brief Base struct from which data structs of different scalers can be derived. */ struct ScalerData { - SCALER_TYPE type{SCALER_TYPE::IDENTITY}; /*!< Type of scaler*/ + ScalerData(): type(SCALER_TYPE::IDENTITY) {}; + SCALER_TYPE type; /*!< Type of scaler*/ std::unordered_map<SCALER_PARAMETER, std::vector<double>> parameters; /*!< Unordered map containing the different scaler parameters*/ }; diff --git a/common/src/modelParser.cpp b/common/src/modelParser.cpp index ec17824cef97361f243569e94d0991a5cc9d7e14..67d8fb309f7cea2ab6d4e00b2ce31fb90f76063f 100644 --- a/common/src/modelParser.cpp +++ b/common/src/modelParser.cpp @@ -15,56 +15,67 @@ #include "modelParser.h" -#include <string> // std::string +#include <string> // std::string +#include <filesystem> // std::filesystem::path, std::filesystem::absolute using namespace melon; std::string ModelParser::_format_file_path(const std::string modelPath, const std::string modelName, const MODEL_FILE_TYPE fileType) { - std::string filePath = modelPath; + std::filesystem::path filePath = modelPath; + filePath /= modelName; + filePath = filePath.lexically_normal(); - // Append filename to path - if (!filePath.empty() && filePath.back() != '/' && filePath.back() != '\\') { - filePath.push_back('/'); - } - filePath = filePath + modelName; - - // Check if filename already contains file extension, if not append it - std::string typeString; + std::string expectedExtension; switch (fileType) { case MODEL_FILE_TYPE::JSON: - typeString = ".json"; + expectedExtension = ".json"; break; case MODEL_FILE_TYPE::XML: - typeString = ".xml"; + expectedExtension = ".xml"; break; case MODEL_FILE_TYPE::CSV: - typeString = ".csv"; + expectedExtension = ".csv"; break; default: throw MelonException(" Error while formatting file path: Unkown file type."); } - std::string nameEnding = modelName.substr(modelName.length() - typeString.length()); - if (nameEnding != typeString) { - filePath += typeString; + // Check if filename already contains file extension and append it otherwise + if (filePath.has_extension()) { + std::string extension = filePath.extension().string(); + + // Check if file extension matches the expected extension + if (expectedExtension != filePath.extension()) { + throw MelonException(" Error while formatting file path: The file extension in modelName (\"" + modelName + "\") does not match the extension of the provided filetype (\"" + expectedExtension + "\")."); + } + } + else { + filePath += expectedExtension; } - return filePath; + std::filesystem::path test = std::filesystem::current_path(); + + // Convert filepath into absolut one in case it is relative + if (filePath.is_relative()) { + filePath = std::filesystem::absolute(filePath); + } + + return filePath.string(); } ///////////////////////////////////////////////////////////////////////// // Turns a string containing the name of an scaler type in the correct enum representation SCALER_TYPE ModelParser::_string_to_scaler_type(const std::string scalerTypeName) { - if (scalerTypeName.compare("Identity") == 0) { + if (scalerTypeName == "Identity") { return SCALER_TYPE::IDENTITY; } - else if (scalerTypeName.compare("MinMax") == 0) { + else if (scalerTypeName == "MinMax") { return SCALER_TYPE::MINMAX; } - else if (scalerTypeName.compare("Standard") == 0) { + else if (scalerTypeName == "Standard") { return SCALER_TYPE::STANDARD; } else { @@ -76,19 +87,19 @@ SCALER_TYPE ModelParser::_string_to_scaler_type(const std::string scalerTypeName ///////////////////////////////////////////////////////////////////////// // Turns a string containing the name of an scaler type in the correct enum representation SCALER_PARAMETER ModelParser::_string_to_scaler_parameter(const std::string scalerParameterName) { - if (scalerParameterName.compare("min") == 0) { + if (scalerParameterName == "min") { return SCALER_PARAMETER::LOWER_BOUNDS; } - else if (scalerParameterName.compare("max") == 0) { + else if (scalerParameterName == "max") { return SCALER_PARAMETER::UPPER_BOUNDS; } - else if (scalerParameterName.compare("mean") == 0) { + else if (scalerParameterName == "mean") { return SCALER_PARAMETER::MEAN; } - else if (scalerParameterName.compare("stddev") == 0) { + else if (scalerParameterName == "stddev") { return SCALER_PARAMETER::STD_DEV; } else { throw MelonException(" Error while parsing file: Unkown scaler parameter\"" + scalerParameterName + "\""); } -} \ No newline at end of file +} diff --git a/feedforward neural network/model/src/AnnParserCSV.cpp b/feedforward neural network/model/src/AnnParserCSV.cpp index ba4bd78e7274a7549742d358505c7f8ec7e5c0e0..6c19964b976ee274283d1e6ebc263357e048aa36 100644 --- a/feedforward neural network/model/src/AnnParserCSV.cpp +++ b/feedforward neural network/model/src/AnnParserCSV.cpp @@ -388,8 +388,8 @@ std::vector<std::vector<double>> AnnParserCsv::_csv_to_double_matrix(std::string std::ifstream file(filePath); if (!file.is_open()) { - throw MelonException(" Error while parsing file (csv): File \"" + filePath + "\" not found"); - } + throw MelonException(" Error while parsing model file : File \"" + filePath + "\" not found. \n Please make sure that the model file is located in the correct directory."); + } // Load lines from file to vector std::string line; @@ -423,7 +423,7 @@ std::vector<std::vector<std::string>> AnnParserCsv::_csv_to_string_matrix(std::s std::ifstream file(filePath); if (!file.is_open()) { - throw MelonException(" Error while parsing file (csv): File \"" + filePath + "\" not found"); + throw MelonException(" Error while parsing model file : File \"" + filePath + "\" not found. \n Please make sure that the model file is located in the correct directory."); } // Load lines from file to vector diff --git a/feedforward neural network/model/src/AnnParserXML.cpp b/feedforward neural network/model/src/AnnParserXML.cpp index c02f49d40b5516a0fd3681a9b3c1a81df31b45d9..8b67258456663a5d2235ad2b6f060d8fd87ac2cd 100644 --- a/feedforward neural network/model/src/AnnParserXML.cpp +++ b/feedforward neural network/model/src/AnnParserXML.cpp @@ -36,8 +36,8 @@ std::shared_ptr<ModelData> AnnParserXml::parse_model(const std::string modelPath const char* charFilePath = (char*)filePath.data(); tinyxml2::XMLError eResult = xmlDoc.LoadFile(charFilePath); if (eResult == 3) { - throw MelonException(" Error while parsing network file (xml): File \"" + filePath + "\" not found"); - } + throw MelonException(" Error while parsing model file : File \"" + filePath + "\" not found. \n Please make sure that the model file is located in the correct directory."); + } // Get root elements tinyxml2::XMLNode * pRoot = xmlDoc.FirstChild(); diff --git a/gaussian process/model/src/gpParser.cpp b/gaussian process/model/src/gpParser.cpp index 18d44b4514538bf57c3aff98ce4cc33285174833..71388df9a800ee439c99dd9da61963cc6351311f 100644 --- a/gaussian process/model/src/gpParser.cpp +++ b/gaussian process/model/src/gpParser.cpp @@ -88,7 +88,7 @@ std::shared_ptr<ModelData> GpParser::parse_model(const std::string modelPath, co // Start parsing file std::ifstream ifs(filePath); if (!ifs.is_open()) { - throw MelonException(" Error while parsing file : File \"" + filePath + "\" not found"); + throw MelonException(" Error while parsing model file : File \"" + filePath + "\" not found. \n Please make sure that the model file is located in the correct directory."); } json j;