forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_reducer.py
117 lines (91 loc) · 4.04 KB
/
example_reducer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
#pylint: disable=invalid-name
"""
Simple Reducer example
"""
from reduction import Reducer
from reduction.instruments.example.ExampleRedStep import ExampleRedStep
# Validate_step is a decorator that allows both Python algorithms and ReductionStep objects to be passed to the Reducer.
# It also does minimal type checking to ensure that the object that is passed is valid
from reduction import validate_step, validate_loader
import mantid
from mantid.simpleapi import *
class ExampleReducer(Reducer):
# Place holders for the reduction steps
_first_step = None
_second_step = None
# Algorithm used as part of a step
_normalizer = None
def __init__(self):
super(ExampleReducer, self).__init__()
#@validate_step
@validate_loader
def set_first_step(self, step):
"""
Set a reduction step. The 'step' parameter is expected to be
a ReductionStep object at this point, but this should be
transparent to the developer. Nothing but setting the private
data member should be done here.
@validate_step processes the original inputs. There are two
ways to call set_first_step():
Method 1 (old): reducer.set_first_step(reduction_step)
where reduction_step is a ReductionStep object
Method 2 (new): reducer.set_first_step(Algorithm, *args, **argv)
where Algorithm is an anlgorithm function from mantid.simpleapi,
following by its arguments.
For example:
reducer.set_first_step(Scale, InputWorkspace=None, OutputWorkspace=None, Factor='2.5')
The arguments follow the signature of the mantid.simpleapi function.
See original ticket for more details:
http://trac.mantidproject.org/mantid/ticket/2129l
"""
self._first_step = step
@validate_step
def set_second_step(self, step):
self._second_step = step
@validate_step
def set_normalizer(self, normalize_algorithm):
"""
This is an example of a plug-and-play algorithm to be
used as part of a reduction step
"""
self._normalizer = normalize_algorithm
def pre_process(self):
"""
Create the list of algorithms that will be run.
- This is the place to set algorithm properties if you need to
"""
if self._first_step is not None:
self.append_step(self._first_step)
if self._second_step is not None:
# When we are able to set an algorithm object as a property,
# it should be done here:
self._second_step.get_algorithm().setProperty("Algorithm", self._normalizer.get_algorithm())
self.append_step(self._second_step)
if __name__ == '__main__':
# Instantiate the Reducer object
r = ExampleReducer()
# Append a fake data file
r.append_data_file("AsciiExample.txt")
# Example of a standard algorithm used as a reduction step. Note that InputWorkspace and OutputWorkspace
# are overwritten by the Reducer. They can be set to Non at this point.
#r.set_first_step(CreateWorkspace, OutputWorkspace=None, DataX='1', DataY='1', DataE='1')
#step = ExampleLoader()
#step.initialize()
#r.set_first_step(step)
r.set_first_step(LoadAscii, Filename=None, OutputWorkspace=None)
#r._first_step.setProperty("Separator", "Tab")
# Set up an algorithm to be used as part of a reduction step
alg = mantid.api.AlgorithmManager.create("Scale")
alg.setProperty("Factor",2.5)
r.set_normalizer(alg)
# Set up the actual reduction step that will use the algorithm
step = ExampleRedStep()
step.initialize()
r.set_second_step(step)
r.reduce()