Skip to content

Produce empty plot if all values are null for get_column_plot #758

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

Merged
merged 3 commits into from
Apr 10, 2025
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
64 changes: 37 additions & 27 deletions sdmetrics/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,16 @@ def _generate_column_distplot(real_data, synthetic_data, plot_kwargs={}):
'colors': colors,
}

fig = ff.create_distplot(
hist_data,
col_names,
**{**default_distplot_kwargs, **plot_kwargs},
)
has_data = any(len(data) > 0 for data in hist_data)

return fig
if has_data:
return ff.create_distplot(
hist_data,
col_names,
**{**default_distplot_kwargs, **plot_kwargs},
)

return go.Figure()


def _generate_column_plot(
Expand Down Expand Up @@ -317,13 +320,25 @@ def _generate_column_plot(
fig = _generate_column_distplot(real_data, synthetic_data, plot_kwargs)
trace_args = {'fill': 'tozeroy'}

for i, name in enumerate(col_names):
fig.update_traces(
x=pd.to_datetime(fig.data[i].x) if is_datetime_sdtype else fig.data[i].x,
hovertemplate=f'<b>{name}</b><br>Frequency: %{{y}}<extra></extra>',
selector={'name': name},
**trace_args,
)
annotations = []
if fig.data:
for idx, name in enumerate(col_names):
fig.update_traces(
x=pd.to_datetime(fig.data[idx].x) if is_datetime_sdtype else fig.data[idx].x,
hovertemplate=f'<b>{name}</b><br>Frequency: %{{y}}<extra></extra>',
selector={'name': name},
**trace_args,
)
else:
annotations.append({
'xref': 'paper',
'yref': 'paper',
'x': 0.5,
'y': 0.5,
'showarrow': False,
'text': 'No data to visualize',
'font': {'size': PlotConfig.FONT_SIZE * 2},
})

show_missing_values = missing_data_real > 0 or missing_data_synthetic > 0
text = '*Missing Values:'
Expand All @@ -334,20 +349,15 @@ def _generate_column_plot(

text = text[:-2]

annotations = (
[]
if not show_missing_values
else [
{
'xref': 'paper',
'yref': 'paper',
'x': 1.0,
'y': 1.05,
'showarrow': False,
'text': text,
},
]
)
if show_missing_values:
annotations.append({
'xref': 'paper',
'yref': 'paper',
'x': 1.0,
'y': 1.05,
'showarrow': False,
'text': text,
})

if not plot_title:
plot_title = title
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_visualization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from unittest.mock import ANY, Mock, call, patch

import numpy as np
import pandas as pd
import pytest

Expand Down Expand Up @@ -368,6 +369,28 @@ def test_get_column_plot_column_not_found():
get_column_plot(pd.DataFrame({'start_date': []}), synthetic_data, 'start_date')


def test_get_column_plot_nan_data():
"""Test the ``get_column_plot`` method when data is all nans."""
# Setup
real_data = pd.DataFrame({'values': [np.nan] * 5})
synthetic_data = pd.DataFrame({'values': [np.nan] * 5})

# Run
fig_bar = get_column_plot(real_data, synthetic_data, 'values', plot_type='bar')
fig_distplot = get_column_plot(real_data, synthetic_data, 'values', plot_type='distplot')

# Assert
annotation_bar = fig_bar.layout.annotations[0]
assert annotation_bar.text == 'No data to visualize'
assert annotation_bar.x == 0.5
assert annotation_bar.y == 0.5

annotation_distplot = fig_distplot.layout.annotations[0]
assert annotation_distplot.text == 'No data to visualize'
assert annotation_distplot.x == 0.5
assert annotation_distplot.y == 0.5


def test_get_column_plot_bad_plot_type():
"""Test the ``get_column_plot`` method."""
# Setup
Expand Down
Loading