def contrast_from_cols_or_rows(L, D, pseudo=None): """ Construct a contrast matrix from a design matrix D (possibly with its pseudo inverse already computed) and a matrix L that either specifies something in the column space of D or the row space of D. Parameters ---------- L : ndarray Matrix used to try and construct a contrast. D : ndarray Design matrix used to create the contrast. pseudo : None or array-like, optional If not None, gives pseudo-inverse of `D`. Allows you to pass this if it is already calculated. Returns ------- C : ndarray Matrix with C.shape[1] == D.shape[1] representing an estimable contrast. Notes ----- From an n x p design matrix D and a matrix L, tries to determine a p x q contrast matrix C which determines a contrast of full rank, i.e. the n x q matrix dot(transpose(C), pinv(D)) is full rank. L must satisfy either L.shape[0] == n or L.shape[1] == p. If L.shape[0] == n, then L is thought of as representing columns in the column space of D. If L.shape[1] == p, then L is thought of as what is known as a contrast matrix. In this case, this function returns an estimable contrast corresponding to the dot(D, L.T) This always produces a meaningful contrast, not always with the intended properties because q is always non-zero unless L is identically 0. That is, it produces a contrast that spans the column space of L (after projection onto the column space of D). """ L = np.asarray(L) D = np.asarray(D) n, p = D.shape if L.shape[0] != n and L.shape[1] != p: raise ValueError('shape of L and D mismatched') if pseudo is None: pseudo = pinv(D) if L.shape[0] == n: C = np.dot(pseudo, L).T else: C = np.dot(pseudo, np.dot(D, L.T)).T Lp = np.dot(D, C.T) if len(Lp.shape) == 1: Lp.shape = (n, 1) Lp_rank = matrix_rank(Lp) if Lp_rank != Lp.shape[1]: Lp = full_rank(Lp, Lp_rank) C = np.dot(pseudo, Lp).T return np.squeeze(C)