8000 Fix zarr 3.0 compatibility by Czaki · Pull Request #7215 · napari/napari · GitHub
[go: up one dir, main page]

Skip to content

Fix zarr 3.0 compatibility #7215

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

Merged
merged 33 commits into from
Nov 6, 2024
Merged

Fix zarr 3.0 compatibility #7215

merged 33 commits into from
Nov 6, 2024

Conversation

Czaki
Copy link
Collaborator
@Czaki Czaki commented Aug 26, 2024

References and relevant issues

closes #7279

Description

Fix zarr 3.0 related test by used named arguments

@Czaki Czaki added the maintenance PR with maintance changes, label Aug 26, 2024
@Czaki Czaki added this to the 0.5.3 milestone Aug 26, 2024
@github-actions github-actions bot added tests Something related to our tests qt Relates to qt labels Aug 26, 2024
Copy link
codecov bot commented Aug 26, 2024

Codecov Report

Attention: Patch coverage is 85.50725% with 10 lines in your changes missing coverage. Please review.

Project coverage is 92.75%. Comparing base (22a5018) to head (2fd03f1).
Report is 45 commits behind head on main.

Files with missing lines Patch % Lines
napari_builtins/io/_read.py 0.00% 9 Missing ⚠️
napari/layers/labels/_tests/test_labels.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7215      +/-   ##
==========================================
- Coverage   92.88%   92.75%   -0.14%     
==========================================
  Files         623      623              
  Lines       57547    57563      +16     
==========================================
- Hits        53453    53390      -63     
- Misses       4094     4173      +79     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member
@psobolewskiPhD psobolewskiPhD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the big changes of Zarr v3 is that everything now goes in zarr.json instead of .zarray + .zgroup + .zattrs so need to update the logic in builtins to use either or.

@Czaki
Copy link
Collaborator Author
Czaki commented Aug 26, 2024

It looks also like zarr.open is a problem as it does not allow creating group file.

@psobolewskiPhD
Copy link
Member

Looks like need to block pydantic==2.9.0b1 for this PR.

@Czaki
Copy link
Collaborator Author
Czaki commented Aug 26, 2024

There is even issue for this pydantic/pydantic#10239 initially reported here https://x.com/Czaki_PL/status/1828178992332955873

@jni jni modified the milestones: 0.5.3, 0.5.4 Aug 29, 2024
@jni jni changed the title Fix zarr 3.0 compatybility Fix zarr 3.0 compatibility Aug 31, 2024
jni pushed a commit that referenced this pull request Aug 31, 2024
# References and relevant issues

closes #7193

# Description

When try to fix zarr problems with zarr 3.0.0 in #7215 I have found that
it is not easy with this release, so I prefer to block 3.0.0a1 release
and open issue in zarr
@Czaki Czaki marked this pull request as draft August 31, 2024 14:02
import zarr

image = zarr.open(store=path)
shape = image.shape
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Czaki this use case probably also needs to be split into single array/group of arrays? I presume that we can make the earlier ifs more complex: "if there's a .zarray or there's a zarr.json and it only contains one array"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with zarr arrays. I just follow the tests. And now waiting on new Zarr release.

@@ -1,4 +1,3 @@
matplotlib !=3.9.1 ; platform_system == 'Windows'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. This release is yanked from PyPI

8000

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. In general this should probably be a separate PR but let's ignore this time. 🙏

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to edit this file to trigger --pre tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should have a better mechanism for that 😂 but ok!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, but I prefer to validate it in another PR.

@@ -133,6 +133,18 @@ def read_zarr_dataset(path: str):
]
assert image, 'No arrays found in zarr group'
shape = image[0].shape
elif (path / 'zarr.json').exists():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to add a test for zarr v3? Or is this codecov being silly because only pre is testing v3 arrays?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. zarr 3 is only available in --pre tests

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
Copy link
Member
@psobolewskiPhD psobolewskiPhD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested locally and everything works as far as I can tell with a few simple zarr array tests -- there is no migration guide yet for v3.
I think the 1D thing doesn't really affect us, as we skip opening 1D arrays anyways.
To avoid the deprecation, should be no issue to just use create_array instead of create_dataset, because I don't think we care about h5py in our tests?

