Example #1
0
def test_graph_rcm_bucky():
    "Graph: Reverse Cuthill-McKee Ordering (Bucky)"
    B = np.load(pwd+'/bucky.npy')
    B = sp.csr_matrix(B, dtype=float)
    perm = reverse_cuthill_mckee(B)
    ans = np.load(pwd+'/bucky_perm.npy')
    assert_equal(perm, ans)
Example #2
0
def test_graph_rcm_bucky():
    "Graph: Reverse Cuthill-McKee Ordering (Bucky)"
    B = np.load(pwd+'/bucky.npy')
    B = sp.csr_matrix(B, dtype=float)
    perm = reverse_cuthill_mckee(B)
    ans = np.load(pwd+'/bucky_perm.npy')
    assert_equal(perm, ans)
Example #3
0
def test_graph_rcm_simple():
    "Graph: Reverse Cuthill-McKee Ordering (simple)"
    A = np.array([[1, 0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1],
                  [0, 1, 1, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 0],
                  [1, 0, 1, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 1, 0, 1],
                  [0, 0, 0, 1, 0, 0, 1, 0], [0, 1, 0, 0, 0, 1, 0, 1]],
                 dtype=np.int32)
    A = sp.csr_matrix(A)
    perm = reverse_cuthill_mckee(A)
    ans = np.array([6, 3, 7, 5, 1, 2, 4, 0])
    assert_equal((perm - ans).all(), 0)
Example #4
0
def test_graph_rcm_simple():
    "Graph: Reverse Cuthill-McKee Ordering (simple)"
    A = np.array([[1, 0, 0, 0, 1, 0, 0, 0],
                  [0, 1, 1, 0, 0, 1, 0, 1],
                  [0, 1, 1, 0, 1, 0, 0, 0],
                  [0, 0, 0, 1, 0, 0, 1, 0],
                  [1, 0, 1, 0, 1, 0, 0, 0],
                  [0, 1, 0, 0, 0, 1, 0, 1],
                  [0, 0, 0, 1, 0, 0, 1, 0],
                  [0, 1, 0, 0, 0, 1, 0, 1]], dtype=np.int32)
    A = sp.csr_matrix(A)
    perm = reverse_cuthill_mckee(A)
    ans = np.array([6, 3, 7, 5, 1, 2, 4, 0])
    assert_equal((perm - ans).all(), 0)
Example #5
0
def test_graph_rcm_qutip():
    "Graph: Reverse Cuthill-McKee Ordering (qutip)"
    kappa = 1
    gamma = 0.01
    g = 1
    wc = w0 = wl = 0
    N = 2
    E = 1.5
    a = tensor(destroy(N), qeye(2))
    sm = tensor(qeye(N), sigmam())
    H = (w0-wl)*sm.dag(
        )*sm+(wc-wl)*a.dag()*a+1j*g*(a.dag()*sm-sm.dag()*a)+E*(a.dag()+a)
    c_ops = [np.sqrt(2*kappa)*a, np.sqrt(gamma)*sm]
    L = liouvillian(H, c_ops)
    perm = reverse_cuthill_mckee(L.data)
    ans = np.array([12, 14, 4, 6, 10, 8, 2, 15, 0, 13, 7, 5, 9, 11, 1, 3])
    assert_equal(perm, ans)
Example #6
0
def test_graph_rcm_qutip():
    "Graph: Reverse Cuthill-McKee Ordering (qutip)"
    kappa = 1
    gamma = 0.01
    g = 1
    wc = w0 = wl = 0
    N = 2
    E = 1.5
    a = tensor(destroy(N), qeye(2))
    sm = tensor(qeye(N), sigmam())
    H = (w0-wl)*sm.dag(
        )*sm+(wc-wl)*a.dag()*a+1j*g*(a.dag()*sm-sm.dag()*a)+E*(a.dag()+a)
    c_ops = [np.sqrt(2*kappa)*a, np.sqrt(gamma)*sm]
    L = liouvillian(H, c_ops)
    perm = reverse_cuthill_mckee(L.data)
    ans = np.array([12, 14, 4, 6, 10, 8, 2, 15, 0, 13, 7, 5, 9, 11, 1, 3])
    assert_equal(perm, ans)
Example #7
0
def test_graph_rcm_boost():
    "Graph: Reverse Cuthill-McKee Ordering (boost)"
    M = np.zeros((10, 10))
    M[0, [3, 5]] = 1
    M[1, [2, 4, 6, 9]] = 1
    M[2, [3, 4]] = 1
    M[3, [5, 8]] = 1
    M[4, 6] = 1
    M[5, [6, 7]] = 1
    M[6, 7] = 1
    M = M+M.T
    M = sp.csr_matrix(M)
    perm = reverse_cuthill_mckee(M, 1)
    ans_perm = np.array([9, 7, 6, 4, 1, 5, 0, 2, 3, 8])
    assert_equal((perm - ans_perm).all(), 0)
    P = sp_permute(M, perm, perm)
    bw = sp_bandwidth(P)
    assert_equal(bw[2], 4)
