Skip to content

Commit 42873eb

Browse files
committed
feat: more polyline examples
1 parent 03a37c1 commit 42873eb

File tree

3 files changed

+282
-1
lines changed

3 files changed

+282
-1
lines changed

examples/add_polylines.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@
5151
points=[[250, 100], [20, 20]],
5252
props=["#ff0", 30, 7, 9],
5353
),
54+
neuroglancer.PolyLineAnnotation(
55+
id="4",
56+
points=[[250, 100], [20, 20], [10, 10], [1, 2]],
57+
props=["#fa0", 1, 7, 20],
58+
),
5459
],
5560
shader="""
5661
void main() {
5762
setColor(prop_color());
58-
setPointMarkerSize(prop_size());
63+
setPolyEndpointMarkerSize(prop_size());
5964
}
6065
""",
6166
),
6267
)
6368
s.layout = "xy"
6469
s.selected_layer.layer = "a"
70+
s.layers["a"].tool = "annotatePolyline"
6571
print("Use `Control+right click` to display annotation details.")
6672
print(viewer)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import argparse
2+
3+
import neuroglancer
4+
import neuroglancer.cli
5+
6+
if __name__ == "__main__":
7+
ap = argparse.ArgumentParser()
8+
neuroglancer.cli.add_server_arguments(ap)
9+
args = ap.parse_args()
10+
neuroglancer.cli.handle_server_arguments(args)
11+
viewer = neuroglancer.Viewer()
12+
# Make lot of properties to test the performance
13+
lots_of_properties = [
14+
neuroglancer.AnnotationPropertySpec(
15+
id=f"random_{t}",
16+
type="float32",
17+
default=10,
18+
)
19+
for t in range(300)
20+
]
21+
22+
def random_values(i):
23+
return [i * t for t in range(300)]
24+
25+
with viewer.txn() as s:
26+
s.dimensions = neuroglancer.CoordinateSpace(
27+
names=["x", "y"], units="nm", scales=[1, 1]
28+
)
29+
s.position = [150, 150]
30+
s.layers.append(
31+
name="a",
32+
layer=neuroglancer.LocalAnnotationLayer(
33+
dimensions=s.dimensions,
34+
annotation_properties=[
35+
neuroglancer.AnnotationPropertySpec(
36+
id="color",
37+
type="rgb",
38+
default="red",
39+
),
40+
neuroglancer.AnnotationPropertySpec(
41+
id="size",
42+
type="float32",
43+
default=10,
44+
),
45+
neuroglancer.AnnotationPropertySpec(
46+
id="p_int8",
47+
type="int8",
48+
default=10,
49+
),
50+
neuroglancer.AnnotationPropertySpec(
51+
id="p_uint8",
52+
type="uint8",
53+
default=10,
54+
),
55+
*lots_of_properties,
56+
],
57+
annotations=[
58+
neuroglancer.PolyLineAnnotation(
59+
id="1",
60+
points=[[150, 150], [200, 100], [30, 40]],
61+
props=["#0f0", 5, 6, 7, *random_values(1)],
62+
),
63+
neuroglancer.PolyLineAnnotation(
64+
id="2",
65+
points=[
66+
[250, 100],
67+
[20, 20],
68+
[32, 42],
69+
[50, 60],
70+
[70, 80],
71+
[90, 100],
72+
],
73+
props=["#ff0", 30, 7, 9, *random_values(2)],
74+
),
75+
],
76+
shader="""
77+
void main() {
78+
setColor(prop_color());
79+
setPolyEndpointMarkerSize(prop_size());
80+
}
81+
""",
82+
),
83+
)
84+
s.layout = "xy"
85+
s.selected_layer.layer = "a"
86+
print("Use `Control+right click` to display annotation details.")
87+
print(viewer)
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

Comments
 (0)