-
Notifications
You must be signed in to change notification settings - Fork 121
/
Mean.py
48 lines (36 loc) · 1.73 KB
/
Mean.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
#pylint: disable=no-init,invalid-name
from __future__ import (absolute_import, division, print_function)
from mantid.simpleapi import *
from mantid.api import *
from mantid.kernel import *
class Mean(PythonAlgorithm):
def category(self):
return "Arithmetic"
def name(self):
return "Mean"
def summary(self):
return "Calculates the arithemetic mean of the workspaces provided."
def PyInit(self):
mustHaveWorkspaceNames = StringMandatoryValidator()
self.declareProperty("Workspaces", "", validator=mustHaveWorkspaceNames,
direction=Direction.Input,
doc="Input workspaces. Comma separated workspace names")
self.declareProperty(MatrixWorkspaceProperty("OutputWorkspace", "", Direction.Output),
doc="Output mean workspace")
def _are_workspaces_compatible(self, ws_a, ws_b):
sizeA = ws_a.blocksize() * ws_a.getNumberHistograms()
sizeB = ws_b.blocksize() * ws_b.getNumberHistograms()
return sizeA == sizeB
def PyExec(self):
workspaces = self.getProperty("Workspaces").value.split(',')
out_ws = CloneWorkspace(InputWorkspace=mtd[workspaces[0]],
OutputWorkspace=self.getPropertyValue("OutputWorkspace"))
for index in range(1, len(workspaces)):
name = workspaces[index].strip()
workspace = mtd[name]
if not self._are_workspaces_compatible(out_ws, workspace):
raise RuntimeError("Input Workspaces are not the same shape.")
out_ws += workspace
out_ws /= len(workspaces)
self.setProperty("OutputWorkspace", out_ws)
AlgorithmFactory.subscribe(Mean())