Skip to content

Commit 3d1c262

Browse files
Fix AttributeError when design is a DataFrame (#443)
When DeseqDataSet is created with a precomputed design matrix (DataFrame) instead of a formula string, cond() and contrast() raise a confusing AttributeError because formulaic_contrasts is never set. Added guard with a clear error message directing users to pass contrast vectors directly, matching the existing pattern used for the variables property. Fixes #440.
1 parent a19c44b commit 3d1c262

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

pydeseq2/dds.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,25 @@ def cond(self, **kwargs):
575575
ndarray
576576
A contrast vector that aligns to the columns of the design matrix.
577577
"""
578-
return self.formulaic_contrasts.cond(**kwargs)
578+
try:
579+
return self.formulaic_contrasts.cond(**kwargs)
580+
except AttributeError:
581+
raise AttributeError(
582+
"The cond() method requires a formula-based design. "
583+
"When using a precomputed design matrix (DataFrame), "
584+
"pass the contrast vector directly instead."
585+
) from None
579586

580587
def contrast(self, *args, **kwargs):
581588
"""Get a contrast for a simple pairwise comparison."""
582-
return self.formulaic_contrasts.contrast(*args, **kwargs)
589+
try:
590+
return self.formulaic_contrasts.contrast(*args, **kwargs)
591+
except AttributeError:
592+
raise AttributeError(
593+
"The contrast() method requires a formula-based design. "
594+
"When using a precomputed design matrix (DataFrame), "
595+
"pass the contrast vector directly instead."
596+
) from None
583597

584598
def fit_size_factors(
585599
self,

0 commit comments

Comments
 (0)