Skip to content

Commit

Permalink
Deprecate registerAlgorithm in favour of AlgorithmFactory.subscribe
Browse files Browse the repository at this point in the history
This is more in line with how the new functions work and how the C++
side works too. Refs #970
  • Loading branch information
martyngigg committed Apr 18, 2013
1 parent 5ec96b8 commit ee60134
Show file tree
Hide file tree
Showing 35 changed files with 84 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
// include of Python.h which it ensures is done correctly
#include <frameobject.h>

using Mantid::API::AlgorithmFactoryImpl;
using Mantid::API::AlgorithmFactory;
using namespace Mantid::API;
using namespace boost::python;
using Mantid::Kernel::AbstractInstantiator;
using Mantid::PythonInterface::PythonAlgorithm;
using Mantid::PythonInterface::PythonObjectInstantiator;

namespace
{
Expand Down Expand Up @@ -52,43 +54,20 @@ namespace
PyDict_SetItem(registered, name, versions);
}
}

return registered;
}
///@endcond
}

void export_AlgorithmFactory()
{

class_<AlgorithmFactoryImpl,boost::noncopyable>("AlgorithmFactoryImpl", no_init)
.def("getRegisteredAlgorithms", &getRegisteredAlgorithms, "Returns a Python dictionary of currently registered algorithms")
.def("Instance", &AlgorithmFactory::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the AlgorithmFactory singleton")
.staticmethod("Instance")
;

}

//--------------------------------------------- Python algorithm registration ------------------------------------------------
//--------------------------------------------- Python algorithm subscription ------------------------------------------------

namespace
{
using Mantid::PythonInterface::PythonObjectInstantiator;
using Mantid::Kernel::AbstractInstantiator;
using Mantid::PythonInterface::PythonAlgorithm;
using Mantid::API::Algorithm;
using Mantid::API::IAlgorithm_sptr;

/// Python algorithm registration mutex in anonymous namespace (aka static)
// Python algorithm registration mutex in anonymous namespace (aka static)
Poco::Mutex PYALG_REGISTER_MUTEX;

/**
* A free function to register an algorithm from Python
* A free function to subscribe a Python algorithm into the factory
* @param obj :: A Python object that should either be a class type derived from PythonAlgorithm
* or an instance of a class type derived from PythonAlgorithm
*/
void registerAlgorithm(boost::python::object obj)
void subscribe(AlgorithmFactoryImpl & self, const boost::python::object & obj)
{
Poco::ScopedLock<Poco::Mutex> lock(PYALG_REGISTER_MUTEX);

Expand All @@ -109,10 +88,39 @@ namespace
}
boost::python::object classType(handle<>(borrowed(classObject)));
// Takes ownership of instantiator and replaces any existing algorithm
AlgorithmFactory::Instance().subscribe(new PythonObjectInstantiator<Algorithm>(classType), AlgorithmFactoryImpl::OverwriteCurrent);
self.subscribe(new PythonObjectInstantiator<Algorithm>(classType), AlgorithmFactoryImpl::OverwriteCurrent);
}

///@endcond
}

void export_AlgorithmFactory()
{

class_<AlgorithmFactoryImpl,boost::noncopyable>("AlgorithmFactoryImpl", no_init)
.def("getRegisteredAlgorithms", &getRegisteredAlgorithms, "Returns a Python dictionary of currently registered algorithms")
.def("subscribe", &subscribe, "Register a Python class derived from PythonAlgorithm into the factory")
.def("Instance", &AlgorithmFactory::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the AlgorithmFactory singleton")
.staticmethod("Instance")
;

}

namespace
{
// ------ Deprecated ------------
/**
* A free function to register an algorithm from Python
* @param obj :: A Python object that should either be a class type derived from PythonAlgorithm
* or an instance of a class type derived from PythonAlgorithm
*/
void registerAlgorithm(const boost::python::object & obj)
{
PyErr_WarnEx(PyExc_DeprecationWarning, "registerAlgorithm is deprecated. Replace with AlgorithmFactory.subscribe", 1);
subscribe(AlgorithmFactory::Instance(), obj);
}
}

void export_RegisterAlgorithm()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def contains_newapi_algorithm(filename):
if 'registerPyAlgorithm' in line:
alg_found = False
break
if 'registerAlgorithm' in line:
if 'AlgorithmFactory.subscribe' in line or 'registerAlgorithm': # registerAlgorithm deprecated
alg_found = True
break
file.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,5 @@ def _calibData(self, sam_ws, mon_ws):
OutputWorkspace=sam_ws)

