예제 #1
0
def A_square(A, logger, args):
    try:
        transform = args["prox_params"]['transform']
        threshold = args["prox_params"]['threshold']
        power = args["prox_params"]['power']
    except KeyError:
        transform = 0
        power = 2
    if args["embed_option"] == "proximity":
        m = A
        for i in range(power - 1):
            m = np.dot(m, A)
        if log_transform == 1:
            return {0: log_filter(sp.csr_matrix(m))}
        else:
            return {0: sp.csr_matrix(m)}
    elif args["embed_option"] == "struct":
        A = A.todense()
        M = {}
        for i in range(power):
            temp = sp.csr_matrix(np.linalg.matrix_power(A, i + 1))
            if transform == 1:
                M[i] = log_filter(temp)
            elif transform == 2:
                M[i] = binary_filter(temp, logger, threshold)
            else:
                M[i] = temp
        return M
예제 #2
0
파일: PPMI.py 프로젝트: hanbei969/PhUSION
def direct_compute_deepwalk_matrix(A, args, logger):
    res = {}
    try:
        windows = args["prox_params"]['window']
        b = args["prox_params"]['negative']
        transform = args["prox_params"]['transform']
        threshold = args["prox_params"]['threshold']
    except KeyError:
        raise MissingParamError
    for window in windows:
        n = A.shape[0]
        vol = float(A.sum())
        L, d_rt = csgraph.laplacian(A, normed=True, return_diag=True)
        # X = D^{-1/2} A D^{-1/2}
        X = sparse.identity(n) - L
        S = np.zeros_like(X)
        X_power = sparse.identity(n)
        for i in range(window):
            logger.info("Deep Walk %d-th power, %d/%d", i+1, i+1, window)
            X_power = X_power.dot(X)
            S += X_power
        S *= vol / window / b
        D_rt_inv = sparse.diags(d_rt ** -1)
        M = D_rt_inv.dot(D_rt_inv.dot(S).T)
        m = T.matrix()
        if transform == 1:
            logger.info("log transform")
            res[window] = log_filter(M, threshold)
            # res[window] = log_filter(M)
        elif transform == 2:
            res[window] = binary_filter(M, logger, threshold)
        else:
            logger.info("no transform")
            res[window] = sparse.csr_matrix(M)
    return res
예제 #3
0
파일: RWTM.py 프로젝트: hanbei969/PhUSION
def RWTM(matrix, logger, args):
    try:
        transform = args["prox_params"]['transform']
        threshold = args["prox_params"]['threshold']
        power = args["prox_params"]['power']
    except KeyError:
        transform = 0
        power = 2
    if args["embed_option"] == "proximity":
        posinv = np.vectorize(lambda x: float(1.0) / np.sqrt(x)
                              if x > 1e-10 else 0.0)
        D_inv = sc.sparse.diags(
            1 / np.array(posinv(matrix.sum(0))).reshape([
                -1,
            ]), 0)
        logger.info('d1' + str(power))
        R = D_inv.dot(matrix)
        logger.info(R.shape)
        L = R
        logger.info(L.shape)
        if transform == 1:
            return {0: log_filter(sparse.csr_matrix(L), threshold)}
        elif transform == 2:
            return {0: binary_filter(sparse.csr_matrix(L), logger, threshold)}
        else:
            return {0: sparse.csr_matrix(L)}
    elif args["embed_option"] == "struct":
        matrix = matrix.todense()
        posinv = np.vectorize(lambda x: float(1.0) / np.sqrt(x)
                              if x > 1e-10 else 0.0)
        D = sc.sparse.diags(
            np.array(posinv(matrix.sum(0))).reshape([
                -1,
            ]), 0)
        R = pinv(D.todense()).dot(matrix)
        L = R.copy()
        M = {}
        for i in range(power):
            temp = sparse.csr_matrix(np.linalg.matrix_power(L, i + 1))
            if transform == 1:
                M[i] = log_filter(temp, threshold)
            elif transform == 2:
                M[i] = binary_filter(temp, logger, threshold)
            else:
                M[i] = temp
        return M
예제 #4
0
파일: L_inv.py 프로젝트: hanbei969/PhUSION
def inv_lap(matrix, args, logger):
    try:
        transform = args["prox_params"]['transform']
        threshold = args["prox_params"]['threshold']
    except KeyError:
        transform = 0
    L = sparse.csr_matrix(pinv(laplacian(matrix).todense()))
    if transform == 1:
        return {0: log_filter(L, threshold)}
    elif transform == 2:
        return {0: binary_filter(L, logger, threshold)}
    else:
        return {0: L}
