forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NormaliseSpectra.py
76 lines (59 loc) · 2.85 KB
/
NormaliseSpectra.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
# 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
from mantid.kernel import *
from mantid.api import (MatrixWorkspaceProperty, DataProcessorAlgorithm, AlgorithmFactory)
from mantid.simpleapi import *
import numpy as np
class NormaliseSpectra(DataProcessorAlgorithm):
_input_ws_name = None
_input_ws = None
_output_ws_name = None
def category(self):
return 'Workflow\\MIDAS;Inelastic'
def summary(self):
return 'Normalise all spectra to have a max value of 1'
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty('InputWorkspace', '',
direction=Direction.Input),
doc='Input workspace')
self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '',
direction=Direction.Output),
doc='Output workspace')
def PyExec(self):
self._setup()
CloneWorkspace(InputWorkspace=self._input_ws,
OutputWorkspace=self._output_ws_name)
num_hists = self._input_ws.getNumberHistograms()
output_ws = mtd[self._output_ws_name]
for idx in range(num_hists):
single_spectrum = ExtractSpectra(InputWorkspace=self._input_ws, WorkspaceIndexList=idx)
y_data = single_spectrum.readY(0)
ymax = np.nanmax(y_data)
# raises a RuntimeError if the ymax is <= 0
if ymax <= 0:
spectrum_no = single_spectrum.getSpectrum(0).getSpectrumNo()
DeleteWorkspace('single_spectrum')
raise RuntimeError("Spectrum number %d:" % (spectrum_no)
+ "has a maximum y value of 0 or less. "
+ "All spectra must have a maximum y value more than 0")
Scale(InputWorkspace=single_spectrum, Operation="Multiply",
Factor=(1/ymax), OutputWorkspace=single_spectrum)
output_ws.setY(idx, single_spectrum.readY(0))
output_ws.setE(idx, single_spectrum.readE(0))
# Delete extracted spectra workspace
DeleteWorkspace('single_spectrum')
self.setProperty('OutputWorkspace', output_ws)
def _setup(self):
"""
Gets properties.
"""
self._input_ws_name = self.getPropertyValue('InputWorkspace')
self._input_ws = mtd[self._input_ws_name]
self._output_ws_name = self.getPropertyValue('OutputWorkspace')
# Register algorithm with Mantid
AlgorithmFactory.subscribe(NormaliseSpectra)