Skip to content

Commit

Permalink
feat: more examples and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
seankmartin committed Jul 18, 2024
1 parent 198b9bb commit 47a23c5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 10 deletions.
2 changes: 2 additions & 0 deletions examples/allen_zarr_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def make_cordinate_space(state):
scales=[748, 748, 1, 1],
)
state.dimensions = co_ords
state.enable_adaptive_downsampling = False
state.wireFrame = True


if __name__ == "__main__":
Expand Down
37 changes: 37 additions & 0 deletions examples/logo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import neuroglancer

from neuroglancer_utils.create_datasets.create_mc_logo import create_mc_logo
from neuroglancer_utils.viewer_utils import (
launch_nglancer,
open_browser,
)

from time import sleep


def add_image_layer(state):
data = create_mc_logo()
dimensions = neuroglancer.CoordinateSpace(
names=["x", "y", "z"], units="nm", scales=[1, 1, 1]
)
local_volume = neuroglancer.LocalVolume(data, dimensions)
state.layers["image"] = neuroglancer.ImageLayer(
source=local_volume,
volume_rendering=True,
shader="""
#uicontrol invlerp normalized
void main() {
emitRGBA(vec4(0.0, 0.0, normalized(), normalized()));
}
""",
)
state.show_axis_lines = False


if __name__ == "__main__":
viewer = launch_nglancer()
open_browser(viewer, hang=False)
sleep(2)

with viewer.txn() as s:
add_image_layer(s)
4 changes: 2 additions & 2 deletions examples/single_data_point_transfer_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def colormap_version(viewer):
},
"colormap": {
"controlPoints": [[0, "#000000", 0.0], [84, "#ffffff", 1.0]],
"window": [0, 100],
"window": [0, 50],
"channel": [],
"defaultColor": "#ff00ff",
},
Expand All @@ -41,7 +41,7 @@ def colormap_version(viewer):
layer=neuroglancer.ImageLayer(
source=neuroglancer.LocalVolume(
dimensions=s.dimensions,
data=np.full(shape=(1, 1), dtype=np.uint32, fill_value=42),
data=np.full(shape=(1, 1), dtype=np.uint64, fill_value=63),
),
panels=[add_render_panel()],
),
Expand Down
39 changes: 39 additions & 0 deletions examples/small_minimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import neuroglancer
import numpy as np

from neuroglancer_utils.viewer_utils import (
launch_nglancer,
open_browser,
)

from time import sleep


def add_image_layer(state):
data = np.full(shape=(10,) * 3, fill_value=255, dtype=np.uint8)
dimensions = neuroglancer.CoordinateSpace(
names=["x", "y", "z"], units="nm", scales=[1, 1, 1]
)
local_volume = neuroglancer.LocalVolume(data, dimensions)
state.layers["image"] = neuroglancer.ImageLayer(
source=local_volume,
volume_rendering=True,
shader="""
void main() {
emitRGBA(vec4(1.0, 1.0, 1.0, 0.001));
}
""",
)
state.show_axis_lines = False
state.projection_scale = 1e-8
state.position = [5, 5, 5]
state.layout = "3d"


if __name__ == "__main__":
viewer = launch_nglancer()
open_browser(viewer, hang=False)
sleep(2)

with viewer.txn() as s:
add_image_layer(s)
23 changes: 15 additions & 8 deletions examples/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from time import sleep

import neuroglancer
from neuroglancer_utils.create_datasets.create_sphere import create_sphere
from neuroglancer_utils.create_datasets.create_sphere import create_sphere_in_cube
from neuroglancer_utils.local_server import create_server

from neuroglancer_utils.layer_utils import add_render_panel
Expand All @@ -29,9 +29,19 @@
update_title,
)

def add_image_layer(state, **kwargs):

def add_image_layer(state, data, **kwargs):
dimensions = neuroglancer.CoordinateSpace(
names=["x", "y", "z"], units="nm", scales=[40, 40, 40]
)
local_volume = neuroglancer.LocalVolume(
data,
dimensions,
max_downsampling=float("inf"),
max_downsampled_size=8,
)
state.layers["image"] = neuroglancer.ImageLayer(
source="precomputed://http://127.0.0.1:9000/sphere",
source=local_volume,
volume_rendering_mode="max",
tool_bindings={
"A": neuroglancer.VolumeRenderingGainTool(),
Expand All @@ -45,23 +55,20 @@ def get_shader():
return """#uicontrol invlerp normalized(range=[0,255], clamp=true)
void main() {
float val = normalized();
emitIntensity(val);
emitGrayscale(val);
}
"""


if __name__ == "__main__":
create_sphere()
data = create_sphere_in_cube(400, 0.9)
create_server(directory="datasets")
viewer = launch_nglancer()
with viewer.txn() as s:
add_image_layer(s, shader=get_shader())
add_image_layer(s, data, shader=get_shader())
threedee_view(viewer)
remove_axis_lines(viewer)
update_title(viewer, "Sphere example")
set_gpu_memory(viewer, gpu_memory=2)
update_projection(viewer, orientation=[0.25, 0.6, 0.65, 0.3])
open_browser(viewer, hang=False)
sleep(4) # TODO this is a hack to wait for viewer to open
update_projection(viewer, scale=1500, depth=3000)
36 changes: 36 additions & 0 deletions neuroglancer_utils/create_datasets/create_mc_logo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from cloudvolume import CloudVolume
import numpy as np
from PIL import Image
from pathlib import Path


HOMEPATH = Path.home()
FILEPATH = HOMEPATH / "Documents" / "mc_logo.png"


def create_mc_logo(output_path="file://datasets/mc_logo"):
img = Image.open(FILEPATH)
luminance = np.array(img)[:, :, -1]
Image.fromarray(luminance).save(HOMEPATH / "Documents" / "mc_logo_gray.png")
imgdata = luminance.T
dim_size = int((imgdata.shape[0] + imgdata.shape[1]) / 2)
rawdata = np.zeros(
shape=(imgdata.shape[0], imgdata.shape[1], dim_size),
dtype=np.uint8,
)
for i in range(dim_size):
rawdata[:, :, i] = imgdata

# CloudVolume.from_numpy(
# rawdata,
# vol_path=output_path,
# resolution=(1, 1, 1),
# layer_type="image",
# progress=True,
# compress=False,
# )
return rawdata


if __name__ == "__main__":
create_mc_logo()

0 comments on commit 47a23c5

Please sign in to comment.