Skip to content

Commit 2d7cf7f

Browse files
Add function blueprints for ome zarr and cryo et portal loading
1 parent 9ca5503 commit 2d7cf7f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

synapse_net/file_utils.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
import numpy as np
66
import pooch
77

8+
try:
9+
import cryoet_data_portal as cdp
10+
except ImportError:
11+
cdp = None
12+
13+
try:
14+
import zarr
15+
except ImportError:
16+
zarr = None
17+
818

919
def get_cache_dir() -> str:
1020
"""Get the cache directory of synapse net.
@@ -88,3 +98,47 @@ def read_mrc(path: str) -> Tuple[np.ndarray, Dict[str, float]]:
8898
# Transpose the data to match python axis order.
8999
data = np.flip(data, axis=1) if data.ndim == 3 else np.flip(data, axis=0)
90100
return data, voxel_size
101+
102+
103+
def read_ome_zarr(uri: str, scale_level: int = 0) -> Tuple[np.ndarray, Dict[str, float]]:
104+
"""Read data and voxel size from an ome.zarr file.
105+
106+
Args:
107+
uri: Path or url to the ome.zarr file.
108+
scale_level: The level of the multi-scale image pyramid to load.
109+
110+
Returns:
111+
The data read from the file.
112+
The voxel size read from the file.
113+
"""
114+
if zarr is None:
115+
raise RuntimeError("The zarr library is required to read ome.zarr files.")
116+
117+
# TODO handle URLs / make sure that zarr parses it correctly.
118+
with zarr.open(uri, "r") as f:
119+
multiscales = f.attrs["multiscales"][0]
120+
# TODO double check that the metadata is correct and transform the voxel size to a dict.
121+
# TODO voxel size is given in Angstrom, divide by 10 to get nanometer
122+
internal_path = multiscales["dataset"][scale_level]
123+
data = f[internal_path][:]
124+
transformation = multiscales["transformation"][scale_level]
125+
voxel_size = transformation["scale"]
126+
127+
return data, voxel_size
128+
129+
130+
def read_data_from_cryo_et_portal_run(
131+
run_id: int, output_path: Optional[str] = None
132+
) -> Tuple[np.ndarray, Dict[str, float]]:
133+
"""Read data and voxel size from a CryoET Data Portal run.
134+
135+
Args:
136+
run_id: The ID of the experiment run.
137+
output_path: The path for saving the data. The data will be streamed if the path is not given.
138+
139+
Returns:
140+
The data read from the run.
141+
The voxel size read from the run
142+
"""
143+
if cdp is None:
144+
raise RuntimeError("The CryoET Data portal library is required to read data from the portal.")

0 commit comments

Comments
 (0)