Bipartite weighted network

Hello,

I am new to Julia (moving from Python). I am trying to generate a bipartite weighted network from a data frame. The data frame contains the “edgelist” (i.e., a column for bottom nodes, and a column for top nodes), and a column for the weights of the edge. I found the GraphDataFrameBridge package that creates a network from two columns of the data frame, but it creates a unipartite network, and it is not clear how to import the weights. Any suggestions?

Thank you very much!

If you have an edgelist representing a bipartite network (and you’ve verified that the graph represented by those edges is bipartite), I don’t know why GraphDataFrameBridge would be creating something that’s unipartite. Do you have a MWE you can share?

Thank you very much,
I was able to decompose the problem. Now I can import the edgelist as a bipartite network. The problem was that (if I understand correctly) GraphDataFrameBridge accepts only nodes’ labels of the same type. I initially had top nodes’ labels as “Floats” and bottom nodes as “Strings” and when I converted to the same type, some top and bottom nodes turned out to have the same label.

However, I still have 2 issues:

a) (even if is_bipartite(mg) now gives true), when I try to export the adjacency matrix from the example below, I still obtain a 7X7 unweighted matrix instead of a 4X3 weighted matrix.

using Distances
test=DataFrame(A = ["i", "j", "k", "z"], B = ["1", "2", "2", "3"], weights = [2,3,4,1])
mg = MetaGraph(test, :A, :B, weight=:weights)
is_bipartite(mg)
w=Array(adjacency_matrix(mg))
#R = pairwise(Euclidean(), w, dims=2)

b) I cannot figure out how to create a list of correspondence between the indexes assigned by MetaGraph and the labels of the original nodes. Something like:

1 “i”
2 “j”

6 “2”

Thank you very much for your help.

  • Don’t use adjacency_matrix. Use weights. You’re getting a 7x7 because you have 7 distinct vertex labels (i, j, k, z, 1, 2, 3). Adjacency matrices are square.

  • this will require further exploration.

Thank you. I tried weights(mg) but I got the following error in the example above:

MethodError: objects of type Array{Float64,1} are not callable
Use square brackets for indexing an Array.

Any suggestion on how I can output the nXm weighted matrix?

Regarding the second issue, I found the graft.jl that seems to produce the vertex table I was looking for (although it does not seem to be supported anymore). Basically I was wondering if it is possible to output the vertexid and the label of the id. My labels are i,j,k,z,1,2,3 but they are indexed and I do not know what node gets what index. For example, when I output the adjacency matrix I do not know whether the first row is for i,j,…etc. I need this information to merge the output of the network analysis with other variables. Hope I was able to explain myself.

Thank you again for any help.

julia> weights(mg) .* adjacency_matrix(mg)
7×7 Array{Float64,2}:
 0.0  0.0  0.0  2.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  3.0  4.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  1.0
 2.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  3.0  0.0  0.0  0.0  0.0  0.0
 0.0  4.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  0.0  0.0  0.0  0.0

julia> [props(mg, x)[:name] for x in vertices(mg)]
7-element Array{String,1}:
 "1"
 "2"
 "3"
 "i"
 "j"
 "k"
 "z"

Thanks! For some reason I still get the same error when I run
weights(mg) .* adjacency_matrix(mg):

MethodError: objects of type Array{Float64,1} are not callable
Use square brackets for indexing an Array.

That’s probably because you’ve locally defined a vector called weights that is overriding the function LightGraphs.weights.

1 Like