@@ -141,11 +141,30 @@ def z_score(x, df):
141141 return (x - df ['mu' ][:,np .newaxis ])/ df ['sd' ][:,np .newaxis ]
142142
143143def p_adj_bh (x ):
144- '''Adjust p values using Benjamini/Hochberg method'''
145- # Mask out nan values as they cause the multiptests algorithm to return all nan
144+ '''Adjust p values using Benjamini/Hochberg method
145+
146+ Tests
147+ -----
148+ >>> import numpy as np
149+ >>> p_adj_bh(np.array([np.nan, np.nan]))
150+ array([nan, nan])
151+ >>> p_adj_bh(np.array([np.inf, -np.inf]))
152+ array([ inf, -inf])
153+ >>> p_adj_bh(np.array([]))
154+ array([], dtype=float64)
155+ >>> out = p_adj_bh(np.array([0.01, np.nan, 0.05]))
156+ >>> bool(np.isclose(out[0], 0.03, atol=0.01))
157+ True
158+ >>> bool(np.isnan(out[1]))
159+ True
160+ >>> bool(np.isclose(out[2], 0.05, atol=0.01))
161+ True
162+ '''
146163 mask = np .isfinite (x )
147164 pval_corrected = x .copy ()
148- pval_corrected [mask ] = multipletests (x [mask ], method = 'fdr_bh' , returnsorted = False )[1 ]
165+ if not np .any (mask ) or np .sum (mask ) < 1 : # If no finite values, or less than 1, just return the input unchanged
166+ return pval_corrected
167+ pval_corrected [mask ] = multipletests (x [mask ], method = 'fdr_bh' , returnsorted = False )[1 ]
149168 return pval_corrected
150169
151170def glob_list (l ):
0 commit comments