From e669d71a6b2f2c9db7fc1b3c7e9065f52685a53d Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Mon, 8 May 2023 22:04:00 +0300 Subject: [PATCH 1/6] fixing issue #147 --- dython/__init__.py | 2 ++ dython/_private.py | 18 ++++++++++++++++++ dython/data_utils.py | 5 ++--- dython/model_utils.py | 8 +++----- dython/nominal.py | 5 ++--- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/dython/__init__.py b/dython/__init__.py index 72ad0f7..2aa9502 100644 --- a/dython/__init__.py +++ b/dython/__init__.py @@ -1,4 +1,5 @@ from . import nominal, model_utils, sampling, data_utils +from ._private import set_is_jupyter def _get_version_from_setuptools(): @@ -9,3 +10,4 @@ def _get_version_from_setuptools(): __all__ = ["__version__"] __version__ = _get_version_from_setuptools() +set_is_jupyter() diff --git a/dython/_private.py b/dython/_private.py index e278cb7..dd83e57 100644 --- a/dython/_private.py +++ b/dython/_private.py @@ -1,6 +1,24 @@ +import sys import numpy as np import pandas as pd +import matplotlib.pyplot as plt +IS_JUPYTER = None + + +def set_is_jupyter(force_to=None): + global IS_JUPYTER + if force_to is not None: + IS_JUPYTER = force_to + else: + IS_JUPYTER = "ipykernel_launcher.py" in sys.argv[0] + + +def plot_or_not(plot): + if plot: + plt.show() + elif not plot and IS_JUPYTER: + plt.close() def convert(data, to, copy=True): converted = None diff --git a/dython/data_utils.py b/dython/data_utils.py index dfb99bf..da023d8 100644 --- a/dython/data_utils.py +++ b/dython/data_utils.py @@ -1,7 +1,7 @@ import numpy as np import pandas as pd import matplotlib.pyplot as plt -from ._private import convert +from ._private import convert, plot_or_not __all__ = [ @@ -113,8 +113,7 @@ def split_hist( plt.title(title) plt.ylabel(ylabel) ax = plt.gca() - if plot: - plt.show() + plot_or_not(plot) return ax diff --git a/dython/model_utils.py b/dython/model_utils.py index 22c4a0a..e134e4f 100644 --- a/dython/model_utils.py +++ b/dython/model_utils.py @@ -2,7 +2,7 @@ import matplotlib.pyplot as plt from sklearn.metrics import roc_curve, precision_recall_curve, auc from scikitplot.helpers import binary_ks_curve -from ._private import convert +from ._private import convert, plot_or_not __all__ = ["random_forest_feature_importance", "metric_graph", "ks_abc"] @@ -28,8 +28,7 @@ def _display_metric_plot( ax.legend(loc=legend) if filename: plt.savefig(filename) - if plot: - plt.show() + plot_or_not(plot) return ax @@ -516,8 +515,7 @@ def ks_abc( ax.legend(loc=legend) if filename: plt.savefig(filename) - if plot: - plt.show() + plot_or_not(plot) return { "abc": abc, "ks_stat": ks_statistic, diff --git a/dython/nominal.py b/dython/nominal.py index e074a83..644c2c3 100644 --- a/dython/nominal.py +++ b/dython/nominal.py @@ -12,7 +12,7 @@ import seaborn as sns from psutil import cpu_count -from ._private import convert, remove_incomplete_samples, replace_nan_with_value +from ._private import convert, remove_incomplete_samples, replace_nan_with_value, plot_or_not from .data_utils import identify_columns_by_type __all__ = [ @@ -920,8 +920,7 @@ def _plot_associations( plt.title(title) if filename: plt.savefig(filename) - if plot: - plt.show() + plot_or_not(plot) return ax From 843cd932c941111ccf950d929178985635fc7511 Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Mon, 8 May 2023 22:04:14 +0300 Subject: [PATCH 2/6] version 0.7.4.dev --- CHANGELOG.md | 3 +++ VERSION | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db86010..5edfe50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log +## 0.7.4 _(dev)_ +* Handling running plotting functions with `plot=False` in Jupyter and truly avoid plotting (issue [#147](https://github.com/shakedzy/dython/issues/147)) + ## 0.7.3 * _Dython now officially supports only Python 3.8 or above_ (by-product of issue [#137](https://github.com/shakedzy/dython/issues/137)) * Added `nominal.replot_last_associations`: a new method to replot `nominal.associations` heat-maps (issue [#136](https://github.com/shakedzy/dython/issues/136)) diff --git a/VERSION b/VERSION index b09a54c..fc52ec3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.7.3 \ No newline at end of file +0.7.4.dev \ No newline at end of file From 992aa92ba54e330cc6395cf554143f587b52dbec Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Mon, 8 May 2023 22:09:04 +0300 Subject: [PATCH 3/6] black formatting --- dython/_private.py | 1 + dython/nominal.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/dython/_private.py b/dython/_private.py index dd83e57..730bc98 100644 --- a/dython/_private.py +++ b/dython/_private.py @@ -20,6 +20,7 @@ def plot_or_not(plot): elif not plot and IS_JUPYTER: plt.close() + def convert(data, to, copy=True): converted = None if to == "array": diff --git a/dython/nominal.py b/dython/nominal.py index 644c2c3..3fa0e7f 100644 --- a/dython/nominal.py +++ b/dython/nominal.py @@ -12,7 +12,12 @@ import seaborn as sns from psutil import cpu_count -from ._private import convert, remove_incomplete_samples, replace_nan_with_value, plot_or_not +from ._private import ( + convert, + remove_incomplete_samples, + replace_nan_with_value, + plot_or_not, +) from .data_utils import identify_columns_by_type __all__ = [ From 4fc1a4b124eabb73ac1ff05f9861f0fea01b9c91 Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Mon, 8 May 2023 22:13:22 +0300 Subject: [PATCH 4/6] Update nominal.py --- dython/nominal.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dython/nominal.py b/dython/nominal.py index 3fa0e7f..c0cef33 100644 --- a/dython/nominal.py +++ b/dython/nominal.py @@ -522,7 +522,6 @@ def associations( # handling NaN values in data if nan_strategy == _REPLACE: - # handling pandas categorical dataset = _handling_category_for_nan_imputation( dataset, nan_replace_value From 10401944dda2dbc3efaa530692d9a53a2fd95dde Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Tue, 9 May 2023 13:19:27 +0300 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5edfe50..386a795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log -## 0.7.4 _(dev)_ +## 0.7.4 * Handling running plotting functions with `plot=False` in Jupyter and truly avoid plotting (issue [#147](https://github.com/shakedzy/dython/issues/147)) ## 0.7.3 From fdedc8bf2e0500e177715fa30722a6617a20eafa Mon Sep 17 00:00:00 2001 From: Shaked Zychlinski Date: Tue, 9 May 2023 13:19:58 +0300 Subject: [PATCH 6/6] Version 0.7.4 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index fc52ec3..0a1ffad 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.7.4.dev \ No newline at end of file +0.7.4