|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import shutil |
| 4 | + |
| 5 | +import neuroglancer |
| 6 | +import neuroglancer.cli |
| 7 | +import neuroglancer.static_file_server |
| 8 | +import neuroglancer.write_annotations |
| 9 | +import numpy as np |
| 10 | + |
| 11 | + |
| 12 | +def write_some_annotations( |
| 13 | + output_dir: str, coordinate_space: neuroglancer.CoordinateSpace |
| 14 | +): |
| 15 | + properties = [ |
| 16 | + neuroglancer.AnnotationPropertySpec( |
| 17 | + id="color", |
| 18 | + description="Color of the annotation", |
| 19 | + type="rgb", |
| 20 | + default="#ff0000", # red |
| 21 | + ), |
| 22 | + neuroglancer.AnnotationPropertySpec( |
| 23 | + id="size", |
| 24 | + description="Size of the annotation", |
| 25 | + type="float32", |
| 26 | + default=10, |
| 27 | + ), |
| 28 | + neuroglancer.AnnotationPropertySpec( |
| 29 | + id="p_int8", |
| 30 | + type="int8", |
| 31 | + default=10, |
| 32 | + ), |
| 33 | + neuroglancer.AnnotationPropertySpec( |
| 34 | + id="p_uint8", |
| 35 | + type="uint8", |
| 36 | + default=10, |
| 37 | + ), |
| 38 | + neuroglancer.AnnotationPropertySpec( |
| 39 | + id="rgba_color", |
| 40 | + type="rgba", |
| 41 | + default="#00ff00ff", # green with full opacity |
| 42 | + ), |
| 43 | + neuroglancer.AnnotationPropertySpec( |
| 44 | + id="p_enum1", |
| 45 | + type="uint16", |
| 46 | + default=0, |
| 47 | + enum_values=[0, 1, 2, 3], |
| 48 | + enum_labels=[ |
| 49 | + "Option 0", |
| 50 | + "Option 1", |
| 51 | + "Option 2", |
| 52 | + "Option 3", |
| 53 | + ], |
| 54 | + ), |
| 55 | + neuroglancer.AnnotationPropertySpec( |
| 56 | + id="p_fnum32", |
| 57 | + type="float32", |
| 58 | + default=0.0, |
| 59 | + description="A float number property", |
| 60 | + enum_values=[0.0, 1.5, 2.6, 3.0], |
| 61 | + enum_labels=[ |
| 62 | + "Zero", |
| 63 | + "One and a half", |
| 64 | + "Two point six", |
| 65 | + "Three", |
| 66 | + ], |
| 67 | + ), |
| 68 | + ] |
| 69 | + |
| 70 | + writer = neuroglancer.write_annotations.AnnotationWriter( |
| 71 | + coordinate_space=coordinate_space, |
| 72 | + annotation_type="point", |
| 73 | + properties=properties, |
| 74 | + ) |
| 75 | + |
| 76 | + writer.add_point( |
| 77 | + [20, 30, 40], |
| 78 | + color=(0, 255, 0), # RGB color |
| 79 | + size=10, |
| 80 | + p_int8=1, |
| 81 | + p_uint8=2, |
| 82 | + rgba_color=(0, 255, 0, 255), # RGBA color with full opacity |
| 83 | + p_enum1=1, |
| 84 | + p_fnum32=1.5, |
| 85 | + ) |
| 86 | + writer.add_point( |
| 87 | + [50, 51, 52], |
| 88 | + color=(255, 0, 0), # RGB color |
| 89 | + size=30, |
| 90 | + p_int8=2, |
| 91 | + p_uint8=3, |
| 92 | + rgba_color=(255, 0, 0, 255), # RGBA color with full opacity |
| 93 | + p_enum1=2, |
| 94 | + p_fnum32=2.6, |
| 95 | + ) |
| 96 | + writer.add_point( |
| 97 | + [40, 50, 52], |
| 98 | + color=(0, 0, 255), # RGB color |
| 99 | + size=20, |
| 100 | + p_int8=3, |
| 101 | + p_uint8=4, |
| 102 | + rgba_color=(0, 200, 255, 14), # RGBA color with part opacity |
| 103 | + p_enum1=3, |
| 104 | + p_fnum32=3.0, |
| 105 | + ) |
| 106 | + writer.add_point([40, 50, 52]) |
| 107 | + writer.write(os.path.join(output_dir, "point")) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + ap = argparse.ArgumentParser() |
| 112 | + neuroglancer.cli.add_server_arguments(ap) |
| 113 | + args = ap.parse_args() |
| 114 | + neuroglancer.cli.handle_server_arguments(args) |
| 115 | + viewer = neuroglancer.Viewer() |
| 116 | + |
| 117 | + tempdir = "/tmp/neuroglancer_annotations" |
| 118 | + if os.path.exists(tempdir): |
| 119 | + shutil.rmtree(tempdir) |
| 120 | + os.makedirs(tempdir) |
| 121 | + |
| 122 | + coordinate_space = neuroglancer.CoordinateSpace( |
| 123 | + names=["x", "y", "z"], units=["nm", "nm", "nm"], scales=[10, 10, 10] |
| 124 | + ) |
| 125 | + write_some_annotations(output_dir=tempdir, coordinate_space=coordinate_space) |
| 126 | + |
| 127 | + server = neuroglancer.static_file_server.StaticFileServer( |
| 128 | + static_dir=tempdir, bind_address=args.bind_address or "127.0.0.1", daemon=True |
| 129 | + ) |
| 130 | + |
| 131 | + with viewer.txn() as s: |
| 132 | + s.layers["image"] = neuroglancer.ImageLayer( |
| 133 | + source=neuroglancer.LocalVolume( |
| 134 | + data=np.full(fill_value=200, shape=(100, 100, 100), dtype=np.uint8), |
| 135 | + dimensions=coordinate_space, |
| 136 | + ), |
| 137 | + ) |
| 138 | + s.layers["points"] = neuroglancer.AnnotationLayer( |
| 139 | + source=f"precomputed://{server.url}/point", |
| 140 | + tab="rendering", |
| 141 | + shader=""" |
| 142 | +void main() { |
| 143 | + setColor(prop_color()); |
| 144 | + setPointMarkerSize(prop_size()); |
| 145 | +} |
| 146 | + """, |
| 147 | + ) |
| 148 | + s.selected_layer.layer = "points" |
| 149 | + s.selected_layer.visible = True |
| 150 | + s.show_slices = False |
| 151 | + print(viewer) |
0 commit comments