Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] CGNN error: module 'networkx' has no attribute 'adj_matrix' #163

Open
JHPark9090 opened this issue Mar 28, 2024 · 2 comments
Open

Comments

@JHPark9090
Copy link

Hello, I am trying to run CGNN model based on an undirected graph, using the "orient_directed_graph" function.
I am using the exact same codes presented in this example:
https://github.com/FenTechSolutions/CausalDiscoveryToolbox/blob/master/examples/example_cgnn.ipynb

Describe the bug
But whenever, I run the CGNN codes, I obtain this error, which indicates that the 'networkx' library has no attribute 'adj_matrix'.
image

I think it is maybe because the function 'adj_matrix' was deprecated in the 'networkx' library.

Below is the versions of my python and related libraries.
image

I am running my codes on GPUs.

@rafaela-amorim
Copy link

same here

@jason-huanghao
Copy link

Hello, I am trying to run CGNN model based on an undirected graph, using the "orient_directed_graph" function. I am using the exact same codes presented in this example: https://github.com/FenTechSolutions/CausalDiscoveryToolbox/blob/master/examples/example_cgnn.ipynb

Describe the bug But whenever, I run the CGNN codes, I obtain this error, which indicates that the 'networkx' library has no attribute 'adj_matrix'. image

I think it is maybe because the function 'adj_matrix' was deprecated in the 'networkx' library.

Below is the versions of my python and related libraries. image

I am running my codes on GPUs.

The problem is that the adj_matrix() is no more valid in new version of networkx, the new function is adjacency_matrix().

You can go to the cdt.causality.graph.CGNN, and change the function hill_climbing() as following:

def hill_climbing(data, graph, **kwargs):
    """Hill Climbing optimization: a greedy exploration algorithm."""
    if isinstance(data, th.utils.data.Dataset):
        nodelist = data.get_names()
    elif isinstance(data, pd.DataFrame):
        nodelist = list(data.columns)
    else:
        raise TypeError('Data type not understood')
    # tested_candidates = [nx.adj_matrix(graph, nodelist=nodelist, weight=None)]
    tested_candidates = [nx.adjacency_matrix(graph, nodelist=nodelist, weight=None)]
    best_score = parallel_graph_evaluation(data,
                                           tested_candidates[0].todense(),
                                           ** kwargs)
    best_candidate = graph
    can_improve = True
    while can_improve:
        can_improve = False
        for (i, j) in best_candidate.edges():
            test_graph = deepcopy(best_candidate)
            test_graph.add_edge(j, i, weight=test_graph[i][j]['weight'])
            test_graph.remove_edge(i, j)
            # tadjmat = nx.adj_matrix(test_graph, nodelist=nodelist, weight=None)
            tadjmat = nx.adjacency_matrix(test_graph, nodelist=nodelist, weight=None)
            if (nx.is_directed_acyclic_graph(test_graph) and not any([(tadjmat != cand).nnz ==
                                                                      0 for cand in tested_candidates])):
                tested_candidates.append(tadjmat)
                score = parallel_graph_evaluation(data, tadjmat.todense(),
                                                  **kwargs)
                if score < best_score:
                    can_improve = True
                    best_candidate = test_graph
                    best_score = score
                    break
    return best_candidate

Then it should be work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants