def test_get_registered_algs_returns_dictionary_of_known_algorithms(self): all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue( len(all_algs) > 0 ) self.assertTrue( 'ConvertUnits' in all_algs ) # 3 versions of LoadRaw self.assertEquals( len(all_algs['LoadRaw']), 3 ) self.assertEquals( all_algs['LoadRaw'], [1,2,3] )
def test_python_algorithms_are_loaded_recursively(self): """ Test needs improving when the old API goes to just check that everything loads okay """ all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue('SNSPowderReduction' in all_algs) self.assertTrue('Squares' in all_algs)
def test_get_registered_algs_returns_dictionary_of_known_algorithms(self): all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue(len(all_algs) > 0) self.assertTrue('ConvertUnits' in all_algs) # 3 versions of LoadRaw self.assertEquals(len(all_algs['LoadRaw']), 3) self.assertEquals(all_algs['LoadRaw'], [1, 2, 3])
def _mockup(plugins): """ Creates fake, error-raising functions for all loaded algorithms plus any plugins given. The function name for the Python algorithms are taken from the filename so this mechanism requires the algorithm name to match the filename. This mechanism solves the "chicken-and-egg" problem with Python algorithms trying to use other Python algorithms through the simple API functions. The issue occurs when a python algorithm tries to import the simple API function of another Python algorithm that has not been loaded yet, usually when it is further along in the alphabet. The first algorithm stops with an import error as that function is not yet known. By having a pre-loading step all of the necessary functions on this module can be created and after the plugins are loaded the correct function definitions can overwrite the "fake" ones. :param plugins: A list of modules that have been loaded """ #-------------------------------------------------------------------------------------------------------- def create_fake_function(name): """Create fake functions for the given name """ #------------------------------------------------------------------------------------------------ def fake_function(*args, **kwargs): raise RuntimeError("Mantid import error. The mock simple API functions have not been replaced!" + " This is an error in the core setup logic of the mantid module, please contact the development team.") #------------------------------------------------------------------------------------------------ if "." in name: name = name.rstrip('.py') if specialization_exists(name): return fake_function.__name__ = name f = fake_function.func_code c = f.__new__(f.__class__, f.co_argcount, f.co_nlocals, f.co_stacksize, f.co_flags, f.co_code, f.co_consts, f.co_names,\ ("", ""), f.co_filename, f.co_name, f.co_firstlineno, f.co_lnotab, f.co_freevars) # Replace the code object of the wrapper function fake_function.func_code = c globals()[name] = fake_function #-------------------------------------------------------- def create_fake_functions(alg_names): """Create fake functions for all of the listed names """ for alg_name in alg_names: create_fake_function(alg_name) #------------------------------------- # Start with the loaded C++ algorithms from mantid.api import AlgorithmFactory import os cppalgs = AlgorithmFactory.getRegisteredAlgorithms(True) create_fake_functions(cppalgs.keys()) # Now the plugins for plugin in plugins: name = os.path.basename(plugin) name = os.path.splitext(name)[0] create_fake_function(name)
def html_collect_pages(app): """ Write out unversioned algorithm pages that redirect to the highest version of the algorithm """ from mantid.api import AlgorithmFactory template = REDIRECT_TEMPLATE all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) for name, versions in all_algs.iteritems(): redirect_pagename = "algorithms/%s" % name versions.sort() highest_version = versions[-1] target = "%s-v%d.html" % (name, highest_version) context = {"name" : name, "target" : target} yield (redirect_pagename, context, template)
def test_loading_python_algorithm_increases_registered_algs_by_one(self): loaded = plugins.load(self._testdir) self.assertTrue(len(loaded) > 0) expected_name = 'TestPyAlg' # Has the name appear in the module dictionary self.assertTrue(expected_name in sys.modules) # Do we have the registered algorithm algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue(expected_name in algs) # Can it be created? try: test_alg = AlgorithmManager.createUnmanaged(expected_name) self.assertEquals(expected_name, test_alg.name()) self.assertEquals(1, test_alg.version()) except RuntimeError as exc: self.fail("Failed to create plugin algorithm from the manager: '%s' " %s)
def html_collect_pages(dummy_app): """ Write out unversioned algorithm pages that redirect to the highest version of the algorithm """ from mantid.api import AlgorithmFactory template = REDIRECT_TEMPLATE all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) for name, versions in all_algs.iteritems(): redirect_pagename = "algorithms/%s" % name versions.sort() highest_version = versions[-1] target = "%s-v%d.html" % (name, highest_version) context = {"name": name, "target": target} yield (redirect_pagename, context, template)
def _translate(): """ Loop through the algorithms and register a function call for each of them :returns: a list of new function calls """ from mantid.api import AlgorithmFactory, AlgorithmManager new_functions = [] # Names of new functions added to the global namespace new_methods = { } # Method names mapped to their algorithm names. Used to detect multiple copies of same method name # on different algorithms, which is an error algs = AlgorithmFactory.getRegisteredAlgorithms(True) algorithm_mgr = AlgorithmManager for name, versions in algs.iteritems(): if specialization_exists(name): continue try: # Create the algorithm object algm_object = algorithm_mgr.createUnmanaged(name, max(versions)) algm_object.initialize() except Exception: continue algorithm_wrapper = _create_algorithm_function(name, max(versions), algm_object) method_name = algm_object.workspaceMethodName() if len(method_name) > 0: if method_name in new_methods: other_alg = new_methods[method_name] raise RuntimeError( "simpleapi: Trying to attach '%s' as method to point to '%s' algorithm but " "it has already been attached to point to the '%s' algorithm.\n" "Does one inherit from the other? " "Please check and update one of the algorithms accordingly." % (method_name, algm_object.name(), other_alg)) _attach_algorithm_func_as_method(method_name, algorithm_wrapper, algm_object) new_methods[method_name] = algm_object.name() # Dialog variant _create_algorithm_dialog(name, max(versions), algm_object) new_functions.append(name) return new_functions
def _translate(): """ Loop through the algorithms and register a function call for each of them :returns: a list of new function calls """ from mantid.api import AlgorithmFactory, AlgorithmManager new_functions = [] # Names of new functions added to the global namespace new_methods = {} # Method names mapped to their algorithm names. Used to detect multiple copies of same method name # on different algorithms, which is an error algs = AlgorithmFactory.getRegisteredAlgorithms(True) algorithm_mgr = AlgorithmManager for name, versions in iteritems(algs): if specialization_exists(name): continue try: # Create the algorithm object algm_object = algorithm_mgr.createUnmanaged(name, max(versions)) algm_object.initialize() except Exception as exc: logger.warning("Error initializing {0} on registration: '{1}'".format(name, str(exc))) continue algorithm_wrapper = _create_algorithm_function(name, max(versions), algm_object) method_name = algm_object.workspaceMethodName() if len(method_name) > 0: if method_name in new_methods: other_alg = new_methods[method_name] raise RuntimeError( "simpleapi: Trying to attach '%s' as method to point to '%s' algorithm but " "it has already been attached to point to the '%s' algorithm.\n" "Does one inherit from the other? " "Please check and update one of the algorithms accordingly." % (method_name, algm_object.name(), other_alg) ) _attach_algorithm_func_as_method(method_name, algorithm_wrapper, algm_object) new_methods[method_name] = algm_object.name() # Dialog variant _create_algorithm_dialog(name, max(versions), algm_object) new_functions.append(name) return new_functions
def test_loading_python_algorithm_increases_registered_algs_by_one(self): loaded = plugins.load(self._testdir) self.assertTrue(len(loaded) > 0) expected_name = 'TestPyAlg' # Has the name appear in the module dictionary self.assertTrue(expected_name in sys.modules) # Do we have the registered algorithm algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue(expected_name in algs) # Can it be created? try: test_alg = AlgorithmManager.createUnmanaged(expected_name) self.assertEquals(expected_name, test_alg.name()) self.assertEquals(1, test_alg.version()) except RuntimeError, exc: self.fail( "Failed to create plugin algorithm from the manager: '%s' " % s)
def remote_resources_available(self): """ Returns whether or not the application is cluster-enabled. The Remote algorithms have to be available and the cluster submission property has to be ON. """ # Check whether Mantid is available try: from mantid.kernel import ConfigService from mantid.api import AlgorithmFactory if "SubmitRemoteJob" in AlgorithmFactory.getRegisteredAlgorithms(True): config = ConfigService.Instance() if config.hasProperty("cluster.submission") \ and config.getString("cluster.submission").lower()=='on': return True return False except: return False
#pylint: disable=invalid-name """ Main window for reduction UIs """ import sys, os import traceback # Check whether Mantid is available IS_IN_MANTIDPLOT = False try: import mantidplot IS_IN_MANTIDPLOT = True from mantid.kernel import ConfigService from mantid.api import AlgorithmFactory CLUSTER_ENABLED = "SubmitRemoteJob" in AlgorithmFactory.getRegisteredAlgorithms(True) except: import sip sip.setapi('QString',2) sip.setapi('QVariant',2) from PyQt4 import QtGui, QtCore REDUCTION_WARNING = False WARNING_MESSAGE = "" if IS_IN_MANTIDPLOT: try: import reduction if os.path.splitext(os.path.basename(reduction.__file__))[0] == "reduction": REDUCTION_WARNING = True home_dir = os.path.expanduser('~')
def test_module_dict_seems_to_be_correct_size(self): # Check that the module has at least the same number # of attributes as unique algorithms module_dict = dir(simpleapi) all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertGreater(len(module_dict), len(all_algs))
def test_module_dict_seems_to_be_correct_size(self): # Check that the module has at least the same number # of attributes as unique algorithms module_dict = dir(simpleapi) all_algs = AlgorithmFactory.getRegisteredAlgorithms(True) self.assertTrue( len(module_dict) > len(all_algs) )
'WorkspaceGroup'] def outputError(alg, algVersion, description, notes=""): print("%s, %i, %s, %s" % (alg, algVersion, description, notes)) rstdir = r"C:\Mantid\Code\Mantid\docs\source\algorithms" conceptsPattern = {} for concept in concepts: #conceptsPattern[concept] = re.compile(concept, re.IGNORECASE) conceptsPattern[concept] = re.compile(r'`([\w\s]+?)\s*<http:\/\/www\.mantidproject\.org\/(' + concept + r')\s*>`_', re.IGNORECASE) #print r'`([\w\s]+?)\s*<http:\/\/www\.mantidproject\.org\/' + concept + r'\s*>`_' algs = AlgorithmFactory.getRegisteredAlgorithms(True) for alg in algs: algVersion = algs[alg][0] fileFound = False filename = os.path.join(rstdir,alg + "-v" + str(algVersion) + ".rst") if os.path.exists(filename): algText = "" with open (filename, "r") as algRst: fileFound = True algText = algRst.read() for concept in concepts: regex = conceptsPattern[concept] while regex.search(algText) is not None: outputError(alg, algVersion, "found", concept) algText = regex.sub(r":ref:`\1 <\2>`",algText) with open (filename, "w") as algRst:
"""Wraps all Mantid algorithms so they use mslice's wrapped workspaces""" from mantid.simpleapi import * # noqa: F401 from mslice.util.mantid.algorithm_wrapper import wrap_algorithm from mantid.api import AlgorithmFactory algorithms = AlgorithmFactory.getRegisteredAlgorithms(False) for algorithm in algorithms.keys(): globals()[algorithm] = wrap_algorithm(globals()[algorithm])