tsam.config¶
tsam.config
¶
Configuration classes for tsam aggregation.
Distribution
dataclass
¶
Representation that preserves the value distribution (duration curve).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
'cluster' or 'global'
|
"cluster": preserve each cluster's distribution separately "global": preserve the overall time series distribution |
"cluster"
|
preserve_minmax
|
bool
|
If True, also preserves min/max values per timestep (equivalent to old "distribution_minmax"). |
False
|
Source code in src/tsam/config.py
MinMaxMean
dataclass
¶
Representation combining min, max, and mean per column.
Columns not listed in max_columns or min_columns default to mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_columns
|
list[str]
|
Columns represented by their maximum value across cluster members. |
list()
|
min_columns
|
list[str]
|
Columns represented by their minimum value across cluster members. |
list()
|
Source code in src/tsam/config.py
ClusterConfig
dataclass
¶
Configuration for the clustering algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
Clustering algorithm to use: - "averaging": Sequential averaging of periods - "kmeans": K-means clustering (fast, uses centroids) - "kmedoids": K-medoids using MILP optimization (uses actual periods) - "kmaxoids": K-maxoids (selects most dissimilar periods) - "hierarchical": Agglomerative hierarchical clustering - "contiguous": Hierarchical with temporal contiguity constraint |
"hierarchical"
|
representation
|
str, Distribution, or MinMaxMean
|
How to represent cluster centers. Accepts either a string shortcut or a typed representation object for additional options: String shortcuts: - "mean": Centroid (average of cluster members) - "medoid": Actual period closest to centroid - "maxoid": Actual period most dissimilar to others - "distribution": Preserve value distribution (duration curve) - "distribution_minmax": Distribution + preserve min/max values - "minmax_mean": Combine min/max/mean per timestep Typed objects (for additional options):
- Default depends on method: - "mean" for averaging, kmeans - "medoid" for kmedoids, hierarchical, contiguous - "maxoid" for kmaxoids |
None
|
weights
|
dict[str, float]
|
.. deprecated::
Pass |
None
|
normalize_column_means
|
bool
|
Normalize all columns to the same mean before clustering. Useful when columns have very different scales. |
False
|
use_duration_curves
|
bool
|
Sort values within each period before clustering. Matches periods by their value distribution rather than timing. |
False
|
include_period_sums
|
bool
|
Include period totals as additional features for clustering. Helps preserve total energy/load values. |
False
|
solver
|
str
|
MILP solver for kmedoids method. Options: "highs" (default, open source), "cbc", "gurobi", "cplex" |
"highs"
|
Source code in src/tsam/config.py
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 | |
get_representation
¶
Get the representation, using default if not specified.
Source code in src/tsam/config.py
to_dict
¶
Convert to dictionary for JSON serialization.
Source code in src/tsam/config.py
from_dict
classmethod
¶
Create from dictionary (e.g., loaded from JSON).
Source code in src/tsam/config.py
SegmentConfig
dataclass
¶
Configuration for temporal segmentation within periods.
Segmentation reduces the temporal resolution within each typical period, grouping consecutive timesteps into segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_segments
|
int
|
Number of segments per period. Must be less than or equal to the number of timesteps per period. Example: period_duration=24 with hourly data has 24 timesteps, so n_segments could be 1-24. |
required |
representation
|
str, Distribution, or MinMaxMean
|
How to represent each segment:
- "mean": Average value of timesteps in segment
- "medoid": Actual timestep closest to segment mean
- "distribution": Preserve distribution within segment
- |
"mean"
|
Source code in src/tsam/config.py
ClusteringResult
dataclass
¶
Clustering assignments that can be saved/loaded and applied to new data.
This class bundles all clustering and segmentation assignments from an aggregation, enabling: - Simple IO via to_json()/from_json() - Applying the same clustering to different datasets via apply() - Preserving the parameters used to create the clustering
Get this from result.clustering after running an aggregation.
Transfer Fields (used by apply())¶
period_duration : float Length of each period in hours (e.g., 24 for daily periods).
cluster_assignments : tuple[int, ...] Cluster assignments for each original period. Length equals the number of original periods in the data.
n_timesteps_per_period : int Number of timesteps in each period. Used to validate that new data has compatible structure when calling apply().
cluster_centers : tuple[int, ...], optional Indices of original periods used as cluster centers. If not provided, centers will be recalculated when applying.
segment_assignments : tuple[tuple[int, ...], ...], optional Segment assignments per timestep, per typical period. Only present if segmentation was used.
segment_durations : tuple[tuple[int, ...], ...], optional Duration (in timesteps) per segment, per typical period. Required if segment_assignments is present.
segment_centers : tuple[tuple[int, ...], ...], optional Indices of timesteps used as segment centers, per typical period. Required for fully deterministic segment replication.
preserve_column_means : bool, default True Whether to rescale typical periods to match original data means.
rescale_exclude_columns : tuple[str, ...], optional Column names to exclude from rescaling. Useful for binary columns.
representation : str, default "medoid" How to compute typical periods from cluster members.
segment_representation : str, optional How to compute segment values. Only used if segmentation is present.
temporal_resolution : float, optional Time resolution of input data in hours. If not provided, inferred.
Reference Fields (for documentation, not used by apply())¶
cluster_config : ClusterConfig, optional Clustering configuration used to create this result.
segment_config : SegmentConfig, optional Segmentation configuration used to create this result.
extremes_config : ExtremeConfig, optional Extreme period configuration used to create this result.
Examples¶
Get clustering from a result¶
result = tsam.aggregate(df_wind, n_clusters=8) clustering = result.clustering
Save to file¶
clustering.to_json("clustering.json")
Load from file¶
clustering = ClusteringResult.from_json("clustering.json")
Apply to new data¶
result2 = clustering.apply(df_all)
Source code in src/tsam/config.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | |
n_original_periods
property
¶
Number of original periods in the source data.
n_segments
property
¶
Number of segments per period, or None if no segmentation.
to_dataframe
¶
Convert to a readable DataFrame.
Returns a DataFrame with one row per original period showing cluster assignments.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with cluster_assignments indexed by original period. |
Source code in src/tsam/config.py
segment_dataframe
¶
Get segment structure as a readable DataFrame.
Returns a DataFrame showing segment durations per typical period. Returns None if no segmentation is defined.
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
DataFrame with typical periods as rows and segments as columns, values are segment durations in timesteps. |
Source code in src/tsam/config.py
to_dict
¶
Convert to dictionary for JSON serialization.
Source code in src/tsam/config.py
from_dict
classmethod
¶
Create from dictionary (e.g., loaded from JSON).
Source code in src/tsam/config.py
to_json
¶
Save clustering result to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to save to. |
required |
Notes
If the clustering used the 'replace' extreme method, a warning will be
issued because the saved clustering cannot be perfectly reproduced when
loaded and applied later. See :meth:apply for details.
Examples:
Source code in src/tsam/config.py
from_json
classmethod
¶
Load clustering result from a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to load from. |
required |
Returns:
| Type | Description |
|---|---|
ClusteringResult
|
Loaded clustering result. |
Examples:
>>> clustering = ClusteringResult.from_json("clustering.json")
>>> result = clustering.apply(new_data)
Source code in src/tsam/config.py
disaggregate
¶
Expand typical-period data back to the original time series length.
Each original period is replaced by its assigned cluster representative
from data. For segmented data, segments are first expanded back to
full timesteps using the stored segment durations, then periods are
mapped back using cluster assignments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Typical-period data with one of:
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
Disaggregated data with integer-indexed rows
(one row per original timestep). For segmented input,
non-segment-start timesteps are NaN — use |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the index structure, cluster IDs, or number of timesteps/segments do not match this clustering. |
Examples:
>>> clustering = ClusteringResult.from_json("clustering.json")
>>> result = clustering.apply(df)
>>> optimized = run_optimization(result.cluster_representatives)
>>> full_year = clustering.disaggregate(optimized)
Source code in src/tsam/config.py
apply
¶
apply(
data: DataFrame,
*,
temporal_resolution: float | None = None,
round_decimals: int | None = None,
numerical_tolerance: float = 1e-13,
) -> AggregationResult
Apply this clustering to new data.
Uses the stored cluster assignments and transfer fields to aggregate a different dataset with the same clustering structure deterministically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Input time series data with a datetime index. Must have the same number of periods as the original data. |
required |
temporal_resolution
|
float
|
Time resolution of input data in hours. If not provided, uses stored temporal_resolution or infers from data index. |
None
|
round_decimals
|
int
|
Round output values to this many decimal places. |
None
|
numerical_tolerance
|
float
|
Tolerance for numerical precision issues. |
1e-13
|
Returns:
| Type | Description |
|---|---|
AggregationResult
|
Aggregation result using this clustering. |
Notes
Extreme period transfer limitations:
The 'replace' extreme method creates a hybrid cluster representation where some columns use the medoid values and others use the extreme period values. This hybrid representation cannot be perfectly reproduced during transfer. When applying a clustering that used 'replace', a warning will be issued and the transferred result will use the medoid representation for all columns.
For exact transfer with extreme periods, use 'append' or 'new_cluster' extreme methods instead.
Examples:
>>> # Cluster on wind data, apply to full dataset
>>> result_wind = tsam.aggregate(df_wind, n_clusters=8)
>>> result_all = result_wind.clustering.apply(df_all)
>>> # Load saved clustering and apply
>>> clustering = ClusteringResult.from_json("clustering.json")
>>> result = clustering.apply(df)
Source code in src/tsam/config.py
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | |
ExtremeConfig
dataclass
¶
Configuration for preserving extreme periods.
Extreme periods contain critical peak values that must be preserved in the aggregated representation (e.g., peak demand for capacity sizing).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
How to handle extreme periods: - "append": Add extreme periods as additional cluster centers - "replace": Replace the nearest cluster center with the extreme - "new_cluster": Add as new cluster and reassign affected periods |
"append"
|
max_value
|
list[str]
|
Column names where the maximum value should be preserved. The entire period containing that single extreme value becomes an extreme period. Example: ["electricity_demand"] to preserve peak demand hour. |
list()
|
min_value
|
list[str]
|
Column names where the minimum value should be preserved. Example: ["temperature"] to preserve coldest hour. |
list()
|
max_period
|
list[str]
|
Column names where the period with maximum total should be preserved. Example: ["solar_generation"] to preserve highest solar day. |
list()
|
min_period
|
list[str]
|
Column names where the period with minimum total should be preserved. Example: ["wind_generation"] to preserve lowest wind day. |
list()
|
Source code in src/tsam/config.py
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 | |
has_extremes
¶
to_dict
¶
Convert to dictionary for JSON serialization.
Source code in src/tsam/config.py
from_dict
classmethod
¶
Create from dictionary (e.g., loaded from JSON).