Skip to content

Commit a4506c2

Browse files
committed
feat: support convert on nrrd
1 parent ea85009 commit a4506c2

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

examples/load_nrrd.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,40 @@
44
import neuroglancer.cli
55
import nrrd
66
from pathlib import Path
7+
from cloudvolume import CloudVolume
78

89
HERE = Path(__file__).parent
9-
NRRD_FILE_PATH = HERE / "volume1.nrrd"
1010

11+
NAMES = ["12vj", "1567", "101b"]
12+
PATHS = [HERE / f"{name}.nrrd" for name in NAMES]
13+
OUTPUT_PATHS = [f"file://datasets/{name}" for name in NAMES]
1114

12-
def add_image_layer(state):
13-
readdata, header = nrrd.read(NRRD_FILE_PATH)
15+
16+
def convert_to_precomputed(nrrd_path, output_path):
17+
readdata, header = nrrd.read(nrrd_path)
18+
# Cloud volume expects resolution in nm - but given in um
19+
scales = [header["space directions"][i][i] * 1000 for i in range(3)]
20+
CloudVolume.from_numpy(
21+
readdata,
22+
vol_path=output_path,
23+
chunk_size=(256, 256, 128),
24+
resolution=scales,
25+
layer_type="image",
26+
progress=True,
27+
compress=False,
28+
)
29+
30+
31+
def add_image_layer(state, path, name="image"):
32+
readdata, header = nrrd.read(path)
1433
scales = [header["space directions"][i][i] for i in range(3)]
1534
dimensions = neuroglancer.CoordinateSpace(
1635
names=["x", "y", "z"], units="um", scales=scales
1736
)
1837
data = readdata
1938
local_volume = neuroglancer.LocalVolume(data, dimensions)
2039
state.layers.append(
21-
name="image",
40+
name=name,
2241
layer=neuroglancer.ImageLayer(
2342
source=local_volume,
2443
volume_rendering_mode="ON",
@@ -47,7 +66,12 @@ def launch_nglancer():
4766
def main():
4867
viewer = launch_nglancer()
4968
with viewer.txn() as s:
50-
add_image_layer(s)
69+
for i in range(3):
70+
convert_to_precomputed(PATHS[i], OUTPUT_PATHS[i])
71+
path = PATHS[i]
72+
name = NAMES[i]
73+
add_image_layer(s, path, name)
74+
exit(-1)
5175
webbrowser.open_new(viewer.get_viewer_url())
5276

5377

0 commit comments

Comments
 (0)