Salome HOME
Update copyrights
[modules/yacs.git] / src / pmml / PMMLlib.hxx
1 // Copyright (C) 2013-2019  CEA/DEN
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 /*!
21   \file   PMMLlib.hxx
22   \author InckA
23   \date   Wed Nov 20 11:04:17 2013
24
25   \brief  Header de la classe PMMLlib
26
27  */
28
29 #ifndef __PMMLLIB_H__
30 #define __PMMLLIB_H__
31
32 #include "PMMLwin.hxx"
33  
34 #include <libxml/xpathInternals.h>
35 #include <string>
36 #include <vector>
37 #include <sstream>
38
39 namespace PMMLlib
40 {
41
42 template <typename T>
43 std::string NumberToString ( T Number )
44 {
45      std::ostringstream ss;
46      ss << Number;
47      return ss.str();
48 }
49
50 /**
51  * Enumeration to type the PMML file.
52  * UNDEFINED: not yet defined
53  * ANN: Artificial Neural Network
54  * LR:  Linear Regression
55  *
56  * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdGroup_MODEL-ELEMENT
57  */
58 enum PMMLType{kUNDEFINED, kANN, kLR};
59
60
61 /**
62  * @see http://www.dmg.org/v4-1/NeuralNetwork.html#xsdType_ACTIVATION-FUNCTION
63  */
64 enum PMMLActivationFunction{kIDENTITY, kTANH, kLOGISTIC};
65
66
67 /**
68  * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdType_MINING-FUNCTION
69  */
70 enum PMMLMiningFunction{kREGRESSION};
71
72
73 /**
74  * Class PMMLlib
75  */
76 class PMMLlib
77 {
78   
79 private:
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    
89
90     /** @defgroup general General methods
91      *  Common methods to all kinds of PMML files and models
92      *  @{
93      */
94 public:    
95     PMMLLIB_EXPORT PMMLlib(std::string file, 
96                            bool log=false) ;     
97     PMMLLIB_EXPORT PMMLlib(bool log=false); 
98     PMMLLIB_EXPORT ~PMMLlib();
99     PMMLLIB_EXPORT void SetCurrentModel(std::string modelName, 
100                                         PMMLType type); 
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;   
105
106     PMMLLIB_EXPORT void AddDataField(std::string name, 
107                                      std::string displayName,
108                                      std::string optype, 
109                                      std::string dataType, 
110                                      std::string closure, 
111                                      double leftMargin, 
112                                      double rightMargin,
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, 
118                                   std::string appName, 
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();    
128 private: 
129     xmlNodePtr GetChildByName(xmlNodePtr node, 
130                               std::string nodename);
131     xmlNodePtr GetPtr(int ann_index, 
132                       std::string name);
133     xmlNodePtr GetPtr(std::string ann_name, 
134                       std::string name);
135     void CountModels();
136     int CountNeuralNetModels();
137     int CountRegressionModels();
138     void SetRootNode();
139     std::string GetModelName(xmlNodePtr node);
140     std::string GetTypeString();
141     
142     /** @} */ // end of group general   
143
144
145     /** @defgroup ann Methods dedicated to neural networks
146      *  Methods dedicated to neural networks
147      *  @{
148      */
149 public:     
150     PMMLLIB_EXPORT void AddNeuralNetwork(std::string modelName, 
151                                          PMMLMiningFunction functionName);    
152     PMMLLIB_EXPORT void AddNeuralInput(int id, 
153                                        std::string inputName, 
154                                        std::string optype, 
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, 
160                                   double bias, 
161                                   int conNb, 
162                                   int firstFrom, 
163                                   std::vector<double> weights);
164     PMMLLIB_EXPORT void AddNeuralOutput(int outputNeuron, 
165                                         std::string outputName, 
166                                         std::string optype, 
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, 
176                                               double *dnorm);
177     PMMLLIB_EXPORT void GetNormalisationOutput(int output_index, 
178                                                double *dnorm);
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, 
183                                         int neu_index);
184     PMMLLIB_EXPORT double GetPrecNeuronSynapse(int layer_index, 
185                                                int neu_index, 
186                                                int prec_index);
187     PMMLLIB_EXPORT void SetNeuralNetName(int ann_index, 
188                                          std::string ann_name); 
189     PMMLLIB_EXPORT std::string ReadNetworkStructure();    
190 private:   
191     xmlNodePtr GetNeuralNetPtr(std::string ann_name);
192     xmlNodePtr GetNeuralNetPtr(int ann_index);
193     void CheckNeuralNetwork();
194     /** @} */ // end of group ann
195
196
197     /** @defgroup ln Methods dedicated to linear regression
198      *  Methods dedicated to linear regression
199      *  @{
200      */
201 public:    
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, 
208                                             int exponent, 
209                                             double coefficient);
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, 
222                                                             int field_index);
223     PMMLLIB_EXPORT std::string ReadRegressionStructure();                                                              
224 private:    
225     xmlNodePtr GetRegressionPtr(int reg_index); 
226     xmlNodePtr GetRegressionPtr(std::string reg_name);     
227     void CheckRegression();
228     
229     /** @} */ // end of group ln
230     
231
232     /** @defgroup export Methods dedicated to file export
233      *  Methods dedicated to file export
234      *  @{
235      */
236 private:
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 );
241 public:
242     PMMLLIB_EXPORT void ExportCpp(std::string file, 
243                                   std::string functionName, 
244                                   std::string header);
245     PMMLLIB_EXPORT void ExportFortran(std::string file, 
246                                       std::string functionName, 
247                                       std::string header);
248     PMMLLIB_EXPORT void ExportPython(std::string file, 
249                                      std::string functionName, 
250                                      std::string header);
251     PMMLLIB_EXPORT std::string ExportPyStr(std::string functionName, 
252                                            std::string header);
253 private:    
254     void ExportNeuralNetworkCpp(std::string file, 
255                                 std::string functionName, 
256                                 std::string header);
257     void ExportNeuralNetworkFortran(std::string file, 
258                                     std::string functionName, 
259                                     std::string header);
260     void ExportNeuralNetworkPython(std::string file, 
261                                    std::string functionName, 
262                                    std::string header);
263     std::string ExportNeuralNetworkPyStr(std::string functionName, 
264                                          std::string header);
265     
266     void ExportLinearRegressionCpp(std::string, 
267                                    std::string, 
268                                    std::string);
269     void ExportLinearRegressionFortran(std::string, 
270                                        std::string, 
271                                        std::string);
272     void ExportLinearRegressionPython(std::string, 
273                                       std::string, 
274                                       std::string);
275     std::string ExportLinearRegressionPyStr(std::string functionName, 
276                                             std::string header);
277     /** @} */ // end of group export
278
279   private:
280     /*!     * Conversion from a libxml2 string (xmlChar *) to a standard C++ string.
281      *
282      *    \param xs a constant libxml string.
283      *    \return a C++ std::string (contains the same text as xs).
284      */
285      std::string _xmlCharToString(const xmlChar *xs) const;  
286     /*!
287     * Conversion from a standard C++ string to a libxml2 string (xmlChar *)
288     *
289     *    \param s a constant C++ std::string.
290     *    \return a libxml string (contains the same text as s)
291     *
292     * The caller of this function must free the result when it's not needed
293     * anymore (using xmlFree) 
294     */
295     xmlChar * _stringToXmlChar(const std::string &s) const;
296
297     std::string _getProp(const xmlNodePtr node, 
298                          std::string const & prop ) const;
299
300 };
301
302 }
303
304 #endif  //__PMMLLIB_H__