Skip to content

Fix zarr 3.0 compatibility#7215

Merged
Czaki merged 33 commits into
napari:mainfrom
Czaki:fix_zarr_3.0
Nov 6, 2024
Merged

Fix zarr 3.0 compatibility#7215
Czaki merged 33 commits into
napari:mainfrom
Czaki:fix_zarr_3.0

Conversation

@Czaki

@Czaki Czaki commented Aug 26, 2024

Copy link
Copy Markdown
Collaborator

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 topic:qt Relates to qt labels Aug 26, 2024
@codecov

codecov Bot commented Aug 26, 2024

Copy link
Copy Markdown

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.

@psobolewskiPhD psobolewskiPhD left a comment

Copy link
Copy Markdown
Member

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

Czaki commented Aug 26, 2024

Copy link
Copy Markdown
Collaborator Author

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

@psobolewskiPhD

Copy link
Copy Markdown
Member

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

@Czaki

Czaki commented Aug 26, 2024

Copy link
Copy Markdown
Collaborator Author

@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
Comment thread napari_builtins/io/_read.py Outdated
import zarr

image = zarr.open(store=path)
shape = image.shape

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

]
assert image, 'No arrays found in zarr group'
shape = image[0].shape
elif (path / 'zarr.json').exists():

Copy link
Copy Markdown
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
Copy Markdown
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 <[email protected]>

@psobolewskiPhD psobolewskiPhD left a comment

Copy link
Copy Markdown
Member

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?

Comment thread napari_builtins/_tests/test_io.py
Comment thread napari_builtins/_tests/test_io.py
Comment thread napari_builtins/_tests/test_io.py
Comment thread napari_builtins/_tests/test_io.py Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread pyproject.toml
"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
Copy Markdown
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

Czaki commented Nov 4, 2024

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@Czaki
Czaki merged commit e0600ff into napari:main Nov 6, 2024
@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, tests Something related to our tests topic:qt Relates to qt

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[test-bot] pip install --pre is failing

4 participants