Skip to content
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: ✨ Generalize get_viewer util #200

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 52 additions & 60 deletions dacapo/examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,71 @@


def get_viewer(
raw_array: gp.Array | Array | ZarrArray,
labels_array: gp.Array | Array | ZarrArray,
pred_array: Optional[gp.Array | Array | ZarrArray] = None,
pred_labels_array: Optional[gp.Array | Array | ZarrArray] = None,
width: int = 1500,
height: int = 600,
) -> IFrame:
arrays = {
"raw": raw_array,
"labels": labels_array,
}
if pred_array is not None:
arrays["pred"] = pred_array
if pred_labels_array is not None:
arrays["pred_labels"] = pred_labels_array

data = {}
voxel_sizes = {}
for name, array in arrays.items():
arrays: dict, width: int = 1500, height: int = 600, headless: bool = True
) -> neuroglancer.Viewer | IFrame:
for name, array_data in arrays.items():
array = array_data["array"]
if hasattr(array, "to_ndarray"):
data[name] = array.to_ndarray()
arrays[name]["array"] = array.to_ndarray()
else:
data[name] = array.data
arrays[name]["array"] = array.data
if hasattr(array, "voxel_size"):
voxel_sizes[name] = array.voxel_size
arrays[name]["voxel_sizes"] = array.voxel_size
else:
voxel_sizes[name] = array.spec.voxel_size
arrays[name]["voxel_sizes"] = array.spec.voxel_size

neuroglancer.set_server_bind_address("0.0.0.0")
viewer = neuroglancer.Viewer()
with viewer.txn() as state:
state.showSlices = False
add_seg_layer(state, "labels", data["labels"], voxel_sizes["labels"])
for name, array_data in arrays.items():
meshes = "meshes" in array_data and array_data["meshes"]
is_seg = "is_seg" in array_data and array_data["is_seg"]
if is_seg:
add_seg_layer(
state, name, array_data["array"], array_data["voxel_sizes"], meshes
)
else:
add_scalar_layer(
state, name, array_data["array"], array_data["voxel_sizes"]
)

add_scalar_layer(state, "raw", data["raw"], voxel_sizes["raw"])
if headless:
return viewer
else:
return IFrame(src=viewer, width=width, height=height)

if "pred" in data:
add_scalar_layer(state, "pred", data["pred"], voxel_sizes["pred"])

if "pred_labels" in data:
add_seg_layer(
state, "pred_labels", data["pred_labels"], voxel_sizes["pred_labels"]
)
def add_seg_layer(state, name, data, voxel_size, meshes=False):
if meshes:
kwargs = {"segments": np.unique(data[data > 0])}
else:
kwargs = {}
state.layers[name] = neuroglancer.SegmentationLayer(
# segments=[str(i) for i in np.unique(data[data > 0])], # this line will cause all objects to be selected and thus all meshes to be generated...will be slow if lots of high res meshes
source=neuroglancer.LocalVolume(
data=data,
dimensions=neuroglancer.CoordinateSpace(
names=["z", "y", "x"],
units=["nm", "nm", "nm"],
scales=voxel_size,
),
),
**kwargs,
)


return IFrame(src=viewer, width=width, height=height)
def add_scalar_layer(state, name, data, voxel_size):
state.layers[name] = neuroglancer.ImageLayer(
source=neuroglancer.LocalVolume(
data=data,
dimensions=neuroglancer.CoordinateSpace(
names=["z", "y", "x"],
units=["nm", "nm", "nm"],
scales=voxel_size,
),
),
)


class NeuroglancerRunViewer:
Expand Down Expand Up @@ -214,31 +234,3 @@ def update_with_new_validation_if_possible(self):
self.most_recent_iteration,
validation_dataset.name,
)


def add_seg_layer(state, name, data, voxel_size):
state.layers[name] = neuroglancer.SegmentationLayer(
# segments=[str(i) for i in np.unique(data[data > 0])], # this line will cause all objects to be selected and thus all meshes to be generated...will be slow if lots of high res meshes
source=neuroglancer.LocalVolume(
data=data,
dimensions=neuroglancer.CoordinateSpace(
names=["z", "y", "x"],
units=["nm", "nm", "nm"],
scales=voxel_size,
),
),
segments=np.unique(data[data > 0]),
)


def add_scalar_layer(state, name, data, voxel_size):
state.layers[name] = neuroglancer.ImageLayer(
source=neuroglancer.LocalVolume(
data=data,
dimensions=neuroglancer.CoordinateSpace(
names=["z", "y", "x"],
units=["nm", "nm", "nm"],
scales=voxel_size,
),
),
)
Loading