When saving labels to a SpatialData that has already been written to disk (with or without overwrite=True), there are multiple exceptions.
Example code
importnumpyasnpimportspatialdatasdata=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 |
| ifelement_type!="labels": |
| elem_group=root.create_group(name=element_type) ifelement_typenotinrootelseroot[element_type] |
| ifoverwrite: |
| ifelement_type=="labels"andelement_typeinroot: |
| elem_group=root[element_type] |
| ifnameinelem_group: |
| delelem_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 |
| assertisinstance(dims_data, tuple) |
This works with:
xdata=next(iter(e["scale0"].ds.values()))
or converting the dictionary to tuple:
ifisinstance(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()))
When saving labels to a SpatialData that has already been written to disk (with or without
overwrite=True), there are multiple exceptions.Example code
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.
spatialdata/src/spatialdata/_core/spatialdata.py
Lines 409 to 416 in 4dd5250
2. AssertionError on type of dims
For backed labels,
xdatais not aDataArray(wheredimsis("y", "x")) but aDataTreewheredimsisFrozen({"y": 1633, "x": 1290}).spatialdata/src/spatialdata/models/_utils.py
Lines 151 to 153 in 4dd5250
This works with:
or converting the dictionary to tuple:
3. DataTree has no attribute
shapespatialdata/src/spatialdata/transformations/_utils.py
Lines 118 to 119 in 4dd5250
Also here: