Model.new_space_from_model#
- Model.new_space_from_model(source_model, name=None, *, refs_strategy='copy', refs_prefix=None, defined_only=False)[source]#
Create a top-level UserSpace from another Model.
This method creates a new
UserSpacein the model and copies all top-level spaces fromsource_modelinto the created space. The space acts as a namespace wrapper, allowing the source model’s structure to be migrated within the target model.The method provides control over how references are handled during the migration process through the
refs_strategyparameter, and allows filtering of the copied content through thedefined_onlyparameter.- Parameters:
source_model (
Model) – The model whose top-level spaces will be copied into this model. Cannot be the same as the target model (self).name (
str, optional) – Name of the container space in the target model. If not specified, defaults tosource_model.name.refs_strategy (
str, optional) –Strategy for handling global references from
source_model. Must be one of:"copy"(default) - Copy all global references fromsource_model(except__builtins__) into the container space’s references. References are propagated to all descendant spaces."ignore"- Do not copy any references fromsource_model. Only the spaces and cells structure is copied."shadow"- Copy references fromsource_modelonly if they don’t already exist in the target model’s global references. Existing references in the target model take precedence.
refs_prefix (
str, optional) – If specified, prefix to apply to reference names when copying fromsource_model. For example, ifrefs_prefix="src_", a reference nameddatainsource_modelwill be copied assrc_datain the container space. The original name is also preserved as an alias. Only applicable whenrefs_strategyis"copy"or"shadow".defined_only (
bool, optional) – IfTrue, only defined (non-derived)CellsandUserSpaceobjects are copied. Derived cells and spaces resulting from inheritance are excluded. Defaults toFalse.
- Returns:
The created container space containing all copied spaces from
source_model.- Return type:
- Raises:
ValueError – If
source_modelis the same as the target model (self).
Example
Create two models and embed one into the other:
>>> import modelx as mx >>> # Create source model with spaces and references >>> source = mx.new_model("SourceModel") >>> source.param = 100 >>> space1 = source.new_space("Space1") >>> @mx.defcells ... def calc(x): ... return x * _model.param >>> # Create target model >>> target = mx.new_model("TargetModel") >>> # Embed source model as a space >>> container = target.new_space_from_model(source, name="Embedded") >>> # Access embedded content >>> container.Space1.calc(5) 500
Using
refs_prefixto avoid name conflicts:>>> target.rate = 0.05 # Global reference in target >>> source.rate = 0.03 # Global reference in source >>> # Embed with prefix to distinguish references >>> container = target.new_space_from_model( ... source, ... name="Embedded", ... refs_prefix="src_" ... ) >>> # References are available with prefix >>> container.src_rate 0.03 >>> target.rate # Target's original reference unchanged 0.05
Copy only defined cells and spaces:
>>> base_space = source.new_space("Base") >>> derived_space = source.new_space("Derived") >>> derived_space.add_bases(base_space) >>> # Copy only defined content (excludes derived) >>> container = target.new_space_from_model( ... source, ... defined_only=True ... )
See also
Added in version 0.29.2.