Passed
Pull Request — dev (#1226)
by
unknown
01:59
created

segmentation_of_timeseries_with_tsam   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 62
dl 0
loc 94
rs 10
c 0
b 0
f 0
1
import logging
2
import warnings
3
from pathlib import Path
4
import tsam.timeseriesaggregation as tsam
5
6
import numpy as np
7
import pandas as pd
8
from matplotlib import pyplot as plt
9
from oemof.tools import debugging
10
from oemof.tools import logger
11
12
from oemof.solph import Bus
13
from oemof.solph import EnergySystem
14
from oemof.solph import Flow
15
from oemof.solph import Investment
16
from oemof.solph import Model
17
from oemof.solph import Results
18
from oemof.solph import components as cmp
19
from oemof import solph
20
21
warnings.filterwarnings(
22
    "ignore", category=debugging.ExperimentalFeatureWarning
23
)
24
logger.define_logging()
25
26
file_path = Path(__file__).parent
27
28
df = pd.read_csv(
29
    Path(file_path, "energy.csv"),
30
)
31
df["time"] = pd.to_datetime(df["Unix Epoch"], unit="s")
32
# time als Index setzen
33
df = df.set_index("time")
34
df = df.drop(columns=["Unix Epoch"])
35
print(df)
36
37
time_index = df.index
38
39
# Dummy pv profile
40
h = np.arange(len(time_index))
41
pv_profile = df["PV (W)"]
42
43
# Dummy electricity profile
44
df["house_elec_kW"] = 0.3 + 0.7 * np.random.rand(len(time_index))
45
46
# Dummy heat profile
47
df["house_heat_kW"] = 0.3 + 0.7 * np.random.rand(len(time_index))
48
49
# EV-Ladeprofil
50
df["ev_charge_kW"] = 0.0  # wird automatisch auf alle Zeitschritte gebroadcastet
51
52
# COP-Profil (konstant, später evtl. temperaturabhängig)
53
df["cop_hp"] = 3.5
54
55
#Clustering of Input time-series with TSAM
56
# not a high number of typical periods works with high number of hours per period
57
typical_periods = 7
58
hours_per_period = 24 * 60
59
60
aggregation_no_segmentation = tsam.TimeSeriesAggregation(
61
    timeSeries=df,
62
    noTypicalPeriods=typical_periods,
63
    hoursPerPeriod=hours_per_period,
64
    clusterMethod="k_means",
65
    sortValues=False,
66
    rescaleClusterPeriods=False,
67
)
68
aggregation_no_segmentation.createTypicalPeriods()
69
tindex_agg = pd.date_range(
70
    "2022-01-01", periods=typical_periods * hours_per_period , freq="H"
71
)
72
73
aggregation_no_segmentation.typicalPeriods["house_elec_kW"]
74
75
#aggregation with segmentation
76
# has to be hourly values, other values don't work because it is to big
77
df = df.resample("1h").mean()
78
typical_periods = 40
79
hours_per_period = 24
80
81
aggregation_with_segmentation = tsam.TimeSeriesAggregation(
82
    timeSeries=df,
83
    noTypicalPeriods=typical_periods,
84
    hoursPerPeriod=hours_per_period,
85
    clusterMethod="k_means",
86
    sortValues=False,
87
    rescaleClusterPeriods=False,
88
    segmentation=True,
89
    noSegments=6,
90
)
91
aggregation_with_segmentation.createTypicalPeriods()
92
93
aggregation_with_segmentation.typicalPeriods["house_elec_kW"]
94