Version: 9.15.0
PMMLlib.hxx
Go to the documentation of this file.
1 // Copyright (C) 2013-2025 CEA
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU Lesser General Public License as published
5 // by the Free Software Foundation, either version 3 of the License, or any
6 // later version.
7 //
8 // This program 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
11 // GNU Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
25 #ifndef __PMMLLIB_H__
26 #define __PMMLLIB_H__
27 
28 #include "PMMLwin.hxx"
29 
30 #include <libxml/xpathInternals.h>
31 #include <string>
32 #include <vector>
33 #include <sstream>
34 
35 namespace PMMLlib
36 {
37 
38 template <typename T>
39 std::string NumberToString ( T Number )
40 {
41  std::ostringstream ss;
42  ss << Number;
43  return ss.str();
44 }
45 
55 
56 
61 
62 
67 
68 
72 class PMMLlib
73 {
74 
75 private:
76  bool _log;
77  std::string _pmmlFile;
78  xmlDocPtr _doc;
79  xmlNodePtr _rootNode;
80  xmlNodePtr _currentNode;
81  int _nbModels;
82  std::string _currentModelName;
84  xmlNodePtr _currentModelNode;
85 
90 public:
91  PMMLLIB_EXPORT PMMLlib(std::string file,
92  bool log=false) ;
93  PMMLLIB_EXPORT PMMLlib(bool log=false);
95  PMMLLIB_EXPORT void SetCurrentModel(std::string modelName,
96  PMMLType type);
97  PMMLLIB_EXPORT void SetCurrentModel(std::string modelName);
98  PMMLLIB_EXPORT void SetCurrentModel();
99  PMMLLIB_EXPORT std::string makeLog() const;
100  PMMLLIB_EXPORT void printLog() const;
101 
102  PMMLLIB_EXPORT void AddDataField(std::string name,
103  std::string displayName,
104  std::string optype,
105  std::string dataType,
106  std::string closure,
107  double leftMargin,
108  double rightMargin,
109  bool interval=false);
110  PMMLLIB_EXPORT void AddMiningSchema(std::string name,
111  std::string usageType);
112  PMMLLIB_EXPORT void SetHeader(std::string copyright,
113  std::string description,
114  std::string appName,
115  std::string appVersion,
116  std::string annotation);
117  PMMLLIB_EXPORT void UnlinkNode();
118  PMMLLIB_EXPORT void BackupNode();
119  PMMLLIB_EXPORT int GetModelsNb();
120  PMMLLIB_EXPORT void Write();
121  PMMLLIB_EXPORT void Write(std::string file);
122  PMMLLIB_EXPORT PMMLType GetCurrentModelType();
123  PMMLLIB_EXPORT std::string GetCurrentModelName();
124 private:
125  xmlNodePtr GetChildByName(xmlNodePtr node,
126  std::string nodename);
127  xmlNodePtr GetPtr(int ann_index,
128  std::string name);
129  xmlNodePtr GetPtr(std::string ann_name,
130  std::string name);
131  void CountModels();
132  int CountNeuralNetModels();
133  int CountRegressionModels();
134  void SetRootNode();
135  std::string GetModelName(xmlNodePtr node);
136  std::string GetTypeString();
137  // end of group general
139 
140 
145 public:
146  PMMLLIB_EXPORT void AddNeuralNetwork(std::string modelName,
147  PMMLMiningFunction functionName);
148  PMMLLIB_EXPORT void AddNeuralInput(int id,
149  std::string inputName,
150  std::string optype,
151  std::string dataType,
152  double orig1, double norm1,
153  double orig2, double norm2);
154  PMMLLIB_EXPORT void AddNeuralLayer(PMMLActivationFunction activationFunction);
155  PMMLLIB_EXPORT void AddNeuron(int id,
156  double bias,
157  int conNb,
158  int firstFrom,
159  std::vector<double> weights);
160  PMMLLIB_EXPORT void AddNeuralOutput(int outputNeuron,
161  std::string outputName,
162  std::string optype,
163  std::string dataType,
164  double orig1, double norm1,
165  double orig2, double norm2);
166  PMMLLIB_EXPORT int GetNbInputs();
167  PMMLLIB_EXPORT int GetNbOutputs();
168  PMMLLIB_EXPORT std::string GetNameInput(int input_index);
169  PMMLLIB_EXPORT std::string GetNameOutput(int output_index);
170  PMMLLIB_EXPORT int GetNormalizationType();
171  PMMLLIB_EXPORT void GetNormalisationInput(int input_index,
172  double *dnorm);
173  PMMLLIB_EXPORT void GetNormalisationOutput(int output_index,
174  double *dnorm);
175  PMMLLIB_EXPORT int GetNbHiddenLayers();
176  PMMLLIB_EXPORT int GetNbLayers();
177  PMMLLIB_EXPORT int GetNbNeuronsAtLayer(int layer_index);
178  PMMLLIB_EXPORT double GetNeuronBias(int layer_index,
179  int neu_index);
180  PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index,
181  int neu_index,
182  int prec_index);
183  PMMLLIB_EXPORT void SetNeuralNetName(int ann_index,
184  std::string ann_name);
185  PMMLLIB_EXPORT std::string ReadNetworkStructure();
186 private:
187  xmlNodePtr GetNeuralNetPtr(std::string ann_name);
188  xmlNodePtr GetNeuralNetPtr(int ann_index);
189  void CheckNeuralNetwork(); // end of group ann
191 
192 
197 public:
198  PMMLLIB_EXPORT void AddRegressionModel(std::string modelName,
199  PMMLMiningFunction functionName,
200  std::string targetFieldName);
201  PMMLLIB_EXPORT void AddRegressionTable();
202  PMMLLIB_EXPORT void AddRegressionTable(double intercept);
203  PMMLLIB_EXPORT void AddNumericPredictor(std::string neuronName,
204  int exponent,
205  double coefficient);
206  PMMLLIB_EXPORT void AddPredictorTerm(double coefficient,
207  std::vector<std::string> fieldRef);
208  PMMLLIB_EXPORT bool HasIntercept();
209  PMMLLIB_EXPORT double GetRegressionTableIntercept();
210  PMMLLIB_EXPORT int GetNumericPredictorNb();
211  PMMLLIB_EXPORT int GetPredictorTermNb();
212  PMMLLIB_EXPORT std::string GetNumericPredictorName(int num_pred_index);
213  PMMLLIB_EXPORT std::string GetPredictorTermName(int num_pred_index);
214  PMMLLIB_EXPORT double GetNumericPredictorCoefficient(int num_pred_index);
215  PMMLLIB_EXPORT double GetPredictorTermCoefficient(int pred_term_index);
216  PMMLLIB_EXPORT int GetPredictorTermFieldRefNb(int pred_term_index);
217  PMMLLIB_EXPORT std::string GetPredictorTermFieldRefName(int pred_term_index,
218  int field_index);
219  PMMLLIB_EXPORT std::string ReadRegressionStructure();
220 private:
221  xmlNodePtr GetRegressionPtr(int reg_index);
222  xmlNodePtr GetRegressionPtr(std::string reg_name);
223  void CheckRegression();
224  // end of group ln
226 
227 
232 private:
233  void fillVectorsForExport(int nInput, int nOutput, int nHidden, int normType,
234  std::vector<double> &minInput, std::vector<double> &maxInput,
235  std::vector<double> &minOutput, std::vector<double> &maxOutput,
236  std::vector<double> &valW );
237 public:
238  PMMLLIB_EXPORT void ExportCpp(std::string file,
239  std::string functionName,
240  std::string header);
241  PMMLLIB_EXPORT void ExportFortran(std::string file,
242  std::string functionName,
243  std::string header);
244  PMMLLIB_EXPORT void ExportPython(std::string file,
245  std::string functionName,
246  std::string header);
247  PMMLLIB_EXPORT std::string ExportPyStr(std::string functionName,
248  std::string header);
249 private:
250  void ExportNeuralNetworkCpp(std::string file,
251  std::string functionName,
252  std::string header);
253  void ExportNeuralNetworkFortran(std::string file,
254  std::string functionName,
255  std::string header);
256  void ExportNeuralNetworkPython(std::string file,
257  std::string functionName,
258  std::string header);
259  std::string ExportNeuralNetworkPyStr(std::string functionName,
260  std::string header);
261 
262  void ExportLinearRegressionCpp(std::string,
263  std::string,
264  std::string);
265  void ExportLinearRegressionFortran(std::string,
266  std::string,
267  std::string);
268  void ExportLinearRegressionPython(std::string,
269  std::string,
270  std::string);
271  std::string ExportLinearRegressionPyStr(std::string functionName,
272  std::string header); // end of group export
274 
275  private:
281  std::string _xmlCharToString(const xmlChar *xs) const;
291  xmlChar * _stringToXmlChar(const std::string &s) const;
292 
293  std::string _getProp(const xmlNodePtr node,
294  std::string const & prop ) const;
295 
296 };
297 
298 }
299 
300 #endif //__PMMLLIB_H__
#define PMMLLIB_EXPORT
Definition: PMMLwin.hxx:42
std::string _pmmlFile
Name of the associated PMML file.
Definition: PMMLlib.hxx:77
xmlNodePtr _currentModelNode
Pointer to the current model node
Definition: PMMLlib.hxx:84
bool _log
Log Printing.
Definition: PMMLlib.hxx:76
int _nbModels
Number of models (all kinds)
Definition: PMMLlib.hxx:81
std::string _currentModelName
Name of the current model
Definition: PMMLlib.hxx:82
xmlDocPtr _doc
Associated DOM documents.
Definition: PMMLlib.hxx:78
PMMLType _currentModelType
Type of the current model.
Definition: PMMLlib.hxx:83
xmlNodePtr _rootNode
Root node of the document.
Definition: PMMLlib.hxx:79
xmlNodePtr _currentNode
Pointer to the current node
Definition: PMMLlib.hxx:80
xmlNodePtr GetRegressionPtr(int reg_index)
@ kANN
Definition: PMMLlib.hxx:54
@ kUNDEFINED
Definition: PMMLlib.hxx:54
PMMLMiningFunction
Definition: PMMLlib.hxx:66
@ kREGRESSION
Definition: PMMLlib.hxx:66
PMMLActivationFunction
Definition: PMMLlib.hxx:60
@ kIDENTITY
Definition: PMMLlib.hxx:60
@ kTANH
Definition: PMMLlib.hxx:60
@ kLOGISTIC
Definition: PMMLlib.hxx:60
std::string NumberToString(T Number)
Definition: PMMLlib.hxx:39
string log
Definition: logview.py:23