forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConjoinSpectra.py
128 lines (110 loc) · 5.3 KB
/
ConjoinSpectra.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
# 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
from mantid.api import *
from mantid.kernel import *
from mantid.simpleapi import *
class ConjoinSpectra(PythonAlgorithm):
"""
Conjoins spectra from several workspaces into a single workspace
Spectra to be conjoined must be equally binned in order for ConjoinSpectra
to work. If necessary use RebinToWorkspace first.
"""
def category(self):
return "Transforms\\Merging"
def seeAlso(self):
return [ "AppendSpectra","ConjoinWorkspaces" ]
def name(self):
return "ConjoinSpectra"
def summmary(self):
return "Joins individual spectra from a range of workspaces into a single workspace for plotting or further analysis."
def PyInit(self):
self.declareProperty("InputWorkspaces","", validator=StringMandatoryValidator(),
doc="Comma seperated list of workspaces to use, group workspaces "
+ "will automatically include all members.")
self.declareProperty(WorkspaceProperty("OutputWorkspace", "", direction=Direction.Output),
doc="Name the workspace that will contain the result")
self.declareProperty("WorkspaceIndex", 0, doc="The workspace index of the spectra in each workspace to extract. Default: 0")
self.declareProperty("LabelUsing", "", doc="The name of a log value used to label the resulting spectra. "
+ "Default: The source workspace name")
labelValueOptions = ["Mean","Median","Maximum","Minimum","First Value"]
self.declareProperty("LabelValue", "Mean", validator=StringListValidator(labelValueOptions),
doc="How to derive the value from a time series property")
def PyExec(self):
# get parameter values
wsOutput = self.getPropertyValue("OutputWorkspace")
wsIndex = int(self.getPropertyValue("WorkspaceIndex"))
wsString = self.getPropertyValue("InputWorkspaces").strip()
labelUsing = self.getPropertyValue("LabelUsing").strip()
labelValue = self.getPropertyValue("LabelValue")
#internal values
wsTemp = "__ConjoinSpectra_temp"
loopIndex=0
#get the wokspace list
wsNames = []
for wsName in wsString.split(","):
#check the ws is in mantid
ws = mtd[wsName.strip()]
#if we cannot find the ws then stop
if ws is None:
raise RuntimeError ("Cannot find workspace '" + wsName.strip() + "', aborting")
if isinstance(ws, WorkspaceGroup):
wsNames.extend(ws.getNames())
else:
wsNames.append(wsName)
ta = TextAxis.create(len(wsNames))
if mtd.doesExist(wsOutput):
DeleteWorkspace(Workspace=wsOutput)
for wsName in wsNames:
#extract the spectrum
ExtractSingleSpectrum(InputWorkspace=wsName,OutputWorkspace=wsTemp,
WorkspaceIndex=wsIndex)
labelString =""
if labelUsing != "":
labelString = self.GetLogValue(mtd[wsName.strip()],labelUsing,labelValue)
if labelString == "":
labelString =wsName+"_"+str(wsIndex)
ta.setLabel(loopIndex,labelString)
loopIndex += 1
if mtd.doesExist(wsOutput):
ConjoinWorkspaces(InputWorkspace1=wsOutput,
InputWorkspace2=wsTemp,
CheckOverlapping=False)
if mtd.doesExist(wsTemp):
DeleteWorkspace(Workspace=wsTemp)
else:
RenameWorkspace(InputWorkspace=wsTemp,OutputWorkspace=wsOutput)
wsOut = mtd[wsOutput]
#replace the spectrun axis
wsOut.replaceAxis(1,ta)
self.setProperty("OutputWorkspace",wsOut)
def GetLogValue(self,ws,labelUsing,labelValue):
labelString = ""
run=ws.getRun()
try:
prop = run.getProperty(labelUsing)
try:
stats = prop.getStatistics()
if labelValue == "Mean":
labelString = str(stats.mean)
elif labelValue == "Median":
labelString = str(stats.median)
elif labelValue == "Maximum":
labelString = str(stats.maximum)
elif labelValue == "Minimum":
labelString = str(stats.minimum)
else:
labelString = str(prop.value[0])
except AttributeError:
#this is not a time series property - just return the value
labelString = str(prop.value)
except RuntimeError:
#failed to find the property
#log and pass out an empty string
logger.information("Could not find log " + labelUsing + " in workspace " + str(ws) + " using workspace label instead.")
return labelString
AlgorithmFactory.subscribe(ConjoinSpectra)