MeLOn
AnnParser.h
Go to the documentation of this file.
1 /**********************************************************************************
2 * Copyright (c) 2020 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * SPDX-License-Identifier: EPL-2.0
9 *
10 * @file AnnParser.h
11 *
12 * @brief File containing declaration of the ann parser classes.
13 *
14 **********************************************************************************/
15 
16 #pragma once
17 
18 #include <string> // std::string
19 #include <vector> // std::vector
20 #include <memory> // std::shared_ptr, std::unique_ptr
21 
22 #include "tinyxml2.h"
23 #include "AnnProperties.h"
24 #include "modelParser.h"
25 
26 namespace melon {
27 
34  class AnnParser: public ModelParser {
35 
36  protected:
44  ACTIVATION_FUNCTION _string_to_activation_function(const std::string& activationFunctionName);
45  public:
46 
56  virtual std::shared_ptr<ModelData> parse_model(const std::string modelPath, const std::string modelName) = 0;
57  };
58 
65  class AnnParserCsv : public AnnParser {
66 
67  const int LAYER_INDICATOR_BASE = 900;
68 
76  std::vector<std::vector<std::string>> _csv_to_string_matrix(std::string fileName);
77 
85  std::vector<std::vector<double>> _csv_to_double_matrix(std::string fileName);
86 
92  void _parse_config_file(AnnStructure& structure);
93 
103  void _parse_scalers(std::shared_ptr<ScalerData> inputScalerData, std::shared_ptr<ScalerData> outputScalerData, const AnnStructure& structure);
104 
112  void _parse_bias_weights(const AnnStructure& structure, AnnWeights& weights);
113 
121  void _parse_layer_weights(const AnnStructure& structure, AnnWeights& weights);
122 
130  void _parse_input_weights(const AnnStructure& structure, AnnWeights& weights);
131 
132 
140  bool _check_if_layer_indicator(int number);
141 
142 
150  int _get_layer_index_from_indicator(int indicator);
151  public:
161  std::shared_ptr<ModelData> parse_model(const std::string modelPath, const std::string modelName);
162  };
163 
170  class AnnParserXml : public AnnParser {
182  tinyxml2::XMLError _parse_vector_int(tinyxml2::XMLElement *parentElement, const std::string vectorName, std::vector<int>& vector);
183 
195  tinyxml2::XMLError _parse_vector_double(tinyxml2::XMLElement *parentElement, const std::string vectorName, std::vector<double>& vector);
196  public:
197 
207  std::shared_ptr<ModelData> parse_model(const std::string modelPath, const std::string modelName);
208  };
209 
210 
215  class AnnParserFactory: public ModelParserFactory {
216  public:
217 
225  std::unique_ptr<ModelParser> create_model_parser(const MODEL_FILE_TYPE fileType) override;
226  };
227 
228 }
melon::MODEL_FILE_TYPE
MODEL_FILE_TYPE
Enum for representing the parsable filetypes.
Definition: modelParser.h:43
melon::AnnParserCsv::_parse_bias_weights
void _parse_bias_weights(const AnnStructure &structure, AnnWeights &weights)
Parses the bias weights.
Definition: AnnParserCSV.cpp:177
melon::AnnParser::parse_model
virtual std::shared_ptr< ModelData > parse_model(const std::string modelPath, const std::string modelName)=0
Abstract function for defining the structure of the parsing function which is used to get the ANN dat...
tinyxml2::XMLError
XMLError
Definition: tinyxml2.h:522
melon::AnnParserXml::parse_model
std::shared_ptr< ModelData > parse_model(const std::string modelPath, const std::string modelName)
Parsing function which is used to get the ANN data from a xml file.
Definition: AnnParserXML.cpp:26
melon::AnnParserCsv::_csv_to_double_matrix
std::vector< std::vector< double > > _csv_to_double_matrix(std::string fileName)
Parses the content of an csv file into a double matrix.
Definition: AnnParserCSV.cpp:384
melon::AnnParserCsv::_parse_layer_weights
void _parse_layer_weights(const AnnStructure &structure, AnnWeights &weights)
Parses the layer weights.
Definition: AnnParserCSV.cpp:309
melon::ACTIVATION_FUNCTION
ACTIVATION_FUNCTION
Enum for representing the available types of scaler parameters.
Definition: AnnProperties.h:43
melon::AnnParserCsv::_get_layer_index_from_indicator
int _get_layer_index_from_indicator(int indicator)
Extracts layer index from a layer indicator.
Definition: AnnParserCSV.cpp:461
melon::AnnParserCsv::_check_if_layer_indicator
bool _check_if_layer_indicator(int number)
Checks if passed number is a layer indicator.
Definition: AnnParserCSV.cpp:454
melon::AnnParserFactory
This class is a factory class for creating child instances of AnnParser.
Definition: AnnParser.h:228
melon::AnnParser::_string_to_activation_function
ACTIVATION_FUNCTION _string_to_activation_function(const std::string &activationFunctionName)
Turns a string containing the name of an activation function in the correct enum representation.
Definition: AnnParser.cpp:41
melon::AnnParserCsv::parse_model
std::shared_ptr< ModelData > parse_model(const std::string modelPath, const std::string modelName)
Parsing function which is used to get the ANN data from a csv file.
Definition: AnnParserCSV.cpp:33
melon::AnnParserXml::_parse_vector_int
tinyxml2::XMLError _parse_vector_int(tinyxml2::XMLElement *parentElement, const std::string vectorName, std::vector< int > &vector)
Parses child elements of an xml element into an vector of int.
Definition: AnnParserXML.cpp:363
melon::AnnParserFactory::create_model_parser
std::unique_ptr< ModelParser > create_model_parser(const MODEL_FILE_TYPE fileType) override
Factory function for creating a instance of an ann parser corresponding to the specified file type.
Definition: AnnParser.cpp:25
AnnProperties.h
melon::AnnParserXml::_parse_vector_double
tinyxml2::XMLError _parse_vector_double(tinyxml2::XMLElement *parentElement, const std::string vectorName, std::vector< double > &vector)
Parses child elements of an xml element into an vector of double.
Definition: AnnParserXML.cpp:384
melon::ModelParserFactory
This class is a abstract parent class for model parser factories implemented in the MeLOn library.
Definition: modelParser.h:112
melon::AnnParserCsv::_csv_to_string_matrix
std::vector< std::vector< std::string > > _csv_to_string_matrix(std::string fileName)
Parses the content of an csv file into a string matrix.
Definition: AnnParserCSV.cpp:419
tinyxml2.h
melon
Definition: kernel.h:21
melon::AnnParserCsv::_parse_config_file
void _parse_config_file(AnnStructure &structure)
Parses the configuration csv file.
Definition: AnnParserCSV.cpp:59
melon::AnnParserCsv::_parse_scalers
void _parse_scalers(std::shared_ptr< ScalerData > inputScalerData, std::shared_ptr< ScalerData > outputScalerData, const AnnStructure &structure)
Parses the input and output scalers.
Definition: AnnParserCSV.cpp:131
tinyxml2::XMLElement
Definition: tinyxml2.h:1259
melon::AnnParserCsv::_parse_input_weights
void _parse_input_weights(const AnnStructure &structure, AnnWeights &weights)
Parses the input weights.
Definition: AnnParserCSV.cpp:239
melon::AnnParserCsv::LAYER_INDICATOR_BASE
const int LAYER_INDICATOR_BASE
Definition: AnnParser.h:80
modelParser.h