예제 #5
0
def A_A_square(A, logger, args):
    try:
        log_transform = args['transform']
        threshold = args['threshold']
        power = args['power']
    except KeyError:
        log_transform = 0
        power = 2
    M = A + 0.5 * np.dot(A, A)
    if log_transform == 1:
        print('\n\n\n\n1\n\n\n\n')
        return {0: log_filter(sp.csr_matrix(M), threshold)}
    elif log_transform == 2:
        print('\n\n\n\n2\n\n\n\n')
        return {0: binary_filter(sp.csr_matrix(M), logger, threshold)}
    else:
        return {0: sp.csr_matrix(M)}
예제 #6
0
def belief_propgation(A, args, logger):
    '''
    use Fast Belief Propagatioin
    CITATION: Danai Koutra, Tai-You Ke, U. Kang, Duen Horng Chau, Hsing-Kuo
    Kenneth Pao, Christos Faloutsos
    Unifying Guilt-by-Association Approaches
    return [I+a*D-c*A]^-1
    '''
    try:
        transform = args["prox_params"]['transform']
        threshold = args["prox_params"]['threshold']
        scale1 = args["prox_params"]['scale1']
        scale2 = args["prox_params"]['scale2']
    except KeyError:
        transform = 0
        scale1 = 1
        scale2 = 1
    I = sparse.identity(A.shape[0])  # identity matirx
    D = sparse.diags(sum(A).toarray(), [0])  # diagonal degree matrix

    c1 = np.trace(D.toarray()) + 2
    c2 = np.trace(np.square(D).toarray()) - 1
    h_h = math.sqrt((-c1 + math.sqrt(c1 * c1 + 4 * c2)) / (8 * c2))

    a = scale1 * 4 * h_h * h_h / (1 - 4 * h_h * h_h)
    c = scale2 * 2 * h_h / (1 - 4 * h_h * h_h)

    # M=I-c*A+a*D
    # S=inv(M.toarray())
    '''
    compute the inverse of matrix [I+a*D-c*A]
    use the method propose in Unifying Guilt-by-Association equation 5
    '''
    M = c * A - a * D
    S = I
    mat = M
    power = 1
    while np.amax(M.toarray()) > 10**(-9) and power < 7:
        S = S + mat
        mat = mat * M
        power += 1
    if transform == 1:
        S = log_filter(S, threshold)
    elif transform == 2:
        S = binary_filter(S, logger, threshold)
    return {0: S}
예제 #7
0
파일: PPR.py 프로젝트: hanbei969/PhUSION
def PPR(A, args, logger):
    # beta: higher order coefficient
    # default value of beta = 0.01
    try:
        beta = args['beta']
        transform = args['transform']
        threshold = args['threshold']
    except KeyError:
        beta = 0.01
        transform = 0
    A = np.array(A.todense())
    n_nodes, _ = A.shape
    M_g = np.eye(n_nodes) - beta * A
    M_l = beta * A
    S = np.dot(np.linalg.inv(M_g), M_l)
    if transform == 1:
        return {0: log_filter(sparse.csr_matrix(S), threshold)}
    elif transform == 2:
        return {1: binary_filter(sparse.csr_matrix(S), logger, threshold)}
    else:
        return {0: sparse.csr_matrix(S)}
예제 #8
0
def InverseMatrix(A, args):
    '''
    use Fast Belief Propagatioin
    CITATION: Danai Koutra, Tai-You Ke, U. Kang, Duen Horng Chau, Hsing-Kuo
    Kenneth Pao, Christos Faloutsos
    Unifying Guilt-by-Association Approaches
    return [I+a*D-c*A]^-1
    '''
    try:
        log_transform = args['log_transform']
    except KeyError:
        log_transform = 0
    I = identity(A.shape[0])  # identity matirx
    D = diags(sum(A).toarray(), [0])  # diagonal degree matrix

    c1 = trace(D.toarray()) + 2
    c2 = trace(square(D).toarray()) - 1
    h_h = sqrt((-c1 + sqrt(c1 * c1 + 4 * c2)) / (8 * c2))

    a = 4 * h_h * h_h / (1 - 4 * h_h * h_h)
    c = 2 * h_h / (1 - 4 * h_h * h_h)

    # M=I-c*A+a*D
    # S=inv(M.toarray())
    '''
    compute the inverse of matrix [I+a*D-c*A]
    use the method propose in Unifying Guilt-by-Association equation 5
    '''
    M = c * A - a * D
    S = I
    mat = M
    power = 1
    while amax(M.toarray()) > 10**(-9) and power < 7:
        S = S + mat
        mat = mat * M
        power += 1
    if log_transform == 1:
        return {0: log_filter(sparse.csr_matrix(S))}
    else:
        return {0: S}
