Skip to content

Commit

Permalink
Refs #11353 Use specialised CutMD simpleapi implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Jeffery committed Mar 20, 2015
1 parent 28e731b commit 695c8b6
Showing 1 changed file with 57 additions and 18 deletions.
75 changes: 57 additions & 18 deletions Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py
Expand Up @@ -31,7 +31,7 @@

#------------------------ Specialized function calls --------------------------
# List of specialized algorithms
__SPECIALIZED_FUNCTIONS__ = ["Load", "Fit"]
__SPECIALIZED_FUNCTIONS__ = ["Load", "Fit", "CutMD"]
# List of specialized algorithms
__MDCOORD_FUNCTIONS__ = ["PeakIntensityVsRadius", "CentroidPeaksMD","IntegratePeaksMD"]
# The "magic" keyword to enable/disable logging
Expand Down Expand Up @@ -257,22 +257,65 @@ def FitDialog(*args, **kwargs):

#--------------------------------------------------- --------------------------

#This dictionary maps algorithm names to functions that preprocess their inputs
#in the simpleapi. The functions take args and kwargs as regular arguments and
#modify them as required.
def CutMD(*args, **kwargs):
"""
Description TODO
"""
(InputWorkspace,) = _get_mandatory_args('CutMD', ["InputWorkspace"], *args, **kwargs)
# Remove from keywords so it is not set twice
if "InputWorkspace" in kwargs:
del kwargs['InputWorkspace']

# Create and execute
algm = _create_algorithm_object('CutMD')
_set_logging_option(algm, kwargs)

#Split PBins up into P1Bin, P2Bin, etc.
if "PBins" in kwargs:
bins = kwargs["PBins"]
del kwargs["PBins"]
if isinstance(bins, tuple) or isinstance(bins, list):
for bin in range(len(bins)):
kwargs["P{0}Bin".format(bin+1)] = bins[bin]

algm.setProperty('InputWorkspace', InputWorkspace)
# Set all workspace properties before others
for key in kwargs.keys():
if key.startswith('InputWorkspace_'):
algm.setProperty(key, kwargs[key])
del kwargs[key]

lhs = _kernel.funcreturns.lhs_info()
# If the output has not been assigned to anything, i.e. lhs[0] = 0 and kwargs does not have OutputWorkspace
# then raise a more helpful error than what we would get from an algorithm
if lhs[0] == 0 and 'OutputWorkspace' not in kwargs:
raise RuntimeError("Unable to set output workspace name. Please either assign the output of "
"CutMD to a variable or use the OutputWorkspace keyword.")

lhs_args = _get_args_from_lhs(lhs, algm)
final_keywords = _merge_keywords_with_lhs(kwargs, lhs_args)
# Check for any properties that aren't known and warn they will not be used
for key in final_keywords.keys():
if key not in algm:
raise RuntimeError("Unknown property: {0}".format(key))
_set_properties(algm, **final_keywords)
algm.execute()

_algorithm_preprocessors = dict()
return _gather_returns('CutMD', lhs, algm)

def _pp_cutmd(args, kwargs):
if "PBins" in kwargs:
bins = kwargs["PBins"]
del kwargs["PBins"]
if isinstance(bins, tuple) or isinstance(bins, list):
#PBin has been provided, we need to split it out into P1Bin, P2Bin, etc.
for bin in range(len(bins)):
kwargs["P{0}Bin".format(bin+1)] = bins[bin]
# Have a better load signature for autocomplete
_signature = "\bInputWorkspace"
# Getting the code object for Load
_f = CutMD.func_code
# Creating a new code object nearly identical, but with the two variable names replaced
# by the property list.
_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,\
(_signature, "kwargs"), _f.co_filename, _f.co_name, _f.co_firstlineno, _f.co_lnotab, _f.co_freevars)

_algorithm_preprocessors["CutMD"] = _pp_cutmd
# Replace the code object of the wrapper function
CutMD.func_code = _c

#--------------------------------------------------- --------------------------

def _get_function_spec(func):
"""Get the python function signature for the given function object
Expand Down Expand Up @@ -568,10 +611,6 @@ def algorithm_wrapper(*args, **kwargs):
the proper version of the algorithm without failing.
"""

# If needed, preprocess this algorithm's input
if algorithm in _algorithm_preprocessors:
_algorithm_preprocessors[algorithm](args, kwargs)

_version = version
if "Version" in kwargs:
_version = kwargs["Version"]
Expand Down

0 comments on commit 695c8b6

Please sign in to comment.