# Register algorithm with Mantid.
registerAlgorithm(BASISReduction)
AlgorithmFactory.subscribe(BASISReduction)

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*WIKI*"""

from mantid.api import PythonAlgorithm, registerAlgorithm, ITableWorkspaceProperty, WorkspaceFactory
from mantid.api import PythonAlgorithm, AlgorithmFactory, ITableWorkspaceProperty, WorkspaceFactory
from mantid.kernel import Direction

# Create an empty table workspace to be populated by a python script.
Expand All @@ -21,5 +21,5 @@ def PyExec(self):
self.setProperty("OutputWorkspace", tableWS)

# Register algorithm with Mantid
registerAlgorithm(CreateEmptyTableWorkspace)
AlgorithmFactory.subscribe(CreateEmptyTableWorkspace)

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
This algorithm is designed to work with other algorithms to do Le Bail fit. The introduction can be found in the wiki page of [[LeBailFit]].
*WIKI*"""
from mantid.api import PythonAlgorithm, registerAlgorithm, ITableWorkspaceProperty, WorkspaceFactory, FileProperty, FileAction
from mantid.api import PythonAlgorithm, AlgorithmFactory, ITableWorkspaceProperty, WorkspaceFactory, FileProperty, FileAction
from mantid.kernel import Direction, StringListValidator, FloatBoundedValidator

import mantid.simpleapi as api
Expand Down Expand Up @@ -245,5 +245,5 @@ def createReflectionWorkspace(self, hkldict, tablews):


# Register algorithm with Mantid
registerAlgorithm(CreateLeBailFitInput)
AlgorithmFactory.subscribe(CreateLeBailFitInput)

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Compare two nexus files containing matrix workspaces and output chi squared into a file
*WIKI*"""

from mantid.api import PythonAlgorithm, registerAlgorithm,MatrixWorkspaceProperty,PropertyMode
from mantid.api import PythonAlgorithm, AlgorithmFactory,MatrixWorkspaceProperty,PropertyMode
from mantid.kernel import Direction,IntBoundedValidator,FloatBoundedValidator
import mantid.simpleapi
import mantid
Expand Down Expand Up @@ -88,4 +88,4 @@ def PyExec(self):
if (len(soeName)==0):
mantid.simpleapi.DeleteWorkspace(__soe.getName())

registerAlgorithm(DakotaChiSquared)
AlgorithmFactory.subscribe(DakotaChiSquared)
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def PyExec(self):

#############################################################################################

registerAlgorithm(Squares)
AlgorithmFactory.subscribe(Squares)
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ def PyExec(self):

self.setProperty("OutputWorkspace", output_ws)

registerAlgorithm(FindReflectometryLines())
AlgorithmFactory.subscribe(FindReflectometryLines())
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ def PyExec(self):
mantid.simpleapi.DeleteWorkspace(__w.getName())
return

mantid.api.registerAlgorithm(GenerateGroupingSNSInelastic)
mantid.api.AlgorithmFactory.subscribe(GenerateGroupingSNSInelastic)

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
a copy of the monitor workspace, then fed to GetEi algorithm. The output of this algorithm is identical to that of [[GetEi]].
*WIKI*"""

from mantid.api import PythonAlgorithm, registerAlgorithm,WorkspaceProperty
from mantid.api import PythonAlgorithm, AlgorithmFactory,WorkspaceProperty
from mantid.kernel import Direction,IntBoundedValidator,FloatBoundedValidator
import mantid.simpleapi
import mantid
Expand Down Expand Up @@ -135,4 +135,4 @@ def PyExec(self):
return


