|
| 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 | + lots_of_properties = [ |
| 16 | + neuroglancer.AnnotationPropertySpec( |
| 17 | + id=f"random_{t}", |
| 18 | + type="float32", |
| 19 | + default=10, |
| 20 | + ) |
| 21 | + for t in range(300) |
| 22 | + ] |
| 23 | + |
| 24 | + def random_values(i): |
| 25 | + return [i * t for t in range(300)] |
| 26 | + |
| 27 | + def property_value_dict(t): |
| 28 | + random_values_ = random_values(t) |
| 29 | + return { |
| 30 | + **{f"random_{i}": random_values_[i] for i in range(300)}, |
| 31 | + } |
| 32 | + |
| 33 | + properties = [ |
| 34 | + neuroglancer.AnnotationPropertySpec(id="size", type="float32"), |
| 35 | + neuroglancer.AnnotationPropertySpec(id="cell_type", type="uint16"), |
| 36 | + neuroglancer.AnnotationPropertySpec(id="point_color", type="rgba"), |
| 37 | + *lots_of_properties, |
| 38 | + ] |
| 39 | + |
| 40 | + writer = neuroglancer.write_annotations.AnnotationWriter( |
| 41 | + coordinate_space=coordinate_space, |
| 42 | + annotation_type="point", |
| 43 | + properties=properties, |
| 44 | + ) |
| 45 | + |
| 46 | + writer.add_point( |
| 47 | + [20, 30, 40], |
| 48 | + size=10, |
| 49 | + cell_type=16, |
| 50 | + point_color=(0, 255, 0, 255), |
| 51 | + **property_value_dict(1), |
| 52 | + ) |
| 53 | + writer.add_point( |
| 54 | + [50, 51, 52], |
| 55 | + size=30, |
| 56 | + cell_type=16, |
| 57 | + point_color=(255, 0, 0, 255), |
| 58 | + **property_value_dict(2), |
| 59 | + ) |
| 60 | + writer.add_point( |
| 61 | + [40, 50, 52], |
| 62 | + size=30, |
| 63 | + cell_type=16, |
| 64 | + point_color=(255, 0, 0, 255), |
| 65 | + **property_value_dict(2), |
| 66 | + ) |
| 67 | + writer.write(os.path.join(output_dir, "point")) |
| 68 | + |
| 69 | + writer = neuroglancer.write_annotations.AnnotationWriter( |
| 70 | + coordinate_space=coordinate_space, |
| 71 | + annotation_type="polyline", |
| 72 | + properties=properties, |
| 73 | + ) |
| 74 | + writer.add_polyline( |
| 75 | + [[25, 10, 20], [10, 15, 20]], |
| 76 | + size=10, |
| 77 | + cell_type=16, |
| 78 | + point_color=(0, 255, 0, 255), |
| 79 | + **property_value_dict(3), |
| 80 | + ) |
| 81 | + writer.add_polyline( |
| 82 | + [[5, 17, 29], [10, 15, 30], [40, 20, 25]], |
| 83 | + size=30, |
| 84 | + cell_type=16, |
| 85 | + point_color=(255, 0, 0, 255), |
| 86 | + **property_value_dict(4), |
| 87 | + ) |
| 88 | + writer.add_polyline( |
| 89 | + [[5, 17, 29], [10, 15, 30], [40, 20, 2], [100, 10, 0]], |
| 90 | + size=1, |
| 91 | + cell_type=16, |
| 92 | + point_color=(0, 0, 100, 255), |
| 93 | + **property_value_dict(5), |
| 94 | + ) |
| 95 | + writer.write(os.path.join(output_dir, "polyline")) |
| 96 | + |
| 97 | + writer = neuroglancer.write_annotations.AnnotationWriter( |
| 98 | + coordinate_space=coordinate_space, |
| 99 | + annotation_type="polyline", |
| 100 | + properties=( |
| 101 | + neuroglancer.AnnotationPropertySpec(id="size", type="float32"), |
| 102 | + neuroglancer.AnnotationPropertySpec(id="cell_type", type="uint16"), |
| 103 | + neuroglancer.AnnotationPropertySpec(id="point_color", type="rgba"), |
| 104 | + ), |
| 105 | + ) |
| 106 | + writer.add_polyline( |
| 107 | + [[20, 5, 20], [10, 15, 20]], |
| 108 | + size=10, |
| 109 | + cell_type=16, |
| 110 | + point_color=(0, 255, 0, 255), |
| 111 | + ) |
| 112 | + writer.add_polyline( |
| 113 | + [[5, 17, 2], [10, 2, 30], [40, 20, 25]], |
| 114 | + size=30, |
| 115 | + cell_type=16, |
| 116 | + point_color=(255, 0, 0, 255), |
| 117 | + ) |
| 118 | + writer.add_polyline( |
| 119 | + [[5, 17, 10], [10, 6, 30], [40, 20, 2], [10, 10, 0]], |
| 120 | + size=1, |
| 121 | + cell_type=16, |
| 122 | + point_color=(0, 0, 100, 255), |
| 123 | + ) |
| 124 | + writer.write(os.path.join(output_dir, "simplepoly")) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + ap = argparse.ArgumentParser() |
| 129 | + neuroglancer.cli.add_server_arguments(ap) |
| 130 | + args = ap.parse_args() |
| 131 | + neuroglancer.cli.handle_server_arguments(args) |
| 132 | + viewer = neuroglancer.Viewer() |
| 133 | + |
| 134 | + tempdir = "/tmp/neuroglancer_annotations" |
| 135 | + if os.path.exists(tempdir): |
| 136 | + shutil.rmtree(tempdir) |
| 137 | + os.makedirs(tempdir) |
| 138 | + |
| 139 | + coordinate_space = neuroglancer.CoordinateSpace( |
| 140 | + names=["x", "y", "z"], units=["nm", "nm", "nm"], scales=[10, 10, 10] |
| 141 | + ) |
| 142 | + write_some_annotations(output_dir=tempdir, coordinate_space=coordinate_space) |
| 143 | + |
| 144 | + server = neuroglancer.static_file_server.StaticFileServer( |
| 145 | + static_dir=tempdir, bind_address=args.bind_address or "127.0.0.1", daemon=True |
| 146 | + ) |
| 147 | + |
| 148 | + with viewer.txn() as s: |
| 149 | + s.layers["image"] = neuroglancer.ImageLayer( |
| 150 | + source=neuroglancer.LocalVolume( |
| 151 | + data=np.full(fill_value=200, shape=(100, 100, 100), dtype=np.uint8), |
| 152 | + dimensions=coordinate_space, |
| 153 | + ), |
| 154 | + ) |
| 155 | + s.layers["points"] = neuroglancer.AnnotationLayer( |
| 156 | + source=f"precomputed://{server.url}/point", |
| 157 | + tab="rendering", |
| 158 | + shader=""" |
| 159 | +void main() { |
| 160 | + setColor(prop_point_color()); |
| 161 | + setPointMarkerSize(prop_size()); |
| 162 | +} |
| 163 | + """, |
| 164 | + ) |
| 165 | + s.layers["polylines"] = neuroglancer.AnnotationLayer( |
| 166 | + source=f"precomputed://{server.url}/polyline", |
| 167 | + tab="rendering", |
| 168 | + shader=""" |
| 169 | +void main() { |
| 170 | +setColor(prop_point_color()); |
| 171 | +setLineWidth(prop_size()); |
| 172 | +} |
| 173 | + """, |
| 174 | + ) |
| 175 | + s.layers["polylinesa"] = neuroglancer.AnnotationLayer( |
| 176 | + source=f"precomputed://{server.url}/simplepoly", |
| 177 | + tab="rendering", |
| 178 | + shader=""" |
| 179 | +void main() { |
| 180 | +setColor(prop_point_color()); |
| 181 | +setLineWidth(prop_size()); |
| 182 | +} |
| 183 | + """, |
| 184 | + ) |
| 185 | + s.selected_layer.layer = "points" |
| 186 | + s.selected_layer.visible = True |
| 187 | + s.show_slices = False |
| 188 | + print(viewer) |
0 commit comments