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 docstring of visualization tabular #840

Merged
merged 3 commits into from
Aug 14, 2024
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
8 changes: 7 additions & 1 deletion dianna/visualization/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def plot_tabular(
"""Plot feature importance with segments highlighted.

Args:
x (np.ndarray): Array of feature importance scores
x (np.ndarray): 1D array of feature importance scores of one instance
y (List[str]): List of feature names
x_label (str): Label for the x-axis
y_label (str): Label or list of labels for the y-axis
Expand All @@ -32,6 +32,12 @@ def plot_tabular(
Returns:
plt.Figure
"""
# check type and shape of x should be 1D array
if not isinstance(x, np.ndarray):
raise TypeError("x should be a numpy array")
if x.ndim != 1:
raise ValueError("x should be a 1D array")

if not num_features:
num_features = len(x)
abs_values = [abs(i) for i in x]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def test_plot_tabular(tmpdir):

assert output_path.exists()

def test_plot_tabular_with_ndarray():
"""Test plot tabular data with ndarray."""
x = np.random.rand(5, 3)
y = [f"Feature {i}" for i in range(x.shape[1])]
# check ValueError
with pytest.raises(ValueError):
plot_tabular(x=x, y=y, show_plot=False)


def test_plot_timeseries_univariate(tmpdir, random):
"""Test plot univariate time series."""
Expand Down
Loading