Initializing help system before first use

I/O Driver

Topics covered in this chapter:

This package provides the moselpy: I/O driver at the Mosel language level.

When used without a label, the moselpy: I/O driver must be employed as a file name for initializations blocks for bidirectional data exchange between Python and Mosel models. Data passed to Model.run becomes available to the model via initializations from "moselpy:", and data written via initializations to "moselpy:" is returned by Model.run.

When specifying a label (e.g. moselpy:name), the driver acts as a data stream (binary or textual depending on the associated Python entity) that may appear wherever a file name is expected. In particular, it is used for redirecting default input/output streams of Mosel (see Model.set_default_stream).

Driver moselpy without a label

The driver exchanges data by identifier name. Input data is provided as a dictionary to Model.run:

# Python
model = mp.load_model("model.bim")
output = model.run(input_data={
    "scalar_int": 42,
    "scalar_real": 3.14,
    "scalar_str": "hello",
    "array_1d": {1: 10, 2: 20, 3: 30},
    "array_2d": {(1,1): 100, (1,2): 200, (2,1): 300, (2,2): 400, (3,1): 500, (3,2): 600}
})
print(output["myresult"])

The corresponding Mosel model reads and writes using the driver:

model 'example'
declarations
  scalar_int: integer
  scalar_real: real
  scalar_str: string
  array_1d: array(R: range) of integer
  array_2d: array(R, C: range) of integer
  myresult: integer
end-declarations

! Explicit set specifications may be required to state the full index domain
initializations from "moselpy:"
  scalar_int scalar_real scalar_str array_1d array_2d
end-initializations

! Compute myresult using both arrays
myresult := scalar_int + sum(i in R) array_1d(i) + sum(i in R, j in C) array_2d(i, j)

! Send output to Python
initializations to "moselpy:"
  myresult
end-initializations
end-model

When arrays are passed from Python, Mosel populates array index sets (ranges and sets) automatically from the keys of the incoming data. Explicit initialization of R or C from Python is not needed. Explicit set specifications are only required for sparse arrays where the full index domain must be declared independently of the data.

Type conversion

The following type conversions are performed automatically:

Python Type Mosel Type Notes
int integer
float real
str string
bool boolean
dict array Keys become indices
list, tuple, set set or list Determined by Mosel declaration
bytes Raw bytes For binary driver
pandas Series array Index becomes array key; name used as identifier if not specified
pandas DataFrame array(row, col) Row index and column name form a 2-tuple key
NumPy ndarray array Integer-indexed from 1

For arrays, dictionary keys can be:

  • Integers for single-index arrays: {1: 10, 2: 20} (NumPy integer types such as np.int64 are also accepted)
  • Tuples for multi-index arrays: {(1,1): 100, (1,2): 200} (tuples of NumPy integers are also accepted)
  • Strings for string-indexed arrays: {"a": 10, "b": 20}

For Mosel set and list types, any Python iterable (list, tuple, set, frozenset) can be used. The target type is determined by the Mosel declaration, not the Python type.

Sparse arrays

Sparse arrays are supported naturally since Python dictionaries only contain defined entries:

# Python - sparse data (only some indices defined)
model.run(input_data={
    "sparse_arr": {1: 100, 5: 500, 10: 1000}
})
! Mosel - receives sparse array
declarations
  sparse_arr: dynamic array(range) of integer
end-declarations
initializations from "moselpy:"
  sparse_arr  ! Only indices 1, 5, 10 will be set
end-initializations

Driver moselpy with a label

The label associated to the moselpy: driver must have been registered in the symbols registry (see Model.symbols).

Example generating a binary data file in memory (see to_binary):

# Python
bin_data = mp.to_binary({"x": 42, "arr": {1: 10, 2: 20}})
model.symbols["mydata"] = bin_data
model.run()
! Mosel
initializations from "bin:moselpy:mydata"
  x arr
end-initializations

Example using a text string as a model source:

# Python
from io import StringIO

model_source = """model hello
  writeln('hello world')
end-model
"""
mp.symbols["mosel_source"] = StringIO(model_source)
mp.execute_model("moselpy:mosel_source")

© 2001-2026 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.