forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.py
252 lines (217 loc) · 8.92 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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
"""
Base reduction class. Uses version 2 python API.
"""
import os
import sys
import time
import random
import string
from mantid.api import AlgorithmManager, AnalysisDataService
from mantid.kernel import ConfigService, Logger
class Reducer(object):
"""
Base reducer class. Instrument-specific reduction processes should be
implemented in a child of this class.
"""
## Path for data files
_data_path = '.'
## Path for output files
_output_path = None
## List of data files to process
_data_files = {}
## List of reduction steps
_reduction_steps = []
## Log
log_text = ''
## Output workspaces
output_workspaces = []
reduction_properties = {}
instrument_name = ""
setup_algorithm = None
reduction_algorithm = None
def __init__(self):
self.UID = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for x in range(5))
self.property_manager = "__reduction_parameters_"+self.UID
self._data_files = {}
self._reduction_steps = []
self.reduction_properties = {}
def get_reduction_table_name(self):
return self.property_manager
def set_reduction_table_name(self, name):
self.property_manager = str(name)
def set_instrument(self, instrument, setup_algorithm=None,
reduction_algorithm=None):
self.instrument_name = instrument
self.setup_algorithm = setup_algorithm
self.reduction_algorithm = reduction_algorithm
def get_instrument(self):
return self.instrument_name
def set_data_path(self, path):
"""
Set the path for data files
@param path: data file path
"""
path = os.path.normcase(path)
if os.path.isdir(path):
self._data_path = path
ConfigService.Instance().appendDataSearchDir(path)
else:
raise RuntimeError("Reducer.set_data_path: provided path is not a directory (%s)" % path)
def set_output_path(self, path):
"""
Set the path for output files
@param path: output file path
"""
path = os.path.normcase(path)
if os.path.isdir(path):
self._output_path = path
else:
raise RuntimeError("Reducer.set_output_path: provided path is not a directory (%s)" % path)
def clear_data_files(self):
"""
Empty the list of files to reduce while keeping all the
other options the same.
"""
self._data_files = {}
def append_data_file(self, data_file, workspace=None):
"""
Append a file to be processed.
@param data_file: name of the file to be processed
@param workspace: optional name of the workspace for this data,
default will be the name of the file
TODO: this needs to be an ordered list
"""
if data_file is None:
if AnalysisDataService.doesExist(workspace):
self._data_files[workspace] = None
return
else:
raise RuntimeError("Trying to append a data set without a file name or an existing workspace.")
if isinstance(data_file, list):
if workspace is None:
# Use the first file to determine the workspace name
workspace = extract_workspace_name(data_file[0])
else:
if workspace is None:
workspace = extract_workspace_name(data_file)
self._data_files[workspace] = data_file
def pre_process(self):
"""
Reduction steps that are meant to be executed only once per set
of data files. After this is executed, all files will go through
the list of reduction steps.
"""
Logger("Reducer").information("Setting up reduction options")
if self.setup_algorithm is not None:
alg = AlgorithmManager.create(self.setup_algorithm)
alg.initialize()
props = [p.name for p in alg.getProperties()]
for key in list(self.reduction_properties.keys()):
if key in props:
try:
alg.setProperty(key, self.reduction_properties[key])
except:
msg = "Error setting %s=%s" % (key, str(self.reduction_properties[key]))
msg += "\n %s" % sys.exc_info()[1]
Logger("Reducer").error(msg)
else:
Logger("Reducer").warning("Setup algorithm has no %s property" % key)
if "ReductionProperties" in props:
alg.setPropertyValue("ReductionProperties",
self.get_reduction_table_name())
alg.execute()
def post_process(self):
"""
Reduction steps to be executed after all data files have been
processed.
"""
pass
def reduce(self):
"""
Go through the list of reduction steps
"""
t_0 = time.time()
self.output_workspaces = []
# Log text
self.log_text = "%s reduction - %s\n" % (self.instrument_name, time.ctime())
self.log_text += "Mantid Python API v2\n"
# Go through the list of steps that are common to all data files
self.pre_process()
if self.reduction_algorithm is None:
Logger("Reducer").error("A reduction algorithm wasn't set: stopping")
return
_first_ws_name = None
for ws in list(self._data_files.keys()):
if _first_ws_name is None:
_first_ws_name = ws
alg = AlgorithmManager.create(self.reduction_algorithm)
alg.initialize()
props = [p.name for p in alg.getProperties()]
# Check whether the data is already available or needs to be loaded
if self._data_files[ws] is not None:
datafile = self._data_files[ws]
if isinstance(datafile, list):
datafile=','.join(datafile)
if "Filename" in props:
alg.setPropertyValue("Filename", datafile)
else:
msg = "Can't set the Filename property on %s" % self.reduction_algorithm
Logger("Reducer").error(msg)
else:
if "InputWorkspace" in props:
alg.setPropertyValue("InputWorkspace", ws)
else:
msg = "Can't set the InputWorkspace property on %s" % self.reduction_algorithm
Logger("Reducer").error(msg)
if "ReductionProperties" in props:
alg.setPropertyValue("ReductionProperties",
self.get_reduction_table_name())
if "OutputWorkspace" in props:
alg.setPropertyValue("OutputWorkspace", ws)
alg.execute()
if "OutputMessage" in props:
self.log_text += alg.getProperty("OutputMessage").value+'\n'
#any clean up, possibly removing workspaces
self.post_process()
# Determine which directory to use
output_dir = self._data_path
if self._output_path is not None:
if os.path.isdir(self._output_path):
output_dir = self._output_path
else:
output_dir = os.path.expanduser('~')
self.log_text += "Reduction completed in %g sec\n" % (time.time()-t_0)
if _first_ws_name is not None:
log_path = os.path.join(output_dir, "%s_reduction.log" % _first_ws_name)
else:
log_path = os.path.join(output_dir,"%s_reduction.log" % self.instrument_name)
self.log_text += "Log saved to %s" % log_path
# Write the log to file
f = open(log_path, 'a')
f.write("\n-------------------------------------------\n")
f.write(self.log_text)
f.close()
return self.log_text
def extract_workspace_name(filepath, suffix=''):
"""
Returns a default workspace name for a given data file path.
@param filepath: path of the file to generate a workspace name for
@param suffix: string to append to name
"""
filepath_tmp = filepath
if isinstance(filepath, list):
filepath_tmp = filepath[0]
(head, tail) = os.path.split(filepath_tmp)
basename, extension = os.path.splitext(tail)
if isinstance(filepath, list):
basename += "_combined"
#TODO: check whether the workspace name is already in use
# and modify it if it is.
return basename+suffix