forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCropWorkspaceForMDNorm.py
116 lines (105 loc) · 5.29 KB
/
CropWorkspaceForMDNorm.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
# 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=no-init,invalid-name
import numpy
from mantid.api import (PythonAlgorithm, AlgorithmFactory,
WorkspaceUnitValidator,IEventWorkspaceProperty,
WorkspaceProperty)
from mantid.kernel import (Direction, CompositeValidator, Property,
CompositeRelation, FloatMandatoryValidator)
class CropWorkspaceForMDNorm(PythonAlgorithm):
"""
Crops an event workspace and store the information
about trajectories limits in the run object
"""
def category(self):
"""
Return category
"""
return "Utility\\Workspaces;MDAlgorithms\\Normalisation"
def name(self):
"""
Return name
"""
return "CropWorkspaceForMDNorm"
def seeAlso(self):
return ["RecalculateTrajectoriesExtents"]
def summary(self):
return "Crops an event workspace and store the information"+\
" about trajectories limits in the run object."
def PyInit(self):
"""
Declare properties
"""
allowed_units = CompositeValidator([WorkspaceUnitValidator("DeltaE"),
WorkspaceUnitValidator("Momentum")],
relation=CompositeRelation.OR)
self.declareProperty(IEventWorkspaceProperty("InputWorkspace", "",
direction=Direction.Input,
validator=allowed_units),
doc="Input workspace. It has to be an event workspace"
+ " with units of energy transfer or momentum")
self.declareProperty(name="XMin", defaultValue=Property.EMPTY_DBL,
direction=Direction.Input,
validator=FloatMandatoryValidator(),
doc="Minimum energy transfer or momentum")
self.declareProperty(name="XMax", defaultValue=Property.EMPTY_DBL,
direction=Direction.Input,
validator=FloatMandatoryValidator(),
doc="Maximum energy transfer or momentum")
self.declareProperty(WorkspaceProperty('OutputWorkspace', '',
direction=Direction.Output),
doc='Output workspace')
def validateInputs(self):
issues = dict()
xmin = self.getProperty('XMin').value
xmax = self.getProperty('XMax').value
if (xmax <= xmin):
issues["XMax"] = "The limits for cropping are invalid (XMax <= XMin)."
return issues
def PyExec(self):
xmin = self.getProperty('XMin').value
xmax = self.getProperty('XMax').value
# rebin
rebin_alg = self.createChildAlgorithm("Rebin", enableLogging=False)
rebin_alg.setProperty("InputWorkspace",self.getProperty('InputWorkspace').value)
rebin_alg.setProperty("Params", "{0},{1},{2}".format(xmin,xmax-xmin,xmax))
rebin_alg.setProperty("PreserveEvents", True)
rebin_alg.execute()
out_ws = rebin_alg.getProperty("OutputWorkspace").value
# crop
crop_alg = self.createChildAlgorithm("CropWorkspace", enableLogging=False)
crop_alg.setProperty("InputWorkspace", out_ws)
crop_alg.setProperty("XMin", xmin)
crop_alg.setProperty("XMax", xmax)
crop_alg.execute()
out_ws = crop_alg.getProperty("OutputWorkspace").value
# add logs
num_spectra = out_ws.getNumberHistograms()
run_obj = out_ws.getRun()
min_values = [xmin]*num_spectra
max_values = [xmax]*num_spectra
if run_obj.hasProperty('MDNorm_low'):
#TODO: when handling of masking bins is implemented, number of spectra will be different
# than the number of limits
try:
min_values = numpy.maximum(min_values, run_obj.getProperty('MDNorm_low').value).tolist()
except ValueError:
raise RuntimeError("Could not compare old and new values for 'MDNorm_low' log. "
+ "Make sure the length of the old data is equal to the number of spectra")
run_obj.addProperty('MDNorm_low', min_values, True)
if run_obj.hasProperty('MDNorm_high'):
try:
max_values = numpy.minimum(max_values, run_obj.getProperty('MDNorm_high').value).tolist()
except ValueError:
raise RuntimeError("Could not compare old and new values for 'MDNorm_high' log. "
+ "Make sure the length of the old data is equal to the number of spectra")
run_obj.addProperty('MDNorm_high', [xmax]*num_spectra, True)
run_obj.addProperty('MDNorm_spectra_index', numpy.arange(num_spectra, dtype=float).tolist(), True)
self.setProperty('OutputWorkspace', out_ws)
# Register algorithm with Mantid.
AlgorithmFactory.subscribe(CropWorkspaceForMDNorm)