Skip to content

Commit e21cc9a

Browse files
committed
Add example for iterating over beam lattices
1 parent d2bf479 commit e21cc9a

7 files changed

Lines changed: 117 additions & 10 deletions

File tree

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Most jobs produce at least one asset. You may access them from the returned
110110

111111
For more complete examples, please refer to the `examples`_ in the source code repo.
112112

113-
.. _API documentation: https://metafold3d.notion.site/Metafold-REST-API-059f01a419a74811a9d2dcafe75c871b#65833c70939d4d6490f2a4defe825d46
113+
.. _API documentation: https://docs.metafold3d.com/lightcycle-api#65833c70939d4d6490f2a4defe825d46
114114
.. _examples: https://github.com/Metafold3d/metafold-python/tree/master/examples
115115

116116
.. toctree::

examples/beam_lattice_metrics.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""Example script demonstrating running multiple jobs (beam lattices).
2+
3+
Usage:
4+
python examples/beam_lattice_metrics.py -t <token> -p <project>
5+
6+
For more details on the available job types please refer to the Metafold REST API
7+
documentation.
8+
"""
9+
from argparse import ArgumentParser
10+
from metafold import MetafoldClient
11+
from pprint import pprint
12+
from typing import TypedDict
13+
import os
14+
import requests
15+
16+
17+
class Lattice(TypedDict):
18+
# There are other fields but we don't care to capture them here
19+
name: str
20+
display_name: str
21+
other_names: str
22+
positions: list[float]
23+
node_ids: list[int]
24+
25+
26+
lattice_url = "https://lightcycle-static.us-southeast-1.linodeobjects.com/lib/beam_lattice.json"
27+
28+
29+
def main() -> None:
30+
parser = ArgumentParser(description="Compute metrics for beam lattices")
31+
parser.add_argument("-t", "--token", type=str, help="access token")
32+
parser.add_argument("-p", "--project", type=str, help="project id", required=True)
33+
34+
args = parser.parse_args()
35+
36+
token = args.token or os.environ.get("METAFOLD_ACCESS_TOKEN")
37+
if not token:
38+
parser.error("access token is required")
39+
40+
metafold = MetafoldClient(token, args.project)
41+
42+
library = get_lattice_library()
43+
44+
for lattice_type in library:
45+
lattice = library[lattice_type]
46+
lattice_name = lattice["display_name"]
47+
48+
# Build arrays-of-arrays by iterating through flattened data in strides
49+
nodes = [
50+
lattice["positions"][i:i + 3]
51+
for i in range(0, len(lattice["positions"]), 3)
52+
]
53+
edges = [
54+
lattice["node_ids"][i:i + 2]
55+
for i in range(0, len(lattice["node_ids"]), 2)
56+
]
57+
58+
print(f"Running evaluate_metrics ({lattice_name}) job...")
59+
job = metafold.jobs.run("evaluate_metrics", {
60+
"graph": {
61+
"operators": [
62+
{
63+
"type": "GenerateSamplePoints",
64+
"parameters": {
65+
"size": [1.0, 1.0, 1.0],
66+
"resolution": [64, 64, 64],
67+
},
68+
},
69+
{
70+
"type": "SampleLattice",
71+
"parameters": {
72+
"lattice_data": {
73+
"nodes": nodes,
74+
"edges": edges,
75+
},
76+
"scale": [1.0, 1.0, 1.0],
77+
},
78+
},
79+
{
80+
"type": "Redistance",
81+
"parameters": {
82+
"size": [1.0, 1.0, 1.0],
83+
},
84+
},
85+
{
86+
"type": "Threshold",
87+
"parameters": {
88+
"width": 0.04,
89+
},
90+
},
91+
],
92+
"edges": [
93+
{"source": 0, "target": [1, "Points"]},
94+
{"source": 1, "target": [2, "Samples"]},
95+
{"source": 2, "target": [3, "Samples"]},
96+
],
97+
},
98+
"point_source": 0,
99+
})
100+
print(f"{lattice_type}:")
101+
pprint(job.meta)
102+
103+
104+
def get_lattice_library() -> dict[str, Lattice]:
105+
r = requests.get(lattice_url)
106+
return r.json()
107+
108+
109+
if __name__ == "__main__":
110+
main()

examples/lattice_infill.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
Vec3: TypeAlias = [float, float, float]
1717

18+
1819
class Patch(TypedDict):
1920
offset: Vec3
2021
size: Vec3
@@ -95,11 +96,7 @@ def create_graph(volume_filename: str, patch: Patch) -> dict[str, Any]:
9596
"operators": [
9697
{
9798
"type": "GenerateSamplePoints",
98-
"parameters": {
99-
"offset": patch["offset"],
100-
"size": patch["size"],
101-
"resolution": patch["resolution"],
102-
},
99+
"parameters": patch,
103100
},
104101
{
105102
"type": "LoadVolume",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Example script demonstrating running multiple jobs.
22
33
Usage:
4-
python examples/lattice_metrics.py -t <token> -p <project>
4+
python examples/surface_lattice_metrics.py -t <token> -p <project>
55
66
For more details on the available job types please refer to the Metafold REST API
77
documentation.

examples/lattice_metrics_parallel.py renamed to examples/surface_lattice_metrics_parallel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
parallel will give limited performance improvements.
55
66
Usage:
7-
python examples/lattice_metrics_parallel.py -t <token> -p <project> -n <procs>
7+
python examples/surface_lattice_metrics_parallel.py -t <token> -p <project> -n <procs>
88
99
For more details on the available job types please refer to the Metafold REST API
1010
documentation.

tests/test_assets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def do_GET(self):
8787
def do_POST(self):
8888
match self.path:
8989
case "/projects/1/assets":
90-
self.send_response(HTTPStatus.ACCEPTED)
90+
self.send_response(HTTPStatus.CREATED)
9191
self.send_header("Content-Type", "application/json")
9292
self.end_headers()
9393
self._assert_file()

tests/test_jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def do_GET(self):
114114
payload = deepcopy(new_job)
115115
payload["state"] = "started"
116116
else:
117-
self.send_response(HTTPStatus.OK)
117+
self.send_response(HTTPStatus.CREATED)
118118
payload = deepcopy(new_job)
119119
payload.update({
120120
"state": "success",

0 commit comments

Comments
 (0)