Model#

class Model(_impl)[source]#

Top-level container representing a complete modelx model.

Model is the root object in the modelx object hierarchy and serves as the primary container for organizing spaces, cells, and references. Each Model represents an independent, self-contained computational model with its own namespace and execution environment.

A Model contains:

  • UserSpace objects (top-level spaces)

  • Global references accessible throughout the model

  • Serialization and I/O specifications

Key Characteristics:

  • Top-level container: Root of the object hierarchy

  • Independent namespace: Each model has isolated global references

  • Serializable: Can be saved to/loaded from files or zip archives

  • Exportable: Can be exported as a Python package

Creation:

Models are created using the new_model() function:

>>> import modelx as mx
>>> model = mx.new_model()
>>> model
<Model Model1>

>>> # Create with specific name
>>> model = mx.new_model('MyModel')
>>> model
<Model MyModel>
Adding Spaces:

Create child spaces to organize cells and nested structures:

>>> space = model.new_space('Calculations')
>>> space
<UserSpace Calculations in MyModel>
Global References:

Set global references accessible from all spaces in the model:

>>> import numpy as np
>>> model.np = np
>>> model.discount_rate = 0.05
Persistence:

Models can be saved and loaded in multiple formats:

>>> # Save as directory structure with text files
>>> model.write('path/to/model')

>>> # Save as zip archive
>>> model.zip('path/to/model.zip')

>>> # Load a saved model
>>> loaded = mx.read_model('path/to/model')
Memory Management:

Clear calculated values to free memory:

>>> model.clear_all()  # Clear all cells and dynamic spaces in the model
Multiple Models:

Multiple models can coexist in the same session:

>>> model1 = mx.new_model('Model1')
>>> model2 = mx.new_model('Model2')
>>> mx.get_models()
{'Model1': <Model Model1>, 'Model2': <Model Model2>}
Accessing Current Model:

Get the currently active model:

>>> mx.cur_model()
<Model Model2>

See also

Changed in version 0.18.0: Added pandas and module update operations

Model properties#

name

Name of the object.

fullname

Dotted name of the object.

doc

Documentation string

path

A Path object representing the model's path.

model

The model this object belongs to.

parent

The parent of this object.

allow_none

Whether a cells can have None as its value.

properties

spaces

A mapping of the names of child spaces to the Space objects

refs

Return a mapping of global references.

macros

Return a mapping of macros.

iospecs

List of BaseIOSpec objects

tracegraph

A directed graph of cells.

info

An object whose repr summarizes this Interface.

Model operations#

close()

Close the model.

rename(name[, rename_old])

Rename the model itself

set_property(name, value)

Set property name

Macro operations#

new_macro([name, formula])

Create a new Macro in this model.

Saving operations#

write(model_path[, backup, log_input])

Write model to files.

zip(model_path[, backup, log_input, ...])

Archive model to a zip file.

export(path)

Export the model as a Python package.

Child Space operations#

cur_space([name])

Set the current space to Space name and return it.

new_space([name, bases, formula, refs])

Create a child space.

clear_all()

Clears Cells and ItemSpace.

import_module([module, recursive])

Create a child space from an module.

new_space_from_csv(filepath[, space, cells, ...])

Create spaces from from a comma-separated values (csv) file.

new_space_from_excel(book, range_[, sheet, ...])

Create a child space from an Excel range.

new_space_from_module(module[, recursive])

Create a child space from an module.

new_space_from_pandas(obj[, space, cells, ...])

Create child spaces from Pandas DataFrame or Series.

new_space_from_model(source_model[, name, ...])

Create a top-level UserSpace from another Model.

compare_cells(func)

Tentative: Compare cells with the same name across different spaces in the model.

Reference operations#

new_pandas(name, path, data[, file_type, ...])

Create a Reference bound to a pandas DataFrame or Series associating a new PandasData object.

new_module(name, path, module)

Assigns a user module to a Reference associating a new ModuleData object

new_excel_range(name, path, range_[, sheet, ...])

Creates a Reference to an Excel range

update_pandas(old_data[, new_data])

Update a pandas object assigned to References

update_module(old_module[, new_module])

Update an user-defined module assigned to References

get_spec(data)

Get IOSpec associated with data

del_spec(data)

Delete IOSpec associate with data

Run operations#

generate_actions(targets[, step_size])

Generates actions for memory-optimized run

execute_actions(actions)

Performs memory-optimized run