Skip to content
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

fix(metadata): interpret 0 fill value as "0" for Zarr V2 string arrays #142

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions zarrs/tests/data/v2_str0_zarr_python_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "zarr==2.18.4",
# ]
# ///

import zarr

path_out = "tests/data/zarr_python_compat/str_v2_fv_0.zarr"
array = zarr.open(
path_out,
dtype=str,
shape=(5,),
chunks=(2,),
compressor=None,
mode='w',
)
array[0] = "a"
array[1] = "bb"
array[2] = ""
print(array.info)
print(array[:])
assert (array[:] == ["a", "bb", "", "0", "0"]).all()

path_out = "tests/data/zarr_python_compat/str_v2_fv_null.zarr"
array = zarr.open(
path_out,
dtype=str,
shape=(5,),
chunks=(2,),
fill_value = None,
compressor=None,
mode='w',
)
array[0] = "a"
array[1] = "bb"
array[2] = ""
print(array.info)
print(array[:])
assert (array[:] == ["a", "bb", "", "", None]).all() # Yikes
33 changes: 16 additions & 17 deletions zarrs/tests/data/zarr_python_compat/str_v2_fv_0.zarr/.zarray
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"shape": [
5
],
"chunks": [
2
],
"fill_value": 0,
"order": "C",
"filters": [
{
"id": "vlen-utf8"
}
],
"dimension_separator": ".",
"compressor": null,
"zarr_format": 2,
"dtype": "|O"
"chunks": [
2
],
"compressor": null,
"dtype": "|O",
"fill_value": "0",
"filters": [
{
"id": "vlen-utf8"
}
],
"order": "C",
"shape": [
5
],
"zarr_format": 2
}

This file was deleted.

Binary file modified zarrs/tests/data/zarr_python_compat/str_v2_fv_0.zarr/1
Binary file not shown.
33 changes: 16 additions & 17 deletions zarrs/tests/data/zarr_python_compat/str_v2_fv_null.zarr/.zarray
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"shape": [
5
],
"chunks": [
2
],
"fill_value": null,
"order": "C",
"filters": [
{
"id": "vlen-utf8"
}
],
"dimension_separator": ".",
"compressor": null,
"zarr_format": 2,
"dtype": "|O"
"chunks": [
2
],
"compressor": null,
"dtype": "|O",
"fill_value": null,
"filters": [
{
"id": "vlen-utf8"
}
],
"order": "C",
"shape": [
5
],
"zarr_format": 2
}

This file was deleted.

4 changes: 3 additions & 1 deletion zarrs/tests/zarr_python_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn zarr_python_compat_fletcher32_v2() -> Result<(), Box<dyn Error>> {
Ok(())
}

// Data generated by v2_str0_zarr_python_v2.py
#[test]
fn zarr_python_v2_compat_str_fv_0() -> Result<(), Box<dyn Error>> {
let store = Arc::new(FilesystemStore::new(
Expand All @@ -53,11 +54,12 @@ fn zarr_python_v2_compat_str_fv_0() -> Result<(), Box<dyn Error>> {
let subset_all = array.subset_all();
let elements = array.retrieve_array_subset_elements::<String>(&subset_all)?;

assert_eq!(elements, &["a", "bb", "", "", ""]);
assert_eq!(elements, &["a", "bb", "", "0", "0"]);

Ok(())
}

// Data generated by v2_str0_zarr_python_v2.py
#[test]
fn zarr_python_v2_compat_str_fv_null() -> Result<(), Box<dyn Error>> {
let store = Arc::new(FilesystemStore::new(
Expand Down
3 changes: 3 additions & 0 deletions zarrs_metadata/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Interpret a `0` fill value as `"0"` for Zarr V2 string arrays (for `zarr-python` 2.x.x compatibility)

## [0.3.3] - 2025-02-06

### Fixed
Expand Down
5 changes: 3 additions & 2 deletions zarrs_metadata/src/v2_to_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,10 @@
}
}
} else if data_type.name() == "string" {
// Add a special case for `zarr-python` string data with a 0 fill value -> empty string
// Add a special case for string data with a 0 fill value -> "0"
// This was the default in zarr-python 2.x.x.
if let Some(0) = fill_value.try_as_uint::<u64>() {
fill_value = FillValueMetadataV3::String(String::new());
fill_value = FillValueMetadataV3::String("0".to_string());

Check warning on line 263 in zarrs_metadata/src/v2_to_v3.rs

View check run for this annotation

Codecov / codecov/patch

zarrs_metadata/src/v2_to_v3.rs#L263

Added line #L263 was not covered by tests
}
}

Expand Down