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

Exceptions when adding labels to backed SpatialData #347

Closed
aeisenbarth opened this issue Aug 31, 2023 · 2 comments
Closed

Exceptions when adding labels to backed SpatialData #347

aeisenbarth opened this issue Aug 31, 2023 · 2 comments

Comments

@aeisenbarth
Copy link
Contributor

aeisenbarth commented Aug 31, 2023

When saving labels to a SpatialData that has already been written to disk (with or without overwrite=True), there are multiple exceptions.

Example code

import numpy as np
import spatialdata

sdata = spatialdata.SpatialData()
sdata.write("/tmp/spatialdata.zarr")
labels = spatialdata.models.Labels2DModel.parse(np.ones((2, 2)))
sdata.add_labels(name="my_label", labels=labels, overwrite=True)

1. UnboundLocalError: local variable 'elem_group' referenced before assignment

A variable is accessed (L415) which is only conditionally declared. It would be safer either to assign a default value, or to access it only in the same conditional branch.

I don't understand the reasoning of the comment, but to me it seems a fix would be to remove the condition above L411.

# not need to create the group for labels as it is already handled by ome-zarr-py
if element_type != "labels":
elem_group = root.create_group(name=element_type) if element_type not in root else root[element_type]
if overwrite:
if element_type == "labels" and element_type in root:
elem_group = root[element_type]
if name in elem_group:
del elem_group[name]

Traceback (most recent call last):
  File "<ipython-input-6-6d9e217edb59>", line 1, in <module>
    sdata.add_labels(name="my_label", labels=labels, overwrite=True)
  File "…/spatialdata/_core/spatialdata.py", line 779, in add_labels
    elem_group = self._init_add_element(name=name, element_type="labels", overwrite=overwrite)
  File "…/spatialdata/_core/spatialdata.py", line 415, in _init_add_element
    if name in elem_group:
UnboundLocalError: local variable 'elem_group' referenced before assignment

2. AssertionError on type of dims

For backed labels, xdata is not a DataArray (where dims is ("y", "x")) but a DataTree where dims is Frozen({"y": 1633, "x": 1290}).

xdata = e["scale0"].values().__iter__().__next__()
dims_data = xdata.dims
assert isinstance(dims_data, tuple)

This works with:

xdata = next(iter(e["scale0"].ds.values()))

or converting the dictionary to tuple:

        if isinstance(dims_data, Mapping):
            dims_data = tuple(dims_data.keys())

3. DataTree has no attribute shape

xdata = list(node.values())[0]
new_shape = np.array(xdata.shape)

Also here:

        xdata = next(iter(e["scale0"].ds.values()))
@LucaMarconato
Copy link
Member

LucaMarconato commented Oct 10, 2023

Thanks for reporting, I will look into this and continue from your PR.

@LucaMarconato
Copy link
Member

Closing because the IO apis changed in main with #329 (please note that the PR is not in the current version v0.0.15).

I have opened a separate issue to track the other points #446.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants