diff --git a/Project.toml b/Project.toml index 8746b0d..00d85a8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SimpleHypergraphs" uuid = "aa4a32ff-dd5d-5357-90e3-e7a9512f0501" authors = ["Przemysław Szufel ", "Bogumił Kamiński ", "Carmine Spagnuolo ", "Alessia Antelmi "] -version = "0.1.12" +version = "0.1.13" [deps] Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d" @@ -23,6 +23,6 @@ JSON3 = "1.0.1" LightGraphs = "1.3" PyCall = "1.91.2" PyPlot = "2.8.2" -StatsBase = "0.33" +StatsBase = "0.33.0" StructTypes = "1.0.1" julia = "1.0.0" diff --git a/src/SimpleHypergraphs.jl b/src/SimpleHypergraphs.jl index 7e213df..e1a7c63 100644 --- a/src/SimpleHypergraphs.jl +++ b/src/SimpleHypergraphs.jl @@ -41,31 +41,33 @@ export distance export HyperNetX, GraphBased export draw -const hnx = PyNULL() +const hnx = PyNULL() const nx = PyNULL() -const pynull = PyNULL() +const has_plotting = Ref(false) function __init__() - plot_ok = true + has_networkx = false + has_hypernetx = false try - copy!(nx, pyimport("networkx")) - catch e - @warn "Python networkx not found. Plotting functionality of HyperNetX will not work." - plot_ok = false - end - try - copy!(hnx, pyimport("hypernetx")) - catch e - @warn "Python HyperNetX not found. Plotting functionality will not work." - plot_ok = false - end + copy!(nx, pyimport("networkx")) + has_networkx = true + catch e; end + try + copy!(hnx, pyimport("hypernetx")) + has_hypernetx = true + catch e; end + has_plotting[] = has_networkx && has_hypernetx + if !has_plotting[] + @warn "The plotting functionality of HyperNetX will not work!\n"* + (has_networkx ? "" : "Conda Python networkx not found.\n")* + (has_hypernetx ? "" : "Conda Python HyperNetX not found.\n")* + "To test your installation try running `using PyCall;pyimport(\"networkx\");pyimport(\"hypernetx\")`" + end end function support_hypernetx() - return ((SimpleHypergraphs.nx != SimpleHypergraphs.pynull) && - (SimpleHypergraphs.hnx != SimpleHypergraphs.pynull)) - + return has_plotting[] end diff --git a/src/viz/drawing.jl b/src/viz/drawing.jl index f113ea3..bea25a2 100644 --- a/src/viz/drawing.jl +++ b/src/viz/drawing.jl @@ -200,10 +200,10 @@ function draw( with_node_labels::Bool=true, label_alpha::Float64=.35 ) - if (!SimpleHypergraphs.support_hypernetx()) - throw("HyperNetX is not installed in Python used by this Julia. Install HyperNetX and reload SimpleHypergraphs.jl") - end - + if (!SimpleHypergraphs.support_hypernetx()) + throw("HyperNetX is not installed in Python used by this Julia. Install HyperNetX and reload SimpleHypergraphs.jl") + end + h_hnx = _convert_to_hnx(h; node_labels=node_labels, edge_labels=edge_labels @@ -261,7 +261,6 @@ function _convert_to_hnx(h::Hypergraph; node_labels::Union{Dict{Int, String}, Nothing}=nothing, edge_labels::Union{Dict{Int, String}, Nothing}=nothing, ) - h_hnx = hnx.Hypergraph() for he=1:nhe(h) diff --git a/test/runtests.jl b/test/runtests.jl index 18b3588..d9c0e0a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -305,12 +305,13 @@ end; cfmr = CFModularityRandom(3,10000) - @test findcommunities(hg,cfmr) == - (bp = Set.([[4, 5, 9], [1, 3, 6, 7], [2, 8, 10]]), bm = 0.21505688117829677) + @test findcommunities(hg,cfmr).bm ≈ 0.21505688117829677 @test modularity(hg, Set.([1:10])) == 0.0 Random.seed!(1234); - @test randompartition(hg, 2) == Set.([[1, 5, 6, 7, 9], [2, 3, 4, 8, 10]]) - + @test typeof(randompartition(hg, 2)) == Vector{Set{Int}} + @test length(randompartition(hg, 2)) == 2 + @test sort(vcat(collect.(randompartition(hg, 2))...))==1:nhv(hg) + hh = Hypergraph{Bool}(7,4) hh[1,1] = true hh[2,1:2] .= true @@ -336,17 +337,17 @@ end; @test cfmr.reps == 10000 @test findcommunities(hh,cfmr).bm ≈ 16/81 Random.seed!(1234); - cnm = CFModularityCNMLike(100) - @test cnm.reps == 100 + cnm = CFModularityCNMLike(1000) + @test cnm.reps == 1000 @test findcommunities(hh, CFModularityRandom(4,10000)).bm ≈ findcommunities(hh, cnm).bm Random.seed!(0); - @test findcommunities(hh, cnm).bm ≈ 223/972 + @test any(isapprox.(findcommunities(hh, cnm).bm, [16/81, 223/972])) end; @testset "SimpleHypergraphs randomized tests " begin Random.seed!(0) - N = 100 + N = 500 res = Vector{Bool}(undef, N) for i in 1:N m1 = CFModularityCNMLike(100) @@ -357,7 +358,7 @@ end; bm2 = findcommunities(hh, m2).bm res[i] = (bm1 > bm2) end - @test sum(res) >= N*0.80 + @test sum(res) >= N*0.75 end @@ -426,10 +427,10 @@ end @testset "SimpleHypergraphs hypernetx bridge " begin - if (!SimpleHypergraphs.support_hypernetx()) - @warn "HyperNetX is not installed. Skipping hypernetx tests" - return - end + if (!SimpleHypergraphs.support_hypernetx()) + @warn "HyperNetX is not installed. Skipping hypernetx tests" + return + end h_hnx = SimpleHypergraphs._convert_to_hnx(h1) data = Dict{String, Array{Int, 1}}(