def test_add_zarr_1d_array_is_ignored():
@pytest.mark.skipif(
parse_version(version('zarr')) > parse_version('3a0'),
reason='zarr 3+ does not support setting 1d data',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -362,7 +362,8 @@ filterwarnings = [
# https://github.com/pydata/xarray/pull/8939
"ignore:__array__ implementation doesn't accept a copy keyword, so passing copy=False failed.",
"ignore:pkg_resources is deprecated",
"ignore:Deprecated call to `pkg_resources.declare_namespace"
"ignore:Deprecated call to `pkg_resources.declare_namespace",
"ignore:Use Group.create_array instead."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made that as a suggestion.

@Czaki
Copy link
Collaborator Author
Czaki commented Nov 4, 2024

To avoid the deprecation, should be no issue to just use create_array instead of create_dataset, because I don't think we care about h5py in our tests?

Unfortunately, zarr 2.x does not have create_array method, so we need to keep create_dataset for zarr 2.x compatybility. See https://github.com/napari/napari/actions/runs/11660697193/job/32463676718?pr=7215#step:13:576

@psobolewskiPhD
Copy link
Member

Gah, sorry! I've was using a zarr v3 env and reading those docs.
Annoying!

image = da.from_zarr(data)
shape = image.shape
else:
image = [data[k] for k in sorted(data)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Czaki why do you think this works? Why is it different from the v2 version?

            image = [
                read_zarr_dataset(subpath)[0]
                for subpath in sorted(path.iterdir())
                if not subpath.name.startswith('.') and subpath.is_dir()
            ]

Incidentally, while exploring this, I found that our current 1D skipping mechanism is completely broken, even in v2. If the first dataset is 1D, we skip the whole subdirectory. If a later dataset is 1D, we don't skip it at all.

Probably, we should merge this and fix that issue separately. Here's the diff to the test that fails, both here and in main:

@@ -356,3 +356,8 @@ def test_add_many_zarr_1d_array_is_ignored(tmp_path):
         else:
             assert isinstance(out[0], da.Array)
             assert out[0].ndim == int(name[0])
+    out1, out2, out3 = npe2.read([zarr_dir.as_posix()], stack=False)
+    assert out1 == (None,)
+    assert out2[0].ndim == 2
+    assert out3[0].ndim == 3

But anyway, to simplify flow between versions, I suggest making this the same as v2:

Suggested change
image = [data[k] for k in sorted(data)]
image = [
read_zarr_dataset(subpath)[0]
for subpath in sorted(path.iterdir())
if not subpath.name.startswith('.') and subpath.is_dir()
]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I use reading subarrays by zarr itself.
At least zarr v3 read properly file with multiple groups.

@Czaki Czaki added the ready to merge Last chance for comments! Will be merged in ~24h label Nov 5, 2024
Comment on lines +324 to +329
@pytest.mark.xfail(
parse_version(version('zarr')) >= parse_version('3.0.0a0')
and os.name == 'nt',
reason='zarr 3 return incorrect key in windows',
strict=True,
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to skip this test on windows because of

zarr-developers/zarr-python#2438

described here https://napari.zulipchat.com/#narrow/channel/212875-general/topic/handling.20zarr.203.2E0.20release/near/480752197

As it is strict xfail it will fail once zarr fix the bug.

@Czaki Czaki merged commit e0600ff into napari:main Nov 6, 2024
42 checks passed
@Czaki Czaki deleted the fix_zarr_3.0 branch November 6, 2024 15:09
@github-actions github-actions bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Nov 6, 2024
@jni jni added the highlight PR that should be mentioned in next release notes label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
highlight PR that should be mentioned in next release notes maintenance PR with maintance changes, qt Relates to qt tests Something related to our tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[test-bot] pip install --pre is failing
4 participants
0