Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / pmml / PMMLlib.hxx
1 //////////////////////////////////////////////////////////////
2 // Copyright (C) 2013-2015 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/>.
16 //////////////////////////////////////////////////////////////
17 /*!
18   \file   PMMLlib.hxx
19   \author InckA
20   \date   Wed Nov 20 11:04:17 2013
21
22   \brief  Header de la classe PMMLlib
23
24  */
25
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
47 /**
48  * Enumeration to type the PMML file.
49  * UNDEFINED: not yet defined
50  * ANN: Artificial Neural Network
51  * LR:  Linear Regression
52  *
53  * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdGroup_MODEL-ELEMENT
54  */
55 enum PMMLType{kUNDEFINED, kANN, kLR};
56
57
58 /**
59  * @see http://www.dmg.org/v4-1/NeuralNetwork.html#xsdType_ACTIVATION-FUNCTION
60  */
61 enum PMMLActivationFunction{kIDENTITY, kTANH, kLOGISTIC};
62
63
64 /**
65  * @see http://www.dmg.org/v4-1/GeneralStructure.html#xsdType_MINING-FUNCTION
66  */
67 enum PMMLMiningFunction{kREGRESSION};
68
69
70 /**
71  * Class PMMLlib
72  */
73 class PMMLlib
74 {
75   
76 private:
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    
86
87     /** @defgroup general General methods
88      *  Common methods to all kinds of PMML files and models
89      *  @{
90      */
91 public:    
92     PMMLLIB_EXPORT PMMLlib(std::string file, 
93                            bool log=false) ;     
94     PMMLLIB_EXPORT PMMLlib(bool log=false); 
95     PMMLLIB_EXPORT ~PMMLlib();
96     PMMLLIB_EXPORT void SetCurrentModel(std::string modelName, 
97                                         PMMLType type); 
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;   
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();
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();    
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     
139     /** @} */ // end of group general   
140
141
142     /** @defgroup ann Methods dedicated to neural networks
143      *  Methods dedicated to neural networks
144      *  @{
145      */
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);
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, 
173                                               double *dnorm);
174     PMMLLIB_EXPORT void GetNormalisationOutput(int output_index, 
175                                                double *dnorm);
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, 
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();
191     /** @} */ // end of group ann
192
193
194     /** @defgroup ln Methods dedicated to linear regression
195      *  Methods dedicated to linear regression
196      *  @{
197      */
198 public:    
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, 
205                                             int exponent, 
206                                             double coefficient);
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, 
219                                                             int field_index);
220     PMMLLIB_EXPORT std::string ReadRegressionStructure();                                                              
221 private:    
222     xmlNodePtr GetRegressionPtr(int reg_index); 
223     xmlNodePtr GetRegressionPtr(std::string reg_name);     
224     void CheckRegression();
225     
226     /** @} */ // end of group ln
227     
228
229     /** @defgroup export Methods dedicated to file export
230      *  Methods dedicated to file export
231      *  @{
232      */
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);
274     /** @} */ // end of group export
275
276   private:
277     /*!     * Conversion from a libxml2 string (xmlChar *) to a standard C++ string.
278      *
279      *    \param xs a constant libxml string.
280      *    \return a C++ std::string (contains the same text as xs).
281      */
282      std::string _xmlCharToString(const xmlChar *xs) const;  
283     /*!
284     * Conversion from a standard C++ string to a libxml2 string (xmlChar *)
285     *
286     *    \param s a constant C++ std::string.
287     *    \return a libxml string (contains the same text as s)
288     *
289     * The caller of this function must free the result when it's not needed
290     * anymore (using xmlFree) 
291     */
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__