Skip to content

Commit

Permalink
Refs #4219. Plotting now doesn't care which Python is used.
Browse files Browse the repository at this point in the history
The functions now check for attributes rather than specific objects
so that they can work transparently with whichever Python api is loaded
  • Loading branch information
martyngigg committed Jan 6, 2012
1 parent 922a084 commit 8c10294
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
Expand Up @@ -27,6 +27,8 @@ void export_Workspace()
register_ptr_to_python<Workspace_sptr>();

class_<Workspace, bases<DataItem>, boost::noncopyable>("Workspace", no_init)
.def("getName", &Workspace::getName, return_value_policy<copy_const_reference>(),
"Returns the name of the workspace. This could be an empty string")
.def("getTitle", &Workspace::getTitle, "Returns the title of the workspace")
.def("getComment", &Workspace::getComment, return_value_policy<copy_const_reference>(), "Returns the comment field on the workspace")
.def("isDirty", &Workspace::isDirty, Workspace_isDirtyOverloads(args("n"), "True if the workspace has run more than n algorithms (Default=1)"))
Expand Down
58 changes: 36 additions & 22 deletions Code/Mantid/MantidPlot/mantidplotpy/mantidplot.py
Expand Up @@ -8,8 +8,7 @@
except ImportError:
raise ImportError('The "mantidplot" module can only be used from within MantidPlot.')

# Grab a few Mantid things so that we can recognise workspace variables
from MantidFramework import WorkspaceProxy, WorkspaceGroup, MatrixWorkspace, mtd

import proxies

from PyQt4 import QtCore, QtGui
Expand All @@ -19,6 +18,22 @@
# (a) don't need a proxy & (b) can be constructed from python
from qti import PlotSymbol, ImageSymbol, ArrowMarker, ImageMarker

#-------------------------- Mantid Python access functions----------------
# Grab a few Mantid things so that we can recognise workspace variables
# While we have 2 APIs we need to figure out which to use so add a little bit of indirection
def get_analysis_data_service():
"""Returns an object that can be used to get a workspace by name from Mantid
Returns:
A object that acts like a dictionary to retrieve workspaces
"""
if 'mantid.api' in sys.modules:
import mantid
return mantid.AnalysisDataService.Instance()
else:
import MantidFramework
return MantidFramework.mtd

#-------------------------- Wrapped MantidPlot functions -----------------

# Overload for consistency with qtiplot table(..) & matrix(..) commands
Expand All @@ -28,7 +43,7 @@ def workspace(name):
Args:
name: The name of the workspace in the Analysis Data Service.
"""
return mtd[name]
return get_analysis_data_service()[name]

def table(name):
"""Get a handle on a table.
Expand Down Expand Up @@ -149,12 +164,11 @@ def plot(source, *args, **kwargs):
Returns:
A handle to the created Graph widget.
"""
if isinstance(source,WorkspaceProxy):
return plotSpectrum(source, *args, **kwargs)
else:
if hasattr(source, '_getHeldObject') and isinstance(source._getHeldObject(), QtCore.QObject):
return proxies.Graph(qti.app.plot(source._getHeldObject(), *args, **kwargs))


else:
plotSpectrum(source, *args, **kwargs)

#-----------------------------------------------------------------------------
def plotSpectrum(source, indices, error_bars = False, type = -1):
"""Open a 1D Plot of a spectrum in a workspace.
Expand Down Expand Up @@ -217,14 +231,15 @@ def stemPlot(source, index, power=None, startPoint=None, endPoint=None):
startPoint=0
if endPoint==None:
endPoint=-1
# If the source is a workspace, create a table from the specified index
if isinstance(source,WorkspaceProxy):

if isinstance(source,proxies.QtProxyObject):
source = source._getHeldObject()
elif hasattr(source, 'getName'):
# If the source is a workspace, create a table from the specified index
wsName = source.getName()
source = qti.app.mantidUI.workspaceToTable(wsName,wsName,[index],False,True)
# The C++ stemPlot method takes the name of the column, so get that
index = source.colName(2)
elif isinstance(source,proxies.QtProxyObject):
source = source._getHeldObject()
# Get column name if necessary
if isinstance(index, int):
index = source.colName(index)
Expand Down Expand Up @@ -563,23 +578,22 @@ def __getWorkspaceNames(source):
for w in source:
names = __getWorkspaceNames(w)
ws_names += names
elif isinstance(source,WorkspaceProxy):
wspace = source._getHeldObject()
elif hasattr(source, 'getName'):
if hasattr(source, '_getHeldObject'):
wspace = source._getHeldObject()
else:
wspace = source
if wspace == None:
return []
if isinstance(wspace,WorkspaceGroup):
grp_names = source.getNames()
if hasattr(wspace, 'getNames'):
grp_names = wspace.getNames()
for n in grp_names:
if n != wspace.getName():
ws_names.append(n)
elif isinstance(wspace,MatrixWorkspace):
ws_names.append(wspace.getName())
else:
# Other non-matrix workspaces
ws_names.append(wspace.getName())
pass
elif isinstance(source,str):
w = mtd[source]
w = get_analysis_data_service()[source]
if w != None:
names = __getWorkspaceNames(w)
for n in names:
Expand Down Expand Up @@ -680,7 +694,7 @@ def __tryPlot(workspace_names, indices, error_bars, type):
def __doPlotting(source, indices, error_bars,type):
if isinstance(source, list) or isinstance(source, tuple):
return __PlotList(source, indices, error_bars,type)
elif isinstance(source, str) or isinstance(source, WorkspaceProxy):
elif isinstance(source, str) or hasattr(source, 'getName'):
return __PlotSingle(source, indices, error_bars,type)
else:
raise TypeError("Source is not a workspace name or a workspace variable")
Expand Down
3 changes: 1 addition & 2 deletions Code/Mantid/MantidPlot/mantidplotpy/proxies.py
Expand Up @@ -6,7 +6,6 @@

from PyQt4 import QtCore
from PyQt4.QtCore import Qt
from MantidFramework import WorkspaceProxy

#-----------------------------------------------------------------------------
#--------------------------- Proxy Objects -----------------------------------
Expand Down Expand Up @@ -160,7 +159,7 @@ def insertCurve(self, *args):
"""
if isinstance(args[0],str):
return self._getHeldObject().insertCurve(*args)
elif isinstance(args[0],WorkspaceProxy):
elif hasattr(args[0], 'getName'):
return self._getHeldObject().insertCurve(args[0].getName(),*args[1:])
else:
return self._getHeldObject().insertCurve(args[0]._getHeldObject(),*args[1:])
Expand Down

0 comments on commit 8c10294

Please sign in to comment.