# Fix "Module 'networkx' Has No Attribute 'from_numpy_matrix'" After a NetworkX 3 Upgrade

Yes. An agent upgrades networkx, reproduces the AttributeError, rewrites from_numpy_matrix and the other removed calls across the repository and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/fix-networkx-from-numpy-matrix-attribute-error/networkx/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and maps every networkx call before changing a version. In a research codebase like ermongroup/GraphScoreMatching, `requirements.txt` pins `networkx==2.3`, `numpy==1.22.0` and `scipy==1.3.0`, and the README says to install PyTorch first and then `pip install -r requirements.txt`. Across its 25 Python files the agent finds `nx.from_numpy_matrix` at `evaluation/stats.py:258` and `utils/visual_utils.py:269`, `nx.to_numpy_matrix` at `utils/arg_helper.py:160`, `G.selfloop_edges()` as a graph method at `evaluation/stats.py:259` and `process_dataset.py:122`, `nx.connected_component_subgraphs` at `process_dataset.py:115` and `utils/data_generators.py:38` and `:56`, and `np.float` nine times in `evaluation/mmd.py` and `utils/visual_utils.py`. Six other call sites already use `nx.from_numpy_array`.

Then it reproduces the break step by step. The pinned networkx 2.3 does not even import on Python 3.11: `from fractions import gcd` in `networkx/algorithms/dag.py` fails, because `fractions.gcd` left the standard library in 3.9. On networkx 2.8.8, the last 2.x release, `from_numpy_matrix` still works but prints `DeprecationWarning: from_numpy_matrix is deprecated and will be removed in NetworkX 3.0. Use from_numpy_array instead`, while `G.selfloop_edges()` and `nx.connected_component_subgraphs` already raise `AttributeError`, since both left in 2.4. On the current networkx 3, `nx.from_numpy_matrix` raises `AttributeError: module 'networkx' has no attribute 'from_numpy_matrix'`, `nx.to_numpy_matrix` the same, and with numpy 2 `np.float` raises `AttributeError: module 'numpy' has no attribute 'float'`.

The rewrite follows the deprecation messages. `nx.from_numpy_matrix(adj)` becomes `nx.from_numpy_array(adj)`; `nx.to_numpy_matrix(g, nodelist=node_list)` becomes `nx.to_numpy_array(g, nodelist=node_list)`, which returns an `ndarray` rather than a `matrix`, so the agent reads `pad_adjs`, the function that consumes it, to confirm it only indexes and pads; `G.selfloop_edges()` becomes `nx.selfloop_edges(G)` wrapped in `list(...)` before `remove_edges_from`, since removing while iterating a view fails; `nx.connected_component_subgraphs(G)` becomes `(G.subgraph(c) for c in nx.connected_components(G))`; and `np.float` becomes the builtin `float`. The agent runs the rewritten `adjs_to_graphs` logic on a small adjacency matrix with a self-loop and an isolated node and gets a graph with 2 nodes and 1 edge, as the original intended.

It is clear about what it did not run. Training needs PyTorch and the datasets the README's `gen_data.py` and `process_dataset.py` scripts generate, and `evaluation/mmd.py` depends on `pyemd`, so the pull request shows the call-site rewrites with their reproductions on small arrays, lists `gen_data.py` and a short evaluation as the commands to run once on a machine with the data, and moves the three pins to the releases the run resolves.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This Python project fails with \"AttributeError: module 'networkx' has no attribute 'from_numpy_matrix'\" after upgrading networkx. Read requirements.txt and list every networkx call in the repository first. Reproduce the error on the current networkx release and quote it with its line, and check each listed call against that release, quoting every other AttributeError or DeprecationWarning you find. Rewrite each removed call with its documented replacement, note any return type that changed and check the code that consumes it, and run the rewritten logic on a small array to show it behaves the same. Fix numpy aliases the same bump removes. Say what you could not run and why. Open a pull request that quotes the failing and passing runs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request touching `evaluation/stats.py`, `evaluation/mmd.py`, `utils/arg_helper.py`, `utils/visual_utils.py`, `utils/data_generators.py`, `process_dataset.py` and `requirements.txt`: every `from_numpy_matrix`, `to_numpy_matrix`, `selfloop_edges` method call and `connected_component_subgraphs` rewritten, `np.float` replaced, and the networkx, numpy and scipy pins moved to current releases. The description quotes the `AttributeError` for each removed name with its line, the 2.8.8 deprecation text that names the replacement, and the small-array check with its node and edge counts.

