|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Generate worker configuration for data sources from .fc file. |
| 4 | +
|
| 5 | +This script extracts source nodes from a .fc flowchart file and generates |
| 6 | +a worker configuration file that can be used with ami-local to test |
| 7 | +the flowchart with mock data (static or random). |
| 8 | +
|
| 9 | +Usage: |
| 10 | + ami-fc-to-source <fc_file> [options] |
| 11 | +
|
| 12 | +Examples: |
| 13 | + # Generate worker.json from .fc file (random source by default) |
| 14 | + ami-fc-to-source tests/graphs/ATM_crix_new.fc |
| 15 | +
|
| 16 | + # Specify custom output file and event count |
| 17 | + ami-fc-to-source my_graph.fc -o my_worker.json -n 100 |
| 18 | +
|
| 19 | + # Generate for static source |
| 20 | + ami-fc-to-source my_graph.fc --source-type static |
| 21 | +
|
| 22 | + # Then use with ami-local |
| 23 | + ami-local -n 3 random://worker.json -l my_graph.fc |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse |
| 27 | +import json |
| 28 | +import sys |
| 29 | +from pathlib import Path |
| 30 | + |
| 31 | + |
| 32 | +def extract_sources_from_fc(fc_path): |
| 33 | + """ |
| 34 | + Parse .fc file and extract source node configurations. |
| 35 | +
|
| 36 | + Args: |
| 37 | + fc_path: Path to .fc file |
| 38 | +
|
| 39 | + Returns: |
| 40 | + dict: Source configurations for worker.json |
| 41 | + """ |
| 42 | + with open(fc_path, "r") as f: |
| 43 | + data = json.load(f) |
| 44 | + |
| 45 | + sources = {} |
| 46 | + for node in data.get("nodes", []): |
| 47 | + if node.get("class") == "SourceNode": |
| 48 | + name = node["name"] |
| 49 | + terminals = node.get("state", {}).get("terminals", {}) |
| 50 | + if "Out" in terminals: |
| 51 | + ttype = terminals["Out"].get("ttype", "") |
| 52 | + sources[name] = map_amitypes_to_config(ttype, source_name=name) |
| 53 | + |
| 54 | + return sources |
| 55 | + |
| 56 | + |
| 57 | +def map_amitypes_to_config(ttype, source_name=""): |
| 58 | + """ |
| 59 | + Map amitypes type string to static source config. |
| 60 | +
|
| 61 | + Args: |
| 62 | + ttype: String like "amitypes.Array2d" or "amitypes.array.Array2d" |
| 63 | + source_name: Name of the source node (e.g., "timing:raw:eventcodes") |
| 64 | +
|
| 65 | + Returns: |
| 66 | + dict: Config for static data source |
| 67 | + """ |
| 68 | + # Default config |
| 69 | + default = {"dtype": "Scalar", "range": [0, 100]} |
| 70 | + |
| 71 | + if not ttype: |
| 72 | + return default |
| 73 | + |
| 74 | + # Extract base type (handle both amitypes.Array2d and amitypes.array.Array2d) |
| 75 | + if "Array2d" in ttype: |
| 76 | + return {"dtype": "Image", "pedestal": 5, "width": 1, "shape": [512, 512]} |
| 77 | + elif "Array1d" in ttype: |
| 78 | + # Special handling for timing event codes |
| 79 | + if source_name == "timing:raw:eventcodes": |
| 80 | + return {"dtype": "Waveform", "pedestal": 0, "width": 0, "shape": [300], "binary": True} |
| 81 | + else: |
| 82 | + return {"dtype": "Waveform", "pedestal": 5, "width": 1, "shape": [1024]} |
| 83 | + elif "Array3d" in ttype: |
| 84 | + return {"dtype": "Image", "pedestal": 5, "width": 1, "shape": [100, 512, 512]} |
| 85 | + elif "int" in ttype.lower(): |
| 86 | + return {"dtype": "Scalar", "range": [0, 100], "integer": True} |
| 87 | + elif "float" in ttype.lower(): |
| 88 | + return {"dtype": "Scalar", "range": [0.0, 100.0]} |
| 89 | + else: |
| 90 | + return default |
| 91 | + |
| 92 | + |
| 93 | +def generate_worker_json(fc_path, num_events=100, repeat=True, interval=0.01, init_time=0.1, source_type="random"): |
| 94 | + """ |
| 95 | + Generate worker configuration from .fc file. |
| 96 | +
|
| 97 | + Args: |
| 98 | + fc_path: Path to .fc file |
| 99 | + num_events: Number of events to generate (default: 100) |
| 100 | + repeat: Whether to loop events (default: True) |
| 101 | + interval: Time between events in seconds (default: 0.01) |
| 102 | + init_time: Initial wait time in seconds (default: 0.1) |
| 103 | + source_type: Type of source - 'static' or 'random' (default: 'random') |
| 104 | +
|
| 105 | + Returns: |
| 106 | + tuple: (source_type, worker_config_dict) |
| 107 | + """ |
| 108 | + source_config = extract_sources_from_fc(fc_path) |
| 109 | + |
| 110 | + if not source_config: |
| 111 | + print(f"Warning: No source nodes found in {fc_path}", file=sys.stderr) |
| 112 | + print("The .fc file may not have any SourceNode entries.", file=sys.stderr) |
| 113 | + |
| 114 | + worker_json = { |
| 115 | + "interval": interval, |
| 116 | + "init_time": init_time, |
| 117 | + "bound": num_events, |
| 118 | + "repeat": repeat, |
| 119 | + "files": "data.xtc2", |
| 120 | + "config": source_config, |
| 121 | + } |
| 122 | + |
| 123 | + return source_type, worker_json |
| 124 | + |
| 125 | + |
| 126 | +def main(): |
| 127 | + parser = argparse.ArgumentParser( |
| 128 | + description="Generate worker configuration for data sources from .fc file", |
| 129 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 130 | + epilog=""" |
| 131 | +Examples: |
| 132 | + # Generate worker.json from .fc file (random source by default) |
| 133 | + %(prog)s tests/graphs/ATM_crix_new.fc |
| 134 | +
|
| 135 | + # Custom output file and event count |
| 136 | + %(prog)s my_graph.fc -o my_worker.json -n 100 |
| 137 | +
|
| 138 | + # Generate for static source |
| 139 | + %(prog)s my_graph.fc --source-type static |
| 140 | +
|
| 141 | + # Don't loop events (stop after bound) |
| 142 | + %(prog)s my_graph.fc --no-repeat |
| 143 | +
|
| 144 | + # Then use with ami-local |
| 145 | + ami-local -n 3 random://worker.json -l my_graph.fc |
| 146 | + """, |
| 147 | + ) |
| 148 | + |
| 149 | + parser.add_argument("fc_file", type=str, help="Path to .fc flowchart file") |
| 150 | + |
| 151 | + parser.add_argument( |
| 152 | + "-o", "--output", type=str, default="worker.json", help="Output worker.json file (default: worker.json)" |
| 153 | + ) |
| 154 | + |
| 155 | + parser.add_argument("-n", "--num-events", type=int, default=100, help="Number of events to generate (default: 100)") |
| 156 | + |
| 157 | + parser.add_argument("--no-repeat", action="store_true", help="Do not loop events (stop after bound)") |
| 158 | + |
| 159 | + parser.add_argument("--interval", type=float, default=0.01, help="Time between events in seconds (default: 0.01)") |
| 160 | + |
| 161 | + parser.add_argument("--init-time", type=float, default=0.1, help="Initial wait time in seconds (default: 0.1)") |
| 162 | + |
| 163 | + parser.add_argument("--show-sources", action="store_true", help="Show detected sources and exit") |
| 164 | + |
| 165 | + parser.add_argument( |
| 166 | + "--source-type", |
| 167 | + type=str, |
| 168 | + choices=["static", "random"], |
| 169 | + default="random", |
| 170 | + help="Type of source to generate for (default: random). " |
| 171 | + "static: constant values (all 1s), random: randomized values based on ranges", |
| 172 | + ) |
| 173 | + |
| 174 | + args = parser.parse_args() |
| 175 | + |
| 176 | + # Check if .fc file exists |
| 177 | + fc_path = Path(args.fc_file) |
| 178 | + if not fc_path.exists(): |
| 179 | + print(f"Error: File not found: {args.fc_file}", file=sys.stderr) |
| 180 | + sys.exit(1) |
| 181 | + |
| 182 | + # Extract sources |
| 183 | + source_config = extract_sources_from_fc(fc_path) |
| 184 | + |
| 185 | + if not source_config: |
| 186 | + print(f"Error: No source nodes found in {args.fc_file}", file=sys.stderr) |
| 187 | + print("Make sure your .fc file has SourceNode entries.", file=sys.stderr) |
| 188 | + sys.exit(1) |
| 189 | + |
| 190 | + # Show sources if requested |
| 191 | + if args.show_sources: |
| 192 | + print(f"Sources detected in {args.fc_file}:") |
| 193 | + for name, config in source_config.items(): |
| 194 | + dtype = config.get("dtype", "unknown") |
| 195 | + print(f" {name:30s} -> {dtype}") |
| 196 | + sys.exit(0) |
| 197 | + |
| 198 | + # Generate worker.json |
| 199 | + source_type, worker_json = generate_worker_json( |
| 200 | + fc_path, |
| 201 | + num_events=args.num_events, |
| 202 | + repeat=not args.no_repeat, |
| 203 | + interval=args.interval, |
| 204 | + init_time=args.init_time, |
| 205 | + source_type=args.source_type, |
| 206 | + ) |
| 207 | + |
| 208 | + # Write output |
| 209 | + output_path = Path(args.output) |
| 210 | + with open(output_path, "w") as f: |
| 211 | + json.dump(worker_json, f, indent=2) |
| 212 | + |
| 213 | + # Print summary |
| 214 | + print(f"✓ Generated {args.output} (for {source_type} source)") |
| 215 | + print(f" Sources detected: {len(source_config)}") |
| 216 | + for name, config in source_config.items(): |
| 217 | + dtype = config.get("dtype", "unknown") |
| 218 | + print(f" - {name:30s} ({dtype})") |
| 219 | + print(f" Events: {args.num_events}") |
| 220 | + print(f" Repeat: {not args.no_repeat}") |
| 221 | + print() |
| 222 | + print("To use with ami-local:") |
| 223 | + print(f" ami-local -n 3 {source_type}://{args.output} -l {args.fc_file}") |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == "__main__": |
| 227 | + main() |
0 commit comments