braindynamicslab / dyneusr

Dynamical Neuroimaging Spatiotemporal Representations (DyNeuSR)
https://braindynamicslab.github.io/dyneusr
BSD 3-Clause Clear License
43 stars 15 forks source link

DyNeuGraph error due to networkx changes #25

Open ErnestJohnston opened 9 months ago

ErnestJohnston commented 9 months ago

Hello,

I am implementing some example code from your website. It seems that it is throwing an error, because the networkx package was updated to use to_numpy_array() instead of to_numpy_matrix(). However, simply making this syntax change in the relevant dyneusr utility script does not fix the problem, as the error chain continues. I am going to try to use an older version of networkx for now, but I wanted to let you know about this in case you are able to update the source code with a fix. Thanks!

Example code with error message:

from dyneusr import DyNeuGraph
from dyneusr.datasets import make_trefoil
from dyneusr.tools import visualize_mapper_stages

import numpy as np 
import pandas as pd

from nilearn.datasets import fetch_haxby
from nilearn.input_data import NiftiMasker

from kmapper import KeplerMapper, Cover
from sklearn.manifold import TSNE
from sklearn.cluster import DBSCAN

# Fetch dataset, extract time-series from ventral temporal (VT) mask
dataset = fetch_haxby()
masker = NiftiMasker(
    dataset.mask_vt[0], 
    standardize=True, detrend=True, smoothing_fwhm=4.0,
    low_pass=0.09, high_pass=0.008, t_r=2.5,
    memory="nilearn_cache")
X = masker.fit_transform(dataset.func[0])

# Encode labels as integers
df = pd.read_csv(dataset.session_target[0], sep=" ")
target, labels = pd.factorize(df.labels.values)
y = pd.DataFrame({l:(target==i).astype(int) for i,l in enumerate(labels)})

# Generate shape graph using KeplerMapper
mapper = KeplerMapper(verbose=1)
lens = mapper.fit_transform(X, projection=TSNE(2))
graph = mapper.map(lens, X, cover=Cover(20, 0.5), clusterer=DBSCAN(eps=20.))

# Visualize the shape graph using DyNeuSR's DyNeuGraph                          
dG = DyNeuGraph(G=graph, y=y)
dG.visualize('dyneusr_output.html')

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Untitled-1.ipynb Cell 1 line 3
     [32](vscode-notebook-cell:Untitled-1.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=31) graph = mapper.map(lens, X, cover=Cover(20, 0.5), clusterer=DBSCAN(eps=20.))
     [34](vscode-notebook-cell:Untitled-1.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=33) # Visualize the shape graph using DyNeuSR's DyNeuGraph                          
---> [35](vscode-notebook-cell:Untitled-1.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=34) dG = DyNeuGraph(G=graph, y=y)
     [36](vscode-notebook-cell:Untitled-1.ipynb?jupyter-notebook#W0sdW50aXRsZWQ%3D?line=35) dG.visualize('dyneusr_output.html')

File [c:\Users\Ernest\miniconda3\envs\pyif\Lib\site-packages\dyneusr\core.py:50](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:50), in DyNeuGraph.__init__(self, **params)
     [28](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:28) """ DyNeuGraph
     [29](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:29) 
     [30](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:30) Parameters
   (...)
     [47](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:47)     dyneuG.visualize()
     [48](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:48) """
     [49](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:49) self.cache_ = dict(params)
---> [50](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:50) self.fit(**params)

File [c:\Users\Ernest\miniconda3\envs\pyif\Lib\site-packages\dyneusr\core.py:106](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:106), in DyNeuGraph.fit(self, G, X, y, node_data, edge_data, G_data, **kwargs)
    [104](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:104) # process graph
    [105](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:105) G = tools.process_graph(G, meta=y, **kwargs)
--> [106](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:106) A, M, TCM = tools.extract_matrices(G, index=data_ids)
    [108](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:108) # create graph from TCM
    [109](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/core.py:109) if G_data is True:
...
--> [420](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/tools/graph_utils.py:420) A = nx.to_numpy_matrix(G).A  # node x node
    [421](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/tools/graph_utils.py:421) M = np.zeros((nTR, A.shape[0]))    #   TR x node
    [422](file:///C:/Users/Ernest/miniconda3/envs/pyif/Lib/site-packages/dyneusr/tools/graph_utils.py:422) T = np.zeros((nTR, nTR))

AttributeError: module 'networkx' has no attribute 'to_numpy_matrix'
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?7372d79f-d26c-4711-8e36-38f0ab3ed62d) or open in a [text editor](command:workbench.action.openLargeOutput?7372d79f-d26c-4711-8e36-38f0ab3ed62d). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...
calebgeniesse commented 9 months ago

Hi @ErnestJohnston, thanks for reporting this! We are in the process of resolving this error in PR 24. I'll report back here when that PR is merged.

calebgeniesse commented 9 months ago

@ErnestJohnston, I just merged PR 24, so feel free to pull and let me know if you have any further issues. I will work on deploying a new pip installable version soon.

ErnestJohnston commented 9 months ago

Thank you! This new version seems to work fine.