From 4e8d5f6a74b4ffd203269029ae28ecf6d3be90e6 Mon Sep 17 00:00:00 2001 From: Jorge Suit Date: Sun, 10 Mar 2024 16:53:00 -0400 Subject: [PATCH 1/3] ISSUE 303, add argument save_kwargs to be propagated in the call to plt.savefig ``` plt.savefig(savefig, **save_kwargs) ``` --- optbinning/binning/binning_statistics.py | 4 ++-- optbinning/binning/multidimensional/binning_statistics_2d.py | 4 ++-- optbinning/binning/piecewise/binning_statistics.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/optbinning/binning/binning_statistics.py b/optbinning/binning/binning_statistics.py index d12fd19..306e399 100644 --- a/optbinning/binning/binning_statistics.py +++ b/optbinning/binning/binning_statistics.py @@ -608,7 +608,7 @@ def build(self, show_digits=2, add_totals=True): return df def plot(self, metric="woe", add_special=True, add_missing=True, - style="bin", show_bin_labels=False, savefig=None, figsize=None): + style="bin", show_bin_labels=False, savefig=None, figsize=None, save_kwargs={}): """Plot the binning table. Visualize the non-event and event count, and the Weight of Evidence or @@ -863,7 +863,7 @@ def plot(self, metric="woe", add_special=True, add_missing=True, if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) - plt.savefig(savefig) + plt.savefig(savefig, **save_kwargs) plt.close() def analysis(self, pvalue_test="chi2", n_samples=100, print_output=True): diff --git a/optbinning/binning/multidimensional/binning_statistics_2d.py b/optbinning/binning/multidimensional/binning_statistics_2d.py index 37ae0b8..7c2aa7c 100644 --- a/optbinning/binning/multidimensional/binning_statistics_2d.py +++ b/optbinning/binning/multidimensional/binning_statistics_2d.py @@ -338,7 +338,7 @@ def build(self, show_digits=2, show_bin_xy=False, add_totals=True): return df - def plot(self, metric="woe", savefig=None): + def plot(self, metric="woe", savefig=None, save_kwargs={}): """Plot the binning table. Visualize the Weight of Evidence or the event rate for each bin as a @@ -437,7 +437,7 @@ def plot(self, metric="woe", savefig=None): if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) - plt.savefig(savefig) + plt.savefig(savefig, **save_kwargs) plt.close() def analysis(self, pvalue_test="chi2", n_samples=100, print_output=True): diff --git a/optbinning/binning/piecewise/binning_statistics.py b/optbinning/binning/piecewise/binning_statistics.py index e05109d..fd38e0c 100644 --- a/optbinning/binning/piecewise/binning_statistics.py +++ b/optbinning/binning/piecewise/binning_statistics.py @@ -177,7 +177,7 @@ def build(self, show_digits=2, add_totals=True): return df - def plot(self, metric="woe", n_samples=10000, savefig=None): + def plot(self, metric="woe", n_samples=10000, savefig=None, save_kwargs={}): """Plot the binning table. Visualize the non-event and event count, and the predicted Weight of @@ -258,7 +258,7 @@ def plot(self, metric="woe", n_samples=10000, savefig=None): if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) - plt.savefig(savefig) + plt.savefig(savefig, **save_kwargs) plt.close() def analysis(self, pvalue_test="chi2", n_samples=100, print_output=True): From fbec93db7d1a18f41e8ad25467f6639982f04a7c Mon Sep 17 00:00:00 2001 From: Jorge Suit Date: Fri, 15 Mar 2024 04:13:59 -0400 Subject: [PATCH 2/3] ISSUE #303, A function argument definition with a mutable type {} is a bad practice. Changing to save_kwargs=None --- optbinning/binning/binning_statistics.py | 8 +++++++- .../binning/multidimensional/binning_statistics_2d.py | 8 +++++++- optbinning/binning/piecewise/binning_statistics.py | 8 +++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/optbinning/binning/binning_statistics.py b/optbinning/binning/binning_statistics.py index 306e399..2872a45 100644 --- a/optbinning/binning/binning_statistics.py +++ b/optbinning/binning/binning_statistics.py @@ -608,7 +608,7 @@ def build(self, show_digits=2, add_totals=True): return df def plot(self, metric="woe", add_special=True, add_missing=True, - style="bin", show_bin_labels=False, savefig=None, figsize=None, save_kwargs={}): + style="bin", show_bin_labels=False, savefig=None, figsize=None, save_kwargs=None): """Plot the binning table. Visualize the non-event and event count, and the Weight of Evidence or @@ -863,6 +863,12 @@ def plot(self, metric="woe", add_special=True, add_missing=True, if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) + if save_kwargs is None: + save_kwargs = {} + else: + if not isinstance(save_kwargs, dict): + raise TypeError("save_kwargs must be a dictionary; got {}." + .format(save_kwargs)) plt.savefig(savefig, **save_kwargs) plt.close() diff --git a/optbinning/binning/multidimensional/binning_statistics_2d.py b/optbinning/binning/multidimensional/binning_statistics_2d.py index 7c2aa7c..c88e563 100644 --- a/optbinning/binning/multidimensional/binning_statistics_2d.py +++ b/optbinning/binning/multidimensional/binning_statistics_2d.py @@ -338,7 +338,7 @@ def build(self, show_digits=2, show_bin_xy=False, add_totals=True): return df - def plot(self, metric="woe", savefig=None, save_kwargs={}): + def plot(self, metric="woe", savefig=None, save_kwargs=None): """Plot the binning table. Visualize the Weight of Evidence or the event rate for each bin as a @@ -437,6 +437,12 @@ def plot(self, metric="woe", savefig=None, save_kwargs={}): if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) + if save_kwargs is None: + save_kwargs = {} + else: + if not isinstance(save_kwargs, dict): + raise TypeError("save_kwargs must be a dictionary; got {}." + .format(save_kwargs)) plt.savefig(savefig, **save_kwargs) plt.close() diff --git a/optbinning/binning/piecewise/binning_statistics.py b/optbinning/binning/piecewise/binning_statistics.py index fd38e0c..ca7810f 100644 --- a/optbinning/binning/piecewise/binning_statistics.py +++ b/optbinning/binning/piecewise/binning_statistics.py @@ -177,7 +177,7 @@ def build(self, show_digits=2, add_totals=True): return df - def plot(self, metric="woe", n_samples=10000, savefig=None, save_kwargs={}): + def plot(self, metric="woe", n_samples=10000, savefig=None, save_kwargs=None): """Plot the binning table. Visualize the non-event and event count, and the predicted Weight of @@ -258,6 +258,12 @@ def plot(self, metric="woe", n_samples=10000, savefig=None, save_kwargs={}): if not isinstance(savefig, str): raise TypeError("savefig must be a string path; got {}." .format(savefig)) + if save_kwargs is None: + save_kwargs = {} + else: + if not isinstance(save_kwargs, dict): + raise TypeError("save_kwargs must be a dictionary; got {}." + .format(save_kwargs)) plt.savefig(savefig, **save_kwargs) plt.close() From c2cde2cf9f4ce6afd4c89170d5944de5bfd9c88a Mon Sep 17 00:00:00 2001 From: Jorge Suit Date: Sun, 24 Mar 2024 10:21:02 -0400 Subject: [PATCH 3/3] ISSUE #303, add documentation for save_kwargs --- optbinning/binning/binning_statistics.py | 3 +++ optbinning/binning/multidimensional/binning_statistics_2d.py | 3 +++ optbinning/binning/piecewise/binning_statistics.py | 3 +++ 3 files changed, 9 insertions(+) diff --git a/optbinning/binning/binning_statistics.py b/optbinning/binning/binning_statistics.py index 2872a45..3260db3 100644 --- a/optbinning/binning/binning_statistics.py +++ b/optbinning/binning/binning_statistics.py @@ -642,6 +642,9 @@ def plot(self, metric="woe", add_special=True, add_missing=True, figsize : tuple or None (default=None) Size of the plot. + + save_kwargs : dict or None (default=None) + Additional keyword arguments to be passed to `plt.savefig`. """ _check_is_built(self) diff --git a/optbinning/binning/multidimensional/binning_statistics_2d.py b/optbinning/binning/multidimensional/binning_statistics_2d.py index c88e563..faf7cb3 100644 --- a/optbinning/binning/multidimensional/binning_statistics_2d.py +++ b/optbinning/binning/multidimensional/binning_statistics_2d.py @@ -352,6 +352,9 @@ def plot(self, metric="woe", savefig=None, save_kwargs=None): savefig : str or None (default=None) Path to save the plot figure. + + save_kwargs : dict or None (default=None) + Additional keyword arguments to be passed to `plt.savefig`. """ _check_is_built(self) diff --git a/optbinning/binning/piecewise/binning_statistics.py b/optbinning/binning/piecewise/binning_statistics.py index ca7810f..ad4328f 100644 --- a/optbinning/binning/piecewise/binning_statistics.py +++ b/optbinning/binning/piecewise/binning_statistics.py @@ -194,6 +194,9 @@ def plot(self, metric="woe", n_samples=10000, savefig=None, save_kwargs=None): savefig : str or None (default=None) Path to save the plot figure. + + save_kwargs : dict or None (default=None) + Additional keyword arguments to be passed to `plt.savefig`. """ _check_is_built(self)