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
29 class PMMLBasicsTest(unittest.TestCase):
32 self.resourcesDir = ".." + os.sep + "Test" + os.sep + "samples" + os.sep ;
33 self.tmpDir = os.sep + "tmp" + os.sep + os.environ['LOGNAME'] + os.sep ;
34 self.tmpDir += "PmmlUnitTest";
35 self.tmpDir += os.sep ;
36 if ( not os.path.exists(self.tmpDir) ):
37 os.makedirs(self.tmpDir);
42 if ( os.path.exists(self.tmpDir) ):
43 shutil.rmtree(self.tmpDir);
47 def testExportPythonNeuralNet(self):
48 pmmlFile = self.resourcesDir + "ann_model.pmml";
50 exportPyScript = self.tmpDir + "swigTestExportPythonNeuralNet.py";
51 refPyFilename = self.resourcesDir + "unittest_ref_ann_model.py";
52 with open(refPyFilename,"r") as f:
53 refLines = f.readlines();
55 p = PMMLlib( pmmlFile );
56 p.SetCurrentModel( model, kANN );
57 p.ExportPython( exportPyScript, "myTestFunc",
58 "File used by unit test\n PMMLBasicsTest1::testExportNeuralNetworkPython" );
59 with open(exportPyScript,"r") as f:
60 myLines = f.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 with open(refPyFilename,"r") as f:
73 refLines = f.readlines();
75 p = PMMLlib( pmmlFile );
76 p.SetCurrentModel( model, kLR );
77 p.ExportPython( exportPyScript, "myTestFunc",
78 "File used by unit test\n PMMLBasicsTest1::testExportLinearRegressionPython" );
79 with open(exportPyScript,"r") as f:
80 myLines = f.readlines();
81 self.assertEqual( len(myLines), len(refLines) );
82 for (i,line) in enumerate(myLines):
83 self.assertEqual( line, refLines[i] );
87 def testPmmlFileNotReadable(self):
88 self.assertRaises( RuntimeError, PMMLlib, "0.mml" );
91 def testPmmlFileNotReadable(self):
92 pmmlFile = self.resourcesDir + "ann_model.pmml";
94 p = PMMLlib(pmmlFile);
95 self.assertRaises( RuntimeError, p.SetCurrentModel, model, kLR );
98 def testPmmlFileNotWritable(self):
100 self.assertRaises( RuntimeError, p.Write );