Skip to content

BaseBlock

Bases: ABC

The base class for elements of a Model such as and .

Methods:

Name Description
estimated_size

Returns the estimated size of the object in bytes.

Attributes:

Name Type Description
data DataFrame

Returns the object's underlying Polars DataFrame.

dimensionless bool

Whether the object has no dimensions.

dimensions list[str] | None

The names of the data's dimensions.

name str
shape dict[str, int]

The number of distinct labels in each dimension.

Source code in pyoframe/_model_element.py
def __init__(self, data: pl.DataFrame, name="unnamed") -> 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: Config.id_dtype})
    if QUAD_VAR_KEY in data.columns:
        data = data.cast({QUAD_VAR_KEY: Config.id_dtype})

    self._data = data
    self._model: Model | None = None
    self.name: str = name  # gets overwritten if object is added to model

data: pl.DataFrame

Returns the object's underlying Polars DataFrame.

dimensionless: bool

Whether the object has no dimensions.

Examples:

A variable with no dimensions

>>> pf.Variable().dimensionless
True

A variable with dimensions of "hour" and "city"

>>> pf.Variable(
...     [
...         {"hour": ["00:00", "06:00", "12:00", "18:00"]},
...         {"city": ["Toronto", "Berlin", "Paris"]},
...     ]
... ).dimensionless
False

dimensions: list[str] | None

The names of the data's dimensions.

Examples:

A variable with no dimensions

>>> pf.Variable().dimensions

A variable with dimensions of "hour" and "city"

>>> pf.Variable(
...     [
...         {"hour": ["00:00", "06:00", "12:00", "18:00"]},
...         {"city": ["Toronto", "Berlin", "Paris"]},
...     ]
... ).dimensions
['hour', 'city']

name: str = name

shape: dict[str, int]

The number of distinct labels in each dimension.

Examples:

A variable with no dimensions

>>> pf.Variable().shape
{}

A variable with dimensions of "hour" and "city"

>>> pf.Variable(
...     [
...         {"hour": ["00:00", "06:00", "12:00", "18:00"]},
...         {"city": ["Toronto", "Berlin", "Paris"]},
...     ]
... ).shape
{'hour': 4, 'city': 3}

estimated_size(unit: pl.SizeUnit = 'b') -> int | float

Returns the estimated size of the object in bytes.

Only considers the size of the underlying DataFrame(s) since other components (e.g., the object name) are negligible.

Parameters:

Name Type Description Default
unit SizeUnit 'b'

Examples:

>>> m = pf.Model()

A dimensionless variable contains just a 32 bit (4 bytes) unsigned integer (the variable ID).

>>> m.x = pf.Variable()
>>> m.x.estimated_size()
4

A dimensioned variable contains, for every row, a 32 bit ID and, in this case, a 64 bit dim_x value (1200 bytes total).

>>> m.y = pf.Variable(pf.Set(dim_x=range(100)))
>>> m.y.estimated_size()
1200
Source code in pyoframe/_model_element.py
def estimated_size(self, unit: pl.SizeUnit = "b") -> int | float:
    """Returns the estimated size of the object in bytes.

    Only considers the size of the underlying DataFrame(s) since other components (e.g., the object name) are negligible.

    Parameters:
        unit:
            See [`polars.DataFrame.estimated_size`](https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.estimated_size.html).

    Examples:
        >>> m = pf.Model()

        A dimensionless variable contains just a 32 bit (4 bytes) unsigned integer (the variable ID).

        >>> m.x = pf.Variable()
        >>> m.x.estimated_size()
        4

        A dimensioned variable contains, for every row, a 32 bit ID and, in this case, a 64 bit `dim_x` value (1200 bytes total).

        >>> m.y = pf.Variable(pf.Set(dim_x=range(100)))
        >>> m.y.estimated_size()
        1200
    """
    return self.data.estimated_size(unit)