English · العربية · Español · Français · 日本語 · 한국어 · Tiếng Việt · 中文 (简体) · 中文(繁體) · Deutsch · Русский
A script-first research repository (historically referred to as inverse_metasurface) for inverse metasurface design in spectral imaging.
The core workflow couples:
- Physics-grounded RCWA simulation (
S4+ Lua) - Data assembly and shape attachment
- Three-stage PyTorch learning (
shape -> spectrum,spectrum -> shape, chained fine-tuning) - Quantitative and qualitative evaluation, with optional neural-vs-S4 consistency checks
Important
Canonical behavior and commands are preserved from the existing project scripts/docs. Where historical references point to absent files, those references are intentionally kept with explicit notes for compatibility.
- 🌟 Snapshot
- ✨ At a Glance
- 🌍 Internationalization (i18n)
- ✨ Features
- 🧭 End-to-End Workflow
- 🧱 Project Structure
- 🛠️ Prerequisites
- 🚀 Installation
▶️ Usage- ⚙️ Configuration
- 🧪 Examples
- 🔬 Research Context
- 🧑💻 Development Notes
- 🧯 Troubleshooting
- 🗺️ Roadmap
- 🤝 Contribution
- 📄 License
- 📚 Citation
| Focus | Status |
|---|---|
| 🧠 Objective | Inverse reconstruction of C4-symmetric metasurface geometry from spectral data |
| 🔧 Core stack | S4 RCWA (Lua) + PyTorch training + optional geometry-to-spectrum revalidation |
| 🧪 Data pipeline | CSV merge/shape attachment → NPZ (uids, spectra, shapes) |
| 🚀 Readiness | Research prototype; scripts and docs kept compatible with historical references |
| Item | Details |
|---|---|
| 🎯 Main task | Infer C4-symmetric metasurface geometry from target transmittance spectra |
| 🔬 Simulator | ../build/S4 called by shell launchers and .lua scripts |
| 🧠 Learning pipeline | Stage A shape -> spectra, Stage B spectra -> shape, Stage C spectra -> shape -> spectra |
| 📦 Data contract | Merged CSV (T@..., metadata, vertices_str) -> compressed NPZ (uids, spectra, shapes) |
| 🧪 Evaluation | MSE metrics, stage visualizations, optional fresh S4 re-simulation |
| 🌐 i18n status | Root-level multilingual README files + existing i18n/ directory |
- Multilingual READMEs are maintained at repository root as
README.<lang>.mdfiles. i18n/directory exists in this repository snapshot.- This file keeps a single language-options line at the top to avoid duplicated language bars.
README.en.mdalso exists in the repo; thisREADME.mdremains the canonical base for this update pass.
- End-to-end inverse-design path from S4 simulation output to trained inverse model.
- C4-symmetric polygon parameterization and Q1-point encoding (
4x3:presence, x, y). - Three-stage model training in one script (
three_stage_transmittance.py). - Merge tooling that preserves spectral precision and attaches per-shape vertices.
- Optional evaluator that compares learned predictions with fresh S4 simulation.
- Extensive exploratory branches (AVIRIS, SWIR/noise, GSST, archived/deprecated variants).
- Generate simulation outputs in
results/and polygon files inshapes/. - Merge S4 CSV files and attach shape vertices.
- Normalize merged column names for training compatibility.
- Preprocess merged CSV files into NPZ tensors.
- Train Stage A/B/C models.
- Evaluate checkpoints and visualize behavior.
- Optionally compare predicted-shape spectra against new S4 runs.
.
├── README.md
├── README.<lang>.md
├── how_to_run.md
├── commands.md
├── commands_updated.md
├── iccp.yaml
│
├── ms.sh
├── ms_final.sh
├── ms_resume.sh
├── ms_resume_allargs.sh
├── ms_resume_random_state.sh
├── ms_resume_random_state_nir.sh
│
├── metasurface_seed.lua
├── metasurface_final.lua
├── metasurface_seed_resume.lua
├── metasurface_allargs_resume.lua
├── metasurface_resume_random_state.lua
├── metasurface_resume_random_state_nir.lua
├── metasurface_fixed_shape_and_c_value.lua
├── metasurface_unique_shape.lua
├── metasurface_gsst_nir.lua
├── run_prediction.lua
│
├── merge_s4_data_full.py
├── three_stage_transmittance.py
├── FilterShapeS4_Evaluator_Transmittance.py
├── FilterShapeS4_Evaluator_Transmittance_Five_Rows.py
├── FilterShapeS4_Evaluator_Transmittance_Five_Rows_Inferno.py
│
├── shapes/
├── gsst_partial_crys_data/
├── outputs_three_stage_*/
├── blind_noise_experiment_all_*/
├── FilterShapeS4_Evaluator_Transmittance_*/
├── AVIRIS / aviris_*.py
├── noise_experiment*.py
├── archived/
├── deprecated/
├── deprecated-part2/
├── deprecated-scripts/
└── deprecated_code/
| Dependency | Notes |
|---|---|
| Linux + Bash | Launcher scripts target shell execution |
| Python 3.9 | Matches iccp.yaml (python=3.9.18) |
| Conda | Recommended for reproducibility |
| S4 binary | Expected at ../build/S4 |
| CUDA GPU (optional) | Speeds up training/evaluation |
git clone <your-repo-url> inverse_metasurface
cd inverse_metasurfaceconda env create -f iccp.yaml
conda activate iccpAlternative note:
# Historical README reference (file may be absent in this snapshot)
pip install -r pip_requirements.txtls -l ../build/S4chmod +x ms.sh ms_final.sh ms_resume.sh ms_resume_allargs.sh ms_resume_random_state.sh ms_resume_random_state_nir.shSimple launcher:
./ms.sh -ns 10000 -r 12345Parameterized launcher:
./ms_final.sh \
-ns 10000 \
-r 12345 \
-p myrun \
-g 80 \
-bo 0.35 \
-ro 0.30Resume-oriented launcher:
./ms_resume_allargs.sh \
-ns 10000 \
-r 12345 \
-p myrun \
-g 80 \
-bo 0.35 \
-ro 0.30Additional resume/random-state example (from command docs):
./ms_resume_random_state.sh \
-p iccp100kG20Ov \
-r 88888 \
-g 20 \
-bo 0.35 \
-ro 0.3 \
-ns 100000Notes:
- Launchers run
NQ=1..4in parallel. - Scripts call
../build/S4with-t 32.
python merge_s4_data_full.py --prefix myrun
# output: merged_s4_shapes_myrun.csvmerge_s4_data_full.py writes folder_key and NQ, while the training path expects prefix and nQ.
python -c "import pandas as pd; p='merged_s4_shapes_myrun.csv'; df=pd.read_csv(p); df=df.rename(columns={'folder_key':'prefix','NQ':'nQ'}); df.to_csv(p,index=False)"mkdir -p merged_csvs
mv merged_s4_shapes_myrun.csv merged_csvs/
python three_stage_transmittance.py \
--preprocess \
--input_folder merged_csvs \
--output_npz preprocessed_t_data.npzpython three_stage_transmittance.py \
--data_npz preprocessed_t_data.npz \
--num_epochs 100 \
--batch_size 1024Outputs are written to:
outputs_three_stage_YYYYMMDD_HHMMSS/stageAoutputs_three_stage_YYYYMMDD_HHMMSS/stageBoutputs_three_stage_YYYYMMDD_HHMMSS/stageC
Historical README command (script name retained for compatibility with prior docs):
python three_stage_transmittance_evaluation.py \
--model_dir outputs_three_stage_YYYYMMDD_HHMMSS \
--data_npz preprocessed_t_data.npz \
--sample_count 8Repository status note: three_stage_transmittance_evaluation.py is not present in this snapshot. Use FilterShapeS4_Evaluator_Transmittance.py for available evaluation functionality.
python FilterShapeS4_Evaluator_Transmittance.py \
--npz_file preprocessed_t_data.npz \
--spec2shape_ckpt outputs_three_stage_YYYYMMDD_HHMMSS/stageC/spec2shape_stageC.pt \
--shape2spec_ckpt outputs_three_stage_YYYYMMDD_HHMMSS/stageA/shape2spec_stageA.pt \
--n_samples 4| Flag | Meaning | Default |
|---|---|---|
-ns, --numshapes |
Number of shapes to generate | 100000 |
-r, --seed |
Random seed | 88888 |
-p, --prefix |
Prefix/resume key | "" |
-g, --numg |
Basis/grid parameter | 80 |
-bo, --baseouter |
Base outer boundary offset | 0.25 |
-ro, --randouter |
Random outer boundary offset | 0.20 |
| Flag | Meaning | Default |
|---|---|---|
--preprocess |
Run preprocessing mode | False |
--input_folder |
Folder containing merged CSV files | "" |
--output_npz |
Output NPZ path | preprocessed_data.npz |
--data_npz |
NPZ dataset for training | "" |
--csv_file |
CSV fallback if NPZ not used | "" |
--test |
Test mode | False |
--num_epochs |
Number of training epochs | 10 |
--batch_size |
Batch size | 4096 |
| Flag | Meaning | Default |
|---|---|---|
--model_dir |
Directory containing stageA/B/C |
required |
--data_npz |
NPZ input | "" |
--csv_file |
CSV input fallback | "" |
--output_dir |
Output directory override | auto under model_dir |
--sample_count |
Number of visualized samples | 4 |
--seed |
Random seed | 23 |
--font_scale |
Plot font scaling | 1.0 |
--batch_size |
Evaluation batch size | 32 |
--plot_only |
Plot training curves only | False |
| Flag | Meaning | Default |
|---|---|---|
--npz_file |
Input NPZ file | preprocessed_t_data.npz |
--spec2shape_ckpt |
Stage C checkpoint path | outputs_three_stage_20250322_145925/stageC/spec2shape_stageC.pt |
--shape2spec_ckpt |
Stage A checkpoint path | outputs_three_stage_20250322_145925/stageA/shape2spec_stageA.pt |
--n_samples |
Number of evaluated samples | 4 |
--seed |
Random seed | 23 |
--max_workers |
S4 worker threads | 4 |
--out_folder |
Output directory | auto timestamp |
./ms_final.sh -ns 1000 -r 42 -p smoke -g 40 -bo 0.25 -ro 0.20
python merge_s4_data_full.py --prefix smoke
python -c "import pandas as pd; p='merged_s4_shapes_smoke.csv'; d=pd.read_csv(p).rename(columns={'folder_key':'prefix','NQ':'nQ'}); d.to_csv(p,index=False)"
mkdir -p merged_csvs && mv merged_s4_shapes_smoke.csv merged_csvs/
python three_stage_transmittance.py --preprocess --input_folder merged_csvs --output_npz smoke.npz
python three_stage_transmittance.py --data_npz smoke.npz --num_epochs 5 --batch_size 128# no overlap, G=40
./ms_resume_allargs.sh -ns 10000 -r 12345 -p fast_without_overlap -g 40 -bo 0.25 -ro 0.2
# overlap, G=80
./ms_resume_allargs.sh -ns 10000 -r 12345 -p overlap -g 80 -bo 0.35 -ro 0.3
# large run, overlap, G=80
./ms_resume_allargs.sh -ns 100000 -r 12345 -p more_basis_overlap -g 80 -bo 0.35 -ro 0.3The current inverse-design setup learns to recover C4-symmetric geometry from transmittance across crystallization states. The transmittance pipeline currently assumes:
- 11 crystallization rows per shape sample (grouped by unique
shape_uid) - 100 wavelength bins per crystallization state (
T@...columns) - Up to 4 Q1 control points encoded as a
4x3tensor:(presence, x, y) - Polygon reconstruction under C4 symmetry for shape visualization and consistency checks
The repository also contains exploratory branches (AVIRIS*, noise_experiment*, archived/) beyond the primary transmittance training path.
- This is a script-centric research repository rather than a packaged Python module.
- Core scripts assume relative paths (especially
../build/S4,results/,shapes/). .gitignoreexcludes many generated experiment artifacts (*.csv,*.npz,*.pt, run folders).- Some files/directories in historical docs are currently absent in this snapshot; these references are intentionally preserved with notes for compatibility.
- macOS sidecar files (
._*) are present and may be non-functional metadata artifacts.
| Symptom | Likely Cause | Fix |
|---|---|---|
../build/S4: No such file or directory |
S4 binary missing at expected relative path | Build or link S4 at ../build/S4, or update launcher paths |
No transmission columns found |
CSV missing T@... columns |
Re-check merge output format |
Must specify either --data_npz or --csv_file |
Missing training/eval data argument | Provide one input explicitly |
No valid shapes => SHIFT->Q1->UpTo4 |
Invalid/empty vertices_str or Q1 filtering removes all samples |
Validate shape files and merge output |
Empty merge output for --prefix |
Prefix does not match files in results/ |
Check exact filename prefix and rerun merge |
| Evaluation checkpoint missing | Missing stageA/B/C checkpoint files |
Verify --model_dir points to complete output folder |
three_stage_transmittance_evaluation.py not found |
Script referenced by historical docs but absent now | Use FilterShapeS4_Evaluator_Transmittance.py or restore that script from prior commits |
- Improve reproducibility with an explicit data-versioning manifest and pinned run configs.
- Consolidate canonical entry points for transmittance, AVIRIS, and noise branches.
- Add automated smoke tests for preprocessing and one mini training epoch.
- Add clearer experiment registry linking output folders to exact command lines.
- Expand multilingual README synchronization workflow (root language files and
i18n/).
Contributions are welcome, especially for reproducibility, testing, and documentation quality.
Suggested process:
- Open an issue with scope and expected behavior.
- Create a focused branch.
- Submit a pull request with runnable commands and outputs.
- Keep changes scoped to one workflow where possible.
| Donate | PayPal | Stripe |
|---|---|---|
No LICENSE file is currently present at repository root in this snapshot. Add one to define usage and redistribution terms.
If you use this repository or build on this work, please cite:
@article{chen2025inverse,
title={Inverse Design of Metasurface for Spectral Imaging},
author={Chen, Rongzhou and Nie, Haitao and Zhu, Shuo and Zhao, Yaping and Wang, Chutian and Lam, Edmund Y},
journal={arXiv preprint arXiv:2510.21924},
year={2025}
}