From e43ed3c56b9fe6ff7871f71e463f84b738803bf2 Mon Sep 17 00:00:00 2001 From: Jeremy Maitin-Shepard Date: Thu, 18 Jan 2024 10:46:30 -0800 Subject: [PATCH] docs(python): add example_coordinate_transform.py Fixes #517. --- .../examples/example_coordinate_transform.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 python/examples/example_coordinate_transform.py diff --git a/python/examples/example_coordinate_transform.py b/python/examples/example_coordinate_transform.py new file mode 100644 index 000000000..b2a228b0c --- /dev/null +++ b/python/examples/example_coordinate_transform.py @@ -0,0 +1,45 @@ +import argparse + +import neuroglancer +import neuroglancer.cli +import numpy as np + +if __name__ == "__main__": + ap = argparse.ArgumentParser() + neuroglancer.cli.add_server_arguments(ap) + args = ap.parse_args() + neuroglancer.cli.handle_server_arguments(args) + viewer = neuroglancer.Viewer() + + ix, iy, iz = np.meshgrid( + *[np.linspace(0, 1, n) for n in [100, 100, 100]], indexing="ij" + ) + data = np.cast[np.uint32]( + np.floor(np.sqrt((ix - 0.5) ** 2 + (iy - 0.5) ** 2 + (iz - 0.5) ** 2) * 10) + ) + data = np.pad(data, 1, "constant") + dimensions = neuroglancer.CoordinateSpace( + names=["x", "y", "z"], units="nm", scales=[10, 10, 10] + ) + + with viewer.txn() as s: + s.dimensions = dimensions + s.layers["original"] = neuroglancer.SegmentationLayer( + source=[ + neuroglancer.LayerDataSource( + neuroglancer.LocalVolume(data=data, dimensions=dimensions) + ) + ], + ) + s.layers["transformed"] = neuroglancer.SegmentationLayer( + source=[ + neuroglancer.LayerDataSource( + neuroglancer.LocalVolume(data=data, dimensions=dimensions), + transform=neuroglancer.CoordinateSpaceTransform( + output_dimensions=dimensions, + matrix=[[1, 0, 0, 0], [1, 1, 0, 0], [0, 0, 1, 0]], + ), + ) + ], + ) + print(viewer)