AlgorithmHistoryTest.py 2.67 KB
Newer Older
1
import unittest
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from mantid.simpleapi import Load, CreateTransmissionWorkspaceAuto, CreateWorkspace, _set_properties
from mantid.api import *
from mantid.kernel import *


class ChildAlg(PythonAlgorithm):

    def PyInit(self):
        pass

    def PyExec(self):
        pass

AlgorithmFactory.subscribe(ChildAlg)


class ParentAlg(DataProcessorAlgorithm):

    def PyInit(self):
        self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '', Direction.Output),
                             doc="Name to give the output workspace.")

    def PyExec(self):
        ws_name = self.getProperty("OutputWorkspace").value
26
        alg = self.createChildAlgorithm('ChildAlg')
27
28
29
30
31
32
33
34
35
36
        alg.initialize()
        args = {}
        kwargs = {}
        _set_properties(alg, *args, **kwargs)
        alg.execute()

        ws = CreateWorkspace([0, 1, 2], [0, 1, 2], OutputWorkspace=ws_name)
        self.setProperty('OutputWorkspace', ws)

AlgorithmFactory.subscribe(ParentAlg)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


class AlgorithmHistoryTest(unittest.TestCase):

    def test_nested_history(self):
        trans1 = Load('INTER00013463.nxs', OutputWorkspace="trans1")
        #run an algorithm which has multiple layers of history
        out_ws = CreateTransmissionWorkspaceAuto(FirstTransmissionRun=trans1, AnalysisMode="PointDetectorAnalysis")

        history = out_ws.getHistory()

        alg_hists = history.getAlgorithmHistories()
        self.assertEquals(history.size(), 2)
        self.assertEquals(len(alg_hists), 2)

        parent_alg = history.getAlgorithmHistory(1)
        self.assertEquals(parent_alg.name(), "CreateTransmissionWorkspaceAuto")
        self.assertEquals(parent_alg.version(), 1)

        alg = parent_alg.getChildAlgorithm(0)
        self.assertEquals(alg.name(), "CreateTransmissionWorkspace")

        self.assertEquals(parent_alg.childHistorySize(), 1)

        nested_alg = parent_alg.getChildAlgorithmHistory(0)
        self.assertEquals(nested_alg.name(), "CreateTransmissionWorkspace")
        self.assertEquals(nested_alg.version(), 1)

        basic_child_algs = nested_alg.getChildHistories()
66
        self.assertTrue(len(basic_child_algs) > 0)
67

68
69
70
    def test_disable_history(self):
        ws_name = '__tmp_test_algorithm_history'

71
        alg = AlgorithmManager.createUnmanaged('ParentAlg')
72
        alg.initialize()
73
74
        alg.setChild(True)
        alg.enableHistoryRecordingForChild(False)
75
        alg.setProperty("OutputWorkspace", ws_name)
76
        alg.execute()
77
        history = alg.getProperty("OutputWorkspace").value.getHistory()
78
79

        alg_hists = history.getAlgorithmHistories()
80
81
        self.assertEquals(history.size(), 0)
        self.assertEquals(len(alg_hists), 0)
82
83
84

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