|
| 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() |
0 commit comments