Utils¶
uncertainty_flow.utils
¶
Utilities for uncertainty_flow.
CalibrationError
¶
Bases: UncertaintyFlowError
Base class for calibration-related errors.
Source code in uncertainty_flow/utils/exceptions.py
39 40 | |
CalibrationSizeError
¶
Bases: CalibrationError
Raised when calibration set is too small.
Source code in uncertainty_flow/utils/exceptions.py
43 44 45 46 47 48 49 50 | |
ConfigurationError
¶
Bases: UncertaintyFlowError
Base class for configuration-related errors.
Source code in uncertainty_flow/utils/exceptions.py
53 54 | |
DataError
¶
Bases: UncertaintyFlowError
Base class for data-related errors.
Source code in uncertainty_flow/utils/exceptions.py
28 29 | |
InvalidDataError
¶
Bases: DataError
Raised when input data is invalid.
Source code in uncertainty_flow/utils/exceptions.py
32 33 34 35 36 | |
ModelError
¶
Bases: UncertaintyFlowError
Base class for model-related errors.
Source code in uncertainty_flow/utils/exceptions.py
14 15 | |
ModelNotFittedError
¶
Bases: ModelError
Raised when a model method is called before fitting.
Source code in uncertainty_flow/utils/exceptions.py
18 19 20 21 22 23 24 25 | |
QuantileError
¶
Bases: ConfigurationError
Raised when quantile configuration is invalid.
Source code in uncertainty_flow/utils/exceptions.py
57 58 59 60 61 62 63 64 | |
UncertaintyFlowError
¶
Bases: ValueError
Base error class for uncertainty_flow.
Source code in uncertainty_flow/utils/exceptions.py
4 5 6 7 8 9 10 11 | |
UncertaintyFlowWarning
¶
Bases: UserWarning
Base warning class for uncertainty_flow.
Source code in uncertainty_flow/utils/exceptions.py
67 68 | |
BaseSplit
¶
Bases: ABC
Base class for calibration split strategies.
Source code in uncertainty_flow/utils/split.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
split(data, calibration_size)
abstractmethod
¶
Split data into (train, calibration) sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame |
required |
calibration_size
|
float
|
Fraction of data to use for calibration (0-1) |
required |
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, DataFrame]
|
Tuple of (train_data, calibration_data) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If calibration set would be too small (< 20 samples) |
Source code in uncertainty_flow/utils/split.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
RandomHoldoutSplit
¶
Bases: BaseSplit
Random holdout for tabular data.
Source code in uncertainty_flow/utils/split.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
__init__(random_state=None)
¶
Initialize random holdout splitter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
random_state
|
int | None
|
Random seed for reproducibility |
None
|
Source code in uncertainty_flow/utils/split.py
66 67 68 69 70 71 72 73 | |
split(data, calibration_size)
¶
Split data randomly into train and calibration sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame |
required |
calibration_size
|
float
|
Fraction for calibration (0-1) |
required |
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, DataFrame]
|
Tuple of (train, calibration) DataFrames |
Source code in uncertainty_flow/utils/split.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
RollingOriginSplit
¶
Expanding-window (rolling-origin) split for time series evaluation.
Each fold uses all data up to an origin point as training and the next
horizon rows as the test set. The origin advances by step rows
each fold, producing an expanding training window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_splits
|
int
|
Number of folds. |
5
|
min_train_size
|
int
|
Minimum number of rows in the first training window. |
50
|
horizon
|
int
|
Number of rows in each test set. |
1
|
gap
|
int
|
Number of rows between train end and test start (default 0). |
0
|
step
|
int | None
|
How far the origin advances per fold. Defaults to |
None
|
Source code in uncertainty_flow/utils/split.py
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
splits(data)
¶
Generate expanding-window (train, test) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame assumed to be in temporal order. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[DataFrame, DataFrame]]
|
List of (train_df, test_df) tuples. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is too short for the requested configuration. |
Source code in uncertainty_flow/utils/split.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
SlidingWindowSplit
¶
Fixed-width sliding-window split for time series evaluation.
Each fold uses a training window of fixed train_size rows that slides
forward by step rows each fold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_splits
|
int
|
Number of folds. |
5
|
train_size
|
int
|
Number of rows in each training window. |
100
|
horizon
|
int
|
Number of rows in each test set. |
1
|
gap
|
int
|
Number of rows between train end and test start (default 0). |
0
|
step
|
int | None
|
How far the window advances per fold. Defaults to |
None
|
Source code in uncertainty_flow/utils/split.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
splits(data)
¶
Generate fixed-window (train, test) pairs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame assumed to be in temporal order. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[DataFrame, DataFrame]]
|
List of (train_df, test_df) tuples. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is too short for the requested configuration. |
Source code in uncertainty_flow/utils/split.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
SplitPlanMetadata
dataclass
¶
Metadata describing how validation splits were selected.
Source code in uncertainty_flow/utils/split.py
327 328 329 330 331 332 333 334 335 336 337 338 | |
TemporalHoldoutSplit
¶
Bases: BaseSplit
Holdout from END for time series (no data leakage).
Source code in uncertainty_flow/utils/split.py
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 | |
split(data, calibration_size)
¶
Split data temporally, taking last n% for calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input DataFrame (assumed to be temporally ordered) |
required |
calibration_size
|
float
|
Fraction for calibration (0-1) |
required |
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, DataFrame]
|
Tuple of (train, calibration) DataFrames |
Source code in uncertainty_flow/utils/split.py
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 | |
ValidationSplitPlan
dataclass
¶
Composable split plan with required outer split and optional inner splits.
Source code in uncertainty_flow/utils/split.py
341 342 343 344 345 346 347 | |
to_numpy(data, columns)
¶
Convert Polars DataFrame or LazyFrame to NumPy array.
Raises:
| Type | Description |
|---|---|
InvalidDataError
|
If any column is missing from the data. |
Source code in uncertainty_flow/utils/polars_bridge.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
to_numpy_series(series)
¶
Convert Polars Series to NumPy array, zero-copy when possible.
Falls back to regular conversion if zero-copy isn't possible.
Raises:
| Type | Description |
|---|---|
InvalidDataError
|
If input is not a pl.Series. |
Source code in uncertainty_flow/utils/polars_bridge.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
to_polars(array, columns, index=None)
¶
Convert NumPy array back to Polars DataFrame.
Raises:
| Type | Description |
|---|---|
InvalidDataError
|
If array shape doesn't match columns length. |
Source code in uncertainty_flow/utils/polars_bridge.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
rolling_origin_splits(data, n_splits=5, min_train_size=50, horizon=1, gap=0)
¶
Convenience function for rolling-origin (expanding window) splits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame in temporal order. |
required |
n_splits
|
int
|
Number of folds. |
5
|
min_train_size
|
int
|
Minimum training rows in the first fold. |
50
|
horizon
|
int
|
Test set size per fold. |
1
|
gap
|
int
|
Rows between train end and test start. |
0
|
Returns:
| Type | Description |
|---|---|
list[tuple[DataFrame, DataFrame]]
|
List of (train_df, test_df) tuples. |
Source code in uncertainty_flow/utils/split.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
select_validation_plan(data, *, task_type, random_state=None, holdout_fraction=0.2, small_data_threshold=250, cv_splits=5, hybrid_mode=False, enable_logging=True, rolling_origin=False, rolling_min_train=50, rolling_horizon=1)
¶
Select a deterministic validation split plan for tuning/evaluation.
Hybrid mode means: - time_series: temporal outer split + random out-of-sample inner split(s) on outer-train - tabular: random outer split + random out-of-sample inner split(s) on outer-train
When rolling_origin=True and task_type="time_series", the outer
split uses a single temporal holdout (as before) and the inner splits
use :class:RollingOriginSplit instead of random K-fold.
Source code in uncertainty_flow/utils/split.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |