Skip to content

Commit

Permalink
refactor: throw a ValueError instead of an AssertionError when numeri…
Browse files Browse the repository at this point in the history
…cal constrasts have incorrect shapes
  • Loading branch information
BorisMuzellec committed Oct 31, 2024
1 parent 3e90db5 commit 4cd7376
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pydeseq2/ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ def __init__(
The "contrast" argument must be provided."""
)
elif isinstance(contrast, np.ndarray):
assert (
contrast.shape[0] == self.dds.obsm["design_matrix"].shape[1]
), "The contrast vector must have the same length as the design matrix."
if contrast.shape[0] != self.dds.obsm["design_matrix"].shape[1]:
raise ValueError(
"The contrast vector must have the same length as the design matrix."
)
self.contrast = contrast
self.contrast_vector = contrast
else:
Expand Down
4 changes: 4 additions & 0 deletions tests/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ def test_contrast():
with pytest.raises(ValueError):
DeseqStats(dds, contrast=["condition", "C", "B"])

# Numerical contrast with the wrong number of elements
with pytest.raises(ValueError):
DeseqStats(dds, contrast=np.array([0, 0, 0, 1]))


def test_cooks_not_refitted():
"""Test that an AttributeError is thrown when a `DeseqStats` object is initialized
Expand Down

0 comments on commit 4cd7376

Please sign in to comment.