Example #8
0
def test_graph_rcm_boost():
    "Graph: Reverse Cuthill-McKee Ordering (boost)"
    M = np.zeros((10, 10))
    M[0, [3, 5]] = 1
    M[1, [2, 4, 6, 9]] = 1
    M[2, [3, 4]] = 1
    M[3, [5, 8]] = 1
    M[4, 6] = 1
    M[5, [6, 7]] = 1
    M[6, 7] = 1
    M = M+M.T
    M = sp.csr_matrix(M)
    perm = reverse_cuthill_mckee(M, 1)
    ans_perm = np.array([9, 7, 6, 4, 1, 5, 0, 2, 3, 8])
    assert_equal((perm - ans_perm).all(), 0)
    P = sp_permute(M, perm, perm)
    bw = sp_bandwidth(P)
    assert_equal(bw[2], 4)
Example #9
0
def phones_test():
    import sklearn.preprocessing
    from conceptual_space import monary_load
    topmodel = monary_load("phones", [
        'Volume_(cubic_cm)', 'Display_Width(px)', 'Width_(mm)',
        'Display_Diagonal_(in)', 'Display_Length(px)', 'Depth_(mm)',
        'CPU_Clock_(MHz)', 'Mass_(grams)', 'ROM_Capacity_(Mb)', 'Length_(mm)',
        'RAM_Capacity_(Mb)', 'Release_Year', 'Pixel_Density_(per_inch)'
    ], [])
    F_by_O = topmodel.X.T
    print F_by_O.shape
    scipy.misc.imsave("F_by_O.png", F_by_O)

    print "Data extraction complete"
    #F_by_F = np.corrcoef(F_by_O)
    #O_by_O = np.corrcoef(F_by_O.T)
    #scipy.misc.imsave("O_by_O.png",O_by_O)

    #print "Corrcoefs complete"
    #SVD Calcs for O by O, comments taken from Som's community detection code.
    #k = 13
    for k in range(2, 7):
        for t in [0.75]:
            U, s, V = scipy.sparse.linalg.svds(
                F_by_O.T, k=k
            )  # Question remains whether this should be F by O or O by O!

            s = np.diagflat(s)
            print U.shape
            print s
            print V.shape
            # Perform dimensionality reduction, using parameter k. A good way to decide
            # on an optimal k value is to run the algorithm once, and plot the singular
            # values in decreasing order of magnitude. Then, choose k largest singular
            # values (the ones which show maximum gaps), and rerun algorithm at this k.
            #svdu = U[:,:k]
            #svds = s[:k,:k]
            #svdv = V[:,:k]

            # Generate US or SV products for generating reduced dimensional vectors
            # for nodes. This is the same if the matrix is symmetric and square.
            #u_red = svdu*svds
            u_red = np.matrix(U) * np.matrix(s)
            print "u_red: ", u_red.shape
            #v_red = svds*svdv.T
            #v_red = v_red.T
            v_red = np.matrix(s) * np.matrix(V)
            print "v_red: ", v_red.shape
            print "SVDs complete"

            #scipy.misc.imsave("u_red"+str(k)+"_"+str(t)+".png",u_red)
            #scipy.misc.imsave("v_red"+str(k)+"_"+str(t)+".png",v_red)
            # Compute cosine measurements between all US combinations. Produce the
            # cosine matrix in reduced k-space. Z_u will show communities for only Type
            # 1 nodes (rows of the original matrix).
            #Z_u = sklearn.metrics.pairwise.pairwise_distances(u_red,metric="cosine",n_jobs=-1)
            Z_u = np.corrcoef(u_red) / 2 + 0.5
            Z_u_labels = sklearn.cluster.MiniBatchKMeans(
                n_clusters=k).fit_predict(Z_u)
            clustersort = np.argsort(Z_u_labels)

            cm = matplotlib.cm.get_cmap("jet")
            cm.set_under(color="k", alpha=0)
            Z_u_clusters = np.zeros(Z_u.shape) - 0.01
            #Z_u_clusters = np.dstack([Z_u_clusters,Z_u_clusters,Z_u_clusters,np.ones(Z_u.shape)*255])
            #for i in range(k):
            #	lmask = np.matrix((Z_u_labels == i).astype(int))
            #	Z_u_clusters += (lmask.T * lmask) * (float(i+1)/(k+1))
            for i, l1 in enumerate(Z_u_labels):
                for j, l2 in enumerate(Z_u_labels):
                    if l1 == l2:
                        Z_u_clusters[i, j] = float(l1 + 1) / k
            #scipy.misc.imsave("Z_u_clusters_"+str(k)+".png",Z_u_clusters)

            #Z_u_overlaid = cm(Z_u_clusters, bytes=True)*np.dstack([Z_u,Z_u,Z_u,np.ones((Z_u.shape))])
            #TODO: WOrking on how to get this to add when the second image is 0s, and multiply when it's not

            #Z_u_labels.shape = (Z_u_labels.shape[0],1)
            #Z_u_overlaid = cm(Z_u_labels + Z_u)
            scipy.misc.imsave("Z_u_clusters" + str(k) + "_" + str(t) + ".png",
                              cm(Z_u_clusters, bytes=True))
            #pprint.pprint(Z_u_overlaid)
            #scipy.misc.imsave("Z_u_clusters_"+str(k)+".png",cm(Z_u_clusters, bytes=True))
            #scipy.misc.imsave("Z_u_nocorr_"+str(k)+".png",Z_u)
            scipy.misc.imsave("Z_u_" + str(k) + ".png", Z_u)
            Z_u_bin = sklearn.preprocessing.binarize(Z_u, t)
            #scipy.misc.imsave("Z_u_bin_nocorr_"+str(k)+"_"+str(t)+".png",Z_u_bin)
            scipy.misc.imsave("Z_u_bin_" + str(k) + "_" + str(t) + ".png",
                              Z_u_bin)

            from networkx.utils import reverse_cuthill_mckee_ordering
            rcm_order = qutip.reverse_cuthill_mckee(
                scipy.sparse.csr_matrix(1 - Z_u_bin)
            )  # This *should* produce a good diagonalisation? Hasn't been tested.

            Z_u_rcm = Z_u[rcm_order, :]
            Z_u_rcm = Z_u_rcm[:, rcm_order]
            Z_u_clst = Z_u[clustersort, :]
            Z_u_clst = Z_u_clst[:, clustersort]
            #Z_u_bin_rcm = Z_u_bin[rcm_order,:]
            #Z_u_bin_rcm = Z_u_bin_rcm[:,rcm_order]
            Z_u_clusters_rcm = Z_u_clusters[rcm_order, :]
            Z_u_clusters_rcm = Z_u_clusters_rcm[:, rcm_order]
            Z_u_clusters_clst = Z_u_clusters[clustersort, :]
            Z_u_clusters_clst = Z_u_clusters_clst[:, clustersort]
            #Z_u_overlaid = Z_u_overlaid[rcm_order,:,:]
            #Z_u_overlaid = Z_u_overlaid[:,rcm_order,:]
            #O_by_O_rcm = O_by_O[rcm_order,:]
            #O_by_O_rcm = O_by_O_rcm[:,rcm_order]
            #scipy.misc.imsave("Z_u_rcm_nocorr_"+str(k)+"_"+str(t)+".png",Z_u_rcm)
            #scipy.misc.imsave("O_by_O_rcm_nocorr_"+str(k)+"_"+str(t)+".png",O_by_O_rcm)
            scipy.misc.imsave("Z_u_rcm_" + str(k) + "_" + str(t) + ".png",
                              Z_u_rcm)
            scipy.misc.imsave("Z_u_clst_" + str(k) + "_" + str(t) + ".png",
                              Z_u_clst)
            #scipy.misc.imsave("Z_u_bin_rcm_"+str(k)+"_"+str(t)+".png",Z_u_bin_rcm)
            #scipy.misc.imsave("Z_u_overlaid_rcm"+str(k)+"_"+str(t)+".png",Z_u_overlaid)
            scipy.misc.imsave(
                "Z_u_clusters_rcm_" + str(k) + "_" + str(t) + ".png",
                cm(Z_u_clusters_rcm, bytes=True))
            scipy.misc.imsave(
                "Z_u_clusters_clst_" + str(k) + "_" + str(t) + ".png",
                cm(Z_u_clusters_clst, bytes=True))
            #scipy.misc.imsave("Z_u_bin_rcm_"+str(k)+"_"+str(t)+".png",Z_u_bin_rcm)
            #scipy.misc.imsave("Z_u_clusters_rcm_"+str(k)+"_"+str(t)+".png",cm(Z_u_clusters_rcm, bytes=True))
            #scipy.misc.imsave("O_by_O_rcm_"+str(k)+"_"+str(t)+".png",O_by_O_rcm)
            print "Reorder and image saving complete"
            #Now need to do a k-means on the matrices, pluck out groups, and then describe them.  Also want to pluck groupings at different sizes.
    sys.exit()

    print model_inspector.deep_recon_errors(model["training_data"].X, topmodel)
    errors = np.zeros(
        (model["training_data"].X.shape[0], len(topmodel['layers'])))
    Os = []
    O_recons = []

    for i, x in enumerate(model["training_data"].X):
        Os.append(x)
        errors[i] = model_inspector.deep_recon_errors(x, topmodel)[-1]
        O_recons.append(model_inspector.reconstruct(topmodel, x))