RunPythonScriptTest.py 5.25 KB
Newer Older
1
2
3
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
#   NScD Oak Ridge National Laboratory, European Spallation Source,
#   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
# SPDX - License - Identifier: GPL - 3.0 +
7
8
9
10
11
12
13
import unittest
import os

from mantid.api import *
from mantid.kernel import *
from mantid.simpleapi import *

14
from testhelpers import WorkspaceCreationHelper
15

16

17
18
19
20
class RunPythonScriptTest(unittest.TestCase):
    """
    Try out RunPythonScript
    """
21

22
23
24
25
26
27
28
    def setUp(self):
        CreateWorkspace(OutputWorkspace='ws',DataX='1,2,3,4',DataY='1,2,3',DataE='1,2,3')

    def tearDown(self):
        mtd.clear()

    # ======================== Success cases =====================================================
29

30
31
32
    def test_Code_Without_Workspaces_Is_Successful_If_Code_Does_Not_Use_Workspace_References(self):
        code = "x = 5 + 2"
        RunPythonScript(Code=code)
33

34
35
36
37
    def test_simplePlus(self):
        code = "Plus(LHSWorkspace=input, RHSWorkspace=input, OutputWorkspace=output)"
        RunPythonScript(InputWorkspace="ws", Code=code, OutputWorkspace='ws_out')
        ws_out = mtd['ws_out']
38

39
40
41
42
43
44
45
46
47
        self.assertAlmostEqual(ws_out.dataY(0)[0], 2.0, 3)
        self.assertAlmostEqual(ws_out.dataY(0)[1], 4.0, 3)
        self.assertAlmostEqual(ws_out.dataY(0)[2], 6.0, 3)

    # Use an operation that sets 'output' to a workspace proxy
    def test_usingOperators(self):
        code = "output = input * 5.0"
        RunPythonScript(InputWorkspace="ws", Code=code, OutputWorkspace='ws_out')
        ws_out = mtd['ws_out']
48

49
50
51
52
        self.assertAlmostEqual(ws_out.dataY(0)[0], 5.0, 3)
        self.assertAlmostEqual(ws_out.dataY(0)[1],10.0, 3)
        self.assertAlmostEqual(ws_out.dataY(0)[2],15.0, 3)

53
54
55
56
57
58
59
60
61
62
63
64
    def test_input_MatrixWorkspace_has_correct_python_type_when_executed(self):
        code = """from mantid.api import MatrixWorkspace
if not isinstance(input, MatrixWorkspace): raise RuntimeError("Input workspace is not a MatrixWorkspace in Python: Type=%s" % str(type(input)))
"""
        RunPythonScript(InputWorkspace="ws", Code=code)

    def test_input_EventWorkspace_has_correct_python_type_when_executed(self):
        test_eventws = ConvertToEventWorkspace(InputWorkspace='ws')
        code = """from mantid.api import IEventWorkspace
if not isinstance(input, IEventWorkspace): raise RuntimeError("Input workspace is not an IEventWorkspace in Python: Type=%s" % str(type(input)))
"""
        RunPythonScript(InputWorkspace=test_eventws, Code=code)
65

66
67
68
69
70
71
72
    # Properties handle MDWorkspace types.
    def test_withMDWorkspace(self):
        CreateMDWorkspace(OutputWorkspace="ws", Dimensions='1', Extents='-10,10', Names='x', Units='m')
        code = "output = input"
        RunPythonScript(InputWorkspace="ws", Code=code, OutputWorkspace='ws_out')
        ws_out = mtd['ws_out']
        self.assertTrue(isinstance(ws_out, IMDWorkspace))
73
74
75
76
77
78

        # Check type
        code = """from mantid.api import IMDEventWorkspace
if not isinstance(input, IMDEventWorkspace): raise RuntimeError("Input workspace is not an IMDHistoWorkspace in Python: Type=%s" % str(type(input)))
"""
        RunPythonScript(InputWorkspace=mtd['ws'], Code=code)
79

80
81
    def test_withNoInputWorkspace(self):
        c = RunPythonScript(Code="output = CreateSingleValuedWorkspace(DataValue='1')")
82
        self.assertEqual(c.name(), "c")
83
84
85
86
87
88
89
        self.assertEqual(c.readY(0)[0], 1)

    def test_algorithm_executes_once_for_whole_input_group_and_not_once_per_group_member(self):
        DeleteWorkspace('ws')
        CreateWorkspace(OutputWorkspace='ws_1',DataX='1,2,3,4',DataY='1,2,3',DataE='1,2,3')
        CreateWorkspace(OutputWorkspace='ws_2',DataX='1,2,3,4',DataY='1,2,3',DataE='1,2,3')
        GroupWorkspaces(InputWorkspaces='ws_1,ws_2',OutputWorkspace='ws')
90
        self.assertEqual(3,mtd.size())
91
92
93
94

        code = "Scale(input,OutputWorkspace=output,Factor=5)"
        RunPythonScript(InputWorkspace='ws',Code=code,OutputWorkspace='ws')

95
        self.assertEqual(3,mtd.size())
96
97
98
99
        group = mtd['ws']
        self.assertTrue(isinstance(group, WorkspaceGroup))
        self.assertAlmostEqual(5.0, mtd['ws_1'].readY(0)[0], 8)

100
    def test_code_with_a_mixture_of_line_endings_succeeds(self):
101
102
103
104
105
106
107
108
109
        code = "Scale(input,OutputWorkspace=output,Factor=5)\n"
        code += "Scale(input,OutputWorkspace=output,Factor=10,Operation='Add')\r"
        code += "Scale(input,OutputWorkspace=output,Factor=20)\r\n"

        RunPythonScript(InputWorkspace='ws',Code=code,OutputWorkspace='ws')
        ws = mtd['ws']
        self.assertAlmostEqual(300.0, mtd['ws'].readY(0)[0], 8)

    # ======================== Failure cases =====================================================
110
111
112
113
    def test_syntax_error_in_code_raises_Runtime_Error(self):
        code = "print 'unclosed quote"
        self.assertRaises(RuntimeError,RunPythonScript,Code=code)

114
115
    def test_Code_Without_Workspaces_Fails_If_Code_Uses_Workspace_References(self):
        code = "output = input*5"
116
        self.assertRaises(RuntimeError,RunPythonScript,Code=code)
117
118
119
120
121
122
123

    def test_Code_Not_Creating_Output_Workspace_Fails_If_OutputWS_Is_Specified(self):
        code = "x = 5"
        self.assertRaises(RuntimeError, RunPythonScript, Code=code, OutputWorkspace='ws_out')

if __name__ == '__main__':
    unittest.main()