#NEWLINE# def contrastfromcols( L , D , pseudo = None ) : #NEWLINE# #INDENT# '\n    From an n x p design matrix D and a matrix L, tries\n    to determine a p x q contrast matrix C which\n    determines a contrast of full rank, i.e. the\n    n x q matrix\n\n    dot(transpose(C), pinv(D))\n\n    is full rank.\n\n    L must satisfy either L.shape[0] == n or L.shape[1] == p.\n\n    If L.shape[0] == n, then L is thought of as representing\n    columns in the column space of D.\n\n    If L.shape[1] == p, then L is thought of as what is known\n    as a contrast matrix. In this case, this function returns an estimable\n    contrast corresponding to the dot(D, L.T)\n\n    Note that this always produces a meaningful contrast, not always\n    with the intended properties because q is always non-zero unless\n    L is identically 0. That is, it produces a contrast that spans\n    the column space of L (after projection onto the column space of D).\n\n    Parameters\n    ----------\n    L : array-like\n    D : array-like\n    ' #NEWLINE# L = np . asarray ( L ) #NEWLINE# D = np . asarray ( D ) #NEWLINE# ( n , p ) = D . shape #NEWLINE# if ( ( L . shape [ 0 ] != n ) and ( L . shape [ 1 ] != p ) ) : #NEWLINE# #INDENT# raise ValueError ( 'shape of L and D mismatched' ) #NEWLINE# #UNINDENT# if ( pseudo is None ) : #NEWLINE# #INDENT# pseudo = np . linalg . pinv ( D ) #NEWLINE# #UNINDENT# if ( L . shape [ 0 ] == n ) : #NEWLINE# #INDENT# C = np . dot ( pseudo , L ) . T #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# C = L #NEWLINE# C = np . dot ( pseudo , np . dot ( D , C . T ) ) . T #NEWLINE# #UNINDENT# Lp = np . dot ( D , C . T ) #NEWLINE# if ( len ( Lp . shape ) == 1 ) : #NEWLINE# #INDENT# Lp . shape = ( n , 1 ) #NEWLINE# #UNINDENT# if ( np_matrix_rank ( Lp ) != Lp . shape [ 1 ] ) : #NEWLINE# #INDENT# Lp = fullrank ( Lp ) #NEWLINE# C = np . dot ( Lp , Lp ) . T #NEWLINE# #UNINDENT# return np . squeeze ( C )