1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2016 CEA/DEN, EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 from PMML import PMMLlib, kANN, kLR
27 from exceptions import RuntimeError
31 class PMMLBasicsTest(unittest.TestCase):
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);
44 if ( os.path.exists(self.tmpDir) ):
45 shutil.rmtree(self.tmpDir);
49 def testExportPythonNeuralNet(self):
50 pmmlFile = self.resourcesDir + "ann_model.pmml";
52 exportPyScript = self.tmpDir + "swigTestExportPythonNeuralNet.py";
53 refPyFilename = self.resourcesDir + "unittest_ref_ann_model.py";
54 refLines = file(refPyFilename).readlines();
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] );
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();
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] );
85 def testPmmlFileNotReadable(self):
86 self.assertRaises( RuntimeError, PMMLlib, "0.mml" );
89 def testPmmlFileNotReadable(self):
90 pmmlFile = self.resourcesDir + "ann_model.pmml";
92 p = PMMLlib(pmmlFile);
93 self.assertRaises( RuntimeError, p.SetCurrentModel, model, kLR );
96 def testPmmlFileNotWritable(self):
98 self.assertRaises( RuntimeError, p.Write );