Skip to main content

tsam_xarray

PyPI Python CI codecov License: MIT Docs

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).

Multi-dimensional input data

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:

Per-column RMSE across all dimensions

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsam_xarray-0.6.1.tar.gz (562.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tsam_xarray-0.6.1-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file tsam_xarray-0.6.1.tar.gz.

File metadata

  • Download URL: tsam_xarray-0.6.1.tar.gz
  • Upload date:
  • Size: 562.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tsam_xarray-0.6.1.tar.gz
Algorithm Hash digest
SHA256 a07631f8d4c3e8a4f093fd5fb3c963915ce1e8fe57dfed712ba3e0f34f2fd6cb
MD5 1b5448101c9e14065be7c5263289e721
BLAKE2b-256 867a1e192d88e3bd04a9ddf7e58e7c0df085e7fb8c77961412836316d9f4689b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tsam_xarray-0.6.1.tar.gz:

Publisher: publish.yaml on FBumann/tsam_xarray

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tsam_xarray-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: tsam_xarray-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tsam_xarray-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f789a0c1f9221dd81efc3ed2e3f9c15a75d3e7f3688a920c150127bcc55d8aa5
MD5 d156961e1a359284253814014394efe8
BLAKE2b-256 8d8fb27372fbad7a74cf4218c405b56ec484c56c3bee1c77a5318be60cdf8d1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tsam_xarray-0.6.1-py3-none-any.whl:

Publisher: publish.yaml on FBumann/tsam_xarray

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.6.7

2 files

0.6.6

2 files

0.6.5

2 files

0.6.4

2 files

0.6.3

2 files

0.6.2

2 files

This release

0.6.1 This release

2 files

0.6.0

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page