From 18b6d25f7e4f0943b3592f3bb4f6ca5ed9c285d3 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Fri, 19 Jun 2015 22:28:06 +0200 Subject: Add support for Python algorithm plugins --- python/astra/plugin.py | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 python/astra/plugin.py (limited to 'python/astra/plugin.py') 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 . +# +#----------------------------------------------------------------------- + +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 -- cgit v1.2.3 From 11af4b554df9a8a5c31d9dcbc1ea849b32394ba3 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Wed, 24 Jun 2015 18:36:03 +0200 Subject: Better way of passing options to Python plugin using inspect --- python/astra/plugin.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index ccdb2cb..891f6c9 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -26,22 +26,20 @@ from . import plugin_c as p from . import log +import inspect 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 = {} + args, varargs, varkw, defaults = inspect.getargspec(self.initialize) + nopt = len(defaults) + if nopt>0: + req = args[2:-nopt] + opt = args[-nopt:] + else: + req = args[2:] + opt = [] try: optDict = cfg['options'] @@ -60,7 +58,9 @@ class base(object): if not cfgKeys.issubset(reqKeys | optKeys): log.warn(self.__class__.__name__ + ": unused configuration option: " + str(list(cfgKeys.difference(reqKeys | optKeys)))) - self.initialize(cfg) + args = [optDict[k] for k in req] + kwargs = dict((k,optDict[k]) for k in opt if k in optDict) + self.initialize(cfg, *args, **kwargs) except Exception as e: log.error(str(e)) raise -- cgit v1.2.3 From edae78481cf0e9cbffe335de1e541821758c5da1 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Wed, 24 Jun 2015 21:36:04 +0200 Subject: Log error when running Python plugin algorithm --- python/astra/plugin.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index 891f6c9..bbbc450 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -65,6 +65,13 @@ class base(object): log.error(str(e)) raise + def astra_run(self, its): + try: + self.run(its) + except Exception as e: + log.error(str(e)) + raise + def register(name, className): """Register plugin with ASTRA. -- cgit v1.2.3 From 58af62f543bbb0e66247a37dae36698d9fa5d338 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Thu, 9 Jul 2015 23:13:44 +0200 Subject: Allow plugins without keywords --- python/astra/plugin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index bbbc450..be5c155 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -33,7 +33,10 @@ class base(object): def astra_init(self, cfg): try: args, varargs, varkw, defaults = inspect.getargspec(self.initialize) - nopt = len(defaults) + if not defaults is None: + nopt = len(defaults) + else: + nopt = 0 if nopt>0: req = args[2:-nopt] opt = args[-nopt:] -- cgit v1.2.3 From c4d227e4d8fd8809fb3c3bded5540cc1e82746ef Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Fri, 10 Jul 2015 00:23:18 +0200 Subject: Show more useful information when a plugin raises an exception --- python/astra/plugin.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index be5c155..f8fc3bd 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -27,6 +27,7 @@ from . import plugin_c as p from . import log import inspect +import traceback class base(object): @@ -64,15 +65,15 @@ class base(object): args = [optDict[k] for k in req] kwargs = dict((k,optDict[k]) for k in opt if k in optDict) self.initialize(cfg, *args, **kwargs) - except Exception as e: - log.error(str(e)) + except Exception: + log.error(traceback.format_exc().replace("%","%%")) raise def astra_run(self, its): try: self.run(its) - except Exception as e: - log.error(str(e)) + except Exception: + log.error(traceback.format_exc().replace("%","%%")) raise def register(name, className): -- cgit v1.2.3 From d91b51f6d58003de84a9d6dd8189fceba0e81a5a Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Mon, 20 Jul 2015 14:07:21 +0200 Subject: Allow registering plugins without explicit name, and fix exception handling when running in Matlab --- python/astra/plugin.py | 71 +++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index f8fc3bd..4b32e6e 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -32,60 +32,47 @@ import traceback class base(object): def astra_init(self, cfg): - try: - args, varargs, varkw, defaults = inspect.getargspec(self.initialize) - if not defaults is None: - nopt = len(defaults) - else: - nopt = 0 - if nopt>0: - req = args[2:-nopt] - opt = args[-nopt:] - else: - req = args[2:] - opt = [] + args, varargs, varkw, defaults = inspect.getargspec(self.initialize) + if not defaults is None: + nopt = len(defaults) + else: + nopt = 0 + if nopt>0: + req = args[2:-nopt] + opt = args[-nopt:] + else: + req = args[2:] + opt = [] - try: - optDict = cfg['options'] - except KeyError: - optDict = {} + try: + optDict = cfg['options'] + except KeyError: + optDict = {} - cfgKeys = set(optDict.keys()) - reqKeys = set(req) - optKeys = set(opt) + 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 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)))) + if not cfgKeys.issubset(reqKeys | optKeys): + log.warn(self.__class__.__name__ + ": unused configuration option: " + str(list(cfgKeys.difference(reqKeys | optKeys)))) - args = [optDict[k] for k in req] - kwargs = dict((k,optDict[k]) for k in opt if k in optDict) - self.initialize(cfg, *args, **kwargs) - except Exception: - log.error(traceback.format_exc().replace("%","%%")) - raise + args = [optDict[k] for k in req] + kwargs = dict((k,optDict[k]) for k in opt if k in optDict) + self.initialize(cfg, *args, **kwargs) - def astra_run(self, its): - try: - self.run(its) - except Exception: - log.error(traceback.format_exc().replace("%","%%")) - raise - -def register(name, className): +def register(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) + p.register(className) def get_registered(): """Get dictionary of registered plugins. -- cgit v1.2.3 From e509cd013f691acded3dc0d87732ba5257cb0ae4 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Mon, 20 Jul 2015 15:14:50 +0200 Subject: Add ReconstructionAlgorithm2D/3D classes for plugins (matching C++ classes) --- python/astra/plugin.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index 4b32e6e..11cc5cc 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -26,6 +26,10 @@ from . import plugin_c as p from . import log +from . import data2d +from . import data2d_c +from . import data3d +from . import projector import inspect import traceback @@ -65,6 +69,29 @@ class base(object): kwargs = dict((k,optDict[k]) for k in opt if k in optDict) self.initialize(cfg, *args, **kwargs) +class ReconstructionAlgorithm2D(base): + + def astra_init(self, cfg): + self.pid = cfg['ProjectorId'] + self.s = data2d.get_shared(cfg['ProjectionDataId']) + self.v = data2d.get_shared(cfg['ReconstructionDataId']) + self.vg = projector.volume_geometry(self.pid) + self.pg = projector.projection_geometry(self.pid) + if not data2d_c.check_compatible(cfg['ProjectionDataId'], self.pid): + raise ValueError("Projection data and projector not compatible") + if not data2d_c.check_compatible(cfg['ReconstructionDataId'], self.pid): + raise ValueError("Reconstruction data and projector not compatible") + super(ReconstructionAlgorithm2D,self).astra_init(cfg) + +class ReconstructionAlgorithm3D(base): + + def astra_init(self, cfg): + self.s = data3d.get_shared(cfg['ProjectionDataId']) + self.v = data3d.get_shared(cfg['ReconstructionDataId']) + self.vg = data3d.get_geometry(cfg['ReconstructionDataId']) + self.pg = data3d.get_geometry(cfg['ProjectionDataId']) + super(ReconstructionAlgorithm3D,self).astra_init(cfg) + def register(className): """Register plugin with ASTRA. -- cgit v1.2.3 From 14ce794a1654f82fe8ac414e56e842242bece729 Mon Sep 17 00:00:00 2001 From: "Daniel M. Pelt" Date: Tue, 21 Jul 2015 13:51:50 +0200 Subject: Also use ProjectorId in ReconstructionAlgorithm3D plugin base --- python/astra/plugin.py | 1 + 1 file changed, 1 insertion(+) (limited to 'python/astra/plugin.py') diff --git a/python/astra/plugin.py b/python/astra/plugin.py index 11cc5cc..3e3528d 100644 --- a/python/astra/plugin.py +++ b/python/astra/plugin.py @@ -86,6 +86,7 @@ class ReconstructionAlgorithm2D(base): class ReconstructionAlgorithm3D(base): def astra_init(self, cfg): + self.pid = cfg['ProjectorId'] self.s = data3d.get_shared(cfg['ProjectionDataId']) self.v = data3d.get_shared(cfg['ReconstructionDataId']) self.vg = data3d.get_geometry(cfg['ReconstructionDataId']) -- cgit v1.2.3