1 //////////////////////////////////////////////////////////////
2 // Copyright (C) 2013-2015 CEA/DEN
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published
6 // by the Free Software Foundation, either version 3 of the License, or any
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //////////////////////////////////////////////////////////////
20 \date Wed Nov 20 11:04:17 2013
22 \brief Header de la classe PMMLlib
29 #include "PMMLwin.hxx"
31 #include <libxml/xpathInternals.h>
40 std::string NumberToString ( T Number )
42 std::ostringstream ss;
48 * Enumeration to type the PMML file.
49 * UNDEFINED: not yet defined
50 * ANN: Artificial Neural Network
51 * LR: Linear Regression
53 * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdGroup_MODEL-ELEMENT
55 enum PMMLType{kUNDEFINED, kANN, kLR};
59 * @see http://www.dmg.org/v4-1/NeuralNetwork.html#xsdType_ACTIVATION-FUNCTION
61 enum PMMLActivationFunction{kIDENTITY, kTANH, kLOGISTIC};
65 * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdType_MINING-FUNCTION
67 enum PMMLMiningFunction{kREGRESSION};
77 bool _log; //!< Log Printing
78 std::string _pmmlFile; //!< Name of the associated PMML file
79 xmlDocPtr _doc; //!< Associated DOM documents
80 xmlNodePtr _rootNode; //!< Root node of the document
81 xmlNodePtr _currentNode; //!< Pointer to the current node
82 int _nbModels; //!< Number of models (all kinds)
83 std::string _currentModelName; //!< Name of the current model
84 PMMLType _currentModelType; //!< Type of the current model
85 xmlNodePtr _currentModelNode; //!< Pointer to the current model node
87 /** @defgroup general General methods
88 * Common methods to all kinds of PMML files and models
92 PMMLLIB_EXPORT PMMLlib(std::string file,
94 PMMLLIB_EXPORT PMMLlib(bool log=false);
95 PMMLLIB_EXPORT ~PMMLlib();
96 PMMLLIB_EXPORT void SetCurrentModel(std::string modelName,
98 PMMLLIB_EXPORT void SetCurrentModel(std::string modelName);
99 PMMLLIB_EXPORT void SetCurrentModel();
100 PMMLLIB_EXPORT std::string makeLog() const;
101 PMMLLIB_EXPORT void printLog() const;
103 PMMLLIB_EXPORT void AddDataField(std::string name,
104 std::string displayName,
106 std::string dataType,
110 bool interval=false);
111 PMMLLIB_EXPORT void AddMiningSchema(std::string name,
112 std::string usageType);
113 PMMLLIB_EXPORT void SetHeader(std::string copyright,
114 std::string description,
116 std::string appVersion,
117 std::string annotation);
118 PMMLLIB_EXPORT void UnlinkNode();
119 PMMLLIB_EXPORT void BackupNode();
120 PMMLLIB_EXPORT int GetModelsNb();
121 PMMLLIB_EXPORT void Write();
122 PMMLLIB_EXPORT void Write(std::string file);
123 PMMLLIB_EXPORT PMMLType GetCurrentModelType();
124 PMMLLIB_EXPORT std::string GetCurrentModelName();
126 xmlNodePtr GetChildByName(xmlNodePtr node,
127 std::string nodename);
128 xmlNodePtr GetPtr(int ann_index,
130 xmlNodePtr GetPtr(std::string ann_name,
133 int CountNeuralNetModels();
134 int CountRegressionModels();
136 std::string GetModelName(xmlNodePtr node);
137 std::string GetTypeString();
139 /** @} */ // end of group general
142 /** @defgroup ann Methods dedicated to neural networks
143 * Methods dedicated to neural networks
147 PMMLLIB_EXPORT void AddNeuralNetwork(std::string modelName,
148 PMMLMiningFunction functionName);
149 PMMLLIB_EXPORT void AddNeuralInput(int id,
150 std::string inputName,
152 std::string dataType,
153 double orig1, double norm1,
154 double orig2, double norm2);
155 PMMLLIB_EXPORT void AddNeuralLayer(PMMLActivationFunction activationFunction);
156 PMMLLIB_EXPORT void AddNeuron(int id,
160 std::vector<double> weights);
161 PMMLLIB_EXPORT void AddNeuralOutput(int outputNeuron,
162 std::string outputName,
164 std::string dataType,
165 double orig1, double norm1,
166 double orig2, double norm2);
167 PMMLLIB_EXPORT int GetNbInputs();
168 PMMLLIB_EXPORT int GetNbOutputs();
169 PMMLLIB_EXPORT std::string GetNameInput(int input_index);
170 PMMLLIB_EXPORT std::string GetNameOutput(int output_index);
171 PMMLLIB_EXPORT int GetNormalizationType();
172 PMMLLIB_EXPORT void GetNormalisationInput(int input_index,
174 PMMLLIB_EXPORT void GetNormalisationOutput(int output_index,
176 PMMLLIB_EXPORT int GetNbHiddenLayers();
177 PMMLLIB_EXPORT int GetNbLayers();
178 PMMLLIB_EXPORT int GetNbNeuronsAtLayer(int layer_index);
179 PMMLLIB_EXPORT double GetNeuronBias(int layer_index,
181 PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index,
184 PMMLLIB_EXPORT void SetNeuralNetName(int ann_index,
185 std::string ann_name);
186 PMMLLIB_EXPORT std::string ReadNetworkStructure();
188 xmlNodePtr GetNeuralNetPtr(std::string ann_name);
189 xmlNodePtr GetNeuralNetPtr(int ann_index);
190 void CheckNeuralNetwork();
191 /** @} */ // end of group ann
194 /** @defgroup ln Methods dedicated to linear regression
195 * Methods dedicated to linear regression
199 PMMLLIB_EXPORT void AddRegressionModel(std::string modelName,
200 PMMLMiningFunction functionName,
201 std::string targetFieldName);
202 PMMLLIB_EXPORT void AddRegressionTable();
203 PMMLLIB_EXPORT void AddRegressionTable(double intercept);
204 PMMLLIB_EXPORT void AddNumericPredictor(std::string neuronName,
207 PMMLLIB_EXPORT void AddPredictorTerm(double coefficient,
208 std::vector<std::string> fieldRef);
209 PMMLLIB_EXPORT bool HasIntercept();
210 PMMLLIB_EXPORT double GetRegressionTableIntercept();
211 PMMLLIB_EXPORT int GetNumericPredictorNb();
212 PMMLLIB_EXPORT int GetPredictorTermNb();
213 PMMLLIB_EXPORT std::string GetNumericPredictorName(int num_pred_index);
214 PMMLLIB_EXPORT std::string GetPredictorTermName(int num_pred_index);
215 PMMLLIB_EXPORT double GetNumericPredictorCoefficient(int num_pred_index);
216 PMMLLIB_EXPORT double GetPredictorTermCoefficient(int pred_term_index);
217 PMMLLIB_EXPORT int GetPredictorTermFieldRefNb(int pred_term_index);
218 PMMLLIB_EXPORT std::string GetPredictorTermFieldRefName(int pred_term_index,
220 PMMLLIB_EXPORT std::string ReadRegressionStructure();
222 xmlNodePtr GetRegressionPtr(int reg_index);
223 xmlNodePtr GetRegressionPtr(std::string reg_name);
224 void CheckRegression();
226 /** @} */ // end of group ln
229 /** @defgroup export Methods dedicated to file export
230 * Methods dedicated to file export
234 void fillVectorsForExport(int nInput, int nOutput, int nHidden, int normType,
235 std::vector<double> &minInput, std::vector<double> &maxInput,
236 std::vector<double> &minOutput, std::vector<double> &maxOutput,
237 std::vector<double> &valW );
239 PMMLLIB_EXPORT void ExportCpp(std::string file,
240 std::string functionName,
242 PMMLLIB_EXPORT void ExportFortran(std::string file,
243 std::string functionName,
245 PMMLLIB_EXPORT void ExportPython(std::string file,
246 std::string functionName,
248 PMMLLIB_EXPORT std::string ExportPyStr(std::string functionName,
251 void ExportNeuralNetworkCpp(std::string file,
252 std::string functionName,
254 void ExportNeuralNetworkFortran(std::string file,
255 std::string functionName,
257 void ExportNeuralNetworkPython(std::string file,
258 std::string functionName,
260 std::string ExportNeuralNetworkPyStr(std::string functionName,
263 void ExportLinearRegressionCpp(std::string,
266 void ExportLinearRegressionFortran(std::string,
269 void ExportLinearRegressionPython(std::string,
272 std::string ExportLinearRegressionPyStr(std::string functionName,
274 /** @} */ // end of group export
277 /*! * Conversion from a libxml2 string (xmlChar *) to a standard C++ string.
279 * \param xs a constant libxml string.
280 * \return a C++ std::string (contains the same text as xs).
282 std::string _xmlCharToString(const xmlChar *xs) const;
284 * Conversion from a standard C++ string to a libxml2 string (xmlChar *)
286 * \param s a constant C++ std::string.
287 * \return a libxml string (contains the same text as s)
289 * The caller of this function must free the result when it's not needed
290 * anymore (using xmlFree)
292 xmlChar * _stringToXmlChar(const std::string &s) const;
294 std::string _getProp(const xmlNodePtr node,
295 std::string const & prop ) const;
301 #endif //__PMMLLIB_H__