From 0d06afc38d7a8443a079d25d72ec4b4b15353974 Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Tue, 3 Jul 2018 14:32:23 +0200 Subject: Allow access to python plugin object via algorithm interface --- python/astra/algorithm.py | 11 +++++++++++ python/astra/algorithm_c.pyx | 17 +++++++++++++++++ python/astra/src/PythonPluginAlgorithm.cpp | 8 ++++++++ python/astra/src/PythonPluginAlgorithm.h | 3 +++ python/builder.py | 2 +- 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/python/astra/algorithm.py b/python/astra/algorithm.py index 33cca85..8862af3 100644 --- a/python/astra/algorithm.py +++ b/python/astra/algorithm.py @@ -66,6 +66,17 @@ def delete(ids): """ return a.delete(ids) +def get_plugin_object(i): + """Return the Python object instance of a plugin algorithm. + + :param i: ID of object corresponding to a plugin algorithm. + :type i: :class:`int` + :returns: The Python object instance of the plugin algorithm. + + """ + return a.get_plugin_object(i) + + def clear(): """Clear all matrix objects.""" return a.clear() diff --git a/python/astra/algorithm_c.pyx b/python/astra/algorithm_c.pyx index a190ee1..50f6052 100644 --- a/python/astra/algorithm_c.pyx +++ b/python/astra/algorithm_c.pyx @@ -47,6 +47,13 @@ cdef extern from *: CReconstructionAlgorithm2D * dynamic_cast_recAlg2D "dynamic_cast" (CAlgorithm * ) CReconstructionAlgorithm3D * dynamic_cast_recAlg3D "dynamic_cast" (CAlgorithm * ) +cdef extern from "src/PythonPluginAlgorithm.h" namespace "astra": + cdef cppclass CPluginAlgorithm: + object getInstance() + +cdef extern from *: + CPluginAlgorithm * dynamic_cast_PluginAlg "dynamic_cast" (CAlgorithm * ) + def create(config): cdef Config * cfg = utils.dictToConfig(six.b('Algorithm'), config) @@ -104,6 +111,16 @@ def delete(ids): manAlg.remove(ids) +def get_plugin_object(algorithm_id): + cdef CAlgorithm *alg + cdef CPluginAlgorithm *pluginAlg + alg = getAlg(algorithm_id) + pluginAlg = dynamic_cast_PluginAlg(alg) + if not pluginAlg: + raise Exception("Not a plugin algorithm") + return pluginAlg.getInstance() + + def clear(): manAlg.clear() diff --git a/python/astra/src/PythonPluginAlgorithm.cpp b/python/astra/src/PythonPluginAlgorithm.cpp index d461f06..3c892ff 100644 --- a/python/astra/src/PythonPluginAlgorithm.cpp +++ b/python/astra/src/PythonPluginAlgorithm.cpp @@ -124,6 +124,14 @@ void CPluginAlgorithm::run(int _iNrIterations){ PyGILState_Release(state); } +PyObject *CPluginAlgorithm::getInstance() const { + if (instance) + Py_INCREF(instance); + return instance; +} + + + CPythonPluginAlgorithmFactory::CPythonPluginAlgorithmFactory(){ if(!Py_IsInitialized()){ Py_Initialize(); diff --git a/python/astra/src/PythonPluginAlgorithm.h b/python/astra/src/PythonPluginAlgorithm.h index 947e30d..b7072d6 100644 --- a/python/astra/src/PythonPluginAlgorithm.h +++ b/python/astra/src/PythonPluginAlgorithm.h @@ -49,6 +49,9 @@ public: bool initialize(const Config& _cfg); void run(int _iNrIterations); + // Return instance (including INCREF) + PyObject *getInstance() const; + private: PyObject * instance; diff --git a/python/builder.py b/python/builder.py index dd99633..b373366 100644 --- a/python/builder.py +++ b/python/builder.py @@ -66,7 +66,7 @@ ext_modules = cythonize(os.path.join(self_path, 'astra', '*.pyx'), cmdclass = {'build_ext': build_ext} for m in ext_modules: - if m.name == 'astra.plugin_c': + if m.name in ('astra.plugin_c', 'astra.algorithm_c'): m.sources.append(os.path.join(self_path, 'astra', 'src', 'PythonPluginAlgorithm.cpp')) -- cgit v1.2.3