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