Skip to content

Commit

Permalink
Merge pull request #316 from mantidproject/11025_plotSlice_crashes_on…
Browse files Browse the repository at this point in the history
…_peaks_ws

plotSlice on wrong workspace types: prevent crash and give informative error message
  • Loading branch information
eXeC64 committed Feb 27, 2015
2 parents cbc7d62 + 2e7e9a1 commit 8e8ab9d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Code/Mantid/MantidPlot/pymantidplot/__init__.py
Expand Up @@ -670,6 +670,8 @@ def plotSlice(source, label="", xydim=None, slicepoint=None,
SliceViewer, e.g. setting the view and slice point.
"""
workspace_names = getWorkspaceNames(source)
__checkPlotSliceWorkspaces(workspace_names)

try:
import mantidqtpython
except:
Expand Down Expand Up @@ -1032,4 +1034,37 @@ def __checkPlotMDWorkspaces(workspace_names):
if not isinstance(mantid.api.mtd[name], mantid.api.IMDWorkspace):
raise ValueError("Workspace '%s' is not an IMDWorkspace" % name)


def __checkPlotSliceWorkspaces(ws_names):
"""Checks that a list of workspaces is valid as input to plotSlice.
That means that the list should not be empty, all the workspaces
given must be present in the analysis data service, and all of
them must be MDworkspace. If any of these conditions is not met,
it raises an exception with specific error message.
Throws Python-level exceptions (ValueError) if the list is empty
or any of the spaces don't exist or are not of IMDWorkspace type.
Args:
ws_names: list of names of workspace(s)
Returns:
Nothing, just throws exceptions in case of error/inconsistent input
"""
if len(ws_names) == 0:
raise ValueError("No workspace name(s) given. You need to specify at least one.")
for name in ws_names:
if not mantid.api.mtd.doesExist(name):
raise ValueError("Workspace '%s' does not exist in the workspace list" % name)

if not isinstance(mantid.api.mtd[name], mantid.api.IMDWorkspace):
if isinstance(mantid.api.mtd[name], mantid.api.IPeaksWorkspace):
raise ValueError("'%s' is not an IMDWorkspace as expected, but an "
"IPeaksWorkspace instead. Hint: if you want to overlay "
"peaks and use the Peaks Viewer, maybe "
"setPeaksWorkspaces is what you need?" % name)
else:
raise ValueError("%s is not an IMDWorkspace as expected." % name)

#-----------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions Code/Mantid/MantidQt/SliceViewer/src/LineViewer.cpp
Expand Up @@ -656,6 +656,8 @@ bool LineViewer::getFixedBinWidthMode() const
* @param ws :: IMDWorkspace */
void LineViewer::setWorkspace(Mantid::API::IMDWorkspace_sptr ws)
{
if(!ws)
throw std::runtime_error("LineViewer::setWorkspace(): Invalid workspace.");
m_ws = ws;
m_thickness = VMD(ws->getNumDims());
createDimensionWidgets();
Expand Down

0 comments on commit 8e8ab9d

Please sign in to comment.