registerAlgorithm(GetEiMonDet)
AlgorithmFactory.subscribe(GetEiMonDet)
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,4 @@ def _validate_spectrum_number(self, spectrum_no):
#########################################################################################

# Registration
registerAlgorithm(LoadVesuvio)
AlgorithmFactory.subscribe(LoadVesuvio)
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ def FormatLine(self,number,UDET,offset,select,group):

#############################################################################################

registerAlgorithm(MaskWorkspaceToCalFile())
AlgorithmFactory.subscribe(MaskWorkspaceToCalFile())
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,4 @@ def getNumberOfSplittedWorkspace(self, splitwksp):
return len(wscountdict.keys())

# Register algorthm with Mantid.
registerAlgorithm(SNSPowderReduction)
AlgorithmFactory.subscribe(SNSPowderReduction)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
########################################################################################

from mantid.api import PythonAlgorithm, registerAlgorithm, ITableWorkspaceProperty, WorkspaceFactory, FileProperty, FileAction
from mantid.api import PythonAlgorithm, AlgorithmFactory, ITableWorkspaceProperty, WorkspaceFactory, FileProperty, FileAction
from mantid.kernel import Direction, StringListValidator

_OUTPUTLEVEL = "NOOUTPUT"
Expand Down Expand Up @@ -226,5 +226,5 @@ def parseZscoreFilter(self, zscorefilterstr):


# Register algorithm with Mantid
registerAlgorithm(SelectPowderDiffPeaks)
AlgorithmFactory.subscribe(SelectPowderDiffPeaks)

Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ def PyExec(self):
def GetXValue(self, xs):
return np.linalg.norm(xs)

registerAlgorithm(SortByQVectors)
AlgorithmFactory.subscribe(SortByQVectors)
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ def PyExec(self):

#############################################################################################

registerAlgorithm(Stitch1D())
AlgorithmFactory.subscribe(Stitch1D())
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def PyExec(self):
logger.notice("Input type: %s" % str(type(ws2)))


registerAlgorithm(TestWorkspaceGroupProperty)
AlgorithmFactory.subscribe(TestWorkspaceGroupProperty)
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,4 @@ def parseTableWorkspace(self, tablews):


# ENDCLASS
mantid.api.registerAlgorithm(UpdatePeakParameterTableValue)
mantid.api.AlgorithmFactory.subscribe(UpdatePeakParameterTableValue)
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,4 @@ def _get_binning(self, workspace, wavelength_min, wavelength_max):
qmax = math.pow(10.0, math.log10(qmin)+qstep*n_step)
return qmin, -(math.pow(10.0,qstep)-1.0), qmax

registerAlgorithm(EQSANSAzimuthalAverage1D)
AlgorithmFactory.subscribe(EQSANSAzimuthalAverage1D)
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,4 @@ def _crop_and_compute(wl_min_prop, wl_max_prop, suffix):
TransmissionUtils.apply_transmission(self, workspace, trans_ws)


registerAlgorithm(EQSANSDirectBeamTransmission)
AlgorithmFactory.subscribe(EQSANSDirectBeamTransmission)
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,4 @@ def _normalise_to_monitor(self):
self.setProperty("OutputMessage", "Monitor not available. Data [%s] NOT normalized to monitor" % (input_ws_name))
#############################################################################################

registerAlgorithm(EQSANSNormalise)
AlgorithmFactory.subscribe(EQSANSNormalise)
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,4 @@ def _save_output(self, iq_output, iqxy_output, output_dir, property_manager):

#############################################################################################

registerAlgorithm(HFIRSANSReduction)
AlgorithmFactory.subscribe(HFIRSANSReduction)
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ def PyExec(self):
self.setProperty("OutputWorkspace", output_ws_name)
self.setProperty("OutputMessage", "Normalised by thickness [%g cm]" % thickness)

