pnnl / HyperNetX

Python package for hypergraph analysis and visualization.
https://hypernetx.readthedocs.io
Other
506 stars 86 forks source link

Started work on support for weighted hypergraphs #36

Closed jim-rafferty closed 3 years ago

jim-rafferty commented 4 years ago

Hi,

I thought I would try to make it a bit easier to create a weighted hypergraph, using a similar syntax to that in networkx. Now you can add weighted edges like this:

w = 0.019281
G = hnx.Hypergraph()
G.add_edge(("A", "B"), weight=w)

I've also added support for calculating the weighted node adjacency matrix:

w0 = 0.98279384
w1 = 0.2309
G = hnx.Hypergraph()
e0 = [("A", "B"), ("B", "C")]
e1 = [("A", "B", "C")]
G.add_edges_from(e0, weight=w0)
G.add_edges_from(e1, weight=w1)

print(G.adjacency_matrix())
print(G.adjacency_matrix(weight="weight"))

I have an issue with the edge adjacency matrix, because I would ordinarily calculate it with the following matrix formula: \sqrt(W) M^T M * \sqrt(W), but this won't currently work as edge_adjacency_matrix calls __incidence_to_adjacency, the same method as is called by adjacency_matrix. I would be grateful for your thoughts on this, I've currently left the weighted edge adjacency to throw a NotImplementedError.

All of these changes play quite nicely with the existing code, but I wanted to submit them for your consideration before doing too much more.

Many thanks. :)

brendapraggastis commented 4 years ago

Thanks Jim! Great to have you contributing. :) We will review the changes and let you know how we will incorporate them.

jim-rafferty commented 3 years ago

Upon reflection, I think the right way to handle weights in __incidence_to_adjacency is to pass in M for unweighted hypergaphs and M.dot(np.diag(weight)) for weighted hypergraphs, rather than handling the weighted case inside the __incidence_to_adjacency function. That way it should work for the hypergraph and the dual hypergraph without modification.

brendapraggastis commented 3 years ago

@jim-rafferty Could you push a branch to the repository and make a pull request to develop from the branch? We need to pull and test before we can integrate. Thanks.

jim-rafferty commented 3 years ago

Hi @brendapraggastis , just so I'm clear about what you want me to do:

Fork pnnl/HyperNetX to jim-rafferty/HyperNetX create a new branch in jim-rafferty/HyperNetX and push changes to that branch Send you a PR for those changes so they are not automatically merged into master.

Just a thumbs up is fine if this is right, thanks :)

bpraggastis commented 3 years ago

@jim-rafferty That sounds right. You are the second outside contributor to the project and the first from outside the Lab so we are still working on protocols. We are also creating a major rewrite for scaling HNX to larger hypergraphs, so stay tuned. Hopefully you will see a major lift by Christmas.

jim-rafferty commented 3 years ago

Oh, I reset the head of the master back to where it started and it automatically closed the PR?! Cool! :)

That's great to hear about being the first outside contributor to the project. I hope it goes well, and paves the way for more people to be involved going forward. I will open another PR with the code in a new branch.