Skip to content

model_element

ModelElement(data, **kwargs)

Bases: ABC

Source code in pyoframe/model_element.py
def __init__(self, data: pl.DataFrame, **kwargs) -> None:
    # Sanity checks, no duplicate column names
    assert len(data.columns) == len(
        set(data.columns)
    ), "Duplicate column names found."

    cols = _get_dimensions(data)
    if cols is None:
        cols = []
    cols += [col for col in RESERVED_COL_KEYS if col in data.columns]

    # Reorder columns to keep things consistent
    data = data.select(cols)

    # Cast to proper dtype
    if COEF_KEY in data.columns:
        data = data.cast({COEF_KEY: pl.Float64})
    if VAR_KEY in data.columns:
        data = data.cast({VAR_KEY: pl.UInt32})

    self._data = data
    self._model: Optional[Model] = None
    self.name = None
    super().__init__(**kwargs)

dimensions: Optional[List[str]] property

The names of the data's dimensions.

Examples:

>>> from pyoframe.core import Variable
>>> # A variable with no dimensions
>>> Variable().dimensions
>>> # A variable with dimensions of "hour" and "city"
>>> Variable([{"hour": ["00:00", "06:00", "12:00", "18:00"]}, {"city": ["Toronto", "Berlin", "Paris"]}]).dimensions
['hour', 'city']

dimensions_unsafe: List[str] property

Same as dimensions but returns an empty list if there are no dimensions instead of None. When unsure, use dimensions instead since the type checker forces users to handle the None case (no dimensions).

shape: Dict[str, int] property

The number of indices in each dimension.

Examples:

>>> from pyoframe.core import Variable
>>> # A variable with no dimensions
>>> Variable().shape
{}
>>> # A variable with dimensions of "hour" and "city"
>>> Variable([{"hour": ["00:00", "06:00", "12:00", "18:00"]}, {"city": ["Toronto", "Berlin", "Paris"]}]).shape
{'hour': 4, 'city': 3}

ModelElementWithId(data, **kwargs)

Bases: ModelElement, AttrContainerMixin

Provides a method that assigns a unique ID to each row in a DataFrame. IDs start at 1 and go up consecutively. No zero ID is assigned since it is reserved for the constant variable term. IDs are only unique for the subclass since different subclasses have different counters.

Source code in pyoframe/model_element.py
def __init__(self, data: pl.DataFrame, **kwargs) -> None:
    super().__init__(data, **kwargs)
    self._data = self._assign_ids(self.data)

get_id_column_name() abstractmethod classmethod

Returns the name of the column containing the IDs.

Source code in pyoframe/model_element.py
@classmethod
@abstractmethod
def get_id_column_name(cls) -> str:
    """
    Returns the name of the column containing the IDs.
    """

reset_counters() classmethod

Resets all the ID counters. This function is called before every unit test to reset the code state.

Source code in pyoframe/model_element.py
@classmethod
def reset_counters(cls):
    """
    Resets all the ID counters.
    This function is called before every unit test to reset the code state.
    """
    cls._id_counters = defaultdict(lambda: 1)

SupportPolarsMethodMixin

Bases: ABC