Salome HOME
Merge branch 'master' into V9_dev
[modules/yacs.git] / src / pmml / pmml_swig / PMMLBasicsTest.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library 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 GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 # imports Salomé
22 from PMML import PMMLlib, kANN, kLR
23
24 # imports python
25 import unittest
26 import os
27 import shutil
28
29 class PMMLBasicsTest(unittest.TestCase):
30
31     def setUp(self):
32         self.resourcesDir = ".." + os.sep + "Test" + os.sep + "samples" + os.sep ;
33         self.tmpDir = os.sep + "tmp" + os.sep + os.environ['LOGNAME'] + os.sep ;
34         self.tmpDir += "PmmlUnitTest";
35         self.tmpDir += os.sep ;
36         if ( not os.path.exists(self.tmpDir) ):
37             os.makedirs(self.tmpDir);
38             pass
39         pass
40
41     def tearDown(self):
42         if ( os.path.exists(self.tmpDir) ):
43             shutil.rmtree(self.tmpDir);
44             pass 
45         pass
46
47     def testExportPythonNeuralNet(self):
48         pmmlFile = self.resourcesDir + "ann_model.pmml";
49         model = "sANNName";
50         exportPyScript = self.tmpDir + "swigTestExportPythonNeuralNet.py";
51         refPyFilename = self.resourcesDir + "unittest_ref_ann_model.py";
52         with open(refPyFilename,"r") as f:
53             refLines = f.readlines(); 
54         #
55         p = PMMLlib( pmmlFile );
56         p.SetCurrentModel( model, kANN );
57         p.ExportPython( exportPyScript, "myTestFunc", 
58                         "File used by unit test\n PMMLBasicsTest1::testExportNeuralNetworkPython" );
59         with open(exportPyScript,"r") as f:
60             myLines = f.readlines();
61         self.assertEqual( len(myLines), len(refLines) );
62         for (i,line) in enumerate(myLines):
63             self.assertEqual( line, refLines[i] );
64             pass
65         pass
66   
67     def testExportPythonRegression(self):
68         pmmlFile = self.resourcesDir + "lr_model.pmml";
69         model = "Modeler[LinearRegression]Tds[steamplant]Predictor[x6:x8:x6x8:x6x6x8]Target[x1]";
70         exportPyScript = self.tmpDir + "swigTestExportPythonRegression.py";
71         refPyFilename = self.resourcesDir + "unittest_ref_lr_model.py";
72         with open(refPyFilename,"r") as f:
73             refLines = f.readlines(); 
74         #
75         p = PMMLlib( pmmlFile );
76         p.SetCurrentModel( model, kLR );
77         p.ExportPython( exportPyScript, "myTestFunc", 
78                                "File used by unit test\n PMMLBasicsTest1::testExportLinearRegressionPython" );
79         with open(exportPyScript,"r") as f:
80             myLines = f.readlines();
81         self.assertEqual( len(myLines), len(refLines) );
82         for (i,line) in enumerate(myLines):
83             self.assertEqual( line, refLines[i] );
84             pass
85         pass
86
87     def testPmmlFileNotReadable(self):
88         self.assertRaises( RuntimeError, PMMLlib, "0.mml" );
89         pass
90
91     def testPmmlFileNotReadable(self):
92         pmmlFile = self.resourcesDir + "ann_model.pmml";
93         model = "sANNName";
94         p = PMMLlib(pmmlFile);
95         self.assertRaises( RuntimeError, p.SetCurrentModel, model, kLR );
96         pass
97
98     def testPmmlFileNotWritable(self):
99         p = PMMLlib();
100         self.assertRaises( RuntimeError, p.Write );
101         pass
102     pass
103
104 unittest.main()