Lightweight xarray wrapper for tsam time series aggregation
Project description
tsam_xarray
DataArray in, DataArray out — multi-dimensional time series aggregation with tsam and xarray.
The problem
Energy system data is multi-dimensional — variables, regions, scenarios, years. Some dimensions should be clustered together (solar and wind profiles in the same region should see the same typical days), while others need independent clustering (each scenario has its own weather patterns).
tsam works on flat DataFrames. With multi-dimensional data, you end up writing boilerplate: loop over scenarios, convert to DataFrame, aggregate, extract results, convert back, concatenate, hope the dims line up. Accuracy metrics come back as unlabeled pd.Series. Saving a clustering means managing raw dicts.
The solution
import tsam_xarray
result = tsam_xarray.aggregate(
da, # (time, variable, region, scenario)
time_dim="time",
cluster_dim=["variable", "region"], # clustered together
n_clusters=4,
)
# scenario is sliced independently — each gets its own clustering
Everything comes back as labeled xarray objects:
result.cluster_representatives # (scenario, cluster, timestep, variable, region)
result.reconstructed # same shape as input
result.cluster_assignments # (scenario, period)
Accuracy metrics preserve all dimensions — see exactly where the approximation is good or bad:
result.accuracy.rmse # DataArray (scenario, variable, region)
result.accuracy.weighted_rmse # DataArray (scenario,) — per-slice summary
Save, load, reuse
# Save clustering (not the data — just the mapping)
result.clustering.to_json("clustering.json")
# Load and inspect — no original data needed
clustering = tsam_xarray.load_clustering("clustering.json")
clustering.n_clusters # 4
clustering.cluster_assignments # DataArray (scenario, period)
clustering.cluster_occurrences # DataArray (scenario, cluster)
# Apply to new data or disaggregate optimization results
new_result = clustering.apply(new_da)
full_timeseries = clustering.disaggregate(optimized_data)
Tuning
Find optimal hyperparameters across all slices:
grid = tsam_xarray.grid_search(
da,
time_dim="time",
cluster_dim=["variable", "region"],
timesteps=np.geomspace(2, 48, num=12, dtype=int), # sparse search
)
grid.summary_matrix["rmse"] # heatmap-ready (n_clusters, n_segments)
grid.accuracy["weighted_rmse"] # per-slice weighted RMSE for every config
Installation
pip install tsam-xarray
Documentation
Full docs with interactive examples: tsam-xarray.readthedocs.io
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tsam_xarray-0.5.2.tar.gz.
File metadata
- Download URL: tsam_xarray-0.5.2.tar.gz
- Upload date:
- Size: 559.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
309072188a43589fbebe8e3105abd33b51d031e3f1eb1b6792aff0b878edf3a4
|
|
| MD5 |
37619f94aff217fa33db2c6e390ed436
|
|
| BLAKE2b-256 |
f9c60b550c879315ad7beae28e56a1652d56ddddb694eb82943a2b03d9cb34a0
|
Provenance
The following attestation bundles were made for tsam_xarray-0.5.2.tar.gz:
Publisher:
publish.yaml on FBumann/tsam_xarray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tsam_xarray-0.5.2.tar.gz -
Subject digest:
309072188a43589fbebe8e3105abd33b51d031e3f1eb1b6792aff0b878edf3a4 - Sigstore transparency entry: 1205917329
- Sigstore integration time:
-
Permalink:
FBumann/tsam_xarray@9064784d098358186a38a925a4c54a7abb6ef9ae -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/FBumann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9064784d098358186a38a925a4c54a7abb6ef9ae -
Trigger Event:
push
-
Statement type:
File details
Details for the file tsam_xarray-0.5.2-py3-none-any.whl.
File metadata
- Download URL: tsam_xarray-0.5.2-py3-none-any.whl
- Upload date:
- Size: 24.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e240109a5599e685185624dfeb5fe20a5a18aca9262f218d6ccc40ff6476752
|
|
| MD5 |
c3693a235ae3838a684e1001b7f51a32
|
|
| BLAKE2b-256 |
c3e18874537fe42936a168ba43c48b8875e85ab7b72d39bd87ff671ef93cb4b7
|
Provenance
The following attestation bundles were made for tsam_xarray-0.5.2-py3-none-any.whl:
Publisher:
publish.yaml on FBumann/tsam_xarray
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tsam_xarray-0.5.2-py3-none-any.whl -
Subject digest:
2e240109a5599e685185624dfeb5fe20a5a18aca9262f218d6ccc40ff6476752 - Sigstore transparency entry: 1205917343
- Sigstore integration time:
-
Permalink:
FBumann/tsam_xarray@9064784d098358186a38a925a4c54a7abb6ef9ae -
Branch / Tag:
refs/tags/v0.5.2 - Owner: https://github.com/FBumann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@9064784d098358186a38a925a4c54a7abb6ef9ae -
Trigger Event:
push
-
Statement type: