Skip to content

Commit

Permalink
Merge pull request #647 from chanzuckerberg/joyce/fix-colors
Browse files Browse the repository at this point in the history
fix: only validate colors if the key is specified
  • Loading branch information
joyceyan authored Sep 26, 2023
2 parents 40d56c8 + 1aa127d commit 518d7e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
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

0 comments on commit 518d7e6

Please sign in to comment.