-
Notifications
You must be signed in to change notification settings - Fork 154
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
(feat): [WIP] support physical quantities via Pint #1481
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
from scipy import sparse | ||
|
||
import anndata as ad | ||
from anndata import AnnData, Raw | ||
from anndata import AnnData, Raw, units | ||
from anndata._core import views | ||
from anndata._core.index import _normalize_indices | ||
from anndata._core.merge import intersect_keys | ||
|
@@ -293,6 +293,27 @@ | |
_writer.write_elem(g, "varm", dict(raw.varm), dataset_kwargs=dataset_kwargs) | ||
|
||
|
||
######## | ||
# Pint # | ||
######## | ||
|
||
|
||
@_REGISTRY.register_read(H5Group, IOSpec("pint.Quantity", "0.1.0")) | ||
@_REGISTRY.register_read(ZarrGroup, IOSpec("pint.Quantity", "0.1.0")) | ||
def read_quantity(elem, _reader): | ||
v_magnitude = _reader.read_elem(elem["magnitude"]) | ||
v_units = units[_reader.read_elem(elem["units"])] | ||
return v_magnitude * v_units | ||
|
||
|
||
@_REGISTRY.register_write(H5Group, units.Quantity, IOSpec("pint.Quantity", "0.1.0")) | ||
@_REGISTRY.register_write(ZarrGroup, units.Quantity, IOSpec("pint.Quantity", "0.1.0")) | ||
def write_quantity(f, k, v, _writer, dataset_kwargs=MappingProxyType({})): | ||
g = f.require_group(k) | ||
_writer.write_elem(g, "magnitude", v.magnitude, dataset_kwargs=dataset_kwargs) | ||
_writer.write_elem(g, "units", str(v.units), dataset_kwargs=dataset_kwargs) | ||
Comment on lines
+313
to
+314
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that the units should be written into the |
||
|
||
|
||
############ | ||
# Mappings # | ||
############ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -821,3 +821,17 @@ def test_io_dtype(tmp_path, diskfmt, dtype): | |
curr = read(pth) | ||
|
||
assert curr.X.dtype == dtype | ||
|
||
|
||
def test_readwrite_units(read, write, name, tmp_path): | ||
X_arr = np.array(X_list) | ||
adata = ad.AnnData( | ||
X=X_arr, | ||
uns={"size": 100 * ad.units["um"]}, | ||
obsm={"X_spatial": X_arr * ad.units.mm}, | ||
) | ||
write(tmp_path / name, adata) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd follow the pattern from above, unless I'm missing a good reason not to i.e., read = lambda pth: getattr(ad, f"read_{diskfmt}")(pth)
write = lambda adata, pth: getattr(adata, f"write_{diskfmt}")(pth) and the corresponding fixtures for the |
||
ad_read = read(tmp_path / name) | ||
|
||
assert adata.uns["spot_size"] == ad_read.uns["spot_size"] | ||
assert (adata.obsm["X_spatial_units"] == ad_read.obsm["X_spatial_units"]).all() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might lean towards putting this in its own optional dependency section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flying-sheep thoughts?