Skip to content

Commit

Permalink
Allow unnamed workspaces through the Python simpleapi functions
Browse files Browse the repository at this point in the history
Refs #11960
  • Loading branch information
martyngigg committed Jul 28, 2015
1 parent 69189bd commit 6095196
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
20 changes: 15 additions & 5 deletions Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from mantid.kernel._aliases import *
from mantid.api._aliases import *

import sys

#------------------------ Specialized function calls --------------------------
# List of specialized algorithms
__SPECIALIZED_FUNCTIONS__ = ["Load", "CutMD"]
Expand Down Expand Up @@ -681,14 +683,22 @@ def set_properties(alg_object, *args, **kwargs):
value = kwargs[key]
if value is None:
continue
# The correct parent/child relationship is not quite set up yet: #5157
# ChildAlgorithms in Python are marked as children but their output is in the
# ADS meaning we cannot just set DataItem properties by value. At the moment
# they are just set with strings
if isinstance(value, _kernel.DataItem):
# This is unpleasant. A WorkspaceGroup object is currently required to be in the ADS
# so we have to set it by string value
# Also, any DataItem that 'could' be in the ADS needs to have its string value set
# as well otherwise it can't be added to the ADS at the end as the required name
# will be empty
if isinstance(value, _api.WorkspaceGroup):
alg_object.setPropertyValue(key, value.name())
else:
# Use the actual object type first
alg_object.setProperty(key, value)
# Only try a string if its a dataobject and the string is not empty as
# the WorkspaceProperty will throw and clear the pointer!
if isinstance(value, _kernel.DataItem):
name = value.name()
if name:
alg_object.setPropertyValue(key, name)

def _create_algorithm_function(algorithm, version, _algm_object):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import numpy
import unittest
from mantid.api import (AlgorithmFactory, AlgorithmProxy, IAlgorithm, IEventWorkspace, ITableWorkspace,
PythonAlgorithm, MatrixWorkspace, mtd, WorkspaceGroup)
from mantid.api import (AlgorithmFactory, AlgorithmManager, AlgorithmProxy, AnalysisDataService,
IAlgorithm, IEventWorkspace, ITableWorkspace, PropertyMode,
PythonAlgorithm, MatrixWorkspace, MatrixWorkspaceProperty, mtd,
WorkspaceFactory, WorkspaceGroup)
from mantid.kernel import Direction
import mantid.simpleapi as simpleapi
import numpy
from testhelpers import WorkspaceCreationHelper

#======================================================================================================================

Expand Down Expand Up @@ -59,6 +63,33 @@ def test_function_call_executes_when_algorithm_has_only_inout_workspace_props(se
wavelength = simpleapi.CreateWorkspace(data,data,NSpec=3,UnitX='Wavelength')
simpleapi.MaskDetectors(wavelength,WorkspaceIndexList=[1,2])

def test_function_call_accepts_workspace_not_in_ADS_for_top_level_call(self):
test_ws = WorkspaceCreationHelper.create2DWorkspaceWithFullInstrument(1, 10)
simpleapitest_rebinned = simpleapi.Rebin(test_ws, Params=[0.0,2.0,10.0])
self.assertTrue(isinstance(simpleapitest_rebinned, MatrixWorkspace))
self.assertEquals("simpleapitest_rebinned", simpleapitest_rebinned.name())
AnalysisDataService.remove("simpleapitest_rebinned")

def test_function_call_accepts_workspace_not_in_ADS_for_call_as_child(self):
class FooAlgorithm(PythonAlgorithm):
def PyInit(self):
pass
def PyExec(self):
test_ws = WorkspaceCreationHelper.create2DWorkspaceWithFullInstrument(1, 10)
simpleapitest_rebinned2 = simpleapi.Rebin(test_ws, Params=[0.0,2.0,10.0])
#end
AlgorithmFactory.subscribe(FooAlgorithm)
# temporarily attach it to simpleapi module
name="FooAlgorithm"
algm_object = AlgorithmManager.createUnmanaged(name, 1)
algm_object.initialize()
simpleapi._create_algorithm_function(name, 1, algm_object) # Create the wrapper
# execute
simpleapi.FooAlgorithm()
self.assertTrue("simpleapitest_rebinned2" in AnalysisDataService)
AnalysisDataService.remove("simpleapitest_rebinned2")


def test_function_accepts_EnableLogging_keyword(self):
# The test here is that the algorithm runs without falling over about the EnableLogging keyword being a property
wsname = 'test_function_call_executes_correct_algorithm_when_passed_correct_args'
Expand Down Expand Up @@ -218,8 +249,6 @@ def PyExec(self):

def test_optional_workspaces_are_ignored_if_not_present_in_output_even_if_given_as_input(self):
# Test algorithm
from mantid.api import AlgorithmManager,PropertyMode,PythonAlgorithm,MatrixWorkspaceProperty,WorkspaceFactory
from mantid.kernel import Direction
class OptionalWorkspace(PythonAlgorithm):
def PyInit(self):
self.declareProperty(MatrixWorkspaceProperty("RequiredWorkspace", "", Direction.Output))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,8 @@ def test_SimpleAlgorithm_Accepts_Group_Handle(self):
group = mtd['group']
self.assertEquals(group.getName(), "group")
self.assertEquals(type(group), WorkspaceGroup)
try:
w = Scale(group, 1.5)
mtd.remove(str(w))
except Exception, exc:
self.fail("Algorithm raised an exception with input as WorkspaceGroup: '" + str(exc) + "'")
w = Scale(group, 1.5)
mtd.remove(str(w))
mtd.remove(str(group))

def test_complex_binary_operations_with_group_do_not_leave_temporary_workspaces_in_ADS(self):
Expand Down

0 comments on commit 6095196

Please sign in to comment.