Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Molinelli authored and Evan Molinelli committed Jan 15, 2025
1 parent 7e95c00 commit d9d00a2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cellxgene_schema_cli/cellxgene_schema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

logger = logging.getLogger(__name__)

SPARSE_MATRIX_TYPES = {"csr","csc","coo"}
SPARSE_MATRIX_TYPES = {"csr", "csc", "coo"}
SUPPORTED_SPARSE_MATRIX_TYPES = {"csr"}


Expand Down Expand Up @@ -93,7 +93,7 @@ def get_matrix_format(matrix: DaskArray) -> str:
matrix_format = matrix_slice.format
elif isinstance(matrix_slice, np.ndarray):
matrix_format = "dense"
assert matrix_format in SPARSE_MATRIX_TYPES.union({"unknown","dense"})
assert matrix_format in SPARSE_MATRIX_TYPES.union({"unknown", "dense"})
return matrix_format


Expand Down
16 changes: 13 additions & 3 deletions cellxgene_schema_cli/cellxgene_schema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
from scipy import sparse

from . import gencode, schema
from .utils import SPARSE_MATRIX_TYPES, SUPPORTED_SPARSE_MATRIX_TYPES, get_matrix_format, getattr_anndata, is_ontological_descendant_of, read_h5ad
from .utils import (
SPARSE_MATRIX_TYPES,
SUPPORTED_SPARSE_MATRIX_TYPES,
get_matrix_format,
getattr_anndata,
is_ontological_descendant_of,
read_h5ad,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,6 +59,7 @@

ERROR_SUFFIX_SPARSE_FORMAT = f"Please ensure it is either a dense array or one of the supported sparse matrix encodings ({','.join(SUPPORTED_SPARSE_MATRIX_TYPES)})"


class Validator:
"""Handles validation of AnnData"""

Expand Down Expand Up @@ -1018,7 +1026,9 @@ def _validate_sparsity(self):
if matrix_format in SUPPORTED_SPARSE_MATRIX_TYPES:
continue
elif matrix_format in SPARSE_MATRIX_TYPES and matrix_format not in SUPPORTED_SPARSE_MATRIX_TYPES:
self.errors.append(f"Invalid sparse encoding for {x_name} with encoding {matrix_format}. Onle {','.join(SUPPORTED_SPARSE_MATRIX_TYPES)} sparse encodings are supported.")
self.errors.append(

Check warning on line 1029 in cellxgene_schema_cli/cellxgene_schema/validate.py

View check run for this annotation

Codecov / codecov/patch

cellxgene_schema_cli/cellxgene_schema/validate.py#L1029

Added line #L1029 was not covered by tests
f"Invalid sparse encoding for {x_name} with encoding {matrix_format}. Onle {','.join(SUPPORTED_SPARSE_MATRIX_TYPES)} sparse encodings are supported."
)
continue

Check warning on line 1032 in cellxgene_schema_cli/cellxgene_schema/validate.py

View check run for this annotation

Codecov / codecov/patch

cellxgene_schema_cli/cellxgene_schema/validate.py#L1032

Added line #L1032 was not covered by tests
elif matrix_format == "unknown":
self.errors.append(f"Unknown encoding for matrix {x_name}. {ERROR_SUFFIX_SPARSE_FORMAT}")

Check warning on line 1034 in cellxgene_schema_cli/cellxgene_schema/validate.py

View check run for this annotation

Codecov / codecov/patch

cellxgene_schema_cli/cellxgene_schema/validate.py#L1034

Added line #L1034 was not covered by tests
Expand Down Expand Up @@ -1224,7 +1234,7 @@ def _has_valid_raw(self, force: bool = False) -> bool:
self.errors.append(f"Unknown encoding for matrix {xloc}. {ERROR_SUFFIX_SPARSE_FORMAT}")
self._raw_layer_exists = False
return self._raw_layer_exists

self._raw_layer_exists = True
is_sparse_matrix = matrix_format in SPARSE_MATRIX_TYPES

Expand Down
5 changes: 2 additions & 3 deletions cellxgene_schema_cli/tests/test_schema_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy
import pandas as pd
import pytest
import scipy.sparse
import scipy.sparse
from cellxgene_schema.schema import get_schema_definition
from cellxgene_schema.utils import getattr_anndata, read_h5ad
from cellxgene_schema.validate import (
Expand All @@ -28,7 +28,6 @@
)
from cellxgene_schema.write_labels import AnnDataLabelAppender
from dask.array import from_array

from fixtures.examples_validate import visium_library_id

schema_def = get_schema_definition()
Expand Down Expand Up @@ -278,7 +277,7 @@ def test_raw_values__contains_all_zero_rows_in_tissue_0(self, validator_with_vis

# make sure it's encoded as a sparse matrix to isolate the validation to the contents of the matrix.
validator.adata.X = validator.adata.X.map_blocks(scipy.sparse.csr_matrix)

validator.adata.raw = validator.adata.copy()
validator.adata.raw.var.drop("feature_is_filtered", axis=1, inplace=True)
validator.reset(None, 2)
Expand Down
14 changes: 6 additions & 8 deletions cellxgene_schema_cli/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from cellxgene_ontology_guide.entities import Ontology
from cellxgene_schema.schema import get_schema_definition
from cellxgene_schema.validate import (
ERROR_SUFFIX_SPARSE_FORMAT,
ERROR_SUFFIX_VISIUM_AND_IS_SINGLE_TRUE,
ERROR_SUFFIX_VISIUM_AND_IS_SINGLE_TRUE_FORBIDDEN,
ERROR_SUFFIX_VISIUM_AND_IS_SINGLE_TRUE_IN_TISSUE_0,
ERROR_SUFFIX_VISIUM_AND_IS_SINGLE_TRUE_REQUIRED,
SPATIAL_HIRES_IMAGE_MAX_DIMENSION_SIZE,
SPATIAL_HIRES_IMAGE_MAX_DIMENSION_SIZE_VISIUM_11MM,
ERROR_SUFFIX_SPARSE_FORMAT,
Validator,
validate,
)
Expand Down Expand Up @@ -386,16 +386,16 @@ def test__validate_from_file(self):
assert not validator.errors

def test__validate_spatial_visium_dense_matrix_ok(self):
'''
"""
Test visium specific requirements on a dense X matrix
'''
"""
validator: Validator = Validator()
validator._set_schema_def()
validator.adata = adata_visium.copy()
validator._visium_and_is_single_true_matrix_size = 2
_Xdense = validator.adata.X.compute()
_Xdense[_Xdense==0] = 1 # ensure the matrix doesn't trigger sparsity error
validator.adata.X = from_array(_Xdense.toarray()) # daskify
_Xdense[_Xdense == 0] = 1 # ensure the matrix doesn't trigger sparsity error
validator.adata.X = from_array(_Xdense.toarray()) # daskify
validator.adata.raw = validator.adata.copy()
validator.adata.raw.var.drop("feature_is_filtered", axis=1, inplace=True)
# Confirm spatial is valid.
Expand Down Expand Up @@ -1281,6 +1281,4 @@ def test_has_valid_raw_with_unknown_format(self, mock_get_matrix_format):
data = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
validator = self.create_validator(data, "unknown")
assert validator._has_valid_raw() is False
assert validator.errors == [
f'Unknown encoding for matrix X. {ERROR_SUFFIX_SPARSE_FORMAT}'
]
assert validator.errors == [f"Unknown encoding for matrix X. {ERROR_SUFFIX_SPARSE_FORMAT}"]

0 comments on commit d9d00a2

Please sign in to comment.