Skip to content

Adjacency matrices are not symmetric and correct if retrieved from CC #474

@max-kat

Description

@max-kat

Dear TopoModelX-Team,

I think I encountered a bug when using your toolbox with my data, using a combinatorial complex and higher attention network.
Basically, I create a CC and then want to retrieve the (co)adjacency matrices, and they seem to be wrong. I post a minimal example down below but the main takeaway is that the retrieved adjacency matrices are not symmetrical, are missing values, and from the 4th element in the diagonal onwards seem to incorporate the node degree as well. I am unsure whether I am doing sth particularly wrong and if so I would really know what! Thanks for your help!

import os
import numpy as np
import pandas as pd
import torch
import toponetx
import matplotlib.pyplot as plt

#create instance 
combi_comp_top = toponetx.CombinatorialComplex()
#add cells of rank 0 
for i in range(116):
    combi_comp_top.add_cell(i, rank=0)
#we add the other ranks along the way but we wanna make sure that the forzen set is in ascending order
#add features
df_autocorr = pd.read_csv("df_autocorr.csv")
x_0 = df_autocorr['lag1_mi'].values.reshape(-1, 1)

#get edge features
df_edges_top = pd.read_csv("df_edges_top.csv")
x_1 = df_edges_top['value'].values.reshape(-1, 1)
#add cells of rank 1
for _, row in df_edges_top.iterrows():
    i, j = int(row['i']), int(row['j'])
    combi_comp_top.add_cell([i, j], rank=1)

#get hyperedge features
df_zscore_top = df_triplets.nlargest(n_top, 'combined_z')
x_2 = df_zscore_top[["u1_z", "u2_z"]].to_numpy()
#add cells of rank 2
for _, row in df_zscore_top.iterrows():
    x,y,z = int(row['x']), int(row['y']),int(row['z'])
    combi_comp_top.add_cell([x,y,z], rank=2)


#get adjacency and inspect
a01 = combi_comp_top.adjacency_matrix(0, 1).todense()

#first you can see that the matrix is not symmetric, which it should be 
is_symmetric = np.allclose(a01, a01.T)
print(f"Matrix is symmetric: {is_symmetric}") 

#looking at the matrix one can see this is not an adjacency matrix as it has values along the diagonal
plt.imshow(a01, cmap='gray')
plt.show()  

#looking at the diagonal more in detail, we see that starting with the 4th element the node degree seems to be incorporated?
print(a01[0:10,0:10])

#now let's compare the adjacency of 0,1 to one create by hand 
matrix = np.zeros((116, 116), dtype=int)
for _, row in df_edges_top.iterrows():
    i, j = int(row['i']), int(row['j'])
    matrix[i, j] = 1
    matrix[j, i] = 1
print(matrix[0:10,0:10])


#set the diagonal of the original adjacency from topomodel to 0 
np.fill_diagonal(a01, 0) 
plt.imshow(a01, cmap='gray')
plt.show() 
plt.imshow(matrix, cmap='gray')
plt.show()  
#looking at them side by side one can see that they share largely the main structure but they are off... 
#not sure what is happening there with the casting of the elements

#I observed the same problem for adjacency(1,2) and coadjacency(2,1)
#I think that this behavior is not particularly related to my data structure but I only played around a little bit with that
#so is this a bug or based on some weird operations I do?

df_autocorr.csv
df_edges_top.csv
df_zscore_top.csv

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions