def fdr_correction(pvals, alpha=0.05, method='indep'):
 pvals = np.asarray(pvals)
 shape_init = pvals.shape
 pvals = pvals.ravel()
 pvals_sortind = np.argsort(pvals)
 pvals_sorted = pvals[pvals_sortind]
 sortrevind = pvals_sortind.argsort()
 if (method in ['i', 'indep', 'p', 'poscorr']):
  ecdffactor = _ecdf(pvals_sorted)
 elif (method in ['n', 'negcorr']):
  cm = np.sum((1.0 / np.arange(1, (len(pvals_sorted) + 1))))
  ecdffactor = (_ecdf(pvals_sorted) / cm)
 else:
  raise ValueError("Method should be 'indep' and 'negcorr'")
 reject = (pvals_sorted < (ecdffactor * alpha))
 if reject.any():
  rejectmax = max(np.nonzero(reject)[0])
 else:
  rejectmax = 0
 reject[:rejectmax] = True
 pvals_corrected_raw = (pvals_sorted / ecdffactor)
 pvals_corrected = np.minimum.accumulate(pvals_corrected_raw[::(-1)])[::(-1)]
 pvals_corrected[(pvals_corrected > 1.0)] = 1.0
 pvals_corrected = pvals_corrected[sortrevind].reshape(shape_init)
 reject = reject[sortrevind].reshape(shape_init)
 return (reject, pvals_corrected)

def fdr_correction ( pvals , alpha = 0.05 , method = 'indep' ) : pvals = np . asarray ( pvals ) shape_init = pvals . shape pvals = pvals . ravel ( ) pvals_sortind = np . argsort ( pvals ) pvals_sorted = pvals [ pvals_sortind ] sortrevind = pvals_sortind . argsort ( ) if ( method in [ 'i' , 'indep' , 'p' , 'poscorr' ] ) : ecdffactor = _ecdf ( pvals_sorted ) elif ( method in [ 'n' , 'negcorr' ] ) : cm = np . sum ( ( 1.0 / np . arange ( 1 , ( len ( pvals_sorted ) + 1 ) ) ) ) ecdffactor = ( _ecdf ( pvals_sorted ) / cm ) else : raise ValueError ( "Method should be 'indep' and 'negcorr'" ) reject = ( pvals_sorted < ( ecdffactor * alpha ) ) if reject . any ( ) : rejectmax = max ( np . nonzero ( reject ) [ 0 ] ) else : rejectmax = 0 reject [ : rejectmax ] = True pvals_corrected_raw = ( pvals_sorted / ecdffactor ) pvals_corrected = np . minimum . accumulate ( pvals_corrected_raw [ : : ( - 1 ) ] ) [ : : ( - 1 ) ] pvals_corrected [ ( pvals_corrected > 1.0 ) ] = 1.0 pvals_corrected = pvals_corrected [ sortrevind ] . reshape ( shape_init ) reject = reject [ sortrevind ] . reshape ( shape_init ) return ( reject , pvals_corrected )
adjust pvalues for multiple tests using the false discovery rate method .