From 14a28af3a0a9ae2c0a2884a098b677c39cdd593a Mon Sep 17 00:00:00 2001 From: Roman Tolchenov Date: Fri, 29 May 2015 10:05:20 +0100 Subject: [PATCH] Re #11617. Changes to python api. Fix compiler warnings. --- .../test/CalculateChiSquaredTest.h | 6 +- .../PythonInterface/mantid/simpleapi.py | 116 ++++++++++-------- 2 files changed, 69 insertions(+), 53 deletions(-) diff --git a/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h b/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h index c656ffd4c287..0f428ac39a87 100644 --- a/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h +++ b/Code/Mantid/Framework/CurveFitting/test/CalculateChiSquaredTest.h @@ -180,7 +180,7 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { xBins.resize(nData + dlt); double dx = (xMax - xMin) / double(xBins.size() - 1); for (size_t i = 0; i < xBins.size(); ++i) { - xBins[i] = xMin + i * dx; + xBins[i] = xMin + double(i) * dx; } if (isHisto) { xValues.resize(nData); @@ -219,8 +219,8 @@ class CalculateChiSquaredTest : public CxxTest::TestSuite { bool isExecuted; Tester(size_t np = 3, size_t nd = 10, bool histo = true) - : nParams(np), nData(nd), isHisto(histo), workspaceIndex(0), xMin(-10), - xMax(10), StartX(EMPTY_DBL()), EndX(EMPTY_DBL()), + : nParams(np), nData(nd), isHisto(histo), xMin(-10), xMax(10), + workspaceIndex(0), StartX(EMPTY_DBL()), EndX(EMPTY_DBL()), ignoreInvalidData(false), chiSquared(-1), chiSquaredDividedByDOF(-1), chiSquaredWeighted(-1), chiSquaredWeightedDividedByDOF(-1), isExecuted(false) { diff --git a/Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py b/Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py index 1ded6b0bf035..7f1da756840e 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py +++ b/Code/Mantid/Framework/PythonInterface/mantid/simpleapi.py @@ -171,64 +171,80 @@ def LoadDialog(*args, **kwargs): #---------------------------- Fit --------------------------------------------- -def Fit(*args, **kwargs): - """ - Fit defines the interface to the fitting 562 within Mantid. - It can work with arbitrary data sources and therefore some options - are only available when the function & workspace type are known. +def fitting_algorithm(f): + + function_name = f.__name__ - This simple wrapper takes the Function (as a string) & the InputWorkspace - as the first two arguments. The remaining arguments must be - specified by keyword. + def wrapper(*args, **kwargs): + """ + Fit defines the interface to the fitting 562 within Mantid. + It can work with arbitrary data sources and therefore some options + are only available when the function & workspace type are known. - Example: - Fit(Function='name=LinearBackground,A0=0.3', InputWorkspace=dataWS', - StartX='0.05',EndX='1.0',Output="Z1") - """ - Function, InputWorkspace = _get_mandatory_args('Fit', ["Function", "InputWorkspace"], *args, **kwargs) - # Remove from keywords so it is not set twice - if "Function" in kwargs: - del kwargs['Function'] - if "InputWorkspace" in kwargs: - del kwargs['InputWorkspace'] + This simple wrapper takes the Function (as a string) & the InputWorkspace + as the first two arguments. The remaining arguments must be + specified by keyword. - # Check for behaviour consistent with old API - if type(Function) == str and Function in _api.AnalysisDataService: - raise ValueError("Fit API has changed. The function must now come first in the argument list and the workspace second.") - # Create and execute - algm = _create_algorithm_object('Fit') - _set_logging_option(algm, kwargs) - algm.setProperty('Function', Function) # Must be set first - algm.setProperty('InputWorkspace', InputWorkspace) + Example: + Fit(Function='name=LinearBackground,A0=0.3', InputWorkspace=dataWS', + StartX='0.05',EndX='1.0',Output="Z1") + """ + Function, InputWorkspace = _get_mandatory_args(function_name, ["Function", "InputWorkspace"], *args, **kwargs) + # Remove from keywords so it is not set twice + if "Function" in kwargs: + del kwargs['Function'] + if "InputWorkspace" in kwargs: + del kwargs['InputWorkspace'] + + # Check for behaviour consistent with old API + if type(Function) == str and Function in _api.AnalysisDataService: + raise ValueError("Fit API has changed. The function must now come first in the argument list and the workspace second.") + # Create and execute + algm = _create_algorithm_object(function_name) + _set_logging_option(algm, kwargs) + algm.setProperty('Function', Function) # Must be set first + 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] + # 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() - # Check for any properties that aren't known and warn they will not be used - for key in kwargs.keys(): - if key not in algm: - logger.warning("You've passed a property (%s) to Fit() that doesn't apply to any of the input workspaces." % key) - del kwargs[key] - _set_properties(algm, **kwargs) - algm.execute() + lhs = _kernel.funcreturns.lhs_info() + # Check for any properties that aren't known and warn they will not be used + for key in kwargs.keys(): + if key not in algm: + logger.warning("You've passed a property (%s) to %s() that doesn't apply to any of the input workspaces." % (key,function_name)) + del kwargs[key] + _set_properties(algm, **kwargs) + algm.execute() - return _gather_returns('Fit', lhs, algm) + return _gather_returns(function_name, lhs, algm) -# Have a better load signature for autocomplete -_signature = "\bFunction, InputWorkspace" -# Getting the code object for Load -_f = Fit.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) + # Have a better load signature for autocomplete + _signature = "\bFunction, InputWorkspace" + # Getting the code object for Load + _f = wrapper.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) -# Replace the code object of the wrapper function -Fit.func_code = _c + # Replace the code object of the wrapper function + wrapper.func_code = _c + if not function_name in __SPECIALIZED_FUNCTIONS__: + __SPECIALIZED_FUNCTIONS__.append(function_name) + + return wrapper + +@fitting_algorithm +def Fit(*args, **kwargs): + pass + +@fitting_algorithm +def CalculateChiSquared(*args, **kwargs): + pass def FitDialog(*args, **kwargs): """Popup a dialog for the Load algorithm. More help on the Load function