scverse / scverse/spatialdata

Save custom `.attrs` in SpatialElements to disk

Open
#839 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

Hi! Thanks for the great package!

I am currently working with spatialdata to load and parse microscopy files (particularly in the proprietary Carl Zeiss .czi format). For this purpose, it would be extremely useful to persistently store metadata related to the acquisition of the microscopy data (e.g. magnification, resolution, etc.) in the .attrs attribute of the SpatialElement. While I can manipulate the Elements in memory, it is currently not possible to write them to disk (see example)

While it would be possible to write the metadata to a custom tables object, it feels much more natural to have this data directly associated with the image.

Therefore, it would be fantastic if SpatialElements would support writing the .attrs keys to disk.

Example

import spatialdata as sd
import numpy as np 
import tempfile
import os

dir = tempfile.tempdir
save_path = os.path.join(dir, "blobs.zarr")


# Load dataset and set custom metadata
blobs = sd.datasets.blobs()
blobs.images["blobs_image"].attrs["metadata"] = {
    "info1": 1,
    "info2": "2",
    "info3": np.zeros(shape=(3, 3))
}

sd.models.Image2DModel().validate(blobs.images["blobs_image"])

# Metadata is in memory
assert "metadata" in blobs.images["blobs_image"].attrs

# Writing to disk leads to loss of the information 
blobs.write(save_path)
sdata = sd.read_zarr(save_path)
assert "metadata" not in sdata.images["blobs_image"].attrs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SpatialElement .attrs behavior exercised in the example, then trace blobs.write(save_path) and sd.read_zarr(save_path) to see where the metadata is lost. Use Image2DModel().validate() and the provided assertions to verify that nested metadata, including the NumPy array, survives the round trip.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.