Skip to content

Conversation

@timtreis
Copy link
Member

@timtreis timtreis commented Dec 10, 2024

Gets a specific key from sdata.attrs and then returns it either

a) as is (return_as=None) or
b) converted (return_as="dict" | "json" | "df") and potentially flattened (flatten=True, separated by sep)

Needed for spatialdata-db project, but tried to keep it general.

import spatialdata
from spatialdata.datasets import blobs

sdata = blobs()

sdata.attrs  # empty dictionnary
sdata.attrs["test"] = {"a": {"b": 12}, "c": 8}

sdata.write("blobs.zarr", overwrite=True) 
sdata = spatialdata.read_zarr("blobs.zarr") 

sdata.get_attrs(key="test", return_as=None, flatten=False)

{'a': {'b': 12}, 'c': 8}

import spatialdata
from spatialdata.datasets import blobs

sdata = blobs()

sdata.attrs  # empty dictionnary
sdata.attrs["test"] = {"a": {"b": 12}, "c": 8}

sdata.write("blobs.zarr", overwrite=True) 
sdata = spatialdata.read_zarr("blobs.zarr") 

sdata.get_attrs(key="test", return_as="df", flatten=False)
image
import spatialdata
from spatialdata.datasets import blobs

sdata = blobs()

sdata.attrs  # empty dictionnary
sdata.attrs["test"] = {"a": {"b": 12}, "c": 8}

sdata.write("blobs.zarr", overwrite=True) 
sdata = spatialdata.read_zarr("blobs.zarr") 

sdata.get_attrs(key="test", return_as="df", flatten=True)
image
import spatialdata
from spatialdata.datasets import blobs

sdata = blobs()

sdata.attrs  # empty dictionnary
sdata.attrs["test"] = {"a": {"b": 12}, "c": 8}

sdata.write("blobs.zarr", overwrite=True) 
sdata = spatialdata.read_zarr("blobs.zarr") 

sdata.get_attrs(key="test", return_as="json", flatten=True)
image

FYI @Zethson

@timtreis timtreis added the enhancement ✨ New feature or request label Dec 10, 2024
@codecov
Copy link

codecov bot commented Jan 13, 2025

Codecov Report

Attention: Patch coverage is 86.11111% with 5 lines in your changes missing coverage. Please review.

Project coverage is 91.72%. Comparing base (526a0a2) to head (537617d).
Report is 40 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata/_core/spatialdata.py 86.11% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #806      +/-   ##
==========================================
- Coverage   91.75%   91.72%   -0.03%     
==========================================
  Files          46       46              
  Lines        7130     7166      +36     
==========================================
+ Hits         6542     6573      +31     
- Misses        588      593       +5     
Files with missing lines Coverage Δ
src/spatialdata/_core/spatialdata.py 90.60% <86.11%> (-0.18%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@LucaMarconato LucaMarconato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convenient and useful API! Great addition 😊

@LucaMarconato LucaMarconato enabled auto-merge (squash) January 13, 2025 14:09
@LucaMarconato LucaMarconato merged commit 1a34815 into main Jan 13, 2025
7 of 8 checks passed
@LucaMarconato LucaMarconato deleted the feat/func_to_get_attrs_elements branch January 13, 2025 14:23
@LucaMarconato LucaMarconato changed the title Helper function to return keys in the sdata.attrs slot in an specific format get_attrs(): helper function to return keys in the sdata.attrs slot in an specific format Jan 20, 2025
@LucaMarconato LucaMarconato changed the title get_attrs(): helper function to return keys in the sdata.attrs slot in an specific format get_attrs(): helper function to return keys in the sdata.attrs slot in an specific format Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement ✨ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants