Newer
Older
#pylint: disable=no-init,unused-variable,invalid-name,bare-except
from mantid.api import *
from mantid.kernel import *
class SavePlot1DAsJson(PythonAlgorithm):
""" Save 1D plottable data in json format from workspace.
"""
def category(self):
"""
"""
return "SavePlot1DAsJson"
def summary(self):
""" Return summary
"""
return "Plottable data file in Json format"
def require(self):
try:
import json
except:
raise ImportError("Missing json package")
def PyInit(self):
""" Declare properties
"""
# this is the requirement of using this plugin
# is there a place to register that?
self.require()
self.declareProperty(
MatrixWorkspaceProperty("InputWorkspace", "", Direction.Input),\
"Workspace that contains plottable data")
self.declareProperty(
FileProperty("JsonFilename", "", FileAction.Save, ['.json']),
self.declareProperty("PlotName", "", "Name of the output plot")
return
def PyExec(self):
""" Main Execution Body
"""
# Properties
inputwsname = self.getPropertyValue("InputWorkspace")
outfilename = self.getPropertyValue("JsonFilename")
plotname = self.getPropertyValue("PlotName")
# Check properties
inputws = AnalysisDataService.retrieve(inputwsname)
if inputws is None:
raise ValueError(
"Inputworkspace does not exist.")
if inputws.axes() > 2:
raise ValueError(
"InputWorkspace must be one-dimensional.")
def _save(self, inputws, outpath, plotname):
pname = plotname or workspace.getName()
ishist = workspace.isHistogramData()
plottype = "histogram" if ishist else "point"
for i in range(workspace.getNumberHistograms()):
spectrum_no = workspace.getSpectrum(i).getSpectrumNo()
# Why do we need label?
# label = "%s_spectrum_%d" % (pname, spectrum_no)
# labels.append(label)
# or title?
# title = "%s - spectrum %d" % (workspace.getTitle(), spectrum_no)
arr = [
list(workspace.readX(i)),
list(workspace.readY(i)),
list(workspace.readE(i)),
]
serialized['data'][spectrum_no] = arr
# axes
# .. helper
label = lambda axis: axis.getUnit().caption()
def unit(axis):
s = axis.getUnit().symbol()
try:
return s.latex()
except:
return '%s' % s
axes = dict(
xlabel=label(workspace.getAxis(0)),
ylabel=label(workspace.getAxis(1)),
xunit = unit(workspace.getAxis(0)),
# yunit = unit(workspace.getAxis(1)),
yunit = workspace.YUnitLabel(),
return {pname: serialized}
# Register algorithm with Mantid
AlgorithmFactory.subscribe(SavePlot1DAsJson)