Salome HOME
Save foreach state - work in progress.
[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 exceptions
27 from exceptions import RuntimeError
28 import os
29 import shutil
30
31 class PMMLBasicsTest(unittest.TestCase):
32
33     def setUp(self):
34         self.resourcesDir = ".." + os.sep + "Test" + os.sep + "samples" + os.sep ;
35         self.tmpDir = os.sep + "tmp" + os.sep + os.environ['LOGNAME'] + os.sep ;
36         self.tmpDir += "PmmlUnitTest";
37         self.tmpDir += os.sep ;
38         if ( not os.path.exists(self.tmpDir) ):
39             os.mkdir(self.tmpDir);
40             pass
41         pass
42
43     def tearDown(self):
44         if ( os.path.exists(self.tmpDir) ):
45             shutil.rmtree(self.tmpDir);
46             pass 
47         pass
48
49     def testExportPythonNeuralNet(self):
50         pmmlFile = self.resourcesDir + "ann_model.pmml";
51         model = "sANNName";
52         exportPyScript = self.tmpDir + "swigTestExportPythonNeuralNet.py";
53         refPyFilename = self.resourcesDir + "unittest_ref_ann_model.py";
54         refLines = file(refPyFilename).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         myLines = file(exportPyScript).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         refLines = file(refPyFilename).readlines(); 
73         #
74         p = PMMLlib( pmmlFile );
75         p.SetCurrentModel( model, kLR );
76         p.ExportPython( exportPyScript, "myTestFunc", 
77                                "File used by unit test\n PMMLBasicsTest1::testExportLinearRegressionPython" );
78         myLines = file(exportPyScript).readlines();
79         self.assertEqual( len(myLines), len(refLines) );
80         for (i,line) in enumerate(myLines):
81             self.assertEqual( line, refLines[i] );
82             pass
83         pass
84
85     def testPmmlFileNotReadable(self):
86         self.assertRaises( RuntimeError, PMMLlib, "0.mml" );
87         pass
88
89     def testPmmlFileNotReadable(self):
90         pmmlFile = self.resourcesDir + "ann_model.pmml";
91         model = "sANNName";
92         p = PMMLlib(pmmlFile);
93         self.assertRaises( RuntimeError, p.SetCurrentModel, model, kLR );
94         pass
95
96     def testPmmlFileNotWritable(self):
97         p = PMMLlib();
98         self.assertRaises( RuntimeError, p.Write );
99         pass
100     pass
101
102 unittest.main()