예제 #9
0
def heat_diffusion_ind(graph, args, logger):
    '''
    This method computes the heat diffusion waves for each of the nodes
    INPUT:
    -----------------------
    graph    :    Graph (etworkx)
    taus     :    list of scales for the wavelets. The higher the tau,
                  the better the spread of the heat over the graph
    order    :    order of the polynomial approximation
    proc     :    which procedure to compute the signatures (approximate == that
                  is, with Chebychev approx -- or exact)
    logger   :    The information logger
    OUTPUT:
    -----------------------
    heat     :     tensor of length  len(tau) x n_nodes x n_nodes
                   where heat[tau,:,u] is the wavelet for node u
                   at scale tau
    taus     :     the associated scales
    '''
    # Read parameters
    try:
        taus = args["prox_params"]["taus"]
        proc = args["prox_params"]["proc"]
        order = args["prox_params"]["order"]
        transform = args["prox_params"]["transform"]
        threshold = args["prox_params"]["threshold"]
    except KeyError:
        raise MissingParamError
    ETA_MAX = 0.95
    ETA_MIN = 0.80
    if taus == 'auto':
        lap = laplacian(nx.adjacency_matrix(graph))
        try:
            l1 = np.sort(
                sc.sparse.linalg.eigsh(lap,
                                       2,
                                       which='SM',
                                       return_eigenvectors=False))[1]
        except:
            l1 = np.sort(
                sc.sparse.linalg.eigsh(lap,
                                       5,
                                       which='SM',
                                       return_eigenvectors=False))[1]
        smax = -np.log(ETA_MIN) * np.sqrt(0.5 / l1)
        smin = -np.log(ETA_MAX) * np.sqrt(0.5 / l1)
        taus = [smin, smax / 2 + smin / 2, smax]
        taus = np.array(taus)
        print('here ', taus)
    # Compute Laplacian
    a = nx.adjacency_matrix(graph)
    n_nodes, _ = a.shape
    thres = np.vectorize(lambda x: x if x > 1e-4 * 1.0 / n_nodes else 0)
    lap = laplacian(a)
    n_filters = len(taus)
    if proc == 'exact':
        ### Compute the exact signature
        lamb, U = np.linalg.eigh(lap.todense())
        heat = {}
        for i in range(n_filters):
            heat[i] = U.dot(np.diagflat(np.exp(-taus[i] *
                                               lamb).flatten())).dot(U.T)
    else:
        heat = {
            i: sc.sparse.csc_matrix((n_nodes, n_nodes))
            for i in range(n_filters)
        }
        monome = {0: sc.sparse.eye(n_nodes), 1: lap - sc.sparse.eye(n_nodes)}
        for k in range(2, order + 1):
            logger.info("Heat diffusion %d-th order: %d/%d", k, k, order)
            monome[k] = 2 * (lap - sc.sparse.eye(n_nodes)).dot(
                monome[k - 1]) - monome[k - 2]
        for i in range(n_filters):
            logger.info("Heat diffusion %d-th filter: %d/%d", i + 1, i + 1,
                        n_filters)
            coeffs = compute_cheb_coeff_basis(taus[i], order)
            heat[i] = sc.sum(
                [coeffs[k] * monome[k] for k in range(0, order + 1)])
            temp = sc.sparse.csc_matrix(thres(
                heat[i].A))  # cleans up the small coefficients
            if transform == 1:
                logger.info("log transform")
                """m = T.matrix()
                f = theano.function([m], T.log(T.maximum(m, 0)))
                Y = f(temp.todense().astype(theano.config.floatX))"""
                heat[i] = log_filter(temp, threshold)
            elif transform == 2:
                heat[i] = binary_filter(temp, logger, threshold)
            else:
                heat[i] = temp
    return heat, taus