aristoteleo / dynamo-release

Inclusive model of expression dynamics with conventional or metabolic labeling based scRNA-seq / multiomics, vector field reconstruction and differential geometry analyses
https://dynamo-release.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
414 stars 58 forks source link

Most probably path predictions: error message: 'networkx.convert_matrix' has no attribute 'from_scipy_sparse_matrix' #673

Closed fengshuoliu closed 6 months ago

fengshuoliu commented 6 months ago

@Xiaojieqiu Dear Team, really nice work and I'm excited to be able to use Dynamo in my analysis.

I ran into an error message " module 'networkx.convert_matrix' has no attribute 'from_scipy_sparse_matrix'", when I do Most probably path predictions, for both my own dataset and the demo dataset provided in package.

specifically I encounter this error for the following chunk of code:


%%capture

dyn.dynamo_logger.main_silence()
transition_graph = {}
cell_type = ["HSC", "Meg", "Ery", "Bas", "Mon", "Neu"]
start_cell_indices = [
    HSC_cells_indices,
    Meg_cells_indices,
    Ery_cells_indices,
    Bas_cells_indices,
    Mon_cells_indices,
    Neu_cells_indices,
]
end_cell_indices = start_cell_indices
for i, start in enumerate(start_cell_indices):
    for j, end in enumerate(end_cell_indices):
        if start is not end:
            min_lap_t = True if i == 0 else False
            dyn.pd.least_action(
                adata_labeling,
                [adata_labeling.obs_names[start[0]][0]],
                [adata_labeling.obs_names[end[0]][0]],
                basis="umap",
                adj_key="X_umap_distances",
                min_lap_t= min_lap_t,
                EM_steps=2,
            )
            dyn.pl.least_action(adata_labeling, basis="umap")
            lap = dyn.pd.least_action(
                adata_labeling,
                [adata_labeling.obs_names[start[0]][0]],
                [adata_labeling.obs_names[end[0]][0]],
                basis="pca",
                adj_key="cosine_transition_matrix",
                min_lap_t=min_lap_t,
                EM_steps=2,
            )
            dyn.pl.kinetic_heatmap(
                adata_labeling,
                basis="pca",
                mode="lap",
                genes=adata_labeling.var_names[adata_labeling.var.use_for_transition],
                project_back_to_high_dim=True,
            )
            # The `GeneTrajectory` class can be used to output trajectories for any set of genes of interest
            gtraj = dyn.pd.GeneTrajectory(adata_labeling)
            gtraj.from_pca(lap.X, t=lap.t)
            gtraj.calc_msd()
            ranking = dyn.vf.rank_genes(adata_labeling, "traj_msd")

            print(start, "->", end)
            genes = ranking[:5]["all"].to_list()
            arr = gtraj.select_gene(genes)

            dyn.pl.multiplot(lambda k: [plt.plot(arr[k, :]), plt.title(genes[k])], np.arange(len(genes)))

            transition_graph[cell_type[i] + "->" + cell_type[j]] = {
                "lap": lap,
                "LAP_umap": adata_labeling.uns["LAP_umap"],
                "LAP_pca": adata_labeling.uns["LAP_pca"],
                "ranking": ranking,
                "gtraj": gtraj,
            }

error message:


AttributeError Traceback (most recent call last) Cell In[45], line 17 15 if start is not end: 16 min_lap_t = True if i == 0 else False ---> 17 dyn.pd.least_action( 18 adata_labeling, 19 [adata_labeling.obs_names[start[0]][0]], 20 [adata_labeling.obs_names[end[0]][0]], 21 basis="umap", 22 adj_key="X_umap_distances", 23 min_lap_t= min_lap_t, 24 EM_steps=2, 25 ) 26 dyn.pl.least_action(adata_labeling, basis="umap") 27 lap = dyn.pd.least_action( 28 adata_labeling, 29 [adata_labeling.obs_names[start[0]][0]], (...) 34 EM_steps=2, 35 )

File ~/anaconda3/lib/python3.11/site-packages/dynamo/prediction/least_action_path.py:488, in least_action(adata, init_cells, target_cells, init_states, target_states, paired, min_lap_t, elbow_method, num_t, basis, vf_key, vecfld, adj_key, n_points, init_paths, D, PCs, expr_func, addkey, **kwargs) 485 coords = adata.obsm["X" + basis] 487 T = adata.obsp[adj_key] --> 488 G = nx.convert_matrix.from_scipy_sparse_matrix(T) 490 initstates, , , = fetch_states( 491 adata, 492 init_states, (...) 497 None, 498 ) 499 targetstates, , _, valid_genes = fetch_states( 500 adata, 501 target_states, (...) 506 None, 507 )

AttributeError: module 'networkx.convert_matrix' has no attribute 'from_scipy_sparse_matrix'

I've tried to update the networkx (v3.2.1) and dynamo (v 1.3.2).

Any suggestions are very much appreciated!

Sichao25 commented 6 months ago

Hi, thank you for raising this problem. I remember networkx updatefrom_scipy_sparse_matrix to from_scipy_sparse_array. Thus, you can solve this by using an earlier version of networkx.

In fact, we have updated this in the latest version of Dynamo. Upgrading the Dynamo should also solve the issue. Let me know if this works.

fengshuoliu commented 6 months ago

Hi, thank you for raising this problem. I remember networkx updatefrom_scipy_sparse_matrix to from_scipy_sparse_array. Thus, you can solve this by using an earlier version of networkx.

In fact, we have updated this in the latest version of Dynamo. Upgrading the Dynamo should also solve the issue. Let me know if this works.

Thanks the issue was solved!