Skip to content

Repository files navigation

FlakeFinder

Automated 2D material flake detection system for the Sharpe Lab's Leica DM6M microscope. Scans silicon wafers, detects flakes (hBN, graphene, WSe₂), and uploads results to flakes.sharpelab.science.

Pipeline

The find-flakes command orchestrates the full scan-to-upload pipeline:

  1. Overview scan (2.5x) — continuous-motion snake scan of the full stage area
  2. Stitch + chip detection — assemble frames into panoramic image, find chip boundaries via Otsu thresholding
  3. Per-chip focus map — autofocus at grid points, fit tilt plane for Z tracking
  4. Chip scan (10x or 20x) — continuous-motion scan with real-time Z tracking along the focus plane
  5. Segmentation — per-frame flake detection, classification, and tiered scoring (runs in background)
  6. Revisit (50x) — re-image top-ranked flakes at higher magnification
  7. Upload — package results and POST to flakes.sharpelab.science

Two scan presets:

PresetOverviewChip ScanSpeedUse Case
2.5_102.5x10x10 mm/sFast screening
2.5_202.5x20x5 mm/sHigher resolution

Supports checkpointing — re-run with --resume <run_dir> to pick up where a previous run left off.

See docs/architecture.md for detailed system design and hardware specs.

Material Presets

Segmentation is configured per-material via DetectorConfig presets. Each preset defines contrast thresholds, calibration curves (R/G contrast → thickness), classification gates, and scoring functions.

PresetMaterialSubstrateCalibrationNotes
hbn_mediumhBN90nm SiO₂AFM-verifiedPrimary hBN preset
hbn_thick_90nmhBN90nm SiO₂AFM-verified30-40nm hBN
hbn_medium_285nmhBN285nm SiO₂Transfer matrix modelTighter R gate for tape rejection
graphene_thin_90nmGraphene90nm SiO₂Per-layer contrastLayer counting (0.335 nm/layer)
graphene_thick_90nmGraphene90nm SiO₂Per-layer contrastThick graphene (~5-10 nm); cal TBD
wse2_monolayer_90nmWSe₂90nm SiO₂Point calibrationSingle-layer reference

Detections are classified by proximity to the calibration curve (thin/medium/thick) and assigned a tier (T1 = high confidence, T2 = possible, T3 = unlikely) plus a continuous score based on size, shape, and calibration distance.

Installation

Requirements: Python 3.13+, Windows (for Leica SDK), uv package manager.

git clone git@github.com:sharpelab/flakefinder.git
cd flakefinder
uv sync
git config core.hooksPath hooks/

Leica SDK DLLs must be in src/flakefinder/dlls/ (already in place on the microscope PC). See docs/setup.md for details.

Usage

On the microscope PC

# Full pipeline with default preset (2.5x overview + 10x chip scan)
uv run find-flakes
# Higher resolution preset
uv run find-flakes --preset 2.5_20
# Graphene detection
uv run find-flakes --material graphene_thin_90nm
# Only scan specific chips
uv run find-flakes --chips 0,2,5
# Resume a previous run
uv run find-flakes --resume scans/run_20260301_1430
# Preview without running
uv run find-flakes --dry-run

Remotely via sls

The sls tool runs commands on the microscope PC over SSH:

sls find-flakes --dry-run # run a command
sls stage # check stage position
sls pull scans/run_20260301_1430/ # download scan data
sls push calibration/flatfield.npy # upload a file
sls git status # run git on the microscope

GUIs

CommandFrameworkPurpose
uv run find-flakes-guiTkinterForm-based launcher for find-flakes (for collaborators)
uv run run-viewerTkinterBrowse completed runs, view detections with filters
uv run quick-scanPySide6Interactive stage viewer with live camera feed

Commands

All commands are registered as pyproject.toml entry points. Run with uv run <command> on the microscope or sls <command> remotely.

CommandPurpose
find-flakesFull pipeline orchestrator
scanMulti-row snake scan with continuous motion
stitchStitch scan frames into 2D overview image
find-chipsDetect chips in stitched image via Otsu thresholding
focus-mapAutofocus grid sampling across a chip
analyze-focus-mapAnalyze focus map, fit tilt plane
chip-scanChip scan with continuous Z tracking
autofocusSingle-point Z-scan autofocus
captureSingle image capture
revisitRevisit stage points with autofocus and capture
stageStage position, objective, and lamp control
uploadUpload run results to flakes.sharpelab.science
run-viewerGUI: browse completed runs
find-flakes-guiGUI: form-based pipeline launcher
quick-scanGUI: interactive stage viewer

Analysis Scripts

Post-processing and analysis tools in scripts/. Run with uv run python scripts/<script>.py.

ScriptPurpose
process_overview.pyOverview post-processing (rsync + stitch + chip detection)
process_chip_scan.pyChip scan analysis (rsync + segmentation)
segment_chip_scan.pyRun segmentation on a completed chip scan
segment_flakes.pySegment individual frames
crop_mosaic.pyBuild detection mosaic grids with filtering (--tier, --where, --top)
eval_detections.pyEvaluate detection quality across runs
rerank_detections.pyRe-score detections with updated config
build_flatfield.pyBuild flatfield calibration from blank frames
analyze_chip_scan.pyScan quality analysis (Z tracking, frame pacing)
analyze_autofocus.pyAutofocus quality analysis
hbn_contrast.pyTransfer matrix hBN contrast model
hbn_contrast_widget.pyInteractive R/G contrast explorer with sliders
download_flakes.pyDownload flake images from flakes.sharpelab.science

Tools

ToolPurpose
tools/slsRun commands on the microscope via SSH. Setup: ln -sf $(pwd)/tools/sls ~/.local/bin/sls
tools/scan-nbAppend timestamped entries to scan notebooks. Setup: ln -sf $(pwd)/tools/scan-nb ~/.local/bin/scan-nb

Directory Structure

├── src/flakefinder/
│ ├── leica/ # Hardware library: Stage, Camera, ZDrive, Lamp, etc.
│ ├── commands/ # CLI entry points (see Commands table)
│ ├── segmentation.py # Flake detection: presets, scoring, classification
│ ├── scan_utils.py # Geometry, interpolation, flatfield correction
│ ├── flakes_api.py # Client for flakes.sharpelab.science
│ ├── cli_utils.py # Argparse helpers, metadata builders
│ ├── data_utils.py # Scan data loading
│ └── types.py # NamedTuples (ScanMeta, FrameMeta, etc.)
├── src/quick_scan/ # PySide6 stage viewer GUI
├── scripts/ # Analysis and post-processing scripts
│ └── experiments/ # Hardware characterization experiments
├── tools/ # sls, scan-nb, subtask-launch
├── docs/ # Architecture, scan format, microscope reference, setup
├── calibration/ # Flatfield calibration images
├── scans/ # Scan output data (gitignored)
└── archive/ # Superseded scripts kept for reference

Development

uv sync # install all deps
uv run ruff check --fix .&& uv run ruff format .# lint + format
uv run ty check # type check
uv run pytest # tests

Pre-commit hooks run ruff and ty automatically. Enable with git config core.hooksPath hooks/.

Links

About

Automated 2D material flake detection system for Leica DM6M microscope

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages