diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IPeakFunction.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IPeakFunction.cpp index 1164cfe661d7..0d1a5c71e0a9 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IPeakFunction.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/IPeakFunction.cpp @@ -7,12 +7,18 @@ using Mantid::API::IPeakFunction; using Mantid::PythonInterface::IPeakFunctionAdapter; using namespace boost::python; -void export_IPeakFunction() -{ - class_, boost::shared_ptr, - boost::noncopyable>("IPeakFunction") - .def("functionLocal", (object (IPeakFunctionAdapter::*)(const object &)const)&IPeakFunction::functionLocal, - "Calculate the values of the function for the given x values. The output should be stored in the out array") - ; +void export_IPeakFunction() { + class_, + boost::shared_ptr, boost::noncopyable>( + "IPeakFunction") + .def("functionLocal", + (object (IPeakFunctionAdapter::*)(const object &)const) & + IPeakFunction::functionLocal, + "Calculate the values of the function for the given x values. The " + "output should be stored in the out array") + .def("intensity", &IPeakFunction::intensity, + "Returns the integral intensity of the peak function.") + .def("setIntensity", &IPeakFunction::setIntensity, + "Changes the integral intensity of the peak function by setting its " + "height."); } - diff --git a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/IPeakFunctionTest.py b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/IPeakFunctionTest.py index 3832a39214ed..2ac78d6d5376 100644 --- a/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/IPeakFunctionTest.py +++ b/Code/Mantid/Framework/PythonInterface/test/python/mantid/api/IPeakFunctionTest.py @@ -13,6 +13,41 @@ def init(self): def functionLocal(self, xvals): return 5*xvals +class RectangularFunction(IPeakFunction): + def init(self): + self.declareParameter("Height") + self.declareParameter("Fwhm") + self.declareParameter("Center") + + def centre(self): + return self.getParameterValue("Center") + + def setCentre(self, newCenter): + self.setParameter("Center", newCenter) + + def height(self): + return self.getParameterValue("Height") + + def setHeight(self, newHeight): + self.setParameter("Height", newHeight) + + def fwhm(self): + return self.getParameterValue("Fwhm") + + def setFwhm(self, newFwhm): + self.setParameter("Fwhm", newFwhm) + + def functionLocal(self, xvals): + center = self.getParameterValue("Center") + fwhm = self.getParameterValue("Fwhm") + height = self.getParameterValue("Height") + + values = np.zeros(xvals.shape) + nonZero = (xvals > (center - fwhm/2.0)) & (xvals < (center + fwhm / 2.0)) + values[nonZero] = height + + return values + class IPeakFunctionTest(unittest.TestCase): def test_instance_can_be_created_standalone(self): @@ -38,5 +73,23 @@ def test_functionLocal_can_be_called_directly(self): self.assertEquals(10., out[1]) self.assertEquals(15., out[2]) + def test_get_set_intensity(self): + func = RectangularFunction() + func.initialize() + func.setCentre(1.0) + func.setHeight(2.0) + func.setFwhm(3.0) + + # This is a rectangle function with height 2 and width 3, centered + # around 1.0. The intensity should be 6.0 (height * width) + self.assertAlmostEquals(func.intensity(), 6.0, delta=1e-10) + + # Setting the intensity only changes height, not width + func.setIntensity(12.0) + + self.assertEquals(func.fwhm(), 3.0) + self.assertAlmostEquals(func.height(), 4.0, delta=1e-10) + self.assertAlmostEquals(func.intensity(), 12.0, delta=1e-10) + if __name__ == '__main__': unittest.main()