NetworkInference.jl icon indicating copy to clipboard operation
NetworkInference.jl copied to clipboard

How to get the weight matrix?

Open Vivianstats opened this issue 5 years ago • 3 comments

Hello!

I'm not very familiar with Julia, and I wonder after I get the inferred_network object, instead of obtaining the adjacency matrix with different thresholds, is there a way to get the full weight matrix between nodes?

Vivianstats avatar Oct 30 '19 16:10 Vivianstats

Any suggestions?

Vivianstats avatar Nov 04 '19 03:11 Vivianstats

Hi, thanks for the question. There's not currently a way to do that via the library, but it's a good idea for a feature request, so lets keep this issue open.

In the meantime, you could do something like the following, modified from get_adjacency_matrix:

number_of_nodes = length(inferred_network.nodes)
weight_matrix = zeros(number_of_nodes, number_of_nodes)

labels_to_ids = Dict{String,Int}()
ids_to_labels = Dict{Int,String}()
i = 1
for (i, node) in enumerate(inferred_network.nodes)
    labels_to_ids[node.label] = i
    ids_to_labels[i] = node.label
    i += 1
end

for edge in inferred_network.edges
    node1 = labels_to_ids[edge.nodes[1].label]
    node2 = labels_to_ids[edge.nodes[2].label]
    weight_matrix[node1, node2] = edge.weight
    weight_matrix[node2, node1] = edge.weight
end

Note that this will give a symmetrical matrix with 0 along the leading diagonal.

Tchanders avatar Nov 04 '19 12:11 Tchanders

Thank you very much. That's really helpful.

Vivianstats avatar Nov 07 '19 04:11 Vivianstats