pnnl / HyperNetX

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

the _s_centrality function is overwriting the dictionary. #151

Closed robotheus closed 3 weeks ago

robotheus commented 4 months ago

Sorry for my bad english.

In this [1] code snippet when the line "if h.shape[edges * 1] == 1:" encounters a singleton the assignment stats = {v: 0 for v in vertices} overwrites the "stats" dictionary. In my tests, when using a graph with 5000 edges and the last edge checked had a centrality equal to 0, as it was a singleton, I obtained a dictionary with only one edge and centrality value.

To fix this, just use the same update method used in else, so that it is equal to [2]. This way I obtained a complete dictionary, with all the edges of the hypergraph and their centralities.

[1] - buggy code

stats = dict()
    for h in comps:
        if edges:
            vertices = h.edges
        else:
            vertices = h.nodes

        if h.shape[edges * 1] == 1:
            stats = {v: 0 for v in vertices}
        else:
            g = h.get_linegraph(s=s, edges=edges)
            stats.update({k: v for k, v in func(g, **kwargs).items()})
        if f:
            return {f: stats[f]}

    return stats

[2] - corrected code:

stats = dict()
    for h in comps:
        if edges:
            vertices = h.edges
        else:
            vertices = h.nodes

        if h.shape[edges * 1] == 1:
            stats.update({v: 0 for v in vertices})
        else:
            g = h.get_linegraph(s=s, edges=edges)
            stats.update({k: v for k, v in func(g, **kwargs).items()})

    return stats

ty for the library.

brendapraggastis commented 3 weeks ago

@robotheus Good catch!! we will correct. :)

bonicim commented 3 weeks ago

Fixed in v2.3.4