# Fix "ImportError: cannot import name 'compare_ssim' from 'skimage.measure'"

Yes. An agent moves the metric calls to skimage.metrics, replaces the scipy image reader that fails first, and reruns the evaluation script on sample images.

[Run this on Rush](https://cloud.getrush.ai/run/fix-cannot-import-name-compare-ssim-from-skimage-measure/scikit-image/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

scikit-image moved its comparison functions in 0.16: `compare_ssim`, `compare_psnr`, `compare_mse` and `compare_nrmse` became `structural_similarity`, `peak_signal_noise_ratio`, `mean_squared_error` and `normalized_root_mse` in a new `skimage.metrics` module, and the old names in `skimage.measure` were kept with a warning that names the release they would vanish in, 0.18. That release came in December 2020, so any environment built since then with an unpinned scikit-image raises the ImportError in the title. A pinned environment does not escape either: the 0.14 and 0.16 lines have no wheels for a current Python, so moving the interpreter forces the upgrade.

An inpainting research repository like knazeri/edge-connect has the typical shape. `requirements.txt` pins `scikit-image ~= 0.14.0` beside `scipy ~= 1.0.1`, `numpy ~= 1.19` and `matplotlib ~= 2.2.2`, and the README's evaluation section says to run `python ./scripts/metrics.py --data-path [validation set] --output-path [model output]`. That script imports `compare_ssim` and `compare_psnr` from `skimage.measure`, reads each image pair with `imread` from `scipy.misc`, converts with `rgb2gray`, calls `compare_psnr(img_gt, img_pred, data_range=1)` and `compare_ssim(img_gt, img_pred, data_range=1, win_size=51)`, prints running means every 100 images and saves `metrics.npz`. On a current install the script stops one line before the title error, because `scipy.misc` no longer has `imread` at all; fix that and the scikit-image import fails next.

The agent reproduces both in a clean environment with the current releases, then measures the boundary rather than quoting it: on Python 3.8, scikit-image 0.16.2 and 0.17.2 import the old name and print `DEPRECATED: skimage.measure.compare_ssim has been moved to skimage.metrics.structural_similarity. It will be removed from skimage.measure in version 0.18.` when it is called, and 0.18.1 raises the ImportError. It reads the new function's signature from the installed package, `structural_similarity(im1, im2, *, win_size=None, gradient=False, data_range=None, channel_axis=None, gaussian_weights=False, full=False, **kwargs)`, and notes the one trap in it: the old `multichannel=True` keyword is gone, and because the function swallows unknown keywords, passing it on a colour image does not fail on the keyword but on a misleading `win_size exceeds image extent` error. This script converts to grey first, so the trap does not bite here, but the agent greps the rest of the repository for `multichannel` and for the other removed names, and finds `scipy.misc.imread` again in `src/dataset.py` and `scripts/fid_score.py`, plus `scipy.misc.imresize` on line 168 of the dataset loader.

The port of the metrics script is three import lines: `skimage.io.imread` for the reader, and the two metric functions imported under their old names so the body of the script does not change. The agent proves it by generating a few pairs of small images, running the script against them, and reading back `metrics.npz` to check it holds a PSNR, SSIM and MAE value per image. It then pins the scikit-image and scipy it tested, and lists the dataset loader's `imread` and `imresize` as a second change with its own replacement, since that path runs during training and the agent did not run training.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "This project's evaluation script fails with ImportError: cannot import name 'compare_ssim' from 'skimage.measure'. In a clean virtual environment install the current scikit-image, scipy and numpy, run the script and quote every error in the order they appear, including any that comes before the scikit-image one. Find every use of the renamed functions (compare_ssim, compare_psnr, compare_mse, compare_nrmse), every multichannel keyword, and every scipy.misc image function (imread, imsave, imresize) across the repository. Read the new signatures from the installed skimage.metrics module and say which keyword arguments changed. Port the evaluation script: skimage.io or imageio for reading, skimage.metrics for the metrics, channel_axis where multichannel was used. Generate a few pairs of small test images, run the script on them, and read the saved results file back to confirm it holds one value per image per metric. Pin the versions you tested. For uses in training or data-loading code that you cannot run end to end, list each with its replacement as a separate item rather than changing it silently. Open a pull request with the error sequence, the diff, the rerun output and that list.",
  "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

A pull request that changes the imports in the evaluation script and pins scikit-image and scipy in `requirements.txt`. The description opens with the error sequence as the script meets it, the scipy reader first and the scikit-image import second, so a reviewer understands why a report of the title error does not match the first line of the traceback they see.

Below that is the boundary the agent measured, the releases that warn and the one that removes, the new signature with the keywords that changed, and the rerun: the command on the generated images, the printed means, and the shape of each array read back from `metrics.npz`. The session's record keeps the installs and every command.

The description ends with the list of the same removed names elsewhere in the repository, each with file, line and replacement, marked as not run because they sit in the training path. A reviewer who can run a training step decides whether to take those in the same pull request or the next one.

## Run it on every new environment that fails on this import

A research repository meets this error when someone new sets it up, not when its code changes. If the repository has a CI job, add an automation to the Project with the prompt above and a `github` trigger on `workflow_run.failure`; the failed workflow is placed after the prompt as the event, and a failure with another cause gets a short report and no pull request. Fork pull requests and bot senders never fire the trigger, so outside contributions cannot start runs.

A repository with no CI can use a `schedule` trigger once a month with the same prompt, installing the current scikit-image in a fresh sandbox and running the evaluation script on generated images. A run that finds nothing to fix ends without a pull request and says so.

## Why run it on Rush?

The error is two renames deep: the traceback a user reports names scikit-image, the first thing that actually fails is scipy, and the fix for one keyword in the new signature fails with a message about window size. Sorting that out needs a clean environment, the installed signatures and a run on real images, which a Rush sandbox gives the agent without disturbing anyone's working setup, and the session keeps the sequence so the pull request explains it.

It also separates what was proven from what was read. Evaluation scripts can be run on generated images in seconds; data loaders run inside training that needs a GPU and a dataset. The agent fixes and runs the first and lists the second with replacements, so the reviewer knows exactly which lines carry evidence.

## Frequently asked questions

### Why does the traceback mention scipy when the bug report says scikit-image?

Because the script imports `imread` from `scipy.misc` one line above the scikit-image import, and current scipy has no image functions there. Whoever reported the scikit-image error had an older scipy. The agent reports the sequence as it appears on a current install.

### What replaces compare_ssim and compare_psnr?

`skimage.metrics.structural_similarity` and `skimage.metrics.peak_signal_noise_ratio`, available since 0.16. Importing them under the old names keeps the body of a script unchanged. The `multichannel` keyword became `channel_axis`; `data_range` and `win_size` kept their names.

### Can I pin scikit-image below 0.18?

On a Python those releases have wheels for, yes, and the deprecation warning tells you which name to use. On a current Python the pin fails to install, which is usually how this error is discovered. The agent reports what the pin does on the interpreter the project uses.

### Why did passing multichannel=True give a win_size error instead of an argument error?

Because `structural_similarity` accepts extra keywords and passes them on, so a removed name is not rejected; the image is then treated as three-dimensional greyscale and the default window is larger than its third axis. The fix is `channel_axis=-1` for an image whose last axis is colour.

## More jobs like this: Fix a bug from an alert

- [Fix "ImportError: cannot import name 'executor' from 'aiogram'" in a Telegram bot](https://getrush.ai/run/fix-cannot-import-name-executor-from-aiogram/aiogram/)
- [Fix ImportError: Cannot Import Name 'json' From 'itsdangerous' in a Flask 1 App](https://getrush.ai/run/fix-cannot-import-name-json-from-itsdangerous/itsdangerous/)
- [Fix "ImportError: cannot import name 'mock_s3' from 'moto'" in a pytest suite](https://getrush.ai/run/fix-cannot-import-name-mock-s3-from-moto/moto/)
- [Fix "ImportError: cannot import name 'plot_confusion_matrix' from 'sklearn.metrics'"](https://getrush.ai/run/fix-cannot-import-name-plot-confusion-matrix-from-sklearn-metrics/scikit-learn/)
- [Fix "ImportError: cannot import name 'secure_filename' from 'werkzeug'" in a Flask-Uploads App](https://getrush.ai/run/fix-cannot-import-name-secure-filename-from-werkzeug/flask-uploads/)
- [Fix "ImportError: cannot import name 'simps' from 'scipy.integrate'" in SciPy 1.14](https://getrush.ai/run/fix-cannot-import-name-simps-from-scipy-integrate/scipy/)

## 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/)
