util
File containing utility functions and classes.
Container(setter, getter)
A placeholder object that makes it easy to set and get attributes. Used in Model.attr and Model.params, for example.
Examples:
>>> x = {}
>>> params = Container(setter=lambda n, v: x.__setitem__(n, v), getter=lambda n: x[n])
>>> params.a = 1
>>> params.b = 2
>>> params.a
1
>>> params.b
2
Source code in pyoframe/util.py
NamedVariableMapper(cls)
Maps variables to a string representation using the object's name and dimensions.
Examples:
>>> import polars as pl
>>> m = pf.Model()
>>> m.foo = pf.Variable(pl.DataFrame({"t": range(4)}))
>>> pf.sum(m.foo)
<Expression size=1 dimensions={} terms=4>
foo[0] + foo[1] + foo[2] + foo[3]
Source code in pyoframe/util.py
cast_coef_to_string(df, column_name=COEF_KEY, drop_ones=True)
Converts column column_name
of the dataframe df
to a string. Rounds to Config.print_float_precision
decimal places if not None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The input DataFrame. |
required |
column_name
|
str
|
The name of the column to be casted. |
COEF_KEY
|
drop_ones
|
bool
|
If True, 1s are replaced with an empty string for non-constant terms. |
True
|
Examples:
>>> import polars as pl
>>> df = pl.DataFrame({"x": [1.0, -2.0, 1.0, 4.0], VAR_KEY: [1, 2, 0, 4]})
>>> cast_coef_to_string(df, "x")
shape: (4, 2)
┌─────┬───────────────┐
│ x ┆ __variable_id │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═══════════════╡
│ + ┆ 1 │
│ -2 ┆ 2 │
│ +1 ┆ 0 │
│ +4 ┆ 4 │
└─────┴───────────────┘
Source code in pyoframe/util.py
concat_dimensions(df, prefix=None, keep_dims=True, ignore_columns=RESERVED_COL_KEYS, replace_spaces=True, to_col='concated_dim')
Returns a new DataFrame with the column 'concated_dim'. Reserved columns are ignored.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The input DataFrame. |
required |
prefix
|
Optional[str]
|
The prefix to be added to the concated dimension. |
None
|
keep_dims
|
bool
|
If True, the original dimensions are kept in the new DataFrame. |
True
|
replace_spaces
|
bool, optional If True, replaces spaces with underscores. |
True
|
Examples:
>>> import polars as pl
>>> df = pl.DataFrame(
... {
... "dim1": [1, 2, 3, 1, 2, 3],
... "dim2": ["Y", "Y", "Y", "N", "N", "N"],
... }
... )
>>> concat_dimensions(df)
shape: (6, 3)
┌──────┬──────┬──────────────┐
│ dim1 ┆ dim2 ┆ concated_dim │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════╪══════════════╡
│ 1 ┆ Y ┆ [1,Y] │
│ 2 ┆ Y ┆ [2,Y] │
│ 3 ┆ Y ┆ [3,Y] │
│ 1 ┆ N ┆ [1,N] │
│ 2 ┆ N ┆ [2,N] │
│ 3 ┆ N ┆ [3,N] │
└──────┴──────┴──────────────┘
>>> concat_dimensions(df, prefix="x")
shape: (6, 3)
┌──────┬──────┬──────────────┐
│ dim1 ┆ dim2 ┆ concated_dim │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════╪══════════════╡
│ 1 ┆ Y ┆ x[1,Y] │
│ 2 ┆ Y ┆ x[2,Y] │
│ 3 ┆ Y ┆ x[3,Y] │
│ 1 ┆ N ┆ x[1,N] │
│ 2 ┆ N ┆ x[2,N] │
│ 3 ┆ N ┆ x[3,N] │
└──────┴──────┴──────────────┘
>>> concat_dimensions(df, keep_dims=False)
shape: (6, 1)
┌──────────────┐
│ concated_dim │
│ --- │
│ str │
╞══════════════╡
│ [1,Y] │
│ [2,Y] │
│ [3,Y] │
│ [1,N] │
│ [2,N] │
│ [3,N] │
└──────────────┘
>>> # Properly handles cases with no dimensions and ignores reserved columns
>>> df = pl.DataFrame({VAR_KEY: [1, 2]})
>>> concat_dimensions(df, prefix="x")
shape: (2, 2)
┌───────────────┬──────────────┐
│ __variable_id ┆ concated_dim │
│ --- ┆ --- │
│ i64 ┆ str │
╞═══════════════╪══════════════╡
│ 1 ┆ x │
│ 2 ┆ x │
└───────────────┴──────────────┘
Source code in pyoframe/util.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
dataframe_to_tupled_list(df, num_max_elements=None)
Converts a dataframe into a list of tuples. Used to print a Set to the console. See examples for behaviour.
Examples:
>>> df = pl.DataFrame({"x": [1, 2, 3, 4, 5]})
>>> dataframe_to_tupled_list(df)
'[1, 2, 3, 4, 5]'
>>> dataframe_to_tupled_list(df, 3)
'[1, 2, 3, ...]'
>>> df = pl.DataFrame({"x": [1, 2, 3, 4, 5], "y": [2, 3, 4, 5, 6]})
>>> dataframe_to_tupled_list(df, 3)
'[(1, 2), (2, 3), (3, 4), ...]'
Source code in pyoframe/util.py
for_solvers(*solvers)
Decorator that limits the function to only be called when the solver is in the only
list.
Source code in pyoframe/util.py
get_obj_repr(obj, _props=(), **kwargs)
Helper function to generate repr strings for classes. See usage for examples.
Source code in pyoframe/util.py
parse_inputs_as_iterable(*inputs)
Converts a parameter *x: Any | Iteraable[Any] to a single Iterable[Any] object. This is helpful to support these two ways of passing arguments: - foo([1, 2, 3]) - foo(1, 2, 3)
Inspired from the polars library.
Source code in pyoframe/util.py
unwrap_single_values(func)
Decorator for functions that return DataFrames. Returned dataframes with a single value will instead return the value.