Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
chengwuxinlin authored Jun 26, 2023
1 parent 59e8d38 commit fa56fe3
Showing 2 changed files with 205 additions and 0 deletions.
133 changes: 133 additions & 0 deletions SPADE_score/SPF.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
include("decompositionW2.jl")
include("Clique_sm.jl")
include("mx_func.jl")
include("StarW.jl")
include("Filter_fast2.jl")
include("h_scoreW.jl")
include("INC3.jl")
include("sparsification.jl")
include("HyperNodes.jl")
include("Mapping_fast.jl")
include("Unmapping.jl")
include("decompositionW2.jl")



using SparseArrays
using LinearAlgebra
using Clustering
using NearestNeighbors
using Distances
using Laplacians
using Arpack
using Statistics
using DelimitedFiles
using StatsBase
using Laplacians#master
using Random

using PyCall

function SPF(PyA, L, ICr)

m, n = PyA.shape
colPtr = Int[i+1 for i in PyArray(PyA."indptr")]
rowVal = Int[i+1 for i in PyArray(PyA."indices")]
nzVal = Vector{Float64}(PyArray(PyA."data"))
A = SparseMatrixCSC{Float64,Int64}(m, n, colPtr, rowVal, nzVal)

fdnz = findnz(tril(A, -1))

rr = fdnz[1]
cc = fdnz[2]
W = fdnz[3]

ar = Any[]

for ii = 1:length(rr)

push!(ar, sort([rr[ii], cc[ii]]))

end

ar_org = copy(ar)

RedR = 1

#@time ar, idx_mat, ar_mat, Emat = decompositionW2(ar, L, RedR, W)
@time ar, idx_mat, ar_mat, Emat = decompositionW2_fast(A, L)

avgN = mx_func(ar_org) / mx_func(ar)
println("-------------avgN = ", avgN)

Lmat = length(ar_mat)

NN = [1,Lmat]

arF, V = sparsification(NN, ar, idx_mat, ar_mat)

## Adding inter-cluster edges
V = 1:mx_func(ar)

for ii = 1:length(idx_mat)

id1 = idx_mat[end-ii+1]

V = V[id1]

end

dict2 = Dict{Any, Any}()

for ii =1:length(ar_org)

nd1 = sort(ar_org[ii])

Vnd = sort(V[nd1])

if Vnd[1]!=Vnd[2]

vals = get!(Array{Int64,1},dict2, Vnd)

push!(vals, ii)

end

end # for ii

KS = collect(keys(dict2))

VL = collect(values(dict2))

EM1 = Emat[1]

for ii = 1:length(VL)

vl1 = VL[ii]

SP = sortperm(EM1[vl1])

TH = ceil(Int, length(SP) * ICr)

ES = vl1[SP[1:TH]]

append!(arF, ar_org[ES])

end

println("-------------Density = ", length(arF)/mx_func(arF))

AS = Clique_sm(arF)

# Import the required Python library
scipy_sparse = pyimport("scipy.sparse")
# Extract the components of the SparseMatrixCSC and adjust indices for 0-based Python
indptr = AS.colptr .- 1 # Subtract 1 for Python's 0-based indexing
indices = AS.rowval .- 1 # Subtract 1 for Python's 0-based indexing
data = AS.nzval
# Create a scipy.sparse.csr_matrix in Python directly using Julia arrays
PyAS = scipy_sparse.csr_matrix((data, indices, indptr), shape=(AS.m, AS.n))

return PyAS

end
72 changes: 72 additions & 0 deletions SPADE_score/eigen.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using LinearAlgebra
using LinearMaps
using MAT
using SparseArrays
using Arpack

using PyCall, SparseArrays

function scipyCSC_to_julia(A)
m, n = A.shape
colPtr = Int[i+1 for i in PyArray(A."indptr")]
rowVal = Int[i+1 for i in PyArray(A."indices")]
nzVal = Vector{Float64}(PyArray(A."data"))
B = SparseMatrixCSC{Float64,Int}(m, n, colPtr, rowVal, nzVal)
return PyCall.pyjlwrap_new(B)
end


function main(PyX, PyY, k::Int64)

rowval = Int[i+1 for i in PyArray(PyX."row")]
colval = Int[i+1 for i in PyArray(PyX."col")]
Val = Vector{Float64}(PyArray(PyX."data"))
X = sparse(rowval, colval, Val)

rowval = Int[i+1 for i in PyArray(PyY."row")]
colval = Int[i+1 for i in PyArray(PyY."col")]
Val = Vector{Float64}(PyArray(PyY."data"))
Y = sparse(rowval, colval, Val)



(Λ, V) = eigs(X, Y, nev=k, tol=1e-6, which=:LM)

return Λ, V
end

function not_main(PyX, k::Int64)

rowval = Int[i+1 for i in PyArray(PyX."row")]
colval = Int[i+1 for i in PyArray(PyX."col")]
Val = Vector{Float64}(PyArray(PyX."data"))
X = sparse(rowval, colval, Val)

(Λ, V) = eigs(X, nev=k, which=:SM,maxiter=500000)

idx = sortperm(abs.(Λ))
Λ_sorted = Λ[idx]
V_sorted = V[:, idx]

# Remove the first (smallest) eigenvalue and corresponding eigenvector
Λ_sorted = Λ_sorted[2:end]
V_sorted = V_sorted[:, 2:end]
return Λ_sorted, V_sorted
end

function plot_main(PyX, k::Int64)

rowval = Int[i+1 for i in PyArray(PyX."row")]
colval = Int[i+1 for i in PyArray(PyX."col")]
Val = Vector{Float64}(PyArray(PyX."data"))
X = sparse(rowval, colval, Val)

(Λ, V) = eigs(X, nev=k, which=:SM,maxiter=500000)

idx = sortperm(abs.(Λ))
Λ_sorted = Λ[idx]
V_sorted = V[:, idx]

return Λ_sorted, V_sorted
end

0 comments on commit fa56fe3

Please sign in to comment.