Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Warnings raised by underlying seaborn and numpy libraries #425

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
jxnior01 marked this conversation as resolved.
Show resolved Hide resolved
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