10.4.6 Run a User-Defined Python Function Multiple Times

Use the oml.index_apply function to run a Python function multiple times in Python engines spawned by the database environment.

The syntax of the function is the following:

oml.index_apply(times, func, func_owner=None, parallel=None, graphics=False, **kwargs)

The times argument is an int that specifies the number of times to run the func function.

The func argument is the function to run. It may be one of the following:

  • A Python function

  • A string that is the name of a user-defined Python function in the OML4Py script repository

  • A string that defines a Python function
  • An oml.script.script.Callable object returned by the oml.script.load function

The optional func_owner argument is a string or None (the default) that specifies the owner of the registered user-defined Python function when argument func is a registered user-defined Python function name.

The parallel argument is a boolean, an int, or None (the default) that specifies the preferred degree of parallelism to use in the Embedded Python Execution job. The value may be one of the following:

  • A positive integer greater than or equal to 1 for a specific degree of parallelism

  • False, None, or 0 for no parallelism

  • True for the default data parallelism

The graphics argument is a boolean that specifies whether to look for images. The default value is True.

With the **kwargs parameter, you can pass additional arguments to the func function. Special control arguments, which start with oml_, are not passed to the function specified by func, but instead control what happens before or after the running of the function.

See Also: About Special Control Arguments

The oml.index_apply function returns a list of Python objects or a list of oml.embed.data_image._DataImage objects. If no image is rendered in the user-defined Python function, oml.index_apply returns a list of the Python objects returned by the user-defined Python function. Otherwise, it returns a list of oml.embed.data_image._DataImage objects.

See Also: About Output

Example 10-10 Using the oml.index_apply Function

This example defines a function that returns the mean of a set of random numbers the specified number of times.

import oml
import pandas as pd

def compute_random_mean(index):
    import numpy as np
    import scipy
    from statistics import mean 
    np.random.seed(index)
    res = np.random.random((100,1))*10
    return mean(res[1])
res = oml.index_apply(times=10, func=compute_random_mean)
type(res)
res

Listing for This Example

>>> import oml
>>> import pandas as pd
>>>
>>> def compute_random_mean(index):
...     import numpy as np
...     import scipy
...     from statistics import mean 
...     np.random.seed(index)
...     res = np.random.random((100,1))*10
...     return mean(res[1])
...
>>> res = oml.index_apply(times=10, func=compute_random_mean)
>>> type(res)
<class 'list'>
>>> res
[7.203244934421581, 0.25926231827891333, 7.081478226181048,
5.4723224917572235, 8.707323061773764, 3.3197980530117723,
7.7991879224011464, 9.68540662820932, 5.018745921487388, 
0.207519493594015]