Model

class modelx.core.model.Model[source]

Bases: modelx.core.spacecontainer.EditableSpaceContainer

Top-level container in modelx object hierarchy.

Model instances are the top-level objects and directly contain UserSpace objects, which in turn contain other spaces or Cells objects.

A model can be created by new_model API function.

Methods

close() Close the model.
cur_space([name]) Set the current space to Space name and return it.
import_module([module, recursive]) Create a child space from an module.
new_space([name, bases, formula, refs]) Create a child space.
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.
rename(name[, rename_old]) Rename the model itself
save(filepath) Save the model to a file.
set_property(name, value) Set property name
write(model_path) Write model to files.

Attributes

allow_none Whether a cells can have None as its value.
cellgraph A directed graph of cells.
doc Description string
fullname Dotted name of the object.
model The model this object belongs to.
name Name of the object.
parent The parent of this object.
properties
refs Return a mapping of global references.
spaces A mapping of the names of child spaces to the Space objects
rename(name, rename_old=False)[source]

Rename the model itself

save(filepath)[source]

Save the model to a file.

close()[source]

Close the model.

doc

Description string

When models or spaces are imported from modules, taken from modules docstring. For cells, set to its formula’s docstring.

write(model_path)[source]

Write model to files.

This method performs the write_model() on self. See write_model() section for the details.

Parameters:model_path (str) – Folder(directory) path where the model is saved.
cellgraph

A directed graph of cells.

refs

Return a mapping of global references.

_baseattrs

A dict of members expressed in literals

_to_attrdict(attrs=None)

Get extra attributes

allow_none

Whether a cells can have None as its value.

This is a property of Model, Space and Cells. If allow_none of a cells is False, the cells cannot have None as its value. Assigning None to the cells or its formula returning None raises an Error. If True, the cells can have None as their value. If set to None, allow_none of its parent is looked up, and the search continues until True or False is found.

Returns:True if the cells can have None, False if it cannot, or None if a default value from the parent is to be used.
cur_space(name=None)

Set the current space to Space name and return it.

If called without arguments, the current space is returned. Otherwise, the current space is set to the space named name and the space is returned.

fullname

Dotted name of the object.

Names joined by dots, such as ‘Model1.Space1.Cells1’, each element in the string is the name of the parent object of the next one joined by a dot.

import_module(module=None, recursive=False, **params)

Create a child space from an module.

Parameters:
  • module – a module object or name of the module object.
  • recursive – Not yet implemented.
  • **params – arguments to pass to new_space
Returns:

The new child space created from the module.

model

The model this object belongs to.

This is a property of Model, Space and Cells. For models, this property is themselves.

name

Name of the object.

new_space(name=None, bases=None, formula=None, refs=None)

Create a child space.

Parameters:
  • name (str, optional) – Name of the space. Defaults to SpaceN, where N is a number determined automatically.
  • bases (optional) – A space or a sequence of spaces to be the base space(s) of the created space.
  • formula (optional) – Function to specify the parameters of dynamic child spaces. The signature of this function is used for setting parameters for dynamic child spaces. This function should return a mapping of keyword arguments to be passed to this method when the dynamic child spaces are created.
Returns:

The new child space.

new_space_from_csv(filepath, space=None, cells=None, param=None, space_params=None, cells_params=None, *args, **kwargs)

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

This method internally calls Pandas read_csv function, and creates cells by passing the returned DataFrame object to new_space_from_pandas(). The filepath argument to this method is passed to to read_csv as filepath_or_buffer, and the user can pass other arguments to read_csv by supplying those arguments to this method as variable-length parameters, args and kargs.

