Skip to content

Commit

Permalink
Use managed algorithms again at the top level in Python.
Browse files Browse the repository at this point in the history
This restores the progress reporting to the GUI as anything going through
the AlgorithmManager::create method is automatically tracked.
Refs #9203
  • Loading branch information
martyngigg committed Mar 19, 2014
1 parent eac5434 commit 1aca819
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,23 @@ namespace
PyObject * createAlgorithm(FrameworkManagerImpl & self, const std::string & name, const int version = -1)
{
UNUSED_ARG(self);
IAlgorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(name, version);
IAlgorithm_sptr alg;
if( Mantid::PythonInterface::Environment::isInCallStack("PyExec") )
{
alg = AlgorithmManager::Instance().createUnmanaged(name, version);
alg->setChild(true); // Ensures locking behaves correctly
alg->setLogging(true);
alg->setAlwaysStoreInADS(true);
alg->enableHistoryRecordingForChild(true);
alg->initialize();
}
else
{
// creating through the manager ensures that observers can listen for things like
// progress & cancellation notifications
alg = AlgorithmManager::Instance().create(name, version);
}
alg->setRethrows(true);
alg->initialize();

return converter::shared_ptr_to_python(alg);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest
import testhelpers
from mantid.api import FrameworkManager, FrameworkManagerImpl, IAlgorithm
from mantid.api import FrameworkManager, FrameworkManagerImpl, IAlgorithm, AlgorithmProxy

def _is_initialized_test(testobj, alg, version, expected_child):
def _is_initialized_test(testobj, alg, version, expected_class, expected_child):
testobj.assertTrue(alg.isInitialized())
testobj.assertEquals(expected_child,alg.isChild())
testobj.assertEquals(alg.version(), version)
testobj.assertTrue(isinstance(alg, IAlgorithm))
testobj.assertTrue(isinstance(alg, expected_class))


class FrameworkManagerTest(unittest.TestCase):
Expand All @@ -20,11 +20,13 @@ def test_clear_functions_do_not_throw(self):

def test_create_algorithm_produces_initialized_alorithm(self):
alg = FrameworkManager.createAlgorithm("Rebin")
_is_initialized_test(self, alg, 1, expected_child=False)
_is_initialized_test(self, alg, 1, expected_class=AlgorithmProxy,
expected_child=False)

def test_create_algorithm_with_version_produces_initialized_alorithm(self):
alg = FrameworkManager.createAlgorithm("LoadRaw", 2)
_is_initialized_test(self, alg, 2, expected_child=False)
_is_initialized_test(self, alg, 2, expected_class=AlgorithmProxy,
expected_child=False)

def test_create_algorithm_produces_child_inside_PyExec(self):
# A small test class to have a PyExec method call the
Expand All @@ -35,7 +37,8 @@ def __init__(self, test_object):

def PyExec(self):
alg = FrameworkManager.createAlgorithm("Rebin")
_is_initialized_test(self._test_obj, alg, 1, expected_child=True)
_is_initialized_test(self._test_obj, alg, 1,
expected_class=IAlgorithm, expected_child=True)

top_level = TestAlg(self)
top_level.PyExec()
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/scripts/reduction/reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def execute(self, reducer, inputworkspace=None, outputworkspace=None):
data_file = self._data_file

alg = mantid.api.FrameworkManager.createAlgorithm(algorithm)
if not isinstance(alg, mantid.api.IAlgorithm):
if not isinstance(alg, mantid.api.AlgorithmProxy):
raise RuntimeError, "Reducer expects an Algorithm object from FrameworkManager, found '%s'" % str(type(alg))

propertyOrder = alg.orderedProperties()
Expand Down Expand Up @@ -221,7 +221,7 @@ def execute(self, reducer, inputworkspace=None, outputworkspace=None):
if outputworkspace is None:
outputworkspace = inputworkspace
alg = mantid.FrameworkManager.createAlgorithm(algorithm)
if not isinstance(alg, mantid.api.IAlgorithm):
if not isinstance(alg, mantid.api.AlgorithmProxy):
raise RuntimeError, "Reducer expects an Algorithm object from FrameworkManager, found '%s'" % str(type(alg))

propertyOrder = alg.orderedProperties()
Expand Down

0 comments on commit 1aca819

Please sign in to comment.