-
Notifications
You must be signed in to change notification settings - Fork 56
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
attempts to create a writer #69
Changes from 4 commits
447ffd6
ad261bc
2d83edd
07e6cb8
3424efa
552ea9d
212db6a
c970c82
ffa2bda
371404e
2a12507
7b9599a
d55edae
7c86ddf
fce7c96
fe5d61c
f6123c2
9731606
d5f1614
6c91ed0
64bafb1
b832df5
e51b1be
d35cc5e
0d0ebfd
f22c564
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#name: z | ||
name: z | ||
channels: | ||
- defaults | ||
- ome | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Image writer utility | ||
|
||
""" | ||
import logging | ||
|
||
from typing import Any, Dict, Iterator, List, Optional, Type, Union, Tuple, cast | ||
import zarr | ||
|
||
import numpy as np | ||
|
||
from .io import parse_url | ||
from .reader import Node | ||
|
||
|
||
LOGGER = logging.getLogger("ome_zarr.writer") | ||
|
||
def write_image( | ||
path: str, | ||
image: np.ndarray, | ||
name: str = "0", | ||
group: str = None, | ||
chunks: Union[Tuple[int], int] = None, | ||
byte_order: Union[str, List[str]] = "tczyx", | ||
**metadata): | ||
"""Writes an image to the zarr store according to ome-zarr specification | ||
|
||
Parameters | ||
---------- | ||
path: str, | ||
a path to the zarr store location | ||
image: np.ndarray | ||
the image to save | ||
group: str, optional | ||
the group within the zarr store to store the data in | ||
chunks: int or tuple of ints, | ||
size of the saved chunks to store the image | ||
byte_order: str or list of str, default "tczyx" | ||
combination of the letters defining the order | ||
in which the dimensions are saved | ||
""" | ||
zarr_location = parse_url(path) | ||
|
||
## I assume this should be dealt with in | ||
## the ZarrLocation classes | ||
store = zarr.DirectoryStore(path) | ||
image = np.asarray(image) | ||
|
||
if image.ndim > 5: | ||
# Maybe we can split the more than 5D images in subroups? | ||
raise ValueError("Only images of 5D or less are supported") | ||
|
||
shape_5d = (*(1,)*(5 - image.ndim), *image.shape) | ||
image = image.reshape(shape_5d) | ||
omero = metadata.get("omero", {}) | ||
|
||
# Update the size entry anyway | ||
omero["size"]= { | ||
"t": image.shape[0], | ||
"c": image.shape[1], | ||
"z": image.shape[2], | ||
"height": image.shape[3], | ||
"width": image.shape[4], | ||
} | ||
|
||
metadata["omero"] = omero | ||
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. This is currently missing the |
||
|
||
root = zarr.group(store) | ||
if group is not None: | ||
grp = root.create_group(group) | ||
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.
|
||
else: | ||
grp = root | ||
|
||
grp.create_dataset(name, data=image, chunks=chunks) | ||
|
||
for entry, value in metadata.items(): | ||
grp.attrs[entry] = value |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
from ome_zarr.writer import write_image | ||
from ome_zarr.io import parse_url | ||
from ome_zarr.reader import Reader | ||
|
||
|
||
class TestWriter: | ||
@pytest.fixture(autouse=True) | ||
def initdir(self, tmpdir): | ||
self.path = tmpdir.mkdir("data") | ||
|
||
def create_data(self, shape, dtype, mean_val=10): | ||
rng = np.random.default_rng(0) | ||
return rng.poisson(mean_val, size=shape).astype(dtype) | ||
|
||
def test_writer(self): | ||
|
||
data = self.create_data((1, 2, 1, 256, 256), np.uint8) | ||
write_image(self.path, data) | ||
reader = Reader(self.path) | ||
assert len(list(reader())) == 3 |
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.
Agreed. I think what's needed first is a way to pass
mode
through to theparse_url
and*Location
classes.