1 | """*WIKI* |
---|
2 | |
---|
3 | This algorithm allows a single spectrum to be extracted from a range of workspaces and placed into a single workspace for comparison and plotting. The LabelUsing property allows you to control what the end labels applied to each spectra will be. The default is to use the source workspace name, but you can specify the name of a log value to use as the label, e.g. Temp_Sample. the LabelValue property allows control of how a single value is extracted from time series logs. |
---|
4 | Modified from standard ConjoinSpectra to make the Y axis numeric and so allow Colour Fill and Contour plots of the resulting Workspace2D. (JSL) |
---|
5 | |
---|
6 | *WIKI*""" |
---|
7 | |
---|
8 | from mantid.api import * |
---|
9 | from mantid.kernel import * |
---|
10 | from mantid.simpleapi import * |
---|
11 | import os |
---|
12 | |
---|
13 | class ConjoinSpectraNumAx(PythonAlgorithm): |
---|
14 | """ |
---|
15 | Conjoins spectra from several workspaces into a single workspaceExists |
---|
16 | |
---|
17 | Spectra to be conjoined must be equally binned in order for ConjoinSpectra to work. If necessary use RebinToWorkspace first. |
---|
18 | """ |
---|
19 | |
---|
20 | def category(self): |
---|
21 | return "Transforms\\Merging;PythonAlgorithms" |
---|
22 | |
---|
23 | def name(self): |
---|
24 | return "ConjoinSpectraNumAx" |
---|
25 | |
---|
26 | def PyInit(self): |
---|
27 | self.declareProperty("InputWorkspaces","", validator=StringMandatoryValidator(), doc="Comma seperated list of workspaces to use, group workspaces will automatically include all members.") |
---|
28 | self.declareProperty(WorkspaceProperty("OutputWorkspace", "", direction=Direction.Output), doc="Name the workspace that will contain the result") |
---|
29 | self.declareProperty("WorkspaceIndex", 0, doc="The workspace index of the spectra in each workspace to extract. Default: 0") |
---|
30 | self.declareProperty("LabelUsing", "", doc="The name of a log value used to label the resulting spectra. Default: The source workspace name") |
---|
31 | labelValueOptions = ["Mean","Median","Maximum","Minimum","First Value","Last Value"] |
---|
32 | self.declareProperty("LabelValue", "Mean", validator=StringListValidator(labelValueOptions), doc="How to derive the value from a time series property") |
---|
33 | |
---|
34 | |
---|
35 | def PyExec(self): |
---|
36 | # get parameter values |
---|
37 | wsOutput = self.getPropertyValue("OutputWorkspace") |
---|
38 | wsIndex = int(self.getPropertyValue("WorkspaceIndex")) |
---|
39 | wsString = self.getPropertyValue("InputWorkspaces").strip() |
---|
40 | labelUsing = self.getPropertyValue("LabelUsing").strip() |
---|
41 | labelValue = self.getPropertyValue("LabelValue") |
---|
42 | |
---|
43 | #internal values |
---|
44 | wsTemp = "__ConjoinSpectra_temp" |
---|
45 | loopIndex=0 |
---|
46 | |
---|
47 | #get the wokspace list |
---|
48 | wsNames = [] |
---|
49 | for wsName in wsString.split(","): |
---|
50 | wsName = wsName.strip() |
---|
51 | #check the ws is in mantid |
---|
52 | #if we cannot find the ws then stop |
---|
53 | if wsName not in mtd: |
---|
54 | raise RuntimeError ("Cannot find workspace '" + wsName.strip() + "', aborting") |
---|
55 | |
---|
56 | ws = mtd[wsName] |
---|
57 | |
---|
58 | if isinstance(ws, WorkspaceGroup): |
---|
59 | wsNames.extend(ws.getNames()) |
---|
60 | else: |
---|
61 | wsNames.append(wsName) |
---|
62 | |
---|
63 | #ta = TextAxis.create(len(wsNames)) |
---|
64 | na = NumericAxis.create(len(wsNames)) |
---|
65 | #if (labelUsing != ""): |
---|
66 | # na.title(labelUsing) |
---|
67 | #else: |
---|
68 | # na.title("Workspaces") |
---|
69 | na.setUnit("TOF") |
---|
70 | #if wsOutput in mtd: |
---|
71 | # DeleteWorkspace(Workspace=wsOutput) |
---|
72 | for wsName in wsNames: |
---|
73 | #extract the spectrum |
---|
74 | ExtractSingleSpectrum(InputWorkspace=wsName,OutputWorkspace=wsTemp,WorkspaceIndex=wsIndex) |
---|
75 | |
---|
76 | labelDouble =0.0 |
---|
77 | if (labelUsing != ""): |
---|
78 | labelDouble = self.GetLogValue(mtd[wsName.strip()],labelUsing,labelValue) |
---|
79 | else: |
---|
80 | labelDouble = loopIndex + 0.0 |
---|
81 | #ta.setValue(loopIndex,labelString) |
---|
82 | na.setValue(loopIndex,labelDouble) |
---|
83 | loopIndex += 1 |
---|
84 | if wsOutput in mtd: |
---|
85 | ConjoinWorkspaces(InputWorkspace1=wsOutput,InputWorkspace2=wsTemp,CheckOverlapping=False) |
---|
86 | if wsTemp in mtd: |
---|
87 | DeleteWorkspace(Workspace=wsTemp) |
---|
88 | else: |
---|
89 | RenameWorkspace(InputWorkspace=wsTemp,OutputWorkspace=wsOutput) |
---|
90 | |
---|
91 | wsOut = mtd[wsOutput] |
---|
92 | #replace the spectrun axis |
---|
93 | wsOut.replaceAxis(1,na) |
---|
94 | |
---|
95 | |
---|
96 | self.setProperty("OutputWorkspace",wsOut) |
---|
97 | |
---|
98 | def GetLogValue(self,ws,labelUsing,labelValue): |
---|
99 | labelDouble = 0.0 |
---|
100 | run=ws.getRun() |
---|
101 | try: |
---|
102 | prop = run.getProperty(labelUsing) |
---|
103 | try: |
---|
104 | stats = prop.getStatistics() |
---|
105 | if (labelValue == "Mean"): |
---|
106 | labelDouble = stats.mean |
---|
107 | elif (labelValue == "Median"): |
---|
108 | labelDouble = stats.median |
---|
109 | elif (labelValue == "Maximum"): |
---|
110 | labelDouble = stats.maximum |
---|
111 | elif (labelValue == "Minimum"): |
---|
112 | labelDouble = stats.minimum |
---|
113 | elif (labelValue == "Last Value"): |
---|
114 | labelDouble = prop.value[-1] + 0.0 |
---|
115 | else: |
---|
116 | labelDouble = prop.value[0] + 0.0 |
---|
117 | except: |
---|
118 | #this is not a time series property - just return the value |
---|
119 | labelDouble = prop.value + 0.0 |
---|
120 | except: |
---|
121 | #failed to find the property |
---|
122 | #log and pass out zero |
---|
123 | logger.notice("Could not find log " + labelUsing + " in workspace " + str(ws) + " using workspace label instead.") |
---|
124 | return labelDouble |
---|
125 | |
---|
126 | AlgorithmFactory.subscribe(ConjoinSpectraNumAx) |
---|