1 // Copyright (C) 2013-2019 CEA/DEN
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 \date Wed Nov 20 11:04:17 2013
25 \brief Header de la classe PMMLlib
32 #include "PMMLwin.hxx"
34 #include <libxml/xpathInternals.h>
43 std::string NumberToString ( T Number )
45 std::ostringstream ss;
51 * Enumeration to type the PMML file.
52 * UNDEFINED: not yet defined
53 * ANN: Artificial Neural Network
54 * LR: Linear Regression
56 * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdGroup_MODEL-ELEMENT
58 enum PMMLType{kUNDEFINED, kANN, kLR};
62 * @see http://www.dmg.org/v4-1/NeuralNetwork.html#xsdType_ACTIVATION-FUNCTION
64 enum PMMLActivationFunction{kIDENTITY, kTANH, kLOGISTIC};
68 * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdType_MINING-FUNCTION
70 enum PMMLMiningFunction{kREGRESSION};
80 bool _log; //!< Log Printing
81 std::string _pmmlFile; //!< Name of the associated PMML file
82 xmlDocPtr _doc; //!< Associated DOM documents
83 xmlNodePtr _rootNode; //!< Root node of the document
84 xmlNodePtr _currentNode; //!< Pointer to the current node
85 int _nbModels; //!< Number of models (all kinds)
86 std::string _currentModelName; //!< Name of the current model
87 PMMLType _currentModelType; //!< Type of the current model
88 xmlNodePtr _currentModelNode; //!< Pointer to the current model node
90 /** @defgroup general General methods
91 * Common methods to all kinds of PMML files and models
95 PMMLLIB_EXPORT PMMLlib(std::string file,
97 PMMLLIB_EXPORT PMMLlib(bool log=false);
98 PMMLLIB_EXPORT ~PMMLlib();
99 PMMLLIB_EXPORT void SetCurrentModel(std::string modelName,
101 PMMLLIB_EXPORT void SetCurrentModel(std::string modelName);
102 PMMLLIB_EXPORT void SetCurrentModel();
103 PMMLLIB_EXPORT std::string makeLog() const;
104 PMMLLIB_EXPORT void printLog() const;
106 PMMLLIB_EXPORT void AddDataField(std::string name,
107 std::string displayName,
109 std::string dataType,
113 bool interval=false);
114 PMMLLIB_EXPORT void AddMiningSchema(std::string name,
115 std::string usageType);
116 PMMLLIB_EXPORT void SetHeader(std::string copyright,
117 std::string description,
119 std::string appVersion,
120 std::string annotation);
121 PMMLLIB_EXPORT void UnlinkNode();
122 PMMLLIB_EXPORT void BackupNode();
123 PMMLLIB_EXPORT int GetModelsNb();
124 PMMLLIB_EXPORT void Write();
125 PMMLLIB_EXPORT void Write(std::string file);
126 PMMLLIB_EXPORT PMMLType GetCurrentModelType();
127 PMMLLIB_EXPORT std::string GetCurrentModelName();
129 xmlNodePtr GetChildByName(xmlNodePtr node,
130 std::string nodename);
131 xmlNodePtr GetPtr(int ann_index,
133 xmlNodePtr GetPtr(std::string ann_name,
136 int CountNeuralNetModels();
137 int CountRegressionModels();
139 std::string GetModelName(xmlNodePtr node);
140 std::string GetTypeString();
142 /** @} */ // end of group general
145 /** @defgroup ann Methods dedicated to neural networks
146 * Methods dedicated to neural networks
150 PMMLLIB_EXPORT void AddNeuralNetwork(std::string modelName,
151 PMMLMiningFunction functionName);
152 PMMLLIB_EXPORT void AddNeuralInput(int id,
153 std::string inputName,
155 std::string dataType,
156 double orig1, double norm1,
157 double orig2, double norm2);
158 PMMLLIB_EXPORT void AddNeuralLayer(PMMLActivationFunction activationFunction);
159 PMMLLIB_EXPORT void AddNeuron(int id,
163 std::vector<double> weights);
164 PMMLLIB_EXPORT void AddNeuralOutput(int outputNeuron,
165 std::string outputName,
167 std::string dataType,
168 double orig1, double norm1,
169 double orig2, double norm2);
170 PMMLLIB_EXPORT int GetNbInputs();
171 PMMLLIB_EXPORT int GetNbOutputs();
172 PMMLLIB_EXPORT std::string GetNameInput(int input_index);
173 PMMLLIB_EXPORT std::string GetNameOutput(int output_index);
174 PMMLLIB_EXPORT int GetNormalizationType();
175 PMMLLIB_EXPORT void GetNormalisationInput(int input_index,
177 PMMLLIB_EXPORT void GetNormalisationOutput(int output_index,
179 PMMLLIB_EXPORT int GetNbHiddenLayers();
180 PMMLLIB_EXPORT int GetNbLayers();
181 PMMLLIB_EXPORT int GetNbNeuronsAtLayer(int layer_index);
182 PMMLLIB_EXPORT double GetNeuronBias(int layer_index,
184 PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index,
187 PMMLLIB_EXPORT void SetNeuralNetName(int ann_index,
188 std::string ann_name);
189 PMMLLIB_EXPORT std::string ReadNetworkStructure();
191 xmlNodePtr GetNeuralNetPtr(std::string ann_name);
192 xmlNodePtr GetNeuralNetPtr(int ann_index);
193 void CheckNeuralNetwork();
194 /** @} */ // end of group ann
197 /** @defgroup ln Methods dedicated to linear regression
198 * Methods dedicated to linear regression
202 PMMLLIB_EXPORT void AddRegressionModel(std::string modelName,
203 PMMLMiningFunction functionName,
204 std::string targetFieldName);
205 PMMLLIB_EXPORT void AddRegressionTable();
206 PMMLLIB_EXPORT void AddRegressionTable(double intercept);
207 PMMLLIB_EXPORT void AddNumericPredictor(std::string neuronName,
210 PMMLLIB_EXPORT void AddPredictorTerm(double coefficient,
211 std::vector<std::string> fieldRef);
212 PMMLLIB_EXPORT bool HasIntercept();
213 PMMLLIB_EXPORT double GetRegressionTableIntercept();
214 PMMLLIB_EXPORT int GetNumericPredictorNb();
215 PMMLLIB_EXPORT int GetPredictorTermNb();
216 PMMLLIB_EXPORT std::string GetNumericPredictorName(int num_pred_index);
217 PMMLLIB_EXPORT std::string GetPredictorTermName(int num_pred_index);
218 PMMLLIB_EXPORT double GetNumericPredictorCoefficient(int num_pred_index);
219 PMMLLIB_EXPORT double GetPredictorTermCoefficient(int pred_term_index);
220 PMMLLIB_EXPORT int GetPredictorTermFieldRefNb(int pred_term_index);
221 PMMLLIB_EXPORT std::string GetPredictorTermFieldRefName(int pred_term_index,
223 PMMLLIB_EXPORT std::string ReadRegressionStructure();
225 xmlNodePtr GetRegressionPtr(int reg_index);
226 xmlNodePtr GetRegressionPtr(std::string reg_name);
227 void CheckRegression();
229 /** @} */ // end of group ln
232 /** @defgroup export Methods dedicated to file export
233 * Methods dedicated to file export
237 void fillVectorsForExport(int nInput, int nOutput, int nHidden, int normType,
238 std::vector<double> &minInput, std::vector<double> &maxInput,
239 std::vector<double> &minOutput, std::vector<double> &maxOutput,
240 std::vector<double> &valW );
242 PMMLLIB_EXPORT void ExportCpp(std::string file,
243 std::string functionName,
245 PMMLLIB_EXPORT void ExportFortran(std::string file,
246 std::string functionName,
248 PMMLLIB_EXPORT void ExportPython(std::string file,
249 std::string functionName,
251 PMMLLIB_EXPORT std::string ExportPyStr(std::string functionName,
254 void ExportNeuralNetworkCpp(std::string file,
255 std::string functionName,
257 void ExportNeuralNetworkFortran(std::string file,
258 std::string functionName,
260 void ExportNeuralNetworkPython(std::string file,
261 std::string functionName,
263 std::string ExportNeuralNetworkPyStr(std::string functionName,
266 void ExportLinearRegressionCpp(std::string,
269 void ExportLinearRegressionFortran(std::string,
272 void ExportLinearRegressionPython(std::string,
275 std::string ExportLinearRegressionPyStr(std::string functionName,
277 /** @} */ // end of group export
280 /*! * Conversion from a libxml2 string (xmlChar *) to a standard C++ string.
282 * \param xs a constant libxml string.
283 * \return a C++ std::string (contains the same text as xs).
285 std::string _xmlCharToString(const xmlChar *xs) const;
287 * Conversion from a standard C++ string to a libxml2 string (xmlChar *)
289 * \param s a constant C++ std::string.
290 * \return a libxml string (contains the same text as s)
292 * The caller of this function must free the result when it's not needed
293 * anymore (using xmlFree)
295 xmlChar * _stringToXmlChar(const std::string &s) const;
297 std::string _getProp(const xmlNodePtr node,
298 std::string const & prop ) const;
304 #endif //__PMMLLIB_H__