A reviewer checks the two places where the replacement is not a plain rename: `to_numpy_array` returning an array instead of a matrix, and the generator of subgraphs where a list used to be, and then runs the data generation script once with the real dependencies before merging. The run's record holds each reproduction with its output, so every quoted error can be opened rather than taken on trust.

## Run it on every Monday morning

Add an automation to the Project with this prompt and a `schedule` trigger, `cron` set to `0 9 * * 1` in your `timezone`. Each Monday one run bumps networkx and numpy, checks every call site and opens one pull request on its own branch; a week where a removed call has no documented replacement ends `needs_review` with the error attached, and a week already on current releases opens nothing.

The same body works from your own code with `POST /o/{org}/sessions` and an API key when a dependency bot's networkx bump turns red; the key's `webhook_url` receives `run.completed` or `run.failed` with the session URL and the pull request when the run ends.

## Why run it on Rush?

The error in the ticket is the first of several. Behind `from_numpy_matrix` wait `to_numpy_matrix`, two names that left in 2.4, and a numpy alias that left in 1.24, and a laptop fix finds them one crash at a time. The agent lists every networkx call first, checks each against the new release in its sandbox, and keeps every reproduction and its output in the run's record, so the pull request is the whole upgrade rather than its first step.

A research repository rarely has a test suite, so the proof has to be built. The agent runs the rewritten functions on a small adjacency matrix with a self-loop and an isolated node, where the right answer is known, and reports what it could not run, which is what a reviewer needs to merge a change to code that has not been touched since the pin was set.

## Frequently asked questions

### Why is from_numpy_matrix gone?

NetworkX 3.0 removed the `numpy.matrix` based functions in favour of `from_numpy_array` and `to_numpy_array`, which work on plain arrays, after deprecating them through the 2.x line. The 2.8.8 warning names the replacement, and the agent quotes it.

### Does to_numpy_array change behavior?

It returns an `ndarray` where `to_numpy_matrix` returned a `numpy.matrix`, so `*` means element-wise multiplication instead of matrix product and indexing a row gives a 1-D array. The agent reads the consuming code for those cases; in this repository the result is only padded and stacked.

### What if my pinned networkx does not install at all?

That is the case here: networkx 2.3 imports `fractions.gcd`, which Python 3.9 removed, so the pin fails on any current Python. The agent records that, which is the reason the bump cannot be postponed.

### Will it run the experiments?

Not when they need PyTorch, generated datasets or a package it cannot build. It rewrites the call sites, proves each on small inputs and names the commands to run once with the real data, rather than reporting an end-to-end run it did not do.

## More NetworkX jobs

- [Fix "AttributeError: 'Graph' object has no attribute 'node'" in NetworkX](https://getrush.ai/run/fix-graph-object-has-no-attribute-node/networkx/)

## More jobs like this: Upgrade dependencies weekly

- [Fix "No module named 'distutils'" on Python 3.12](https://getrush.ai/run/fix-no-module-named-distutils/distutils/)
- [Fix npm "ERESOLVE unable to resolve dependency tree" Without legacy-peer-deps](https://getrush.ai/run/fix-npm-eresolve-peer-conflicts/npm/)
- [Fix OneHotEncoder Unexpected Keyword Argument sparse After a scikit-learn Upgrade](https://getrush.ai/run/fix-onehotencoder-unexpected-keyword-argument-sparse/scikit-learn/)
- [Fix page.waitForTimeout Is Not a Function After a Puppeteer Upgrade](https://getrush.ai/run/fix-page-waitfortimeout-is-not-a-function/puppeteer/)
- [Fix _.pluck Is Not a Function After a Lodash 4 Upgrade](https://getrush.ai/run/fix-pluck-is-not-a-function/lodash/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
