Skip to content

Commit

Permalink
Refs #11104. Python exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Feb 18, 2015
1 parent 7a4cefb commit 5a2a070
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
Expand Up @@ -7,12 +7,18 @@ using Mantid::API::IPeakFunction;
using Mantid::PythonInterface::IPeakFunctionAdapter;
using namespace boost::python;

void export_IPeakFunction()
{
class_<IPeakFunction, bases<IFunction1D>, boost::shared_ptr<IPeakFunctionAdapter>,
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_<IPeakFunction, bases<IFunction1D>,
boost::shared_ptr<IPeakFunctionAdapter>, 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.");
}

Expand Up @@ -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):
Expand All @@ -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()

0 comments on commit 5a2a070

Please sign in to comment.