tsam.result¶
tsam.result
¶
Result classes for tsam aggregation.
AccuracyMetrics
dataclass
¶
Accuracy metrics comparing aggregated to original time series.
Attributes:
| Name | Type | Description |
|---|---|---|
rmse |
Series
|
Root Mean Square Error per column, comparing the original and reconstructed time series point-by-point over time. |
mae |
Series
|
Mean Absolute Error per column, comparing the original and reconstructed time series point-by-point over time. |
rmse_duration |
Series
|
RMSE on duration curves per column. Duration curves are created by sorting values in descending order, so this metric captures how well the aggregation preserves the overall value distribution regardless of temporal ordering. |
rescale_deviations |
DataFrame
|
Rescaling deviation information per column. Contains columns: - deviation_pct: Final deviation percentage after rescaling - converged: Whether rescaling converged within max iterations - iterations: Number of iterations used Only populated if rescaling was enabled, otherwise empty DataFrame. |
weighted_rmse |
float
|
Weighted root-mean-square of per-column RMSE values:
|
weighted_mae |
float
|
Weighted arithmetic mean of per-column MAE values:
|
weighted_rmse_duration |
float
|
Weighted root-mean-square of per-column duration-curve RMSE values:
|
Source code in src/tsam/result.py
summary
property
¶
Summary DataFrame with all metrics per column.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: rmse, mae, rmse_duration, and deviation_pct (if rescaling was enabled). Index is the original column names. |
AggregationResult
dataclass
¶
Result of time series aggregation.
This class holds all outputs from the aggregation process and provides convenient methods for accessing and exporting the results.
Attributes:
| Name | Type | Description |
|---|---|---|
cluster_representatives |
DataFrame
|
The aggregated typical periods with MultiIndex (cluster, timestep). Each row represents one timestep in one cluster representative. |
cluster_assignments |
ndarray
|
Which cluster each original period belongs to. Length equals the number of original periods. Values are cluster indices (0 to n_clusters-1). |
cluster_weights |
dict[int, int]
|
How many original periods each cluster represents. Keys are cluster indices, values are occurrence counts. |
n_clusters |
int
|
Number of clusters (typical periods). |
n_timesteps_per_period |
int
|
Number of timesteps in each period. |
n_segments |
int | None
|
Number of segments per period if segmentation was used, else None. |
segment_durations |
tuple[tuple[int, ...], ...] | None
|
Duration (in timesteps) for each segment in each typical period. Outer tuple has one entry per typical period, inner tuple has duration for each segment. Use for transferring to another aggregation. |
accuracy |
AccuracyMetrics
|
Accuracy metrics comparing reconstructed to original data. |
clustering_duration |
float
|
Time taken for clustering in seconds. |
is_transferred |
bool
|
Whether this result was created by applying a transferred clustering
(via |
Examples:
>>> result = tsam.aggregate(df, n_clusters=8)
>>> result.cluster_representatives
solar wind demand
cluster timestep
0 0 0.12 0.45 0.78
1 0.15 0.42 0.82
...
Source code in src/tsam/result.py
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 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 219 220 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 297 298 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 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 | |
n_clusters
cached
property
¶
Number of clusters (typical periods).
Derived from the cluster_representatives DataFrame index, which is the authoritative source. Note: cluster_weights may have more entries than actual cluster IDs due to tsam quirks.
n_segments
cached
property
¶
Number of segments per period if segmentation was used, else None.
cluster_assignments
cached
property
¶
Which cluster each original period belongs to.
Length equals the number of original periods. Values are cluster indices (0 to n_clusters-1).
original
cached
property
¶
reconstructed
cached
property
¶
Reconstructed time series from typical periods.
Each original period is replaced by its assigned cluster representative. This is cached for performance since reconstruction can be expensive.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Reconstructed time series with same shape as original. |
Examples:
residuals
cached
property
¶
Residuals (original - reconstructed).
Positive values indicate the original exceeded the reconstruction.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Residual time series with same shape as original. |
Examples:
timestep_index
property
¶
Get the timestep or segment indices.
Returns:
| Type | Description |
|---|---|
list[int]
|
List of indices [0, 1, ..., n-1] where n is n_segments if segmentation was used, otherwise n_timesteps_per_period. |
period_index
property
¶
Get the period (cluster) indices.
Returns the actual cluster IDs from the cluster_representatives DataFrame, which is the authoritative source.
Returns:
| Type | Description |
|---|---|
list[int]
|
Sorted list of cluster indices present in cluster_representatives. |
assignments
property
¶
Get timestep-level assignment information.
Returns a DataFrame with one row per original timestep containing assignment information for transferring results to another aggregation.
Columns
period_idx : int Index of the original period (0-indexed, 0 to n_original_periods-1). timestep_idx : int Timestep index within the period (0 to n_timesteps_per_period-1). cluster_idx : int Which cluster this period is assigned to (0 to n_clusters-1). segment_idx : int (only if segmentation was used) Which segment this timestep belongs to within its period.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame indexed by original time index with assignment columns. |
Examples:
plot
property
¶
Access plotting methods.
Returns a plotting accessor with methods for visualizing the results.
Returns:
| Type | Description |
|---|---|
ResultPlotAccessor
|
Accessor with plotting methods. |
Examples:
>>> result = tsam.aggregate(df, n_clusters=8)
>>> result.plot.compare() # Compare original vs reconstructed
>>> result.plot.residuals() # View reconstruction errors
>>> result.plot.cluster_representatives()
>>> result.plot.cluster_members() # All periods per cluster
>>> result.plot.cluster_weights()
>>> result.plot.accuracy()
disaggregate
¶
Expand typical-period data back to the original time series length.
Each original period is replaced by its assigned cluster representative
from data. The result uses the original datetime index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Typical-period data matching
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Disaggregated data with the original datetime index. For segmented input, non-segment-start timesteps are NaN. |
Examples:
>>> result = tsam.aggregate(df, n_clusters=8)
>>> optimized = run_optimization(result.cluster_representatives)
>>> full_year = result.disaggregate(optimized)
Source code in src/tsam/result.py
to_dict
¶
Export results as a dictionary for serialization.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary containing all result data in serializable format. |