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: only validate colors if the key is specified #647

Merged
merged 3 commits into from
Sep 26, 2023
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
22 changes: 12 additions & 10 deletions cellxgene_schema_cli/cellxgene_schema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,20 @@ def _validate_colors_in_uns_dict(self, uns_dict: dict) -> None:
category_mapping[column_name] = df[column_name].nunique()

for column_name, num_unique_vals in category_mapping.items():
colors_options = uns_dict.get(f"{column_name}_colors", [])
if len(colors_options) < num_unique_vals:
self.errors.append(
f"Annotated categorical field {column_name} must have at least {num_unique_vals} color options "
f"in uns[{column_name}_colors]. Found: {colors_options}"
)
for color in colors_options:
if not self._validate_color(color):
colors_options = uns_dict.get(f"{column_name}_colors")
# If there are no colors specified, that's fine. We only want to validate this field if it's set
if colors_options is not None:
if len(colors_options) < num_unique_vals:
self.errors.append(
f"Color {color} in uns[{column_name}_colors] is not valid. Colors must be a valid hex "
f"code (#08c0ff) or a CSS4 named color"
f"Annotated categorical field {column_name} must have at least {num_unique_vals} color options "
f"in uns[{column_name}_colors]. Found: {colors_options}"
)
for color in colors_options:
if not self._validate_color(color):
self.errors.append(
f"Color {color} in uns[{column_name}_colors] is not valid. Colors must be a valid hex "
f"code (#08c0ff) or a CSS4 named color"
)

def _validate_color(self, color: str) -> bool:
css4_named_colors = [
Expand Down
4 changes: 4 additions & 0 deletions cellxgene_schema_cli/tests/test_schema_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,10 @@ def test_deprecated_fields(self):
],
)

def test_no_colors_should_pass(self):
del self.validator.adata.uns["suspension_type_colors"]
self.assertTrue(self.validator.validate_adata())

def test_not_enough_color_options(self):
self.validator.adata.uns["suspension_type_colors"] = ["green"]
self.validator.validate_adata()
Expand Down
Loading