diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Save1DPlottableAsJson.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Save1DPlottableAsJson.py index d8c6a13919f375196ec59d9de04cf74af5b42e04..dea314d278bc90e579a1eab0942bf38059c1170f 100644 --- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Save1DPlottableAsJson.py +++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Save1DPlottableAsJson.py @@ -1,14 +1,13 @@ -#pylint: disable=no-init,invalid-name -import mantid.simpleapi as api +#pylint: disable=no-init,unused-variable from mantid.api import * from mantid.kernel import * import os - + # See ticket #10234 class Save1DPlottableAsJson(PythonAlgorithm): - """ Save 1D plottable data in json format from workspace. + """ Save 1D plottable data in json format from workspace. """ def category(self): """ @@ -36,7 +35,6 @@ class Save1DPlottableAsJson(PythonAlgorithm): """ # this is the requirement of using this plugin # is there a place to register that? - self.require() self.declareProperty( @@ -46,9 +44,9 @@ class Save1DPlottableAsJson(PythonAlgorithm): self.declareProperty( FileProperty("JsonFilename", "", FileAction.Save, ['.json']), "Name of the output Json file") - + self.declareProperty("PlotName", "", "Name of the output plot") - + return def PyExec(self): @@ -67,46 +65,46 @@ class Save1DPlottableAsJson(PythonAlgorithm): if inputws.axes() > 2: raise ValueError( "InputWorkspace must be one-dimensional.") - + if os.path.exists(outfilename): raise IOError( "Output file %s already exists" % outfilename) # Generate Json file - output = self._save(inputws, outfilename, plotname) + self._save(inputws, outfilename, plotname) return def _save(self, inputws, outpath, plotname): - d = self._serialize(inputws, plotname) + plot = self._serialize(inputws, plotname) import json - json.dump(d, open(outpath, 'wt')) + json.dump(plot, open(outpath, 'w')) return - - def _serialize(self, ws, plotname): - wname = plotname or ws.getName() + + def _serialize(self, workspace, plotname): + wname = plotname or workspace.getName() # init dictionary - ishist = ws.isHistogramData() - type = "histogram" if ishist else "point" - d = {"type": type} + ishist = workspace.isHistogramData() + plottype = "histogram" if ishist else "point" + serialized = {"type": plottype} # helper label = lambda axis: "%s (%s)" % ( axis.getUnit().caption(), axis.getUnit().symbol() or 1, ) # loop over spectra - for i in range(ws.getNumberHistograms()): + for i in range(workspace.getNumberHistograms()): k = "%s%s" % (wname, i) - v = dict( - x = list(ws.readX(i)), - y = list(ws.readY(i)), - e = list(ws.readE(i)), - xlabel = label(ws.getAxis(0)), - ylabel = label(ws.getAxis(1)), - title = "long title of %s" % k, + value = dict( + x=list(workspace.readX(i)), + y=list(workspace.readY(i)), + e=list(workspace.readE(i)), + xlabel=label(workspace.getAxis(0)), + ylabel=label(workspace.getAxis(1)), + title="long title of %s" % k, ) - d[k] = v + serialized[k] = value continue - return d + return serialized # Register algorithm with Mantid diff --git a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/Save1DPlottableAsJsonTest.py b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/Save1DPlottableAsJsonTest.py index 0ce608303fa957c0b0f3291255e88a0ee64605de..1ffc647c2b5953664ed7ada63219e2bcad246d71 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/Save1DPlottableAsJsonTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/plugins/algorithms/Save1DPlottableAsJsonTest.py @@ -1,3 +1,4 @@ +#pylint: disable=invalid-name,too-many-public-methods import unittest import numpy as np import mantid.simpleapi as api @@ -19,19 +20,17 @@ class SaveVulcanGSSTest(unittest.TestCase): # Execute out_path = "tempout_curve.json" alg_test = run_algorithm( - "Save1DPlottableAsJson", + "Save1DPlottableAsJson", InputWorkspace = datawsname, JsonFilename = out_path) - + # executed? self.assertTrue(alg_test.isExecuted()) - # Verify .... d = json.load(open(out_path)) d0 = d[datawsname+'0'] # plots are numbered np.testing.assert_array_equal(d0['x'], E) np.testing.assert_array_equal(d0['y'], I) np.testing.assert_array_equal(d0['e'], err) - # Delete the output file os.remove(out_path) return @@ -46,19 +45,17 @@ class SaveVulcanGSSTest(unittest.TestCase): # Execute out_path = "tempout_hist.json" alg_test = run_algorithm( - "Save1DPlottableAsJson", + "Save1DPlottableAsJson", InputWorkspace = datawsname, JsonFilename = out_path) - + # Executed? self.assertTrue(alg_test.isExecuted()) - # Verify .... d = json.load(open(out_path)) d0 = d[datawsname+'0'] # plots are numbered np.testing.assert_array_equal(d0['x'], E) np.testing.assert_array_equal(d0['y'], I) np.testing.assert_array_equal(d0['e'], err) - # Delete the output file os.remove(out_path) return @@ -69,16 +66,14 @@ class SaveVulcanGSSTest(unittest.TestCase): """ datawsname = "TestTwoCurves" E, I, err, I2, err2 = self._createTwoCurves(datawsname) - # Execute out_path = "tempout_2curves.json" alg_test = run_algorithm( - "Save1DPlottableAsJson", + "Save1DPlottableAsJson", InputWorkspace = datawsname, JsonFilename = out_path) - + # executed? self.assertTrue(alg_test.isExecuted()) - # Verify .... d = json.load(open(out_path)) d0 = d[datawsname+'0'] # plots are numbered @@ -88,7 +83,6 @@ class SaveVulcanGSSTest(unittest.TestCase): d1 = d[datawsname+'1'] # np.testing.assert_array_equal(d1['y'], I2) np.testing.assert_array_equal(d1['e'], err2) - # Delete the output file os.remove(out_path) return @@ -99,17 +93,15 @@ class SaveVulcanGSSTest(unittest.TestCase): """ datawsname = "TestOneCurve" E, I, err = self._createOneCurve(datawsname) - # Execute out_path = "tempout_curve_withname.json" alg_test = run_algorithm( - "Save1DPlottableAsJson", + "Save1DPlottableAsJson", InputWorkspace = datawsname, JsonFilename = out_path, PlotName = "myplot") - + # executed? self.assertTrue(alg_test.isExecuted()) - # Verify .... d = json.load(open(out_path)) plotname = "myplot" @@ -117,7 +109,6 @@ class SaveVulcanGSSTest(unittest.TestCase): np.testing.assert_array_equal(d0['x'], E) np.testing.assert_array_equal(d0['y'], I) np.testing.assert_array_equal(d0['e'], err) - # Delete the output file os.remove(out_path) return @@ -129,11 +120,10 @@ class SaveVulcanGSSTest(unittest.TestCase): E = np.arange(-50, 50, 1.0) I = 1000 * np.exp(-E**2/10**2) err = I ** .5 - + # create workspace dataws = api.CreateWorkspace( - DataX = E, DataY = I, DataE = err, NSpec = 1, + DataX = E, DataY = I, DataE = err, NSpec = 1, UnitX = "Energy(meV)") - # Add to data service AnalysisDataService.addOrReplace(datawsname, dataws) return E, I, err @@ -146,11 +136,10 @@ class SaveVulcanGSSTest(unittest.TestCase): Ecenters = (E[:-1] + E[1:]) / 2 I = 1000 * np.exp(-Ecenters**2/10**2) err = I ** .5 - + # create workspace dataws = api.CreateWorkspace( - DataX = E, DataY = I, DataE = err, NSpec = 1, + DataX = E, DataY = I, DataE = err, NSpec = 1, UnitX = "Energy(meV)") - # Add to data service AnalysisDataService.addOrReplace(datawsname, dataws) return E, I, err @@ -166,10 +155,9 @@ class SaveVulcanGSSTest(unittest.TestCase): # curve 2 I2 = 1000 * (1+np.sin(E/5*np.pi)) err2 = I ** .5 - # workspace ws = WorkspaceFactory.create( - "Workspace2D", NVectors=2, + "Workspace2D", NVectors=2, XLength = E.size, YLength = I.size ) # curve1 @@ -180,10 +168,10 @@ class SaveVulcanGSSTest(unittest.TestCase): ws.dataX(1)[:] = E ws.dataY(1)[:] = I2 ws.dataE(1)[:] = err2 - # Add to data service AnalysisDataService.addOrReplace(datawsname, ws) return E, I, err, I2, err2 -if __name__ == '__main__': unittest.main() +if __name__ == '__main__': + unittest.main()