-
Notifications
You must be signed in to change notification settings - Fork 122
/
DetectorFloodWeighting.py
161 lines (130 loc) · 6.63 KB
/
DetectorFloodWeighting.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
from __future__ import (absolute_import, division, print_function)
import numpy as np
from mantid.api import DataProcessorAlgorithm, AlgorithmFactory, MatrixWorkspaceProperty, WorkspaceUnitValidator, \
PropertyMode, Progress
from mantid.kernel import Direction, FloatArrayProperty, FloatArrayBoundedValidator
class DetectorFloodWeighting(DataProcessorAlgorithm):
def __init__(self):
DataProcessorAlgorithm.__init__(self)
def category(self):
return 'Workflow\\SANS'
def summary(self):
return 'Generates a Detector flood weighting, or sensitivity workspace'
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty('InputWorkspace', '',
direction=Direction.Input, validator=WorkspaceUnitValidator("Wavelength")),
doc='Flood weighting measurement')
self.declareProperty(MatrixWorkspaceProperty('TransmissionWorkspace', '',
direction=Direction.Input, optional=PropertyMode.Optional,
validator=WorkspaceUnitValidator("Wavelength")),
doc='Flood weighting measurement')
validator = FloatArrayBoundedValidator()
validator.setLower(0.)
self.declareProperty(FloatArrayProperty('Bands', [], direction=Direction.Input, validator=validator),
doc='Wavelength bands to use. Single pair min to max.')
self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '',
direction=Direction.Output),
doc='Normalized flood weighting measurement')
self.declareProperty("SolidAngleCorrection", True, direction=Direction.Input, doc="Perform final solid angle correction")
def validateInputs(self):
"""
Validates input ranges.
"""
issues = dict()
bands = self.getProperty('Bands').value
if not any(bands):
issues['Bands'] = 'Bands must be supplied'
return issues # Abort early. Do not continue
if not len(bands)%2 == 0:
issues['Bands'] = 'Even number of Bands boundaries expected'
return issues # Abort early. Do not continue
all_limits=list()
for i in range(0, len(bands), 2):
lower = bands[i]
upper = bands[i+1]
limits = np.arange(lower, upper)
unique = set(limits)
for existing_lims in all_limits:
if unique.intersection(set(existing_lims)):
issues['Bands'] = 'Bands must not intersect'
break
all_limits.append(limits)
if lower >= upper:
issues['Bands'] = 'Bands should form lower, upper pairs'
input_ws = self.getProperty('InputWorkspace').value
trans_ws = self.getProperty('TransmissionWorkspace').value
if trans_ws:
if not trans_ws.getNumberHistograms() == input_ws.getNumberHistograms():
issues['TransmissionWorkspace'] = 'Transmission should have same number of histograms as flood input workspace'
if not trans_ws.blocksize() == input_ws.blocksize():
issues['TransmissionWorkspace'] = 'Transmission workspace should be rebinned the same as the flood input workspace'
return issues
def _divide(self, lhs, rhs):
divide = self.createChildAlgorithm("Divide")
divide.setProperty("LHSWorkspace", lhs)
divide.setProperty("RHSWorkspace", rhs)
divide.execute()
return divide.getProperty("OutputWorkspace").value
def _add(self, lhs, rhs):
divide = self.createChildAlgorithm("Plus")
divide.setProperty("LHSWorkspace", lhs)
divide.setProperty("RHSWorkspace", rhs)
divide.execute()
return divide.getProperty("OutputWorkspace").value
def _integrate_bands(self, bands, in_ws):
# Formulate bands, integrate and sum
accumulated_output = None
for i in range(0, len(bands), 2):
lower = bands[i]
upper = bands[i+1]
step = upper - lower
rebin = self.createChildAlgorithm("Rebin")
rebin.setProperty("Params", [lower, step, upper])
rebin.setProperty("InputWorkspace", in_ws) # Always integrating the same input workspace
rebin.execute()
integrated = rebin.getProperty("OutputWorkspace").value
if accumulated_output:
accumulated_output = self._add(accumulated_output, integrated)
else:
# First band
accumulated_output = integrated
return accumulated_output
def PyExec(self):
progress = Progress(self, 0, 1, 4) # Four coarse steps
in_ws = self.getProperty('InputWorkspace').value
trans_ws = self.getProperty('TransmissionWorkspace').value
bands = self.getProperty('Bands').value
accumulated_output = self._integrate_bands(bands, in_ws)
if trans_ws:
accumulated_trans_output = self._integrate_bands(bands, trans_ws)
progress.report()
# Perform solid angle correction. Calculate solid angle then divide through.
normalized=accumulated_output
if self.getProperty("SolidAngleCorrection").value:
solidAngle = self.createChildAlgorithm("SolidAngle")
solidAngle.setProperty("InputWorkspace", accumulated_output)
solidAngle.execute()
solid_angle_weighting = solidAngle.getProperty("OutputWorkspace").value
normalized = self._divide(normalized, solid_angle_weighting)
progress.report()
# Divide through by the transmission workspace provided
if trans_ws:
normalized = self._divide(normalized, accumulated_trans_output)
# Determine the max across all spectra
y_values = normalized.extractY()
mean_val = np.mean(y_values)
# Create a workspace from the single max value
create = self.createChildAlgorithm("CreateSingleValuedWorkspace")
create.setProperty("DataValue", mean_val)
create.execute()
mean_ws = create.getProperty("OutputWorkspace").value
# Divide each entry by mean
normalized = self._divide(normalized, mean_ws)
progress.report()
# Fix-up ranges
for i in range(normalized.getNumberHistograms()):
normalized.dataX(i)[0] = bands[0]
normalized.dataX(i)[1] = bands[-1]
self.setProperty('OutputWorkspace', normalized)
# Register alg
AlgorithmFactory.subscribe(DetectorFloodWeighting)