Version: 8.3.0
PMMLlib.hxx
Go to the documentation of this file.
1 
2 // Copyright (C) 2013-2016 CEA/DEN
3 //
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
7 // later version.
8 //
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.
13 //
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/>.
17 
26 #ifndef __PMMLLIB_H__
27 #define __PMMLLIB_H__
28 
29 #include "PMMLwin.hxx"
30 
31 #include <libxml/xpathInternals.h>
32 #include <string>
33 #include <vector>
34 #include <sstream>
35 
36 namespace PMMLlib
37 {
38 
39 template <typename T>
40 std::string NumberToString ( T Number )
41 {
42  std::ostringstream ss;
43  ss << Number;
44  return ss.str();
45 }
46 
56 
57 
62 
63 
68 
69 
73 class PMMLlib
74 {
75 
76 private:
77  bool _log;
78  std::string _pmmlFile;
79  xmlDocPtr _doc;
80  xmlNodePtr _rootNode;
81  xmlNodePtr _currentNode;
82  int _nbModels;
83  std::string _currentModelName;
85  xmlNodePtr _currentModelNode;
86 
91 public:
92  PMMLLIB_EXPORT PMMLlib(std::string file,
93  bool log=false) ;
94  PMMLLIB_EXPORT PMMLlib(bool log=false);
96  PMMLLIB_EXPORT void SetCurrentModel(std::string modelName,
97  PMMLType type);
98  PMMLLIB_EXPORT void SetCurrentModel(std::string modelName);
100  PMMLLIB_EXPORT std::string makeLog() const;
101  PMMLLIB_EXPORT void printLog() const;
102 
103  PMMLLIB_EXPORT void AddDataField(std::string name,
104  std::string displayName,
105  std::string optype,
106  std::string dataType,
107  std::string closure,
108  double leftMargin,
109  double rightMargin,
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,
115  std::string appName,
116  std::string appVersion,
117  std::string annotation);
118  PMMLLIB_EXPORT void UnlinkNode();
119  PMMLLIB_EXPORT void BackupNode();
121  PMMLLIB_EXPORT void Write();
122  PMMLLIB_EXPORT void Write(std::string file);
124  PMMLLIB_EXPORT std::string GetCurrentModelName();
125 private:
126  xmlNodePtr GetChildByName(xmlNodePtr node,
127  std::string nodename);
128  xmlNodePtr GetPtr(int ann_index,
129  std::string name);
130  xmlNodePtr GetPtr(std::string ann_name,
131  std::string name);
132  void CountModels();
133  int CountNeuralNetModels();
134  int CountRegressionModels();
135  void SetRootNode();
136  std::string GetModelName(xmlNodePtr node);
137  std::string GetTypeString();
138  // end of group general
140 
141 
146 public:
147  PMMLLIB_EXPORT void AddNeuralNetwork(std::string modelName,
148  PMMLMiningFunction functionName);
149  PMMLLIB_EXPORT void AddNeuralInput(int id,
150  std::string inputName,
151  std::string optype,
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,
157  double bias,
158  int conNb,
159  int firstFrom,
160  std::vector<double> weights);
161  PMMLLIB_EXPORT void AddNeuralOutput(int outputNeuron,
162  std::string outputName,
163  std::string optype,
164  std::string dataType,
165  double orig1, double norm1,
166  double orig2, double norm2);
169  PMMLLIB_EXPORT std::string GetNameInput(int input_index);
170  PMMLLIB_EXPORT std::string GetNameOutput(int output_index);
172  PMMLLIB_EXPORT void GetNormalisationInput(int input_index,
173  double *dnorm);
174  PMMLLIB_EXPORT void GetNormalisationOutput(int output_index,
175  double *dnorm);
178  PMMLLIB_EXPORT int GetNbNeuronsAtLayer(int layer_index);
179  PMMLLIB_EXPORT double GetNeuronBias(int layer_index,
180  int neu_index);
181  PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index,
182  int neu_index,
183  int prec_index);
184  PMMLLIB_EXPORT void SetNeuralNetName(int ann_index,
185  std::string ann_name);
186  PMMLLIB_EXPORT std::string ReadNetworkStructure();
187 private:
188  xmlNodePtr GetNeuralNetPtr(std::string ann_name);
189  xmlNodePtr GetNeuralNetPtr(int ann_index);
190  void CheckNeuralNetwork(); // end of group ann
192 
193 
198 public:
199  PMMLLIB_EXPORT void AddRegressionModel(std::string modelName,
200  PMMLMiningFunction functionName,
201  std::string targetFieldName);
203  PMMLLIB_EXPORT void AddRegressionTable(double intercept);
204  PMMLLIB_EXPORT void AddNumericPredictor(std::string neuronName,
205  int exponent,
206  double coefficient);
207  PMMLLIB_EXPORT void AddPredictorTerm(double coefficient,
208  std::vector<std::string> fieldRef);
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,
219  int field_index);
221 private:
222  xmlNodePtr GetRegressionPtr(int reg_index);
223  xmlNodePtr GetRegressionPtr(std::string reg_name);
224  void CheckRegression();
225  // end of group ln
227 
228 
233 private:
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 );
238 public:
239  PMMLLIB_EXPORT void ExportCpp(std::string file,
240  std::string functionName,
241  std::string header);
242  PMMLLIB_EXPORT void ExportFortran(std::string file,
243  std::string functionName,
244  std::string header);
245  PMMLLIB_EXPORT void ExportPython(std::string file,
246  std::string functionName,
247  std::string header);
248  PMMLLIB_EXPORT std::string ExportPyStr(std::string functionName,
249  std::string header);
250 private:
251  void ExportNeuralNetworkCpp(std::string file,
252  std::string functionName,
253  std::string header);
254  void ExportNeuralNetworkFortran(std::string file,
255  std::string functionName,
256  std::string header);
257  void ExportNeuralNetworkPython(std::string file,
258  std::string functionName,
259  std::string header);
260  std::string ExportNeuralNetworkPyStr(std::string functionName,
261  std::string header);
262 
263  void ExportLinearRegressionCpp(std::string,
264  std::string,
265  std::string);
266  void ExportLinearRegressionFortran(std::string,
267  std::string,
268  std::string);
269  void ExportLinearRegressionPython(std::string,
270  std::string,
271  std::string);
272  std::string ExportLinearRegressionPyStr(std::string functionName,
273  std::string header); // end of group export
275 
276  private:
282  std::string _xmlCharToString(const xmlChar *xs) const;
292  xmlChar * _stringToXmlChar(const std::string &s) const;
293 
294  std::string _getProp(const xmlNodePtr node,
295  std::string const & prop ) const;
296 
297 };
298 
299 }
300 
301 #endif //__PMMLLIB_H__