Skip to content

Commit d4593b0

Browse files
authored
Merge pull request #152 from shakedzy/147-ks_abc-when-run-with-plot=false-still-plots-the-graph
Fixing issue #147
2 parents da87f99 + fdedc8b commit d4593b0

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## 0.7.4
4+
* Handling running plotting functions with `plot=False` in Jupyter and truly avoid plotting (issue [#147](https://github.com/shakedzy/dython/issues/147))
5+
36
## 0.7.3
47
* _Dython now officially supports only Python 3.8 or above_ (by-product of issue [#137](https://github.com/shakedzy/dython/issues/137))
58
* Added `nominal.replot_last_associations`: a new method to replot `nominal.associations` heat-maps (issue [#136](https://github.com/shakedzy/dython/issues/136))

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.7.3
1+
0.7.4

dython/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import nominal, model_utils, sampling, data_utils
2+
from ._private import set_is_jupyter
23

34

45
def _get_version_from_setuptools():
@@ -9,3 +10,4 @@ def _get_version_from_setuptools():
910

1011
__all__ = ["__version__"]
1112
__version__ = _get_version_from_setuptools()
13+
set_is_jupyter()

dython/_private.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
import sys
12
import numpy as np
23
import pandas as pd
4+
import matplotlib.pyplot as plt
5+
6+
IS_JUPYTER = None
7+
8+
9+
def set_is_jupyter(force_to=None):
10+
global IS_JUPYTER
11+
if force_to is not None:
12+
IS_JUPYTER = force_to
13+
else:
14+
IS_JUPYTER = "ipykernel_launcher.py" in sys.argv[0]
15+
16+
17+
def plot_or_not(plot):
18+
if plot:
19+
plt.show()
20+
elif not plot and IS_JUPYTER:
21+
plt.close()
322

423

524
def convert(data, to, copy=True):

dython/data_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pandas as pd
33
import matplotlib.pyplot as plt
4-
from ._private import convert
4+
from ._private import convert, plot_or_not
55

66

77
__all__ = [
@@ -113,8 +113,7 @@ def split_hist(
113113
plt.title(title)
114114
plt.ylabel(ylabel)
115115
ax = plt.gca()
116-
if plot:
117-
plt.show()
116+
plot_or_not(plot)
118117
return ax
119118

120119

dython/model_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import matplotlib.pyplot as plt
33
from sklearn.metrics import roc_curve, precision_recall_curve, auc
44
from scikitplot.helpers import binary_ks_curve
5-
from ._private import convert
5+
from ._private import convert, plot_or_not
66

77
__all__ = ["random_forest_feature_importance", "metric_graph", "ks_abc"]
88

@@ -28,8 +28,7 @@ def _display_metric_plot(
2828
ax.legend(loc=legend)
2929
if filename:
3030
plt.savefig(filename)
31-
if plot:
32-
plt.show()
31+
plot_or_not(plot)
3332
return ax
3433

3534

@@ -516,8 +515,7 @@ def ks_abc(
516515
ax.legend(loc=legend)
517516
if filename:
518517
plt.savefig(filename)
519-
if plot:
520-
plt.show()
518+
plot_or_not(plot)
521519
return {
522520
"abc": abc,
523521
"ks_stat": ks_statistic,

dython/nominal.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
import seaborn as sns
1313
from psutil import cpu_count
1414

15-
from ._private import convert, remove_incomplete_samples, replace_nan_with_value
15+
from ._private import (
16+
convert,
17+
remove_incomplete_samples,
18+
replace_nan_with_value,
19+
plot_or_not,
20+
)
1621
from .data_utils import identify_columns_by_type
1722

1823
__all__ = [
@@ -517,7 +522,6 @@ def associations(
517522

518523
# handling NaN values in data
519524
if nan_strategy == _REPLACE:
520-
521525
# handling pandas categorical
522526
dataset = _handling_category_for_nan_imputation(
523527
dataset, nan_replace_value
@@ -920,8 +924,7 @@ def _plot_associations(
920924
plt.title(title)
921925
if filename:
922926
plt.savefig(filename)
923-
if plot:
924-
plt.show()
927+
plot_or_not(plot)
925928
return ax
926929

927930

0 commit comments

Comments
 (0)