Parameters:
  • filepath (str, path object, or file-like object) – Path to the file.
  • space – Sequence of strings to set cells name. string is also accepted if read_csv returns a Series because of its squeeze parameter set to True.
  • cells – Sequence of strings to overwrite headers for cells names.
  • param – Sequence of strings to set parameter name(s). A single string can also be passed to set a single parameter name when frame has a single level index (i.e. not MultiIndex).
  • space_params – Sequence of strings or integers to specify space parameters by name or index.
  • cells_params – Sequence of strings or integers to specify cells parameters by name or index.
  • args – Any positional arguments to be passed to read_csv.
  • kwargs – Any keyword arguments to be passed to read_csv.

See also

UserSpace.new_cells_from_csv(): Create Cells from CSV.

new_space_from_excel(book, range_, sheet=None, name=None, names_row=0, param_cols=(0, ), space_param_order=None, cells_param_order=None, transpose=False, names_col=None, param_rows=None)

Create a child space from an Excel range.

To use this method, openpyxl package must be installed.

Parameters:
  • book (str) – Path to an Excel file.
  • range (str) – Range expression, such as “A1”, “$G4:$K10”, or named range “NamedRange1”.
  • sheet (str) – Sheet name (case ignored).
  • name (str, optional) – Name of the space. Defaults to SpaceN, where N is a number determined automatically.
  • names_row (optional) – an index number indicating what row contains the names of cells and parameters. Defaults to the top row (0).
  • param_cols (optional) – a sequence of index numbers indicating parameter columns. Defaults to only the leftmost column ([0]).
  • names_col (optional) – an index number, starting from 0, indicating what column contains additional parameters.
  • param_rows (optional) – a sequence of index numbers, starting from 0, indicating rows of additional parameters, in case cells are defined in two dimensions.
  • transpose (optional) – Defaults to False. If set to True, “row(s)” and “col(s)” in the parameter names are interpreted inversely, i.e. all indexes passed to “row(s)” parameters are interpreted as column indexes, and all indexes passed to “col(s)” parameters as row indexes.
  • space_param_order – a sequence to specify space parameters and their orders. The elements of the sequence denote the indexes of param_cols elements, and optionally the index of param_rows elements shifted by the length of param_cols. The elements of this parameter and cell_param_order must not overlap.
  • cell_param_order (optional) – a sequence to reorder the parameters. The elements of the sequence denote the indexes of param_cols elements, and optionally the index of param_rows elements shifted by the length of param_cols. The elements of this parameter and cell_space_order must not overlap.
Returns:

The new child space created from the Excel range.

See also

UserSpace.new_cells_from_excel(): Create Cells from Excel file.

new_space_from_module(module, recursive=False, **params)

Create a child space from an module.

Alias to import_module().

Parameters:
  • module – a module object or name of the module object.
  • recursive – Not yet implemented.
  • **params – arguments to pass to new_space
Returns:

The new child space created from the module.

new_space_from_pandas(obj, space=None, cells=None, param=None, space_params=None, cells_params=None)

Create child spaces from Pandas DataFrame or Series.

Create a space named space and optionally and cells in it from Pandas DataFrame or Series passed in obj. If space is not given, the space is named SpaceN where N is automatically given by modelx. Parameter names are taken from obj indexes, unless param is given to override index names.

obj can have MultiIndex as its index. If the index(es) of obj has/have name(s), the parameter name(s) of the cells is/are set to the name(s), but can be overwritten by param parameter. If the index(es) of obj has/have no name(s), and param is not given, error is raised.

Parameters:
  • obj – DataFrame or Series.
  • space – Space name.
  • param – Sequence of strings to set parameter name(s). A single string can also be passed to set a single parameter name when frame has a single level index (i.e. not MultiIndex).
  • space_params – Sequence of strings or integers to specify space parameters by name or index.
  • cells_params – Sequence of strings or integers to specify cells parameters by name or index.

See also

UserSpace.new_cells_from_pandas(): Create Cells from DataFrame or Series.

parent

The parent of this object. None for models.

The parent object of a cells is a space that contains the cells. The parent object of a space is either a model or another space that contains the space.

set_property(name: str, value)

Set property name

Set value to property name of an interface. Equivalent to x.name = value, where x is a Model/Space/Cells object.

spaces

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