API Reference¶
ETHOS.TSAM's public API. Most users only need aggregate —
the single entry point — plus the configuration objects and the result it
returns. The pipeline internals are here for completeness.
| Topic | Contents |
|---|---|
| Configuration | ClusterConfig, SegmentConfig, ExtremeConfig, Distribution, MinMaxMean |
| Results | AggregationResult, AccuracyMetrics, ConcurrencyMetrics, ClusteringResult |
| Pipeline internals | run_pipeline, the four phases, and the stage functions |
| Tuning | Hyperparameter sweeps over aggregation settings |
| Utilities | Options, weights, plotting, low-level aggregation helpers |
Aggregation¶
tsam.api
¶
New simplified API for tsam aggregation.
Functions:
| Name | Description |
|---|---|
aggregate |
Aggregate time series data into typical periods. |
unstack_to_periods |
Reshape time series data into period structure for visualization. |
aggregate
¶
aggregate(
data: DataFrame,
n_clusters: int,
*,
period_duration: int | float | str = 24,
temporal_resolution: float | str | None = None,
cluster: ClusterConfig | None = None,
segments: SegmentConfig | None = None,
extremes: ExtremeConfig | None = None,
weights: dict[str, float] | None = None,
preserve_column_means: bool = True,
rescale_exclude_columns: list[str] | None = None,
round_decimals: int | None = None,
numerical_tolerance: float = 1e-13,
) -> AggregationResult
Aggregate time series data into typical periods.
This function reduces a time series dataset to a smaller set of representative "typical periods" using clustering algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input time series data with a datetime index. Each column represents a different variable (e.g., solar, wind, demand). The index should be a DatetimeIndex with regular intervals. |
required |
n_clusters
|
int
|
Number of clusters (typical periods) to create. Higher values mean more accuracy but less data reduction. Typical range: 4-20 for energy system models. |
required |
period_duration
|
int | float | str
|
Length of each period. Accepts an int/float in hours (e.g., 24 for daily, 168 for weekly) or a pandas Timedelta string (e.g., '24h', '1d', '1w'). |
24
|
temporal_resolution
|
float | str | None
|
Time resolution of input data. Accepts a float in hours (e.g., 1.0 for hourly, 0.25 for 15-minute) or a pandas Timedelta string (e.g., '1h', '15min', '30min'). If not provided, inferred from the datetime index. |
None
|
cluster
|
ClusterConfig | None
|
Clustering configuration. If not provided, uses defaults: method "hierarchical" and representation "medoid". |
None
|
segments
|
SegmentConfig | None
|
Segmentation configuration for reducing temporal resolution within periods. If not provided, no segmentation is applied. |
None
|
extremes
|
ExtremeConfig | None
|
Configuration for preserving extreme periods. If not provided, no extreme period handling is applied. |
None
|
weights
|
dict[str, float] | None
|
Per-column weights that influence all pipeline stages
(clustering, segmentation, representation, rescaling). Higher weight
means more influence on distance calculations, e.g.
|
None
|
preserve_column_means
|
bool
|
Rescale typical periods so each column's weighted mean matches the original data's mean. Ensures total energy/load is preserved when weights represent occurrence counts. |
True
|
rescale_exclude_columns
|
list[str] | None
|
Column names to exclude from rescaling when
|
None
|
round_decimals
|
int | None
|
Round output values to this many decimal places. If not provided, no rounding is applied. |
None
|
numerical_tolerance
|
float
|
Tolerance for numerical precision issues. Controls when warnings are raised for aggregated values exceeding the original time series bounds. Increase this value to silence warnings caused by floating-point precision errors. |
1e-13
|
Returns:
| Type | Description |
|---|---|
AggregationResult
|
An object containing the aggregated periods |
AggregationResult
|
( |
AggregationResult
|
period, the occurrence count per cluster ( |
AggregationResult
|
metrics (RMSE, MAE), and a |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input data is invalid or parameters are inconsistent. |
TypeError
|
If parameter types are incorrect. |
Examples:
Basic usage with defaults:
>>> import tsam
>>> result = tsam.aggregate(df, n_clusters=8)
>>> typical = result.cluster_representatives
With custom clustering:
>>> from tsam import aggregate, ClusterConfig
>>> result = aggregate(
... df,
... n_clusters=8,
... cluster=ClusterConfig(method="kmeans", representation="mean"),
... )
With segmentation (reduce to 12 timesteps per period):
>>> from tsam import aggregate, SegmentConfig
>>> result = aggregate(
... df,
... n_clusters=8,
... segments=SegmentConfig(n_segments=12),
... )
Preserving peak demand periods:
>>> from tsam import aggregate, ExtremeConfig
>>> result = aggregate(
... df,
... n_clusters=8,
... extremes=ExtremeConfig(max_value=["demand"]),
... )
Transferring assignments to new data:
Note
See ClusterConfig, SegmentConfig, ExtremeConfig, and
AggregationResult for related configuration and result objects.
Source code in src/tsam/api.py
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 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 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 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 | |
unstack_to_periods
¶
Reshape time series data into period structure for visualization.
Transforms a flat time series into a DataFrame with periods as rows and timesteps as a MultiIndex level, suitable for creating heatmaps with plotly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Time series data with datetime index. |
required |
period_duration
|
int | float | str
|
Length of each period. Accepts an int/float in hours (e.g., 24 for daily, 168 for weekly) or a pandas Timedelta string (e.g., '24h', '1d', '1w'). |
24
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Reshaped data with shape (n_periods, n_timesteps_per_period) for each |
DataFrame
|
column. Suitable for |
DataFrame
|
heatmaps. |
Examples:
>>> import tsam
>>> import plotly.express as px
>>>
>>> # Reshape data for heatmap visualization
>>> unstacked = tsam.unstack_to_periods(df, period_duration=24)
>>>
>>> # Create heatmap with plotly
>>> px.imshow(
... unstacked["Load"].values.T,
... labels={"x": "Day", "y": "Hour", "color": "Load"},
... title="Load Heatmap"
... )