Skip to content

Commit

Permalink
fix: Warnings raised by underlying seaborn and numpy libraries (#425)
Browse files Browse the repository at this point in the history
Closes #357 

### Summary of Changes

* Python's `warnings` module now ignores warnings caused by underlying
seaborn and numpy libraries.

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Alexander <47296670+Marsmaennchen221@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 13, 2023
1 parent ada934c commit c4143af
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
47 changes: 36 additions & 11 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,16 +1808,41 @@ def plot_correlation_heatmap(self) -> Image:
"""
only_numerical = self.remove_columns_with_non_numerical_values()

fig = plt.figure()
sns.heatmap(
data=only_numerical._data.corr(),
vmin=-1,
vmax=1,
xticklabels=only_numerical.column_names,
yticklabels=only_numerical.column_names,
cmap="vlag",
)
plt.tight_layout()
if self.number_of_rows == 0:
warnings.warn(
"An empty table has been used. A correlation heatmap on an empty table will show nothing.",
stacklevel=2,
)

with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message=(
"Attempting to set identical low and high (xlims|ylims) makes transformation singular;"
" automatically expanding."
),
)
fig = plt.figure()
sns.heatmap(
data=only_numerical._data.corr(),
vmin=-1,
vmax=1,
xticklabels=only_numerical.column_names,
yticklabels=only_numerical.column_names,
cmap="vlag",
)
plt.tight_layout()
else:
fig = plt.figure()
sns.heatmap(
data=only_numerical._data.corr(),
vmin=-1,
vmax=1,
xticklabels=only_numerical.column_names,
yticklabels=only_numerical.column_names,
cmap="vlag",
)
plt.tight_layout()

buffer = io.BytesIO()
fig.savefig(buffer, format="png")
Expand Down Expand Up @@ -2000,7 +2025,7 @@ def plot_histograms(self) -> Image:
"""
col_wrap = min(self.number_of_columns, 3)

data = pd.melt(self._data, value_vars=self.column_names)
data = pd.melt(self._data.applymap(lambda value: str(value)), value_vars=self.column_names)
grid = sns.FacetGrid(data=data, col="variable", col_wrap=col_wrap, sharex=False, sharey=False)
grid.map(sns.histplot, "value")
grid.set_xlabels("")
Expand Down
Binary file modified tests/resources/image/snapshot_histograms/four_columns.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/image/snapshot_histograms/one_column.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
ids=["normal", "empty"],
)
def test_should_match_snapshot(table: Table, path: str) -> None:
current = table.plot_correlation_heatmap()
if table.number_of_rows == 0:
with pytest.warns(
UserWarning,
match=r"An empty table has been used. A correlation heatmap on an empty table will show nothing.",
):
current = table.plot_correlation_heatmap()
else:
current = table.plot_correlation_heatmap()
snapshot = Image.from_png_file(resolve_resource_path(path))

# Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm.
assertion = snapshot._image.tobytes() == current._image.tobytes()
assertion = snapshot == current
assert assertion
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
[
(Table({"A": [1, 2, 3]}), "./image/snapshot_histograms/one_column.png"),
(
Table({"A": [1, 2, 3], "B": ["A", "A", "Bla"], "C": [True, True, False], "D": [1.0, 2.1, 4.5]}),
Table(
{
"A": [1, 2, 3, 3, 2, 4, 2],
"B": ["a", "b", "b", "b", "b", "b", "a"],
"C": [True, True, False, True, False, None, True],
"D": [1.0, 2.1, 2.1, 2.1, 2.1, 3.0, 3.0],
},
),
"./image/snapshot_histograms/four_columns.png",
),
],
Expand All @@ -19,9 +26,8 @@
def test_should_match_snapshot(table: Table, path: str) -> None:
current = table.plot_histograms()
snapshot = Image.from_png_file(resolve_resource_path(path))

# Inlining the expression into the assert causes pytest to hang if the assertion fails when run from PyCharm.
assertion = snapshot._image.tobytes() == current._image.tobytes()
assertion = snapshot == current
assert assertion


Expand Down

0 comments on commit c4143af

Please sign in to comment.