def get_F_T_projection():
	"""
	INPUT: Takes 
	OUTPUT: U matrix in csr format
	"""
	import scipy.sparse as sp

	GRID_CENTERS = grid_cell_centers()
	F = sp.kron(sp.identity(len(GRID_CENTERS)), numpy.array([1,1,1]))
	F = F.tocsc()
	v = numpy.array([1]*len(GRID_CENTERS))
	factor = cholmod.cholesky_AAt(F)
	FFT_inv_F = sp.lil_matrix((F.shape[0], F.shape[1]))
	for i in range(F.shape[1]):
		FFT_inv_F[:,i] = factor(F[:,i].toarray())
	Proj_matrix = F.T*FFT_inv_F
	Proj_perp_matrix = sp.identity(Proj_matrix.shape[0])-Proj_matrix
	v_bar = F.T*factor(v)

	return (Proj_perp_matrix, v_bar)
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert np.allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert np.allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert np.allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert np.allclose(
                cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert np.allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert np.allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert np.allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert np.allclose(f3(Xty), answer)

            print problem, mode
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert np.allclose(np.prod(f.D()),
                                   np.linalg.det(XtX.todense()))
                assert np.allclose((f.L() * f.L().T).todense(), pXtX)
                L, D = f.L_D()
                assert np.allclose((L * D * L.T).todense(), pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert np.allclose(f.solve_A(b), np.dot(XtX.todense().I, b))
                assert np.allclose(f(b), np.dot(XtX.todense().I, b))
                assert np.allclose(f.solve_LDLt(b),
                                   np.dot((L * D * L.T).todense().I, b))
                assert np.allclose(f.solve_LD(b), np.dot((L * D).todense().I,
                                                         b))
                assert np.allclose(f.solve_DLt(b),
                                   np.dot((D * L.T).todense().I, b))
                assert np.allclose(f.solve_L(b), np.dot(L.todense().I, b))
                assert np.allclose(f.solve_Lt(b), np.dot(L.T.todense().I, b))
                assert np.allclose(f.solve_D(b), np.dot(D.todense().I, b))

                assert np.allclose(f.apply_P(b), b[f.P(), :])
                assert np.allclose(f.solve_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert np.allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert np.allclose(f.solve_Pt(b), b[np.argsort(f.P()), :])
def test_cholesky_matrix_market():
    for problem in ("well1033", "illc1033", "well1850", "illc1850"):
        X = mm_matrix(problem)
        y = mm_matrix(problem + "_rhs1")
        answer = np.linalg.lstsq(X.todense(), y)[0]
        XtX = (X.T * X).tocsc()
        Xty = X.T * y
        for mode in ("auto", "simplicial", "supernodal"):
            assert np.allclose(cholesky(XtX, mode=mode)(Xty), answer)
            assert np.allclose(cholesky_AAt(X.T, mode=mode)(Xty), answer)
            assert np.allclose(cholesky(XtX, mode=mode).solve_A(Xty), answer)
            assert np.allclose(cholesky_AAt(X.T, mode=mode).solve_A(Xty), answer)

            f1 = analyze(XtX, mode=mode)
            f2 = f1.cholesky(XtX)
            assert np.allclose(f2(Xty), answer)
            assert_raises(CholmodError, f1, Xty)
            assert_raises(CholmodError, f1.solve_A, Xty)
            assert_raises(CholmodError, f1.solve_LDLt, Xty)
            assert_raises(CholmodError, f1.solve_LD, Xty)
            assert_raises(CholmodError, f1.solve_DLt, Xty)
            assert_raises(CholmodError, f1.solve_L, Xty)
            assert_raises(CholmodError, f1.solve_D, Xty)
            assert_raises(CholmodError, f1.apply_P, Xty)
            assert_raises(CholmodError, f1.apply_Pt, Xty)
            f1.P()
            assert_raises(CholmodError, f1.L)
            assert_raises(CholmodError, f1.LD)
            assert_raises(CholmodError, f1.L_D)
            assert_raises(CholmodError, f1.L_D)
            f1.cholesky_inplace(XtX)
            assert np.allclose(f1(Xty), answer)

            f3 = analyze_AAt(X.T, mode=mode)
            f4 = f3.cholesky(XtX)
            assert np.allclose(f4(Xty), answer)
            assert_raises(CholmodError, f3, Xty)
            f3.cholesky_AAt_inplace(X.T)
            assert np.allclose(f3(Xty), answer)

            print problem, mode
            for f in (f1, f2, f3, f4):
                pXtX = XtX.todense()[f.P()[:, np.newaxis],
                                     f.P()[np.newaxis, :]]
                assert np.allclose(np.prod(f.D()),
                                   np.linalg.det(XtX.todense()))
                assert np.allclose((f.L() * f.L().T).todense(),
                                   pXtX)
                L, D = f.L_D()
                assert np.allclose((L * D * L.T).todense(),
                                   pXtX)

                b = np.arange(XtX.shape[0])[:, np.newaxis]
                assert np.allclose(f.solve_A(b),
                                   np.dot(XtX.todense().I, b))
                assert np.allclose(f(b),
                                   np.dot(XtX.todense().I, b))
                assert np.allclose(f.solve_LDLt(b),
                                   np.dot((L * D * L.T).todense().I, b))
                assert np.allclose(f.solve_LD(b),
                                   np.dot((L * D).todense().I, b))
                assert np.allclose(f.solve_DLt(b),
                                   np.dot((D * L.T).todense().I, b))
                assert np.allclose(f.solve_L(b),
                                   np.dot(L.todense().I, b))
                assert np.allclose(f.solve_Lt(b),
                                   np.dot(L.T.todense().I, b))
                assert np.allclose(f.solve_D(b),
                                   np.dot(D.todense().I, b))

                assert np.allclose(f.apply_P(b), b[f.P(), :])
                assert np.allclose(f.solve_P(b), b[f.P(), :])
                # Pt is the inverse of P, and argsort inverts permutation
                # vectors:
                assert np.allclose(f.apply_Pt(b), b[np.argsort(f.P()), :])
                assert np.allclose(f.solve_Pt(b), b[np.argsort(f.P()), :])
Exemple #4
0
 def spsolve(sparse_X, dense_b):
     factor = cholesky_AAt(sparse_X.T)
     return factor(sparse_X.T.dot(dense_b)).toarray()
mat_zp = np.zeros((5, J.shape[1]))
mat_zp[0, -5] = 1
mat_zp[1, -4] = 1
mat_zp[2, -3] = 1
mat_zp[3, -2] = 1
mat_zp[4, -1] = 1
J = sparse.coo_matrix(np.append(J.toarray(), mat_zp, axis=0))

sigmas = np.append(sigmas, 1*np.ones(5))
sigmas[-3] = 0.00001
values = np.append(values, np.zeros(5))
W1_2 = np.diag(1/sigmas)
W = sparse.coo_matrix(W1_2**2)
W1_2 = sparse.coo_matrix(W1_2)
VJ = (W1_2 * J).tocsr()
factor = cholesky_AAt(VJ.T)
ga = factor(J.T * W*values)
spec = ga[:n+1]
dist = ga[n+1:-5]
dzps = ga[-5:]
fisher = (VJ.T*VJ).toarray()
cov = np.linalg.inv(fisher)
computed_sigmas = np.diag(np.ones(cov.shape[0]))*cov
computed_sigmas = np.sqrt(computed_sigmas[computed_sigmas!=0].flatten())


#### Fit cosmo
from pycosmo import priors

def cosmo_part(cosmo, zs):
    x0 = cosmo.pars()
def lensModel(imagesetup,sourcesetup,lenses,gals,regconst,csub=11,psf=None,noResid=False,mapping='polygon',outputimages=False,returnRegCoeffs=False,mask2=None):
    image,sig,yc,xc,y0,x0,P,c,C,indx,imshape,mask=imagesetup
    srcx,srcy,srcxaxis,srcyaxis,R,pscale,sshape=sourcesetup
    import pixellatedTools
    import numpy
    import indexTricks as iT
    from imageSim import convolve
    from scipy.sparse.linalg import lsmr
    from scipy.sparse import diags

    galsubimage = image*1.
    for gal in gals:
        gal.setPars()      
        galsubimage -= P*gal.pixeval(x0,y0,csub=csub)

    galsum=(image-galsubimage)#.reshape(imshape)

    for lens in lenses:
        lens.setPars()

    if mapping =='polygon':
        Lm = pixellatedTools.getLensPoly(lenses,indx,xc,yc,srcx,srcy,pscale)
    if mapping =='bilinear':
        Lm = pixellatedTools.getLensMatrixBilinear(lenses,x0,y0,srcx,srcy,srcxaxis,srcyaxis)
    if mapping =='nearest':
        Lm = pixellatedTools.getLensMatrixNNTom(lenses,x0,y0,srcx,srcy,srcxaxis,srcyaxis)

    if regconst!=0:
        Sinv=diags([regconst],[0],shape=R.shape)*R

    Om = P*Lm
    B=Om.T*C*Om

    rhs = Om.T*(galsubimage/sig**2)
    if regconst!=0:lhs = (B)+Sinv
    else: lhs=B

    #old slow method, not using cholesky decomposition
    #fit = lsmr(lhs,rhs,maxiter=5000)
    #fit=fit[0]

    from scikits.sparse.cholmod import cholesky_AAt
    A=lhs
    F=cholesky_AAt((A.T).tocsc())
    fit = F(A.T * rhs)

    imin=image.copy()
    s=sig.copy()
    im=numpy.zeros(imshape).flatten()

    if mask != None:
        s=numpy.ones(imshape).flatten()*1e-99
        s[mask]=sig
        imin=numpy.zeros(imshape).flatten()
        imin[mask]=image

    s=s.reshape(imshape)
    imin=imin.reshape(imshape)
    
    if outputimages:
        import pyfits
        im[mask]+= (Om*fit)
        im[mask]+=galsum
        im=im.reshape(imshape)
        src=fit.reshape(sshape).copy()
        pyfits.PrimaryHDU(im).writeto("model.fits",clobber=True)
        pyfits.PrimaryHDU((imin-im)/s).writeto("resid.fits",clobber=True)
        pyfits.PrimaryHDU(src).writeto("src.fits",clobber=True)
        sig=sig.flatten()


    #if noResid: return [(Om*fit).reshape(imshape),fit]
    
    resid=((Om*fit)-galsubimage)/sig


    #this prunes out the inner region of the fit - i.e. the regime where the regularization has no impact.
    if mask2!=None:
        galsubimage=galsubimage[mask2]
        Om=Om.tocsc()[mask2[0],:]
        C=diags(1./(sig[mask2])**2,0)


    s=numpy.matrix(fit).T
    #d=numpy.matrix(galsubimage).T
    #f=Om
    #Cdinv=C

    Ed=0.5*(resid**2).sum()
    Es=(0.5*((s.T)*(R*s)))[0,0]


    M=-(Ed+regconst*Es)

    return M,Es,B,R,F