Skip to content

Commit

Permalink
Merge branch 'main' into codec_metadata_v2_to_v3
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Feb 3, 2025
2 parents ee0e0f6 + 7dfb22f commit abbfe4e
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 0 deletions.
42 changes: 42 additions & 0 deletions zarrs/tests/data/v2_str0.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==3.0.1",
# ]
# ///

import zarr

path_out = "tests/data/zarr_python_compat/str_v2_fv_0.zarr"
array = zarr.create_array(
path_out,
dtype=str,
shape=(5,),
chunks=(2,),
filters=zarr.codecs.vlen_utf8.VLenUTF8(),
compressors=[None],
fill_value=0,
zarr_format=2,
overwrite=True,
)
array[:3] = ["a", "bb", ""]
print(array.info)
# assert (array[:] == ["a", "bb", "", "", ""]).all() # FAILURE

path_out = "tests/data/zarr_python_compat/str_v2_fv_null.zarr"
array = zarr.create_array(
path_out,
dtype=str,
shape=(5,),
chunks=(2,),
filters=zarr.codecs.vlen_utf8.VLenUTF8(),
compressors=[None],
fill_value=None,
zarr_format=2,
overwrite=True,
)
array[:3] = ["a", "bb", ""]
print(array.info)
print(array[:])
assert (array[:] == ["a", "bb", "", "", ""]).all()
19 changes: 19 additions & 0 deletions zarrs/tests/data/zarr_python_compat/str_v2_fv_0.zarr/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"shape": [
5
],
"chunks": [
2
],
"fill_value": 0,
"order": "C",
"filters": [
{
"id": "vlen-utf8"
}
],
"dimension_separator": ".",
"compressor": null,
"zarr_format": 2,
"dtype": "|O"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions zarrs/tests/data/zarr_python_compat/str_v2_fv_null.zarr/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"shape": [
5
],
"chunks": [
2
],
"fill_value": null,
"order": "C",
"filters": [
{
"id": "vlen-utf8"
}
],
"dimension_separator": ".",
"compressor": null,
"zarr_format": 2,
"dtype": "|O"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file not shown.
Binary file not shown.
28 changes: 28 additions & 0 deletions zarrs/tests/zarr_python_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,31 @@ fn zarr_python_compat_fletcher32_v2() -> Result<(), Box<dyn Error>> {

Ok(())
}

#[test]
fn zarr_python_v2_compat_str_fv_0() -> Result<(), Box<dyn Error>> {
let store = Arc::new(FilesystemStore::new(
"tests/data/zarr_python_compat/str_v2_fv_0.zarr",
)?);
let array = zarrs::array::Array::open(store.clone(), "/")?;
let subset_all = array.subset_all();
let elements = array.retrieve_array_subset_elements::<String>(&subset_all)?;

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

Ok(())
}

#[test]
fn zarr_python_v2_compat_str_fv_null() -> Result<(), Box<dyn Error>> {
let store = Arc::new(FilesystemStore::new(
"tests/data/zarr_python_compat/str_v2_fv_null.zarr",
)?);
let array = zarrs::array::Array::open(store.clone(), "/")?;
let subset_all = array.subset_all();
let elements = array.retrieve_array_subset_elements::<String>(&subset_all)?;

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

Ok(())
}
7 changes: 7 additions & 0 deletions zarrs_metadata/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Derive `Copy` for `ArrayMetadataV2Order`
- Add `codec_metadata_v2_to_v3`

### Fixed
- Interpret a `0` fill value as `""` for Zarr V2 string arrays (for `zarr-python` compatibility) ([#140] by [@zqfang])

[#140]: https://github.com/LDeakin/zarrs/pull/140

## [0.3.1] - 2025-01-29

### Fixed
Expand Down Expand Up @@ -70,3 +75,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.3.0]: https://github.com/LDeakin/zarrs/releases/tag/zarrs_metadata-v0.3.0
[0.2.0]: https://github.com/LDeakin/zarrs/releases/tag/zarrs_metadata-v0.2.0
[0.1.0]: https://github.com/LDeakin/zarrs/releases/tag/zarrs_metadata-v0.1.0

[@zqfang]: https://github.com/zqfang
5 changes: 5 additions & 0 deletions zarrs_metadata/src/v2_to_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ pub fn array_metadata_v2_to_v3(
));

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

View check run for this annotation

Codecov / codecov/patch

zarrs_metadata/src/v2_to_v3.rs#L244-L253

Added lines #L244 - L253 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

zarrs_metadata/src/v2_to_v3.rs#L255

Added line #L255 was not covered by tests
} else if data_type.name() == "string" {
// Add a special case for `zarr-python` string data with a 0 fill value -> empty string
if let Some(0) = fill_value.try_as_uint::<u64>() {
fill_value = FillValueMetadataV3::String(String::new());
}
}

let codecs = codec_metadata_v2_to_v3(
Expand Down

0 comments on commit abbfe4e

Please sign in to comment.