registerAlgorithm(NormaliseByThickness())
AlgorithmFactory.subscribe(NormaliseByThickness())
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ def PyExec(self):
raise RuntimeError, "ReactorSANSResolution could not find all the run parameters needed to compute the resolution."


registerAlgorithm(ReactorSANSResolution)
AlgorithmFactory.subscribe(ReactorSANSResolution)
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@ def _load_data(filename, output_ws):
self.setProperty("OutputWorkspace", output_ws)
self.setProperty("OutputMessage", output_msg)

registerAlgorithm(SANSAbsoluteScale())
AlgorithmFactory.subscribe(SANSAbsoluteScale())
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ def _get_binning(self, workspace, wavelength_min, wavelength_max):
return qmin, -(math.pow(10.0,qstep)-1.0), qmax
#############################################################################################

registerAlgorithm(SANSAzimuthalAverage1D)
AlgorithmFactory.subscribe(SANSAzimuthalAverage1D)
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,4 @@ def _dark(workspace, dark_current_property):

#############################################################################################

registerAlgorithm(SANSBeamSpreaderTransmission)
AlgorithmFactory.subscribe(SANSBeamSpreaderTransmission)
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ def PyExec(self):

#############################################################################################

registerAlgorithm(SANSDirectBeamTransmission)
AlgorithmFactory.subscribe(SANSDirectBeamTransmission)
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ def _mask_detector_side(self, workspace, facility):

self._mask_pixels(id_side, workspace, facility)

registerAlgorithm(SANSMask())
AlgorithmFactory.subscribe(SANSMask())
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@ def _save_output(self, iq_output, iqxy_output, output_dir, property_manager):

return output_msg
#############################################################################################
registerAlgorithm(SANSReduction)
AlgorithmFactory.subscribe(SANSReduction)
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_python_alg_can_use_other_python_alg_through_simple_api(self):
whole mantid module
"""
src = """
from mantid.api import PythonAlgorithm, registerAlgorithm
from mantid.api import PythonAlgorithm, AlgorithmFactory
import mantid.simpleapi as api
from mantid.simpleapi import *
Expand All @@ -199,7 +199,7 @@ def PyExec(self):
%(execline1)s
%(execline2)s
registerAlgorithm(%(name)s)
AlgorithmFactory.subscribe(%(name)s)
"""
name1 = "SimpleAPIPythonAlgorithm1"
name2 = "SimpleAPIPythonAlgorithm2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import testhelpers
from mantid.api import AlgorithmManager
from mantid.api import (IAlgorithm, Algorithm, AlgorithmProxy, PythonAlgorithm,
registerAlgorithm)
AlgorithmFactory)
import sys

class IsAnAlgorithm(PythonAlgorithm):
Expand Down Expand Up @@ -42,11 +42,11 @@ def test_pyalg_isinstance_of_Algorithm(self):
self.assertTrue(isinstance(alg, Algorithm))
self.assertTrue(isinstance(alg, IAlgorithm))

def test_algorithm_registration_with_valid_object_succeeds(self):
testhelpers.assertRaisesNothing(self, registerAlgorithm, IsAnAlgorithm)
def test_algorithm_subscription_with_valid_object_succeeds(self):
testhelpers.assertRaisesNothing(self, AlgorithmFactory.subscribe, IsAnAlgorithm)

def test_algorithm_registration_with_invalid_object_throws(self):
self.assertRaises(ValueError, registerAlgorithm, NotAnAlgorithm)
self.assertRaises(ValueError, AlgorithmFactory.subscribe, NotAnAlgorithm)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def PyExec(self):
outputWS = api.Scale(InputWorkspace=inputWS, Factor=2.0)
self.setProperty("OutputWorkspace", outputWS)
registerAlgorithm(PythonAlgorithmChildAlgCallTestAlg)
AlgorithmFactory.subscribe(PythonAlgorithmChildAlgCallTestAlg)
"""


Expand Down

0 comments on commit ee60134

Please sign in to comment.