Skip to content

Commit

Permalink
Single table Structure property should not have visualization (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Palazzo authored Nov 13, 2023
1 parent e57ad67 commit 6d86f14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
4 changes: 4 additions & 0 deletions sdmetrics/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""Custom errors for SDMetrics."""


class VisualizationUnavailableError(Exception):
"""Raised when a visualization is not available."""


class IncomputableMetricError(Exception):
"""Raised when a metric cannot be computed."""

Expand Down
29 changes: 6 additions & 23 deletions sdmetrics/reports/single_table/_properties/structure.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pandas as pd
import plotly.express as px

from sdmetrics.errors import VisualizationUnavailableError
from sdmetrics.reports.single_table._properties import BaseSingleTableProperty
from sdmetrics.single_table import TableFormat

Expand Down Expand Up @@ -69,28 +69,11 @@ def _generate_details(self, real_data, synthetic_data, metadata, progress_bar=No
return result

def get_visualization(self):
"""Create a plot to show the structure property score.
"""Return the visualization for the property.
Returns:
plotly.graph_objects._figure.Figure
Raise an error in this case because the single table Structure property
does not have a supported visualization.
"""
average_score = self._compute_average()

fig = px.bar(
data_frame=self.details.dropna(subset=['Score']),
y='Score',
title=f'Data Diagnostic: Structure (Average Score={round(average_score, 2)})',
color='Metric',
pattern_shape='Metric',
raise VisualizationUnavailableError(
'The single table Structure property does not have a supported visualization.'
)

fig.update_yaxes(range=[0, 1], title_text='Diagnostic Score')

fig.update_layout(
xaxis_categoryorder='total ascending',
plot_bgcolor='#F5F5F8',
margin={'t': 150},
xaxis_title='Table',
)

return fig
15 changes: 15 additions & 0 deletions tests/unit/reports/single_table/_properties/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import numpy as np
import pandas as pd
import pytest

from sdmetrics.errors import VisualizationUnavailableError
from sdmetrics.reports.single_table._properties.structure import Structure


Expand Down Expand Up @@ -84,3 +86,16 @@ def test__generate_details_with_id_column(self, table_format_mock):
'Score': 0.75,
}, index=[0])
pd.testing.assert_frame_equal(result, expected_details)

def test_get_visualization(self):
"""Test the ``get_visualization`` method."""
# Setup
structure_property = Structure()

# Run and Assert
expected_message = (
'The single table Structure property does not have a'
' supported visualization.'
)
with pytest.raises(VisualizationUnavailableError, match=expected_message):
structure_property.get_visualization()

0 comments on commit 6d86f14

Please sign in to comment.