forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRReductionWithReference.py
209 lines (184 loc) · 7.7 KB
/
LRReductionWithReference.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
# 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 json
import numpy as np
from mantid.api import DataProcessorAlgorithm
from mantid.simpleapi import \
AlgorithmFactory, \
CreateWorkspace, \
DeleteWorkspace, \
Divide, \
LiquidsReflectometryReduction
# Unable to generate this props list in PyInit using AlgorithmManager
# from LiquidsReflectometryReduction to copy properties here
LR_ALG_FOR_PROPS = "LiquidsReflectometryReduction"
PROPS_TO_COPY = [
'RunNumbers',
'InputWorkspace',
'NormalizationRunNumber',
'SignalPeakPixelRange',
'SubtractSignalBackground',
'SignalBackgroundPixelRange',
'NormFlag',
'NormPeakPixelRange',
'SubtractNormBackground',
'NormBackgroundPixelRange',
'LowResDataAxisPixelRangeFlag',
'LowResDataAxisPixelRange',
'LowResNormAxisPixelRangeFlag',
'LowResNormAxisPixelRange',
'TOFRange',
'TOFRangeFlag',
'QMin',
'QStep',
'AngleOffset',
'AngleOffsetError',
'OutputWorkspace',
'ApplyScalingFactor',
'ScalingFactorFile',
'SlitTolerance',
'SlitsWidthFlag',
'IncidentMediumSelected',
'GeometryCorrectionFlag',
'FrontSlitName',
'BackSlitName',
'TOFSteps',
'CropFirstAndLastPoints',
'ApplyPrimaryFraction',
'PrimaryFractionRange']
class LRReductionWithReference(DataProcessorAlgorithm):
def category(self):
return "Reflectometry\\SNS"
def name(self):
return "LRReductionWithReference"
def version(self):
return 1
def summary(self):
return "REFL reduction using a reference measurement for normalization"
def PyInit(self):
self.copyProperties(LR_ALG_FOR_PROPS, PROPS_TO_COPY)
self.declareProperty("Refl1DModelParameters", "",
doc="JSON string for Refl1D theoretical model parameters")
def PyExec(self):
try:
import refl1d # noqa: F401
except ImportError:
err_msg = 'Refl1D not installed, unable to run this algorithm'
raise RuntimeError(err_msg)
# Get properties we copied to run LiquidsReflectometryReduction algorithms
kwargs = dict()
for prop in PROPS_TO_COPY:
kwargs[prop] = self.getProperty(prop).value
# Process the reference normalization run
norm_wksp = LiquidsReflectometryReduction(
RunNumbers=[kwargs['NormalizationRunNumber']],
InputWorkspace='',
NormalizationRunNumber=kwargs['NormalizationRunNumber'],
SignalPeakPixelRange=kwargs['NormPeakPixelRange'],
SubtractSignalBackground=kwargs['SubtractNormBackground'],
SignalBackgroundPixelRange=kwargs['NormBackgroundPixelRange'],
NormFlag=False,
NormPeakPixelRange=kwargs['NormPeakPixelRange'],
SubtractNormBackground=kwargs['SubtractNormBackground'],
NormBackgroundPixelRange=kwargs['NormBackgroundPixelRange'],
LowResDataAxisPixelRangeFlag=kwargs['LowResNormAxisPixelRangeFlag'],
LowResDataAxisPixelRange=kwargs['LowResNormAxisPixelRange'],
LowResNormAxisPixelRangeFlag=kwargs['LowResNormAxisPixelRangeFlag'],
LowResNormAxisPixelRange=kwargs['LowResNormAxisPixelRange'],
TOFRange=kwargs['TOFRange'],
TOFRangeFlag=kwargs['TOFRangeFlag'],
QMin=kwargs['QMin'],
QStep=kwargs['QStep'],
AngleOffset=kwargs['AngleOffset'],
AngleOffsetError=kwargs['AngleOffsetError'],
OutputWorkspace=kwargs['OutputWorkspace'],
ApplyScalingFactor=False,
ScalingFactorFile=kwargs['ScalingFactorFile'],
SlitTolerance=kwargs['SlitTolerance'],
SlitsWidthFlag=kwargs['SlitsWidthFlag'],
IncidentMediumSelected=kwargs['IncidentMediumSelected'],
GeometryCorrectionFlag=kwargs['GeometryCorrectionFlag'],
FrontSlitName=kwargs['FrontSlitName'],
BackSlitName=kwargs['BackSlitName'],
TOFSteps=kwargs['TOFSteps'],
CropFirstAndLastPoints=kwargs['CropFirstAndLastPoints'],
ApplyPrimaryFraction=kwargs['ApplyPrimaryFraction'],
PrimaryFractionRange=kwargs['PrimaryFractionRange'])
# Calculate the theoretical reflectivity for normalization using Refl1D
q = norm_wksp.readX(0)
model_json = self.getProperty("Refl1DModelParameters").value
model_dict = json.loads(model_json)
model_reflectivity = self.calculate_reflectivity(model_dict, q)
model_wksp = CreateWorkspace(
DataX=q,
DataY=model_reflectivity,
DataE=np.zeros(len(q)),
UnitX=norm_wksp.getAxis(0).getUnit().unitID())
# Calculate the incident flux ( measured / model) for reference
incident_flux = Divide(norm_wksp, model_wksp)
# Process the sample run(s)
kwargs['NormFlag'] = False
kwargs['ApplyScalingFactor'] = False
sample_wksp = LiquidsReflectometryReduction(**kwargs)
# Normalize using the incident flux
out_wksp = Divide(sample_wksp, incident_flux)
# Output
self.setProperty('OutputWorkspace', out_wksp)
# Clean up
DeleteWorkspace(model_wksp)
DeleteWorkspace(norm_wksp)
DeleteWorkspace(incident_flux)
def calculate_reflectivity(self, model_description, q, q_resolution=0.025):
"""
Reflectivity calculation using refl1d
:param model_description: dict that holds parameters for the
theoretical Refl1D model.
Example dict for paramters:
{
'back_sld': 2.07,
'back_roughness': 1.0,
'front_sld': 0,
'scale': 1,
'background': 0
'layers': [{
'thickness': 10,
'sld': 3.5,
'isld': 0,
'roughness': 2}]
}
:param q: Momentum transfer (Q) to use to calculate the model
:param q_resolution: Momentum transfer resolution to multiply by Q
:return: Calculate reflectivity of the theoretical model
"""
from refl1d.names import \
Experiment, \
Parameter, \
QProbe, \
Slab, \
SLD
zeros = np.zeros(len(q))
dq = q_resolution * q
# The QProbe object represents the beam
probe = QProbe(q, dq, data=(zeros, zeros))
sample = Slab(
material=SLD(name='back', rho=model_description['back_sld']),
interface=model_description['back_roughness'])
# Add each layer
for i, layer in enumerate(model_description['layers']):
sample = sample | Slab(material=SLD(name='layer%s' % i,
rho=layer['sld'],
irho=layer['isld']),
thickness=layer['thickness'],
interface=layer['roughness'])
sample = sample | Slab(material=SLD(name='front',
rho=model_description['front_sld']))
probe.background = Parameter(value=model_description['background'], name='background')
expt = Experiment(probe=probe, sample=sample)
q, r = expt.reflectivity()
return model_description['scale'] * r
AlgorithmFactory.subscribe(LRReductionWithReference)