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