An XRD-conditioned crystal structure generator, built on top of DiffCSP.
Given an experimental powder pattern (here: RRUFF dolomite, CaMg(CO₃)₂, unseen during training), XRDiff samples candidate structures, each candidate is Rietveld-refined against the measured pattern with GSAS-II, and the lowest-Rwp candidate is taken as the prediction.
python==3.8.13
torch==1.9.0
torch-geometric==1.7.2
pytorch_lightning==1.3.8
pymatgen==2023.8.10
Rename .env.template to .env and set:
PROJECT_ROOT: the absolute path of this repo
HYDRA_JOBS: the absolute path to save hydra outputs
WABDB_DIR: the absolute path to save wandb outputs
G2_PYTHON: the python of your GSAS-II environment (RRUFF ranking only, see scripts/rietveld_ranking/README.md)
Datasets: Alex-MP-20-Poly (alex_mp_20_poly), OQMD-Poly (oqmd_poly) and RRUFF (rruff).
The dataset files will be uploaded to Zenodo very soon (link coming):
data/<dataset>/train_peaks_for_exp.pt # training split, RRUFF-148 structures removed
data/<dataset>/val_peaks.pt, test_peaks.pt # validation / test splits
data/rruff/rruff_pv_data.csv, test_pv_xrd.pt # RRUFF-148 test set
The full patterns binned at 0.1° (train_xrd_0.1_for_exp.pt, val_xrd_0.1.pt, test_xrd_0.1.pt)
are also in the Zenodo record. The augmented training file (<stem>_aug.pt) is generated from
them; see Training.
Building the splits from the CSVs (e.g. for a new dataset). Starting from
data/<dataset>/{train,val,test}.csv, in this order:
# 0. if the CSVs have no simulated PXRD patterns column, run:
python diffcsp/common/xrd_utils.py --datasets <dataset>
# 1. for unfiltered caches {train,val,test}_xrd_0.1.pt, one entry per CSV row, run:
python scripts/build_caches.py --dataset <dataset> --xrd_type full
# 2. to get peak lists {train,val,test}_peak_info.csv, then the peak caches {train,val,test}_peaks.pt, run:
python xrd_scripts/peak_extract.py --dataset <dataset> --extract_peak_info --xrd_type full --xrd_bins 0.1
python scripts/build_caches.py --dataset <dataset> --xrd_type peaks
# 3. remove the training structures that match an RRUFF-148 structure -> train_*_for_exp.pt, run:
python scripts/filter_rruff_matches.py --dataset <dataset>
Step 3 reads the ids to remove from data/rruff/<dataset>_train_rruff_matches.txt. It is provided
for both datasets; for a new dataset, scripts/check_train_rruff_overlap.py lists the matches.
Run the steps before training: run.py builds a missing cache from the CSV, and with
data.for_exp=true it would write an unfiltered training set under the _for_exp name.
<dataset> is alex_mp_20_poly or oqmd_poly. Common options for all XRDiff models:
COMMON="data=<dataset> data.xrd_bins=0.1 data.for_exp=true model.decoder.xrd_cond=true model.decoder.xrd_cond_type=film model.decoder.film_mode=mp"
# full-spectrum conditioning
python diffcsp/run.py $COMMON data.xrd_type=full expname=xrdiff_film
# peak-list conditioning
python diffcsp/run.py $COMMON data.xrd_type=peaks expname=xrdiff_film_peaks
# full spectrum + RRUFF-calibrated augmentation
python xrd_scripts/augment.py --pt_path data/<dataset>/train_xrd_0.1_for_exp.pt # static: peak shapes / intensities -> train_xrd_0.1_for_exp_aug.pt
python diffcsp/run.py $COMMON data.xrd_type=full data.xrd_augmented=true model.xrd_noise_augment=true expname=xrdiff_film_aug
The augmentation constants are fitted on the RRUFF calibration set by
scripts/rruff_eval/calibrate.py and hard-coded in xrd_scripts/augment.py. Run augment.py
with the default single process: --workers > 1 seeds each entry differently.
Training stops early after 100 epochs without a lower validation loss (data.early_stopping_patience),
and the run directory keeps the checkpoint with the lowest validation loss; that checkpoint is the
one evaluated.
Test sets (Alex-MP-20-Poly, OQMD-Poly): see scripts/evaluation/README.md.
python scripts/evaluation/evaluate.py --model_path=<run_dir> --eval_split=test --num_evals=3
python scripts/evaluation/compute_metrics.py --root_path=<run_dir> --eval_split=test --label=n_evals_3 --multi_eval
RRUFF-148 (20 candidates per sample, ranked by Rietveld refinement against the measured
pattern; see scripts/rietveld_ranking/README.md):
python scripts/rruff_eval/build_rruff_pv_peaks.py # once: peak lists of the RRUFF patterns (rruff_pv_peaks)
bash scripts/rruff_eval/rank20.sh <run_dir> rruff_pv_peaks <name> [seed] # peaks models
C=scripts/rruff_eval/configs
python scripts/rruff_eval/summarize.py <name>=$C/<name>_s<seed1>.yaml,$C/<name>_s<seed2>.yaml,...
rank20.sh submits SLURM jobs. Set GPU_SBATCH_ARGS, CPU_SBATCH_ARGS and ENV_SETUP
for your cluster (see the header of the script).
Built on DiffCSP.
