def plotjson(fn):
    """
    plotjson: make plots from json output of fiedler.py

    fn: the filename of the json file
    """

    fo=open(fn)
    data=json.load(fo)
    fo.close()
    if "adj" in data:
        (A,adj,Npts) = fiedler.adj_mat(data["adj"])


        #A = (A.T - A)/2

        A=A.tocoo()
        pos=A.data!=0
        skew = numpy.column_stack((A.row[pos],A.col[pos],A.data[pos])).tolist()
        
      
        # method from ranking driver.py
    
        asc = abstract_simplicial_complex([numpy.column_stack((A.row[pos],A.col[pos])).tolist()])
        B1 = asc.chain_complex()[1] # boundary matrix
        rank = lsqr(B1.T, A.data[pos])[0] # solve least squares problem
        # sc = simplicial_complex(([[el] for el in range(0,A.shape[0])],numpy.column_stack((A.row[pos],A.col[pos])).tolist()))
        # omega = sc.get_cochain(1)
        # omega.v[:] = A.data[pos]
        # p = omega.k
        # alpha = sc.get_cochain(p - 1)
        #
        # alpha.v = rank
        # v = A.data[pos]-d(alpha).v
        #
        # cyclic_adj_list=numpy.column_stack((A.row[pos],A.col[pos],v)).tolist()
        # div_adj_list=numpy.column_stack((A.row[pos],A.col[pos],d(alpha).v)).tolist()

        data["hodge"]=list(rank)
        data["hodgerank"]=list(numpy.argsort(numpy.argsort(rank)))
        print "Adding hodge results to %s"%(os.path.abspath(fn))
        fo = open(fn,"w")
        json.dump(data,fo, indent=2)
        fo.close()
Example #2
0
def MDS(adj_list):
    (A,adj,Npts) = fiedler.adj_mat(adj_list)
    D = A.todense()

    #converting to distance...may not work for importance scores
    D = np.sqrt(1-np.abs(D))

    #from Ryan Tasseff's cmdsAnalysis
    [n,m] = D.shape
    eps = 1E-21
    if n!=m:
        raise ValueError('Wrong size matrix')

    # Construct an n x n centering matrix
    # The form is P = I - (1/n) U where U is a matrix of all ones
    P = np.eye(n) - (1/float(n) * np.ones((n,n)))
    # center the data 
    B = np.dot(np.dot(P,-.5*D**2),P)
    # if len(w)>0:
    #     W = np.diag(np.sqrt(w))
    #     B = np.dot(np.dot(W,B),W)
        
    # Calculate the eigenvalues/vectors
    [E, V] = linalg.eig((B+B.T)/2) # may help with round off error??
    E = np.real(E) # these come out as complex but imaginary part should b ~eps
    V = np.real(V) # same as above
    # if len(w)>0:
    #     W = np.diag(1/np.sqrt(w))
    #     V = np.dot(W,V)
    # sort that mo fo
    ind = np.argsort(E)[::-1]
    E = E[ind]
    V = V[:,ind]
    # lets now create our return matrix 
    if np.sum(E>eps)==0:
        Y = 0
    else:
        Y = V[:,E>eps]*np.sqrt(E[E>eps])
    return {"m1":list(Y[:,0]), "m2":list(Y[:,1])}
def plotjson(fn):
    """
    plotjson: make plots from json output of fiedler.py

    fn: the filename of the json file
    """

    fo=open(fn)
    data=json.load(fo)
    fo.close()
    if "adj" in data:
        (A,adj,Npts) = fiedler.adj_mat(data["adj"])
        #scew symetricise

        A = (A.T - A)/2

        A=A.tocoo()
        pos=A.data>0
        skew = numpy.column_stack((A.row[pos],A.col[pos],A.data[pos])).tolist()
        
        # #method from hodge decomposition driver.py
        # sc = simplicial_complex(([[el] for el in range(0,A.shape[0])],numpy.column_stack((A.row[pos],A.col[pos])).tolist()))
        # omega = sc.get_cochain(1)
        # omega.v[:] = A.data[pos]
        # p = omega.k
        # alpha = sc.get_cochain(p - 1)
        # #beta  = sc.get_cochain(p + 1)    

        # # Solve for alpha
        # A2 = delta(d(sc.get_cochain_basis(p - 1))).v
        # b = delta(omega).v
        # rank=cg( A2, b, tol=1e-8 )[0]

        # method from ranking driver.py

        asc = abstract_simplicial_complex([numpy.column_stack((A.row[pos],A.col[pos])).tolist()])
        B1 = asc.chain_complex()[1] # boundary matrix
        rank = lsqr(B1.T, A.data[pos])[0] # solve least squares problem
        
        sc = simplicial_complex(([[el] for el in range(0,A.shape[0])],numpy.column_stack((A.row[pos],A.col[pos])).tolist()))
        omega = sc.get_cochain(1)
        omega.v[:] = A.data[pos]
        p = omega.k
        alpha = sc.get_cochain(p - 1)
        
        alpha.v = rank
        v = A.data[pos]-d(alpha).v
        
        cyclic_adj_list=numpy.column_stack((A.row[pos],A.col[pos],v)).tolist()
        div_adj_list=numpy.column_stack((A.row[pos],A.col[pos],d(alpha).v)).tolist()

        data["hodge"]=list(rank)
        data["hodgerank"]=list(numpy.argsort(numpy.argsort(rank)))
        fo = open(fn,"w")
        json.dump(data,fo, indent=2)
        fo.close()

        fn=fn+".abstract"
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),cyclic_adj_list,fn+".decomp.cyclic.",widths=[6],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),div_adj_list,fn+".decomp.acyclic.",widths=[6],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),numpy.array(data["f2"]),numpy.array(data["d"]),data["adj"],fn+".decomp.acyclic.over.all.",widths=[6],vsdeg=False,nByi=data["nByi"],adj_list2=div_adj_list,directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),cyclic_adj_list,fn+".decomp.harmonic.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),skew,fn+".decomp.skew.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],directed=True)
        #fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),data["adj"],fn+".decomp.acyclic.over.all.v.grad.",widths=[6],heights=[2],vsdeg=False,nByi=data["nByi"],adj_list2=div_adj_list,directed=True)
        fiedler.doPlots(numpy.array(data["f1"]),-1*numpy.array(rank),numpy.array(data["d"]),data["adj"],fn+".all.v.grad.",widths=[24],heights=[6],vsdeg=False,nByi=data["nByi"],directed=False)