Closed cperezln closed 1 year ago
Hi @cperezln, thanks for you question. This is expected behaviour, two things are happening here.
index=True
, which you used. If you can tell pandas to use the node labels with:
df = pd.DataFrame(a.toarray(), index=r.values(), columns=r.values())
then you get:
from which you can see that node 7 is indeed the most connected, as expected.
To control the default order from point 1., you can always add nodes before the edges with
H = xgi.Hypergraph()
H.add_nodes_from(range(1,8))
H = xgi.add_edges_from([[1, 2, 3, 7], [4], [5, 6, 7]])
This way the nodes will be ordered in ascending order. Converting the adjacency matrix to a Dataframe without specifying the index will still go from 0-6 instead of 1-7 though.
One more thing: all the functions you used can be called directly from xgi.
: xgi.draw()
and xgi.adjacency_matrix()
.
I hope this answers your questions.
Thanks for this suggestion, @cperezln! Here's my thought: let's not fix this but describe ways to permute rows/columns in numpy/scipy/pandas in the header of matrix.py so that there's good documentation on this?
Thank you so much @maximelucas for your help - that was what I needed at the moment. And @nwlandry I haven't been working on this since I asked the issue so, now that I am going deeper on this (and I will need to have a ordered structure for the nodes and computation) I will the ways of doing it you've propossed me. I will let you know how it goes
I wrote this function to sort by the dicts that all the matrix functions output:
def sort_matrix_by_dicts(M, rowdict, coldict):
mat = M.copy()
try:
mat = mat[sorted(rowdict, key=rowdict.get), :]
except ValueError:
warn("Sorting rows unsuccessful")
try:
mat = mat[:, sorted(coldict, key=coldict.get)]
except:
warn("Sorting columns unsuccessful")
return mat
Should I add a code snippet to the comments in header of the matrix.py file or add it to the utilities.py file?
Perhaps a comment makes the most sense.
i have this code
h = xgi.Hypergraph([[1, 2, 3, 7], [4], [5, 6, 7]]) xpl.draw(h, node_labels = True) a, r = mx.adjacency_matrix(h, index = True) df = pd.DataFrame(a.toarray())
and the output of it, in images from xpl draw (being xpl from xgi.drawing import xgi_pylab as xpl) and the dataframe are as you can see, the most connected node in the plot is 7, but in the dataframe its not. also, they seem to be unordered. why is this?