Overriding Task Inputs from Python

Author: Guy Shepherd

Back in April I published a series of articles outlining how Mo.net and Python could be used together to develop potentially useful applications of interest to actuaries and other user communities. 

In the last article I explained how to navigate the challenges of primitive data types available in Python to override the inputs to the Mo.net task.  While this approach was perfectly valid, it was hardly very elegant since it relied on creating a new inputs file in Python and then reading that into the Mo.net task DLL.

As part of Mo.net 7.6, we have extended the external interface to compiled Mo.net tasks so that it now accepts a Python list (or a list of lists to be more precise).  This allows a new array of inputs to be passed from Python to the compiled Mo.net task DLL without having to create an intermediate inputs file.

Python Script

Using the Python script covered in Part 4 of the series as a basis, I have amended the code to simply pass the input array (list of lists) directly to the Mo.net DLL.  The complete script, including the revised code is shown below.

import sys
import clr
    
# define path to the Mo.net Linked DLL and add this to system path

assembly_path = r"C:\Mo.net Projects\LinkedFundsForPython\Linked_DLL"
sys.path.append(assembly_path)

# add Mo.net kernel assembly references to the CLR

clr.AddReference("Sal.CalculationEngine.GenCore")
clr.AddReference("Sal.CalculationEngine.GenDataAccess")
clr.AddReference("Sal.CalculationEngine.Interfaces.Entities")
clr.AddReference("Sal.CalculationEngine.ExcelReporting.TabularValues")

from GenDataAccess import DataRequest

# add Mo.net projection DLL reference to the CLR

clr_ref = clr.AddReference("Linked_Monet")
monet_type = clr_ref.GetType('Monet')

# invoke Mo.net interface methods

initCmdLine_method = monet_type.GetMethod('InitCommandLine')
initCmdLine_method.Invoke(None, [None])

init_method = monet_type.GetMethod('Initialise')
init_method.Invoke(None, [True])

initProj_method = monet_type.GetMethod('InitialiseProjections')
initProj_method.Invoke(None, [])

# obtain a reference to the projection inside the DLL

projections = monet_type.GetProperty('Projections')

# override the default value for the SterlInt projection parameter

linkedproj = projections.GetValue(None).Linked
linkedproj.SterlInt = 0.1

#   input array is really a two dimensional list (or a list of lists)

inputarray = [['PolNo', 'Sex1', 'AgeEnt1', 'PremFreq', 'FundType', 'SumAssured', 'PeriodIF', 'Term', 'PremInit'], 
['1', '1', '54', '4', 'Equity', '100,000', '0', '20', '0']]

#   Passing in the list requires the new constructor available in MDS 7.6.0 and later

linkedproj.InputsTable = DataRequest(inputarray)

# run the projection

linkedproj.Run()

# show the inputs - to check they have been overridden

print(f'PolNo = {linkedproj.Record.PolNo}')
print(f'Sex1 = {linkedproj.Record.Sex1}')
print(f'AgeEnt1 = {linkedproj.Record.AgeEnt1}')

# show the sterling result at time=0 

print(f'SterlInt = {linkedproj.SterlInt}')
print(f'SterlRes(0) = {linkedproj.SterlRes(0)}')

Running this script will produce similar output to before, but now it’s using the input values contained in the list defined at line 45.  Changing these input values and rerunning the script will produce different answers

What’s Going On?

The key elements of the Python script are as follows.

Line NumberCommentary
45- 46This defines a new two-dimensional list of input values with headings called inputarray
50Passes the inputarray to the projection task DLL InputsTable property

It’s possible to create multiple rows of inputs in this way by simply extending the inputarray list to hold multiple row of inputs.

Conclusion

I hope this small extension to the original series is helpful.  We will return to the integration potential of Mo.net and other platforms in further blog articles shortly.

Contact Us

Need a Mo.net licence? Get in touch with us today to discuss your modelling requirements.

Was this article helpful?
YesNo

Comments are closed.