summaryrefslogtreecommitdiffstats
path: root/python/astra/plugin.py
diff options
context:
space:
mode:
authorDaniel M. Pelt <D.M.Pelt@cwi.nl>2015-06-19 22:28:06 +0200
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2015-07-23 11:57:15 +0200
commit18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3 (patch)
tree8919012d1c610eaf6b2e8c157082a85fb71137a4 /python/astra/plugin.py
parent9e077994b382b2df63e4b79dd2afebc50366d419 (diff)
downloadastra-18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3.tar.gz
astra-18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3.tar.bz2
astra-18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3.tar.xz
astra-18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3.zip
Add support for Python algorithm plugins
Diffstat (limited to 'python/astra/plugin.py')
-rw-r--r--python/astra/plugin.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/python/astra/plugin.py b/python/astra/plugin.py
new file mode 100644
index 0000000..ccdb2cb
--- /dev/null
+++ b/python/astra/plugin.py
@@ -0,0 +1,95 @@
+#-----------------------------------------------------------------------
+#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam
+#
+#Author: Daniel M. Pelt
+#Contact: D.M.Pelt@cwi.nl
+#Website: http://dmpelt.github.io/pyastratoolbox/
+#
+#
+#This file is part of the Python interface to the
+#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox").
+#
+#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify
+#it under the terms of the GNU General Public License as published by
+#the Free Software Foundation, either version 3 of the License, or
+#(at your option) any later version.
+#
+#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+#GNU General Public License for more details.
+#
+#You should have received a copy of the GNU General Public License
+#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.
+#
+#-----------------------------------------------------------------------
+
+from . import plugin_c as p
+from . import log
+
+class base(object):
+
+ def astra_init(self, cfg):
+ try:
+ try:
+ req = self.required_options
+ except AttributeError:
+ log.warn("Plugin '" + self.__class__.__name__ + "' does not specify required options")
+ req = {}
+
+ try:
+ opt = self.optional_options
+ except AttributeError:
+ log.warn("Plugin '" + self.__class__.__name__ + "' does not specify optional options")
+ opt = {}
+
+ try:
+ optDict = cfg['options']
+ except KeyError:
+ optDict = {}
+
+ cfgKeys = set(optDict.keys())
+ reqKeys = set(req)
+ optKeys = set(opt)
+
+ if not reqKeys.issubset(cfgKeys):
+ for key in reqKeys.difference(cfgKeys):
+ log.error("Required option '" + key + "' for plugin '" + self.__class__.__name__ + "' not specified")
+ raise ValueError("Missing required options")
+
+ if not cfgKeys.issubset(reqKeys | optKeys):
+ log.warn(self.__class__.__name__ + ": unused configuration option: " + str(list(cfgKeys.difference(reqKeys | optKeys))))
+
+ self.initialize(cfg)
+ except Exception as e:
+ log.error(str(e))
+ raise
+
+def register(name, className):
+ """Register plugin with ASTRA.
+
+ :param name: Plugin name to register
+ :type name: :class:`str`
+ :param className: Class name or class object to register
+ :type className: :class:`str` or :class:`class`
+
+ """
+ p.register(name,className)
+
+def get_registered():
+ """Get dictionary of registered plugins.
+
+ :returns: :class:`dict` -- Registered plugins.
+
+ """
+ return p.get_registered()
+
+def get_help(name):
+ """Get help for registered plugin.
+
+ :param name: Plugin name to get help for
+ :type name: :class:`str`
+ :returns: :class:`str` -- Help string (docstring).
+
+ """
+ return p.get_help(name) \ No newline at end of file