Path: blob/master/examples/e1_time_series_data_example.py
505 views
"""1Goals of this part of the examples:21. Learn how to use `load_time_series_data` and the `.tsd` accessor32. Understand why we use this approach43. Get to know the different processing functions54. See how this compares to the legacy `TimeSeriesData` class6"""7# Start by importing all relevant packages8import pathlib9import numpy as np10import matplotlib.pyplot as plt11# Imports from ebcpy12from ebcpy import load_time_series_data1314# For backwards compatibility example15from ebcpy import TimeSeriesData # Will show a DeprecationWarning161718def main(with_plot=True):19"""20Arguments of this example:2122:param bool with_plot:23Show the plot at the end of the script. Default is True.24"""25# First get the path with relevant input files:26basepath = pathlib.Path(__file__).parents[1].joinpath("tutorial", "data")27# Note: We often use pathlib. If you're not familiar and want to learn more,28# just search for any of the many tutorials available online.2930# ######################### Loading Time Series Data ##########################31# First we open a simulation result file (.mat)32df_mat = load_time_series_data(basepath.joinpath('simulatedData.mat'))33print(df_mat)34# Now a .csv. .xlsx works as well (with sheet_name parameter).35df_csv = load_time_series_data(basepath.joinpath('excelData.csv'))36print(df_csv)37# Or construct like any pandas DataFrame38df_random = load_time_series_data({"A": np.random.rand(100), "B": np.random.rand(100)})39print(df_random)4041# ######################### Why do we use this approach? ##########################42# Unlike the old TimeSeriesData which inherited from DataFrame,43# our new approach uses standard pandas DataFrames with a custom accessor.44# This makes it fully compatible with pandas ecosystem and tools like PyCharm's DataFrame viewer.45# Moreover, the old MultiColumn approach using variable names and tags was useful when processing46# variables with multiple stages, but made data handling much harder for everyone else.47# Obviously, you can still create a multicolumn pd.DataFrame and use the old tag system,48# it is just not the default anymore.49print("The loaded object is a standard", type(df_csv).__name__)50print("Time series functionality is available through the .tsd accessor")5152# ######################### Processing Time Series Data ##########################53# Index changing:54print(df_csv.index)55df_csv.tsd.to_datetime_index(unit_of_index="s")56print(df_csv.index)57df_csv.tsd.to_float_index(offset=0)58print(df_csv.index)5960# Some filter options61# Apply filters and create new columns with results62df_csv["outputs.TRoom_lowPass2"] = df_csv.tsd.low_pass_filter(63crit_freq=0.1, filter_order=2, variable="outputs.TRoom")64print(df_csv)6566# Moving average67df_csv["outputs.TRoom_MovingAverage"] = df_csv.tsd.moving_average(68window=50, variable="outputs.TRoom")69print(df_csv)7071# Plot the different processed signals72plt.figure()73plt.plot(df_csv.index, df_csv["outputs.TRoom"], label="Raw")74plt.plot(df_csv.index, df_csv["outputs.TRoom_lowPass2"], label="Low-pass (order 2)")75plt.plot(df_csv.index, df_csv["outputs.TRoom_MovingAverage"], label="Moving Average")76plt.legend()7778# How-to re-sample your data:79# Call the function. Desired frequency is a string (s: seconds), 60: 60 seconds.80# Play around with this value to see what happens.81# First convert to DateTimeIndex (required for this function)82df_csv.tsd.to_datetime_index(unit_of_index="s")83# Create a copy to later reference the change.84df_csv_ref = df_csv.copy()85df_csv.tsd.clean_and_space_equally(desired_freq="60s")86plt.figure()87plt.plot(df_csv_ref.index, df_csv_ref["outputs.TRoom"], label="Reference", color="blue")88plt.plot(df_csv.index, df_csv["outputs.TRoom"], label="Resampled", color="red")89plt.legend()9091# ######################### Legacy TimeSeriesData Example ##########################92# For reference, here's how the same operations would be done with the legacy class93# Note: This will display a DeprecationWarning94print("\n--- Legacy TimeSeriesData Example (Deprecated) ---")95tsd_legacy = TimeSeriesData(basepath.joinpath('excelData.csv'), use_multicolumn=True)96tsd_legacy.to_datetime_index(unit_of_index="s")97tsd_legacy.low_pass_filter(crit_freq=0.1, filter_order=2,98variable="outputs.TRoom", new_tag="lowPass2")99tsd_legacy.moving_average(window=50, variable="outputs.TRoom",100tag="raw", new_tag="MovingAverage")101print("Legacy TimeSeriesData object with tags:", tsd_legacy.get_tags(variable="outputs.TRoom"))102print(tsd_legacy)103104if with_plot:105plt.show()106107108if __name__ == '__main__':109from ebcpy.utils import reproduction110main()111reproduction.save_reproduction_archive(title="log-testing")112113114