Skip to content

Commit 88d03e9

Browse files
committed
Fix if/elif logic, add more descriptive pyproject.toml.
- Combine if/elif structure in visualization.py. - Add more descriptive ipython dependency into pyproject.toml. - Add test for two_visualization_output if decomposition is missing. - Correct import order. Closes #46, Closes #47
1 parent 625c39e commit 88d03e9

4 files changed

Lines changed: 57 additions & 43 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ dashboard = [
4141
"cryptography",
4242
]
4343

44-
ipython = [
45-
"ipython"
44+
display = [
45+
"ipython>=9.1"
4646
]
4747

4848
test = [

src/simdec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""SimDec main namespace."""
22
from simdec.decomposition import *
3+
from simdec.heterogeneity_indices import *
34
from simdec.sensitivity_indices import *
45
from simdec.visualization import *
5-
from simdec.heterogeneity_indices import *
66

77
__all__ = [
88
"sensitivity_indices",

src/simdec/visualization.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -203,29 +203,29 @@ def visualization(
203203
raise ValueError("'kind' can only be 'histogram' or 'boxplot'")
204204

205205
if print_legend:
206-
if not HAS_IPYTHON:
207-
warnings.warn(
208-
"print_legend=True requires ipython to be installed. "
209-
"Install it with: pip install simdec[ipython]",
210-
stacklevel=2,
211-
)
212-
elif decomposition is None:
206+
if not HAS_IPYTHON or decomposition is None:
207+
missing_requirements = []
208+
if not HAS_IPYTHON:
209+
missing_requirements.append(
210+
"ipython needs to be installed. "
211+
"Install it with: pip install simdec[display]"
212+
)
213+
if decomposition is None:
214+
missing_requirements.append("the decomposition parameter")
213215
warnings.warn(
214-
"print_legend=True requires the decomposition parameter. Table skipped.",
216+
f"print_legend=True requires {' and '.join(missing_requirements)}. Table skipped.",
215217
stacklevel=2,
216218
)
217219
else:
218-
try:
219-
_, styler = tableau(
220-
var_names=decomposition.var_names,
221-
statistic=decomposition.statistic,
222-
states=decomposition.states,
223-
bins=decomposition.bins,
224-
palette=palette,
225-
)
226-
display(styler)
227-
except ImportError:
228-
pass
220+
_, styler = tableau(
221+
var_names=decomposition.var_names,
222+
statistic=decomposition.statistic,
223+
states=decomposition.states,
224+
bins=decomposition.bins,
225+
palette=palette,
226+
)
227+
display(styler)
228+
229229
return ax
230230

231231

@@ -334,29 +334,29 @@ def two_output_visualization(
334334
fig.subplots_adjust(wspace=-0.015, hspace=0)
335335

336336
if print_legend:
337-
if not HAS_IPYTHON:
338-
warnings.warn(
339-
"print_legend=True requires ipython to be installed. "
340-
"Install it with: pip install simdec[ipython]",
341-
stacklevel=2,
342-
)
343-
elif decomposition is None:
337+
if not HAS_IPYTHON or decomposition is None:
338+
missing_requirements = []
339+
if not HAS_IPYTHON:
340+
missing_requirements.append(
341+
"ipython needs to be installed. "
342+
"Install it with: pip install simdec[display]"
343+
)
344+
if decomposition is None:
345+
missing_requirements.append("the decomposition parameter")
344346
warnings.warn(
345-
"print_legend=True requires the decomposition parameter. Table skipped.",
347+
f"print_legend=True requires {' and '.join(missing_requirements)}. Table skipped.",
346348
stacklevel=2,
347349
)
348350
else:
349-
try:
350-
_, styler = tableau(
351-
var_names=decomposition.var_names,
352-
statistic=decomposition.statistic,
353-
states=decomposition.states,
354-
bins=decomposition.bins,
355-
palette=palette,
356-
)
357-
display(styler)
358-
except ImportError:
359-
pass
351+
_, styler = tableau(
352+
var_names=decomposition.var_names,
353+
statistic=decomposition.statistic,
354+
states=decomposition.states,
355+
bins=decomposition.bins,
356+
palette=palette,
357+
)
358+
display(styler)
359+
360360
return fig, axs
361361

362362

tests/test_visualization.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import pytest
21
import pathlib
2+
import pytest
3+
4+
import matplotlib.pyplot as plt
35
import numpy as np
46
import pandas as pd
5-
import matplotlib.pyplot as plt
7+
68
import simdec as sd
79

810

@@ -132,3 +134,15 @@ def test_visualization_missing_decomposition_warning():
132134

133135
with pytest.warns(UserWarning, match="requires the decomposition parameter"):
134136
sd.visualization(bins=bins, palette=pal, print_legend=True, decomposition=None)
137+
138+
139+
def test_two_output_visualization_missing_decomposition_warning():
140+
"""Verify that omitting the decomposition object triggers a warning in two_output."""
141+
bins = pd.DataFrame({"s1": [1, 2]})
142+
bins2 = pd.DataFrame({"s1": [5, 6]})
143+
pal = [[1, 0, 0, 1]]
144+
145+
with pytest.warns(UserWarning, match="requires the decomposition parameter"):
146+
sd.two_output_visualization(
147+
bins=bins, bins2=bins2, palette=pal, print_legend=True, decomposition=None
148+
)

0 commit comments

Comments
 (0)