cellfinder icon indicating copy to clipboard operation
cellfinder copied to clipboard

Tests are failing with napari 0.5.0

Open adamltyson opened this issue 1 year ago • 6 comments

@alessandrofelder's hypothesis - "napari fixtures are now cleverer and fail your tests if you don't clean up your widgets properly"

We should look into this and get the tests passing again (e.g. by making sure all qt widgets have parents)

adamltyson avatar Jul 11 '24 14:07 adamltyson

Based on my investigations I think this is related to the logging level set, if "DEBUG" is set then these errors crop up while if everything is "INFO" and below it seems to work fine. Upon further investigation this seems to only matter for logging to file and not to console.

This was the minimal example I've been working with:

import numpy as np
from fancylog import fancylog
from pathlib import Path
import cellfinder.core as program_for_log


def test_example(make_napari_viewer):
    package_path = Path(__file__).parent

    fancylog.start_logging(
        package_path,
        program_for_log,
        file_log_level="INFO",
        log_header="CELLFINDER TRAINING LOG",
    )

    viewer = make_napari_viewer()
    test_image = np.random.random((100, 100, 100))
    n_layers = len(viewer.layers)
    # adds a "detected" and a "rejected layer"
    viewer.add_image(test_image, name="test_image")

    assert len(viewer.layers) == n_layers + 1

From what I understand it has something to do with the use of WeakSet by napari during clean up. Logging to file with DEBUG keeps a reference to the viewer open and that breaks the teardown code.

IgorTatarnikov avatar Jul 18 '24 17:07 IgorTatarnikov

I've got a better minimal example now that doesn't use fancylog I think this needs to be passed to the napari developers. I've verified that 3 out of 4 tests pass using napari==0.5.0 and 4 for 4 using napari==0.4.19.post1

import numpy as np
import logging

import pytest


@pytest.mark.parametrize("debug_level", ["INFO", "WARNING", "ERROR", "DEBUG",])
def test_example(make_napari_viewer, debug_level):
    logger = logging.getLogger()
    logger.setLevel(getattr(logging, debug_level))

    viewer = make_napari_viewer()
    test_image = np.random.random((100, 100, 100))
    n_layers = len(viewer.layers)
    # adds a "detected" and a "rejected layer"
    viewer.add_image(test_image, name="test_image")

    assert len(viewer.layers) == n_layers + 1

IgorTatarnikov avatar Jul 18 '24 17:07 IgorTatarnikov

Thanks @IgorTatarnikov, yes I'd say it's worth raising an issue, or asking on Zulip.

My only confusion though - I don't think the cellfinder plugin (unlike brainreg) logs to file?

adamltyson avatar Jul 19 '24 08:07 adamltyson

The cellfinder plugin doesn't directly, there's only one time logging is invoked during our test suite:

https://github.com/brainglobe/cellfinder/blob/71d17eee54951edf2d2e9f0bd7ead4244a0f6bae/tests/core/test_integration/test_train.py#L36

Which calls:

https://github.com/brainglobe/cellfinder/blob/71d17eee54951edf2d2e9f0bd7ead4244a0f6bae/cellfinder/core/train/train_yml.py#L265-L274

Commenting out that one line fixes the errors.

IgorTatarnikov avatar Jul 19 '24 10:07 IgorTatarnikov

The cellfinder plugin doesn't directly, there's only one time logging is invoked during our test suite:

So the logger from a non-napari-related test function persists across tests??? :scream: TIL!

alessandrofelder avatar Jul 19 '24 10:07 alessandrofelder

Just noting that following #444 we should make sure to update the tests (removing the xfail) when this issue is addressed.

adamltyson avatar Jul 22 '24 09:07 adamltyson

I created a napari issue for it https://github.com/napari/napari/issues/8287. I was able to replicate the example code in a clean napari only env without cellfinder, so this is not related to cellfinder.

matham avatar Sep 20 '25 18:09 matham

Should be fixed by https://github.com/napari/napari/pull/8305, need to double check once v0.6.5 is released.

IgorTatarnikov avatar Sep 26 '25 18:09 IgorTatarnikov