/klayoutclaw:nanodevice_gdsalignfrom klayoutclaw
Aligns microscope stack images to a GDS fabrication template by detecting lithographic markers and computing an image-to-GDS transform. Outputs warped images and material contours for KLayout.
Ships in klayoutclaw. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Align microscope images to a GDS fabrication template by detecting lithographic markers in both domains and computing a similarity transform.
Coordinate system after gdsalign: Coordinates in
traces_gds.jsonare in the GDS reference frame (floating-point micrometers). When using them inexecute_script, convert to dbu viaint(value_um / layout.dbu). Do NOT apply the flakedetect_commit image-center transform 鈥?gdsalign already handles the coordinate mapping.
instrMCPdev with opencv, numpy, scipy, gdstktraces.json with material contours in pixel coordinates${PYTHON_PATH:-conda run -n instrMCPdev python} <script>If the user does not specify an output path, default to <stack_image_dir>/output/gdsalign/ (the directory containing the source images). Never use /tmp as the default output.
1. extract_markers 鈥?Parse GDS, find 4 innermost L5/0 marker pairs
鈫?gds_markers.json (pair centers + bounding boxes in um)
2. detect_markers 鈥?Find marker crosses in microscope image
鈫?image_markers.json (detected centers in pixel coordinates)
3. align_gds 鈥?Match GDS鈫攊mage marker pairs, compute reflected similarity
鈫?gds_warp.npy (2x3 affine), gds_alignment_report.json
4. commit_gds 鈥?Warp image + transform contours, commit to KLayout
鈫?full_stack_gds.png, traces_gds.json, image_placement.json
${PYTHON_PATH:-conda run -n instrMCPdev python} skills/nanodevice_gdsalign/scripts/extract_markers.py \
--gds Template.gds --output-dir output/gdsalign/
Parses GDS, finds all L5/0 polygons, selects the 8 closest to grid center, groups into 4 pairs by proximity, labels NE/NW/SE/SW.
Outputs: gds_markers.json
${PYTHON_PATH:-conda run -n instrMCPdev python} skills/nanodevice_gdsalign/scripts/detect_markers.py \
--image stack.png --pixel-size <um/px> \
--gds-markers output/gdsalign/gds_markers.json \
--output-dir output/gdsalign/
Renders marker-pair templates from gds_markers.json, then runs multi-method template matching (grayscale, inverted, CLAHE, edge) with geometric consistency filtering to find the 4 marker pairs.
Outputs: image_markers.json, 01_template.png, 03_detections.png
Verification: After running, view 03_detections.png. The 4 detected markers (colored circles) should form a roughly square pattern, ~300-400 um apart (a few thousand pixels at 0.087 um/px), centered in the image near the flake region. The pixel_size parameter is critical for correct template rendering 鈥?use the value specified in the task instructions (typically 0.087 um/px). If detections look wrong (markers on image edges, or only 2-3 found), check that pixel_size matches the actual microscope magnification.
${PYTHON_PATH:-conda run -n instrMCPdev python} skills/nanodevice_gdsalign/scripts/align_gds.py \
--gds-markers output/gdsalign/gds_markers.json \
--image-markers output/gdsalign/image_markers.json \
--output-dir output/gdsalign/
Exhaustive enumeration over 2-point correspondences finds the best reflected similarity (image Y-down 鈫?GDS Y-up), then least-squares refinement over all inliers. Automatically resolves rotational ambiguity (90掳/180掳/270掳) for symmetric marker patterns by preferring the solution closest to 0掳 rotation.
Outputs: gds_warp.npy (2脳3 affine matrix), gds_alignment_report.json
${PYTHON_PATH:-conda run -n instrMCPdev python} skills/nanodevice_gdsalign/scripts/commit_gds.py \
--warp output/gdsalign/gds_warp.npy \
--traces output/combine/traces.json \
--image full_stack_raw.jpg --pixel-size <um/px> \
--gds Template.gds --output-dir output/gdsalign/ [--warp-only]
Warps the microscope image and material contours into GDS coordinates using the affine from align_gds.py. Without --warp-only, also loads the GDS template into KLayout, adds the warped image as a background overlay, and inserts material polygons. The default mapping is top_hBN to 10/0, graphene to 11/0, bottom_hBN to 12/0, and graphite to 13/0.
| Flag | Description |
|---|---|
--warp | Path to gds_warp.npy (2脳3 affine matrix) |
--traces | Path to traces.json from flakedetect (with contour_um fields) |
--image | Full-stack microscope image |
--pixel-size | Image pixel size in um/px |
--gds | Template GDS file (loaded into KLayout during commit) |
--output-dir | Optional output directory for warp results (default: current working directory). Set this to the agent's workspace directory to keep warp outputs scoped. Files written: traces_gds.json, full_stack_gds.png, image_placement.json |
--warp-only | Only produce warped files, skip KLayout commit |
--material-layer-map | Optional path to a JSON object mapping each trace role to a distinct L/D string. An explicit map replaces the default mapping. |
--verify | After insertion, verify that the expected material layers contain shapes. |
--mcp-config | Path to MCP config JSON. Fallback chain: .mcp.json in CWD 鈫?mcp_config.json in project root 鈫?default 127.0.0.1:8765. In Docker, pass a config pointing to http://host.docker.internal:8765/mcp |
--align-report | Path to gds_alignment_report.json for the registration gate. Default: auto-located next to --warp. |
--gds-markers | Path to gds_markers.json for the marker-field frame check. Default: auto-located next to --warp. |
--skip-registration-check | Escape hatch 鈥?strongly discouraged. Bypasses the registration self-validation gate. Only for an intentional hand-built transform. |
Outputs: full_stack_gds.png, traces_gds.json, image_placement.json
When an explicit device SOP supplies a material map, save the map as a JSON
file and pass --material-layer-map <path> --verify. For example,
{"top_graphite": "111/0", "bottom_graphite": "115/0"} describes a
two-role commit. Include every role in the traces and at least one image-frame
contour_um polygon for each role in the map. The explicit path validates
roles and pairs before opening the MCP connection and records the effective
map in traces_gds.json. After loading the template it checks for zero-area
contours on the layout grid before inserting the image or material polygons.
Use distinct pairs that are free in the template.
鈿狅笍 Validated-commit contract 鈥?commit flakes ONLY through
commit_gds.py
commit_gds.pyis the only sanctioned way to place flake polygons in the GDS frame. Do NOT hand-place flakes viaexecute_scriptwith a hand-built transform (a centered shift / Y-flip / "1:1 at origin"). That is the dominant failure mode (HM08/QH06/AH06): the device looks perfect against the agent's own flakes but lands hundreds of um off the real marker field and scores ~0.Before committing,
commit_gds.pyruns a registration self-validation gate (no ground truth 鈥?only the alignment report, the marker grid, and your own warped contours). It hard-fails (exit 3) when:
- there is no
gds_alignment_report.jsonnext to the warp (alignment was skipped / hand-rolled),- the alignment is bad (
< 3inliers or mean residual> 5 um),- the
--warpmatrix does not match the alignment report (a stale/hand-built transform), or- the warped flakes land outside the L5 marker field.
If the gate fails, FIX the alignment 鈥?do not bypass it. Almost always the cause is a wrong
pixel_size: re-runvalidate_pixel_size, thendetect_markers鈫?align_gds, and commit the resultinggds_warp.npy. Only pass--skip-registration-checkif you are deliberately committing a hand-built transform and understand it will likely be off-frame.
| Metric | Pass | Fail |
|---|---|---|
| Markers detected | >= 3 | < 3 |
| Inliers (align_gds) | >= 2 | < 2 |
| Mean residual | < 5.0 um | >= 5.0 um |
Fail on any metric: Do NOT commit to KLayout. Check diagnostic images and retry.
align_gds.py automatically tries all four companions and picks the one with rotation closest to 0掳.full_stack_gds.png) is vertically flipped on save so that row 0 holds GDS-north data. KLayout's pya.Image renders PNG row 0 at the high-Y edge of the placement bbox, so the image ends up right-side-up when placed at origin_um = (x_min, y_min) with identity rotation. If you inspect the PNG in a file viewer it will already look correct (north at top).traces_gds.json) are produced from the same affine. If you re-run the alignment at a different pixel_size, you must also re-run commit_gds.py with the same pixel_size. Mixing a gds_warp.npy produced at one pixel size with commit_gds.py --pixel-size at another will place the image at the wrong scale (the scale factor in the warp matrix compensates for the pixel_size used during alignment).commit_gds.py calls layout.clear() before loading the template into KLayout, so any pre-existing geometry in the current tab is wiped. This is intentional 鈥?it avoids cell-name collisions in Layout.read() that otherwise leave orphaned cell indices. Run commit_gds.py against a fresh tab (or use the create_layout MCP tool first) if you have ongoing work you don't want to lose.The pixel_size parameter (um/px) is critical for correct template rendering in detect_markers.py and image placement in commit_gds.py. Always validate the pixel size before running the pipeline.
Common pixel_size values by objective magnification:
| Objective | Typical pixel_size (um/px) |
|---|---|
| 100x | 0.05 |
| 100x (alt) | 0.087 |
| 50x | 0.1 |
| 20x | 0.25 |
| 10x | 0.5 |
Use validate_pixel_size to confirm the value before running the alignment pipeline. If the pixel size is wrong, marker detection will fail or produce incorrect transforms.
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Error (missing file, insufficient markers, transform failure) |
| 2 | Invalid command arguments or explicit material map, or material verification failure |
| 3 | commit_gds.py registration self-validation failed (skipped/hand-rolled/off-frame transform) 鈥?fix the alignment, do not bypass |
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub caidish/klayoutclaw --plugin klayoutclaw18 skills in this plugin
/autocadEdit and export layout geometry in the optional AutoCAD for Mac backend when the user selects AutoCAD or requests native DWG editing through its MCP server./displayklayoutclaw:displayToggle KLayout layer visibility on and off for better visualization. Use this skill when the user wants to show/hide layers, toggle layer display, change layer visibility, or focus on specific layers in KLayout. Also trigger when the user says things like "hide the metal layer", "show only mesa", "turn off layer 3", or wants to isolate layers for inspection./e2e-judgeklayoutclaw:e2e-judgeRun agentic E2E tests for KlayoutClaw. Gives Claude Code natural-language tasks, lets it autonomously choose MCP tools, independently verifies layout state via direct MCP calls, and judges outcomes with gpt-5-mini. Use when the user wants to run agentic tests, validate KlayoutClaw, or benchmark agent performance./geometryklayoutclaw:geometryCreate geometry in KLayout via MCP — rectangles, polygons, paths, cells, and cell instances. Use this skill whenever the user wants to draw, add, or create shapes/geometry/structures in KLayout, even if they don't say "geometry" explicitly. Trigger on phrases like "draw a rectangle", "add a polygon", "create a cell", "place an instance", "make a shape", or any layout/chip/mask design geometry task./imageklayoutclaw:imageLoad a reference image (microscope photo, optical image, SEM) into KLayout as a background overlay for design alignment. Use this skill whenever the user wants to load an image, add a background image, overlay a microscope photo, import a reference picture, align to a photo, or trace over an image. Also trigger when the user says "load this image", "use this photo as reference", "overlay the microscope image", or needs to position/scale/remove background images./klayout_gds_importImport a GDS file into the current KLayout layout, flatten hierarchy, and merge into a single top cell. Use this instead of raw Layout.read() which leaves orphaned cell indices and multi-top-cell states.Other plugins' skills for the same job