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:
UserSpaceobjects (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
new_model(): Create a new modelread_model(): Load a model from filesget_models(): Get all models in the sessioncur_model(): Get the current modelUserSpace: Container for cells and nested spaceswrite(): Save model to fileszip(): Save model to zip archive
Changed in version 0.18.0: Added pandas and module update operations
Model properties#
Name of the object. |
|
Dotted name of the object. |
|
Documentation string |
|
A Path object representing the model's path. |
|
The model this object belongs to. |
|
The parent of this object. |
|
Whether a cells can have None as its value. |
|
A mapping of the names of child spaces to the Space objects |
|
Return a mapping of global references. |
|
Return a mapping of macros. |
|
List of |
|
A directed graph of cells. |
|
An object whose |
Model operations#
|
Close the model. |
|
Rename the model itself |
|
Set property |
Macro operations#
Saving operations#
Child Space operations#
|
Set the current space to Space |
|
Create a child space. |
|
Create a child space from an module. |
|
Create spaces from from a comma-separated values (csv) file. |
|
Create a child space from an Excel range. |
|
Create a child space from an module. |
|
Create child spaces from Pandas DataFrame or Series. |
|
Create a top-level UserSpace from another Model. |
|
Tentative: Compare cells with the same name across different spaces in the model. |
Reference operations#
|
Create a Reference bound to a pandas DataFrame or Series associating a new |
|
Assigns a user module to a Reference associating a new |
|
Creates a Reference to an Excel range |
|
Update a pandas object assigned to References |
|
Update an user-defined module assigned to References |
|
Get IOSpec associated with |
|
Delete IOSpec associate with |
Run operations#
|
Generates actions for memory-optimized run |
|
Performs memory-optimized run |