Skip to content

Commit

Permalink
Merge pull request #77 from fneum/performance
Browse files Browse the repository at this point in the history
Performance improvements for `_rescaleClusterPeriods`
  • Loading branch information
l-kotzur committed Aug 21, 2023
2 parents c0d5293 + 8cf3e65 commit f45fecc
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions tsam/timeseriesaggregation.py
Expand Up @@ -828,18 +828,19 @@ def _rescaleClusterPeriods(self, clusterOrder, clusterPeriods, extremeClusterIdx
series, without changing the values of the extremePeriods.
"""
weightingVec = pd.Series(self._clusterPeriodNoOccur).values
typicalPeriods = pd.DataFrame(
clusterPeriods, columns=self.normalizedPeriodlyProfiles.columns
)
typicalPeriods = pd.concat([
pd.Series(s, index=self.normalizedPeriodlyProfiles.columns)
for s in self.clusterPeriods
], axis=1).T
idx_wo_peak = np.delete(typicalPeriods.index, extremeClusterIdx)
for column in self.timeSeries.columns:
diff = 1
sum_raw = self.normalizedPeriodlyProfiles[column].sum().sum()
sum_peak = sum(
sum_peak = np.sum(
weightingVec[extremeClusterIdx]
* typicalPeriods[column].loc[extremeClusterIdx, :].sum(axis=1)
)
sum_clu_wo_peak = sum(
sum_clu_wo_peak = np.sum(
weightingVec[idx_wo_peak]
* typicalPeriods[column].loc[idx_wo_peak, :].sum(axis=1)
)
Expand Down Expand Up @@ -869,13 +870,12 @@ def _rescaleClusterPeriods(self, clusterOrder, clusterPeriods, extremeClusterIdx
)

# reset values higher than the upper sacle or less than zero
typicalPeriods[column][typicalPeriods[column] > scale_ub] = scale_ub
typicalPeriods[column][typicalPeriods[column] < 0.0] = 0.0
typicalPeriods[column].clip(lower=0, upper=scale_ub, inplace=True)

typicalPeriods[column] = typicalPeriods[column].fillna(0.0)
typicalPeriods[column].fillna(0.0, inplace=True)

# calc new sum and new diff to orig data
sum_clu_wo_peak = sum(
sum_clu_wo_peak = np.sum(
weightingVec[idx_wo_peak]
* typicalPeriods[column].loc[idx_wo_peak, :].sum(axis=1)
)
Expand Down Expand Up @@ -1066,9 +1066,10 @@ def createTypicalPeriods(self):
)

# put the clustered data in pandas format and scale back
self.normalizedTypicalPeriods = pd.DataFrame(
self.clusterPeriods, columns=self.normalizedPeriodlyProfiles.columns
).stack(level="TimeStep")
self.normalizedTypicalPeriods = pd.concat([
pd.Series(s, index=self.normalizedPeriodlyProfiles.columns)
for s in self.clusterPeriods
], axis=1).unstack("TimeStep").T

if self.segmentation:
from tsam.utils.segmentation import segmentation
Expand Down

0 comments on commit f45fecc

Please sign in to comment.