Skip to content

Commit 28e97b4

Browse files
fix bugs in attributes wrtiting
1 parent 4e0454e commit 28e97b4

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ minversion = "7.0"
155155
testpaths = ["tests"]
156156
filterwarnings = [
157157
"error",
158-
"ignore::FutureWarning", # TODO remove after zarr-python v3
158+
"ignore::zarr.errors.ZarrUserWarning", # required for anndata
159159
]
160160
addopts = [
161161
"-vv",

src/ngio/tables/backends/_abstract_backend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ def write_metadata(self, metadata: dict | None = None) -> None:
198198
if metadata is None:
199199
metadata = {}
200200

201+
attrs = self._group_handler.reopen_group().attrs.asdict()
202+
print(attrs, self._group_handler.full_url)
203+
# This is required by anndata to identify the format
204+
if "encoding-type" in attrs:
205+
metadata["encoding-type"] = attrs["encoding-type"]
206+
if "encoding-version" in attrs:
207+
metadata["encoding-version"] = attrs["encoding-version"]
208+
201209
backend_metadata = BackendMeta(
202210
backend=self.backend_name(),
203211
index_key=self.index_key,

src/ngio/tables/backends/_anndata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def implements_polars() -> bool:
4141

4242
def load_as_anndata(self) -> AnnData:
4343
"""Load the table as an AnnData object."""
44+
settings.zarr_write_format = self._group_handler.zarr_format
4445
anndata = custom_anndata_read_zarr(self._group_handler._group)
4546
anndata = normalize_anndata(anndata, index_key=self.index_key)
4647
return anndata

src/ngio/utils/_zarr_utils.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def __repr__(self) -> str:
166166
@property
167167
def store(self) -> Store:
168168
"""Return the store of the group."""
169-
return self.group.store
169+
return self._group.store
170170

171171
@property
172172
def full_url(self) -> str | None:
@@ -180,7 +180,7 @@ def full_url(self) -> str | None:
180180
@property
181181
def zarr_format(self) -> Literal[2, 3]:
182182
"""Return the Zarr format version."""
183-
return self.group.metadata.zarr_format
183+
return self._group.metadata.zarr_format
184184

185185
@property
186186
def mode(self) -> AccessModeLiteral:
@@ -216,22 +216,30 @@ def remove_lock(self) -> None:
216216

217217
raise NgioValueError("The lock is still in use. Cannot remove it.")
218218

219+
def reopen_group(self) -> zarr.Group:
220+
"""Reopen the group.
221+
222+
This is useful when the group has been modified
223+
outside of the handler.
224+
"""
225+
if self.mode == "r":
226+
mode = "r"
227+
else:
228+
mode = "r+"
229+
return zarr.open_group(
230+
store=self._group.store,
231+
path=self._group.path,
232+
mode=mode,
233+
zarr_format=self._group.metadata.zarr_format,
234+
)
235+
219236
@property
220237
def group(self) -> zarr.Group:
221238
"""Return the group."""
222239
if self._parallel_safe:
223240
# If we are parallel safe, we need to reopen the group
224241
# to make sure that the attributes are up to date
225-
if self.mode == "r":
226-
mode = "r"
227-
else:
228-
mode = "r+"
229-
return zarr.open_group(
230-
store=self._group.store,
231-
path=self._group.path,
232-
mode=mode,
233-
zarr_format=self._group.metadata.zarr_format,
234-
)
242+
return self.reopen_group()
235243
return self._group
236244

237245
def add_to_cache(self, key: str, value: object) -> None:

0 commit comments

Comments
 (0)