Coverage for addmo/s5_insights/model_plots/time_series.py: 94%
51 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-08-31 13:05 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2025-08-31 13:05 +0000
1import pandas as pd
2import matplotlib.dates as mdates
3import matplotlib.pyplot as plt
4from addmo.util import plotting_utils as d
5from addmo.util.load_save import load_data
8def plot_timeseries_combined(config,data):
9 """
10 Returns:
11 - Full time range defined in config.
12 - First 2-week window (if the data spans more than 21 days).
14 Returns one or two matplotlib figures.
15 """
17 # Get date range from config (if it exists):
18 if hasattr(config,'start_train_val') and hasattr(config, 'end_test'):
19 start_date = pd.to_datetime(config.get('start_train_val', data.index.min()))
20 end_date = pd.to_datetime(config.get('end_test', data.index.max()))
21 data = data[(data.index >= start_date) & (data.index <= end_date)]
23 # Check data duration
24 if not pd.api.types.is_datetime64_any_dtype(data.index):
25 data.index = pd.to_datetime(data.index)
26 duration_days = (data.index.max() - data.index.min()).days
27 columns_to_plot = data.columns.tolist()
29 fig_height = max(5, len(columns_to_plot) * 2.5)
30 fig_full, axes_full = plt.subplots(len(columns_to_plot), 1,
31 figsize=(d.cm2inch(15.5), d.cm2inch(fig_height)),
32 sharex=True, gridspec_kw={'wspace': 0, 'hspace': 0.08})
33 plt.subplots_adjust(left=0.12, right=0.97, bottom=0.1, top=0.97)
35 for ax, column in zip(axes_full, columns_to_plot):
36 color = d.red if column == config['name_of_target'] else d.black
37 ax.plot(data.index, data[column], color=color, linewidth=0.75,
38 label='Target' if column == config['name_of_target'] else None)
39 if column == config['name_of_target']:
40 ax.legend(loc='upper right', fontsize=7)
42 ax.set_ylabel(column.replace(' ', '\n').replace('__', '\n'), fontsize=7, labelpad=-1.1)
43 ax.grid()
44 plt.setp(ax.get_yticklabels(), fontsize=7)
46 axes_full[-1].set_xlabel("Time", fontsize=7)
47 axes_full[-1].xaxis.set_major_formatter(mdates.DateFormatter('%m-%d\n%H:%M'))
48 plt.setp(axes_full[-1].xaxis.get_majorticklabels(), rotation=0, ha="center", fontsize=7)
49 fig_full.align_labels()
51 figures = [fig_full]
53 if duration_days > 10:
54 window_start = data.index.min()
55 window_end = window_start + pd.Timedelta(days=5)
56 data_2weeks = data[(data.index >= window_start) & (data.index < window_end)]
58 fig_height_2w = max(5, len(columns_to_plot) * 2.5)
59 fig_2weeks, axes_2weeks = plt.subplots(len(columns_to_plot), 1,
60 figsize=(d.cm2inch(15.5), d.cm2inch(fig_height_2w)),
61 sharex=True, gridspec_kw={'wspace': 0, 'hspace': 0.08})
62 plt.subplots_adjust(left=0.12, right=0.97, bottom=0.1, top=0.97)
64 for ax, column in zip(axes_2weeks, columns_to_plot):
65 color = d.red if column == config['name_of_target'] else d.black
66 ax.plot(data_2weeks.index, data_2weeks[column], color=color, linewidth=0.75,
67 label='Target' if column == config['name_of_target'] else None)
68 if column == config['name_of_target']:
69 ax.legend(loc='upper right', fontsize=7)
71 ax.set_ylabel(column.replace(' ', '\n').replace('__', '\n'), fontsize=7, labelpad=-1.1)
72 ax.grid()
73 plt.setp(ax.get_yticklabels(), fontsize=7)
75 axes_2weeks[-1].set_xlabel("Time", fontsize=7)
76 axes_2weeks[-1].xaxis.set_major_formatter(mdates.DateFormatter('%m-%d\n%H:%M'))
77 plt.setp(axes_2weeks[-1].xaxis.get_majorticklabels(), rotation=0, ha="center", fontsize=7)
78 fig_2weeks.align_labels()
80 figures.append(fig_2weeks)
82 return figures