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: Fix preview_domain() when there are no sample values #92

Merged
merged 2 commits into from
Nov 25, 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
5 changes: 4 additions & 1 deletion bdikit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,10 @@ def preview_domain(
value_names = value_names[:limit]
value_descriptions = value_descriptions[:limit]

domain = {"value_name": value_names}
domain = {}

if len(value_names) > 0:
domain["value_name"] = value_names

if len(value_descriptions) > 0:
domain["value_description"] = value_descriptions
Expand Down
36 changes: 36 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,39 @@ def test_top_value_matches():
assert "source" in df_match.columns
assert "target" in df_match.columns
assert "similarity" in df_match.columns

def test_preview_domain():
aecio marked this conversation as resolved.
Show resolved Hide resolved
# given
source = pd.DataFrame(
{
"name": ["John Doe", "Jane Doe", "Alice Smith", "Bob Smith"],
"age": [30, 25, 45, 35],
}
)

# when
preview = bdi.preview_domain(source, "age")

# then
# preview must contain only the column "value_name" and the unique
# values of the column "age"
assert preview is not None
assert isinstance(preview, pd.DataFrame)
assert "value_name" in preview.columns
assert "column_description" not in preview.columns
assert "value_description" not in preview.columns
assert source["age"].eq(preview["value_name"]).all()

# when
preview = bdi.preview_domain("gdc", "age_at_diagnosis")

# then
# preview must contain only the column "column_description" since there
# are sample values in the GDC dictionary
assert preview is not None
assert isinstance(preview, pd.DataFrame)
assert "value_name" not in preview.columns
assert "value_description" not in preview.columns
assert "column_description" in preview.columns