\(\renewcommand\AA{\unicode{x212B}}\)

Main Algorithm Body

After the properties have been created within the PyInit function it is time to turn attention to what the algorithm does with its inputs, i.e. what happens when we execute it.

In order to define what happens when the algorithm is executed we must write another function called PyExec, whose definition takes only self as an input.

def PyExec(self):
    # Code to run algorithm
    pass

Inside PyExec you can write any python code that is necessary to run your desired processing. It can of course call Mantid python functions but you can also run any python code you like

def PyExec(self):
    sum = 0
    for i in range(100):
        sum += 1

Retrieving Input

It is most likely that the algorithm has input properties and the values that have been given by the user will need to be retrieved. This is done through the getProperty method, which returns the named property. This can then be used to query the value, units, defaults etc. See mantid.kernel.Property for more details. The actual value of the property is retrieved with .value and is returned as an appropriate type.

For example, to parameterize the loop above, instead of the hard-coded 100, we could define an input parameter, NIterations, which will be used to constrain the loop:

 1def PyInit(self):
 2    self.declareProperty("NIterations", 100,
 3                         IntBoundedValidator(lower=0),
 4                         "The number of iterations of the loop to perform (default=100)")
 5
 6def PyExec(self):
 7    nloops = self.getProperty("NIterations").value
 8    sum = 0
 9    for i in range(nloops):
10        sum += 1

Line 7, self.getProperty("[NAME]").value, is valid regardless of the type of the property: float, workspace etc.

Output Properties

Most algorithms will want to do something and return some output to the user. We do this by using output properties so that they can be used in a generic manner, without knowing what needs to be returned.

Output properties are declared in a similar manner to input properties, with the exception that their direction is Direction.Output. As an example, we could extend the code above to output the final sum like so:

 1def PyInit(self):
 2    self.declareProperty(name="NIterations", defaultValue=100,
 3                         validator=IntBoundedValidator(lower=0),
 4                         doc="The number of iterations of the loop to perform (default=100)")
 5    self.declareProperty(name="SummedValue", defaultValue=0,
 6                         doc="Outputs the sum of the n iterations",
 7                         direction = Direction.Output)
 8
 9def PyExec(self):
10    nloops = self.getProperty("NIterations").value
11    sum = 0
12    for i in range(nloops):
13        sum += 1
14
15    self.setProperty("SummedValue", sum)

The second property on lines 5-7, SummedValue, is defined as an output property. The self.setProperty("SummedValue", sum) on line 15 is used as the point to tell the algorithm that this value is to be used for that property. It will then be returned as part of the generated python function wrapper.