From 895c517927d8eebbd37db9a82f772285d6a9e68e Mon Sep 17 00:00:00 2001 From: earthgecko Date: Tue, 16 Mar 2021 13:44:17 +0000 Subject: [PATCH 1/2] plot - save_to_file IssueID 3984: plot - save_to_file - Allow adtk.visualization.plot to save the plot to a file and add a title Modified: src/adtk/visualization/_visualization.py --- src/adtk/visualization/_visualization.py | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/adtk/visualization/_visualization.py b/src/adtk/visualization/_visualization.py index 92c0cce..d455dc9 100644 --- a/src/adtk/visualization/_visualization.py +++ b/src/adtk/visualization/_visualization.py @@ -2,6 +2,7 @@ We don't typing the visualization module because there are a lot recursion on nested tree structure which would be messy if we type rigorously.""" +import os import matplotlib.pyplot as plt import numpy as np import pandas as pd @@ -31,6 +32,8 @@ def plot( axes=None, figsize=None, legend=True, + title="", + save_to_file="", ): """Plot time series and/or anomalies. @@ -191,6 +194,14 @@ def plot( legend: bool, optional Whether to show legend in the plot. Default: True. + title: str, optional + The title add to the plot. + Default: "". + + save_to_file: str, optional + The full path and filename where to save the plot as a png image. + Default: "". + Returns -------- matplotlib Axes object or array of Axes objects @@ -329,6 +340,41 @@ def plot( for ax in axes: ax.legend() + # title + if title != "": + try: + plt.suptitle(title, fontsize=12) + except Exception as e: + print("could not add title to plot - {}".format(e)) + + # save_to_file + save_to_file_path = None + if save_to_file != "": + try: + save_to_file_path = os.path.dirname(save_to_file) + except Exception as e: + print( + "{} not a valid path/filename for " + "save_to_file - %s".format(save_to_file, e)) + + save_to_file_path_exists = False + if save_to_file_path: + try: + if os.path.exists(save_to_file_path): + save_to_file_path_exists = True + except Exception as e: + print( + "{} is not a valid path in " + "save_to_file - {}".format(save_to_file, e)) + if save_to_file_path_exists: + try: + plt.savefig(save_to_file, dpi=100) + os.chmod(save_to_file, mode=0o644) + except Exception as e: + print( + "error: failed to save plot to " + "%s - %s".format(save_to_file, e)) + return axes From 358b77bb825335726875c839ee0b31cc85658795 Mon Sep 17 00:00:00 2001 From: earthgecko Date: Sun, 14 Jan 2024 08:27:04 +0000 Subject: [PATCH 2/2] Replace deprecated pandas.Series.iteritems pandas removed deprecated Series.iteritems(), DataFrame.iteritems(), use obj.items instead #147 IssueID 5224: Replace deprecated pandas.Series.iteritems IssueID 5222: adtk - pandas error - Replace deprecated pandas.Series.iteritems with pandas.Series.items Modified: src/adtk/data/_data.py --- src/adtk/data/_data.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/adtk/data/_data.py b/src/adtk/data/_data.py index f447ee6..9967da7 100644 --- a/src/adtk/data/_data.py +++ b/src/adtk/data/_data.py @@ -161,7 +161,13 @@ def validate_events( merged_event_list = ( [] ) # type: List[Union[Tuple[pd.Timestamp, pd.Timestamp], pd.Timestamp]] - for t, v in time_window_end_series.iteritems(): # type: pd.Timestamp, int + # @modified 20240113 - earthgecko/adtk - Task #5224: Replace deprecated pandas.Series.iteritems + # earthgecko/skyline - Bug #5222: adtk - pandas error + # arundo/adtk - https://github.com/arundo/adtk/issues/147 + # pandas.Series.iteritems - https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.Series.iteritems.html + # Deprecated since version 1.5.0: iteritems is deprecated and will be removed in a future version. Use .items instead. + # for t, v in time_window_end_series.iteritems(): # type: pd.Timestamp, int + for t, v in time_window_end_series.items(): # type: pd.Timestamp, int if (status == 0) and (v > 0): start = t # type: pd.Timestamp status = 1 @@ -588,7 +594,13 @@ def expand_events( # type:ignore right_expand=right_expand, freq_as_period=freq_as_period, ) - for _, s in events.iteritems() + # @modified 20240113 - earthgecko/adtk - Task #5224: Replace deprecated pandas.Series.iteritems + # earthgecko/skyline - Bug #5222: adtk - pandas error + # arundo/adtk - https://github.com/arundo/adtk/issues/147 + # pandas.Series.iteritems - https://pandas.pydata.org/pandas-docs/version/1.5/reference/api/pandas.Series.iteritems.html + # Deprecated since version 1.5.0: iteritems is deprecated and will be removed in a future version. Use .items instead. + # for _, s in events.iteritems() + for _, s in events.iter() ], axis=1, ) # type: pd.DataFrame