Procedures and functions
The procedures and functions of the python3 module fail in case of Python compile-time or run-time errors.
Call a Python object, a function, with optional input arguments and convert the result to a Mosel variable.
|
|
Execute Python statements from a string.
|
|
Get the result of a Python expression as Mosel variable.
|
|
Initialize a list of Mosel arrays from the columns of a pandas DataFrame.
|
|
Initialize the Python interpreter.
|
|
Set
pyusepandas parameter to true and initialize the pandas interface.
|
|
Run a Python script and wait until it is finished.
|
|
Assign a Mosel value to a global Python variable.
|
|
Convert a list of Mosel arrays to a pandas DataFrame.
|
|
Release the Python interpreter and its resources.
|
I/O drivers
The python3 module provides a driver that is designed to be used in initializations blocks for both reading data from and writing data to Python.
Driver python
python:optional_module_name
The driver can only be used in `initializations' blocks. The optional string after the colon is the Python module name to read the data from or write the data to. If a module name is provided, then the optional item labels are understood as attribute names of the specified module. Initializing data to, for example, "python:client_data" will create a new module called client_data. To access that module in Python, you need to import it like a normal Python module ("import client_data"). After importing the module, the converted Mosel variables will be the attributes of the module, for example, client_data.demand.
If no module name is provided, i.e. if the file name is "python:", then the driver behaves like pyset and pyget: When writing data to Python, the optional labels are understood as global variable names. The driver creates or overwrites the global variables by writing the new values to the attributes of the Python __main__ module. If a variable name is not a valid Python variable identifier the driver will succeed anyway and write the value to the module attributes using the name specified in the label. When reading data from Python, the optional labels are understood as Python expressions. At first, the driver checks whether an expression is a global variable and tries to access it by getting it from the attributes of the Python __main__ module. If this fails, the expression is evaluated by Python and the result is written to the Mosel variable.
The driver throws an I/O error if the expression evaluation or the type conversion fails. Use the parameters ioctrl (see setparam) and iostatus (see getparam) to catch I/O errors.
When initializing data from Mosel to Python, a possibly existing Python object with the same name will be replaced by a new Python object with a Python object type that matches the type of the Mosel source variable.
When initializing arrays, sets, or lists from Python to Mosel, the initialization behavior is additive: The elements of the Python structures are added to the Mosel structures and existing elements in the Mosel structures will not be deleted automatically. If, for example, the target Mosel array is dense and the source Python dictionary or Series is sparse, then the Mosel array may contain old and new values after initialization from Python. If the Mosel array is meant to contain only the values retrieved from the Python dictionary, it is recommended to clear the array with reset before initializing it from Python.
Type mapping to Python
- Mosel boolean →
- Python bool
- NumPy bool_ if element of pandas Series
- Mosel integer →
- Python int
- NumPy int64 if element of pandas Series
- Mosel real →
- Python float
- NumPy float64 if element of pandas Series
- Mosel string/text → Python str
- Mosel set → Python set
- Mosel list → Python list
- Mosel array(I) →
- If pyusepandas: pandas Series with one-dimensional index
- If not pyusepandas: Python dictionary with scalar keys
- Mosel array(I, J, ...) →
- If pyusepandas: pandas Series with multidimensional index
- If not pyusepandas: Python dictionary with tuples of scalars as keys
- Mosel list of array →
- If pyusepandas: pandas DataFrame with one- or multidimensional index
- If not pyusepandas: not supported
- Mosel nested types → Python nested type
- Mosel records: not supported
- Other Mosel external types: not supported
Type mapping from Python
The NumPy and pandas types are only supported if the pandas interface is initialized.
- Mosel boolean ← Python bool; NumPy bool_
- Mosel int ← Python int, bool; NumPy integer, bool_
- Mosel real ← Python float, int, bool; Numpy floating, integer, bool_
- Mosel string/text ← String representation of Python object as returned by repr()
- Mosel list ← Python list
- Mosel set ← iterable types (e.g., set, generator expression, classes that implement __iter__())
- Mosel array ←
- pandas Series with one- or multidimensional index
- NumPy ndarray with one or multiple dimensions
- Python dictionary with scalar or tuple keys
- Mosel list of array ←
- pandas DataFrame with one- or multidimensional index
- Mosel nested types ← Python nested type of supported subtypes
- Mosel records: not supported
- Other Mosel external types: not supported
In the following example, a sparse array is transferred to Python and then the same array is reused for retrieving data from a sparse Python dictionary.
model "Python I/O example" uses "python3" declarations I = 1..4 A: dynamic array(I) of integer end-declarations A(1) := 1*2; A(3) := 3*2 initializations to "python:" I as "MyRange" A end-initializations pyrun("io-example.py") reset(A) ! Delete existing elements from array A. initializations from "python:" A end-initializations writeln("Values initialized from Python:") writeln(" A = ", A) end-model
The content of io-example.py:
print("Values initialized to Python:") print(" MyRange =", MyRange) print(" A =", A) print("Modifying data in Python...") A = {i: 2 * i for i in MyRange if i % 2 == 0}
Executing this model generates the following output:
Values initialized to Python: MyRange = range(1, 5) A = {1: 2, 3: 6} Modifying data in Python... Values initialized from Python: A = [(2,4),(4,8)]
Troubleshooting
This section describes some known issues and possible solutions.
- Mosel: E-353: Module `python3' disabled by restrictions. This module does not handle Mosel restrictions, it will therefore fail to load if Mosel is run in restricted mode. Section Xpress Insight 5 configuration provides information about configuring the security restrictions.
- If the initialization of a Python extension module fails when it is imported from within Mosel, then first check which Python environment is used from within Mosel (set pyinitverbose to true) and check whether the extension module is installed in that environment. If it is installed and can be imported from within the interactive shell of that environment, but fails when it is imported from within Mosel, then check whether your environment is set up correctly: Windows Anaconda Setup, Linux Anaconda Setup.
© 2001-2023 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.