Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_hierarchical():
raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','testdata.csv'), index_col = 0)
orig_raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','results','testperiods_hierarchical.csv'), index_col = [0,1])
starttime = time.time()
aggregation = tsam.TimeSeriesAggregation(raw, noTypicalPeriods = 8, hoursPerPeriod = 24,
clusterMethod = 'hierarchical',
extremePeriodMethod = 'new_cluster_center',
addPeakMin = ['T'], addPeakMax = ['Load'] )
typPeriods = aggregation.createTypicalPeriods()
print('Clustering took ' + str(time.time() - starttime))
# sort the typical days in order to avoid error assertion due to different order
sortedDaysOrig = orig_raw.sum(axis=0,level=0).sort_values('GHI').index
sortedDaysTest = typPeriods.sum(axis=0,level=0).sort_values('GHI').index
# rearange their order
orig = orig_raw[typPeriods.columns].unstack().loc[sortedDaysOrig,:].stack()
test = typPeriods.unstack().loc[sortedDaysTest,:].stack()
def test_cluster_order():
raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','testdata.csv'), index_col = 0)
raw_wind = raw.loc[:, 'Wind'].to_frame()
orig_raw_predefClusterOrder = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','results','testperiods_predefClusterOrder.csv'), index_col = [0,1])
orig_raw_predefClusterOrderAndClusterCenters = pd.read_csv(os.path.join(os.path.dirname(__file__), '..', 'examples', 'results', 'testperiods_predefClusterOrderAndClusterCenters.csv'),index_col=[0, 1])
starttime = time.time()
aggregation_wind = tsam.TimeSeriesAggregation(raw_wind, noTypicalPeriods = 8, hoursPerPeriod = 24,
clusterMethod = 'hierarchical')
typPeriods_wind = aggregation_wind.createTypicalPeriods()
aggregation_predefClusterOrder = tsam.TimeSeriesAggregation(raw, noTypicalPeriods=8, hoursPerPeriod=24,
clusterMethod='hierarchical',
predefClusterOrder=aggregation_wind.clusterOrder)
typPeriods_predefClusterOrder = aggregation_predefClusterOrder.createTypicalPeriods()
aggregation_predefClusterOrderAndClusterCenters = tsam.TimeSeriesAggregation(raw,
noTypicalPeriods=8, hoursPerPeriod=24,
clusterMethod='hierarchical',
predefClusterOrder=aggregation_wind.clusterOrder,
predefClusterCenterIndices=aggregation_wind.clusterCenterIndices)
raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','testdata.csv'), index_col = 0)
raw_wind = raw.loc[:, 'Wind'].to_frame()
orig_raw_predefClusterOrder = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','results','testperiods_predefClusterOrder.csv'), index_col = [0,1])
orig_raw_predefClusterOrderAndClusterCenters = pd.read_csv(os.path.join(os.path.dirname(__file__), '..', 'examples', 'results', 'testperiods_predefClusterOrderAndClusterCenters.csv'),index_col=[0, 1])
starttime = time.time()
aggregation_wind = tsam.TimeSeriesAggregation(raw_wind, noTypicalPeriods = 8, hoursPerPeriod = 24,
clusterMethod = 'hierarchical')
typPeriods_wind = aggregation_wind.createTypicalPeriods()
aggregation_predefClusterOrder = tsam.TimeSeriesAggregation(raw, noTypicalPeriods=8, hoursPerPeriod=24,
clusterMethod='hierarchical',
predefClusterOrder=aggregation_wind.clusterOrder)
typPeriods_predefClusterOrder = aggregation_predefClusterOrder.createTypicalPeriods()
aggregation_predefClusterOrderAndClusterCenters = tsam.TimeSeriesAggregation(raw,
noTypicalPeriods=8, hoursPerPeriod=24,
clusterMethod='hierarchical',
predefClusterOrder=aggregation_wind.clusterOrder,
predefClusterCenterIndices=aggregation_wind.clusterCenterIndices)
typPeriods_predefClusterOrderAndClusterCenters = aggregation_predefClusterOrderAndClusterCenters.createTypicalPeriods()
print('Clustering took ' + str(time.time() - starttime))
def test_preprocess():
raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','testdata.csv'), index_col = 0)
raw_wind = raw.loc[:, 'Wind'].to_frame()
aggregation_wind = tsam.TimeSeriesAggregation(raw_wind, noTypicalPeriods = 8, hoursPerPeriod = 24,
clusterMethod = 'hierarchical')
aggregation_wind._preProcessTimeSeries()
test = aggregation_wind.normalizedPeriodlyProfiles
orig = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','results','preprocessed_wind.csv'), index_col = [0], header = [0,1])
np.testing.assert_array_almost_equal(test.values, orig.values,decimal=15)
def test_hierarchical():
raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','testdata.csv'), index_col = 0)
orig_raw = pd.read_csv(os.path.join(os.path.dirname(__file__),'..','examples','results','testperiods_kmedoids.csv'), index_col = [0,1])
starttime = time.time()
aggregation = tsam.TimeSeriesAggregation(raw, noTypicalPeriods = 8, hoursPerPeriod = 24*7,
clusterMethod = 'k_medoids', )
typPeriods = aggregation.createTypicalPeriods()
print('Clustering took ' + str(time.time() - starttime))
# sort the typical days in order to avoid error assertion due to different order
sortedDaysOrig = orig_raw.sum(axis=0,level=0).sort_values('GHI').index
sortedDaysTest = typPeriods.sum(axis=0,level=0).sort_values('GHI').index
# rearange their order
orig = orig_raw[typPeriods.columns].unstack().loc[sortedDaysOrig,:].stack()
test = typPeriods.unstack().loc[sortedDaysTest,:].stack()
np.testing.assert_array_almost_equal(orig.values, test.values,decimal=4)
import tsam.timeseriesaggregation as tsam
import pandas as pd
raw = pd.read_csv('testdata.csv', index_col=0)
aggregation = tsam.TimeSeriesAggregation(raw, noTypicalPeriods = 8,
hoursPerPeriod = 24,
clusterMethod = 'hierarchical')
df = aggregation.createTypicalPeriods()
weights = aggregation.clusterPeriodNoOccur
aggregation.clusterOrder
timesteps = [i for i in range(0, len(df.index.get_level_values('TimeStep')))]
print(aggregation.clusterCenterIndices)
# get all index for every hour that in day of the clusterCenterIndices
days = [d for d in raw.index.dayofyear
if d in aggregation.clusterCenterIndices]
# select the dates based on this
dates = raw.iloc[days].index
dfs = {
r.name: pd.DataFrame(r.read(keyed="True"))
.set_index("timeindex")
.astype(float)
for r in sequence_resources
}
sequences = pd.concat(dfs.values(), axis=1)
if how == "daily":
hoursPerPeriod = 24
elif how == "hourly":
hoursPerPeriod = 1
elif how == "weekly":
hoursPerPeriod = 24 * 7
aggregation = tsam.TimeSeriesAggregation(
sequences,
noTypicalPeriods=n,
rescaleClusterPeriods=False,
hoursPerPeriod=hoursPerPeriod,
clusterMethod="hierarchical",
)
cluster_weights = {
aggregation.clusterCenterIndices[n]: w
for n, w in aggregation.clusterPeriodNoOccur.items()
}
if how == "daily":
temporal = pd.Series(
{
d: cluster_weights[d.dayofyear]
for d in sequences.index