Salome HOME
Merge branch 'V9_9_BR'
[modules/yacs.git] / src / pmml / pmml_swig / PMMLBasicsTest.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2022  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 import platform
30
31 class PMMLBasicsTest(unittest.TestCase):
32
33     def setUp(self):
34         self.resourcesDir = ".." + os.sep + "Test" + os.sep + "samples" + os.sep ;
35         if platform.system() == "Windows" :
36             self.tmpDir = os.environ['TMP'] # %TMP% does exist on WINDOWS
37         else:
38             self.tmpDir = os.sep + "tmp" + os.sep + os.environ['LOGNAME'] + os.sep ;
39         self.tmpDir += "PmmlUnitTest";
40         self.tmpDir += os.sep ;
41         if ( not os.path.exists(self.tmpDir) ):
42             os.makedirs(self.tmpDir);
43             pass
44         pass
45
46     def tearDown(self):
47         if ( os.path.exists(self.tmpDir) ):
48             shutil.rmtree(self.tmpDir);
49             pass 
50         pass
51
52     def testExportPythonNeuralNet(self):
53         pmmlFile = self.resourcesDir + "ann_model.pmml";
54         model = "sANNName";
55         exportPyScript = self.tmpDir + "swigTestExportPythonNeuralNet.py";
56         refPyFilename = self.resourcesDir + "unittest_ref_ann_model.py";
57         with open(refPyFilename,"r") as f:
58             refLines = f.readlines(); 
59         #
60         p = PMMLlib( pmmlFile );
61         p.SetCurrentModel( model, kANN );
62         p.ExportPython( exportPyScript, "myTestFunc", 
63                         "File used by unit test\n PMMLBasicsTest1::testExportNeuralNetworkPython" );
64         with open(exportPyScript,"r") as f:
65             myLines = f.readlines();
66         self.assertEqual( len(myLines), len(refLines) );
67         for (i,line) in enumerate(myLines):
68             self.assertEqual( line, refLines[i] );
69             pass
70         pass
71   
72     def testExportPythonRegression(self):
73         pmmlFile = self.resourcesDir + "lr_model.pmml";
74         model = "Modeler[LinearRegression]Tds[steamplant]Predictor[x6:x8:x6x8:x6x6x8]Target[x1]";
75         exportPyScript = self.tmpDir + "swigTestExportPythonRegression.py";
76         refPyFilename = self.resourcesDir + "unittest_ref_lr_model.py";
77         with open(refPyFilename,"r") as f:
78             refLines = f.readlines(); 
79         #
80         p = PMMLlib( pmmlFile );
81         p.SetCurrentModel( model, kLR );
82         p.ExportPython( exportPyScript, "myTestFunc", 
83                                "File used by unit test\n PMMLBasicsTest1::testExportLinearRegressionPython" );
84         with open(exportPyScript,"r") as f:
85             myLines = f.readlines();
86         self.assertEqual( len(myLines), len(refLines) );
87         for (i,line) in enumerate(myLines):
88             self.assertEqual( line, refLines[i] );
89             pass
90         pass
91
92     def testPmmlFileNotReadable(self):
93         self.assertRaises( RuntimeError, PMMLlib, "0.mml" );
94         pass
95
96     def testPmmlFileNotReadable(self):
97         pmmlFile = self.resourcesDir + "ann_model.pmml";
98         model = "sANNName";
99         p = PMMLlib(pmmlFile);
100         self.assertRaises( RuntimeError, p.SetCurrentModel, model, kLR );
101         pass
102
103     def testPmmlFileNotWritable(self):
104         p = PMMLlib();
105         self.assertRaises( RuntimeError, p.Write );
106         pass
107     pass
108
109 unittest.main()