From e7f7366ad371a567cf8850bce6575609f876e6e2 Mon Sep 17 00:00:00 2001 From: Ekin-Kahraman Date: Sat, 11 Apr 2026 20:38:29 +0100 Subject: [PATCH] Fix AttributeError when design is a DataFrame 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. --- pydeseq2/dds.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pydeseq2/dds.py b/pydeseq2/dds.py index aee7ac44..f0298057 100644 --- a/pydeseq2/dds.py +++ b/pydeseq2/dds.py @@ -575,11 +575,25 @@ def cond(self, **kwargs): ndarray A contrast vector that aligns to the columns of the design matrix. """ - return self.formulaic_contrasts.cond(**kwargs) + try: + return self.formulaic_contrasts.cond(**kwargs) + except AttributeError: + raise AttributeError( + "The cond() method requires a formula-based design. " + "When using a precomputed design matrix (DataFrame), " + "pass the contrast vector directly instead." + ) from None def contrast(self, *args, **kwargs): """Get a contrast for a simple pairwise comparison.""" - return self.formulaic_contrasts.contrast(*args, **kwargs) + try: + return self.formulaic_contrasts.contrast(*args, **kwargs) + except AttributeError: + raise AttributeError( + "The contrast() method requires a formula-based design. " + "When using a precomputed design matrix (DataFrame), " + "pass the contrast vector directly instead." + ) from None def fit_size_factors( self,