XMXMatcher User Guide

XMatcher 正式使用手册

面向粉末 XRD 物相识别的完整软件文档。手册分为三个模块:代码与应用、App 使用、常见问题。点击对应模块即可展开阅读,避免内容混在一起。

Official Release V1.0.0 Author: Dr. Bin Cao XRD Phase Identification

XMatcher Official User Guide

A complete software manual for powder XRD phase identification. The guide is split into three modules: Code & Application, App Usage, and FAQ. Click a module to expand only the part you need.

Official Release V1.0.0 Author: Dr. Bin Cao XRD Phase Identification

项目资源与引用信息

下载软件、阅读在线手册、反馈问题,并复制正式引用信息。

Project Resources And Citation

Download the software, read the online manual, report issues, and copy the formal citation.

作者Author

Dr. Bin Cao (曹斌)

bin-cao.github.io
软件下载Download

正式发行版本与数据文件。Official release and data files.

doi.org/10.6084/m9.figshare.32812985
在线手册Manual

App 和代码文档入口。Entry point for App and code documentation.

asterbin.github.io/Asterbin-XMatcher
问题反馈Issues

报告 bug、使用问题或功能建议。Report bugs, usage questions, or feature requests.

GitHub Issues
引用方式Citation
引用说明: XMatcher 并非 XQueryer 本身,而是作为 XQueryer 相关研究工作的一部分开发和整理的本地 XRD 物相识别软件。目前 XMatcher 没有单独发表论文;如果本软件支持了您的研究,建议引用下面的 XQueryer 论文。
Citation note: XMatcher is not XQueryer itself. It is a local XRD phase-identification software developed and organized as part of the related XQueryer research work. XMatcher currently does not have a separate publication; if this software supports your research, please cite the XQueryer paper below.

Cao B., Zheng Z., Liu Y., Zhang L., Wong L. W. Y., Weng L.-T., Li J., Li H., and Zhang T.-Y. XQueryer: an intelligent crystal structure identifier for powder X-ray diffraction. National Science Review 12(12), nwaf421 (2025).

@article{cao2025xqueryer,
  title={XQueryer: an intelligent crystal structure identifier for powder X-ray diffraction},
  author={Cao, Bin and Zheng, Zinan and Liu, Yang and Zhang, Longhan and Wong, Lawrence WY and Weng, Lu-Tao and Li, Jia and Li, Haoxiang and Zhang, Tong-Yi},
  journal={National Science Review},
  volume={12},
  number={12},
  pages={nwaf421},
  year={2025},
  publisher={Oxford University Press}
}

模块一:代码与应用

背景知识、安装环境、获取数据库、构建理论峰库、第一次代码匹配和 Jupyter API。

点击展开阅读模块一

Module 1: Code & Application

Background, environment setup, database download, theoretical peak database build, first code search, and Jupyter API.

Click to expand Module 1

1. 概览

XMatcher 的推荐工作流有三个阶段。最快方式是直接从 GitHub Releases 下载已经构建好的 MP500_xrd_database.pkl;如果你需要修改构建参数,也可以下载 MP500.db 后自行构建。

1. 获取峰数据库

优先从 GitHub Releases 下载 MP500_xrd_database.pkl。它是检索阶段直接读取的理论峰数据库。

2. 可选:自行构建

如果只下载了 MP500.db,运行 build_database_parallel.py 把它转换成 MP500_xrd_database.pkl

3. 检索实验 XRD

XRDRetriever 读取 MP500_xrd_database.pkl,再输入实验谱进行匹配。

检索阶段只读取 MP500_xrd_database.pkl。不要把原始 MP500.db 直接传给 XRDRetriever

1. Overview

XMatcher has a three-stage recommended workflow. The fastest path is to download the prebuilt MP500_xrd_database.pkl from GitHub Releases. If you need custom build parameters, download MP500.db and build the peak database yourself.

1. Get the peak database

Prefer downloading MP500_xrd_database.pkl from GitHub Releases. It is the theoretical peak database consumed directly by retrieval.

2. Optional: build it yourself

If you downloaded only MP500.db, run build_database_parallel.py to convert it into MP500_xrd_database.pkl.

3. Search experimental XRD

Use XRDRetriever to load MP500_xrd_database.pkl, then match experimental patterns.

The retrieval stage reads only MP500_xrd_database.pkl. Do not pass the raw MP500.db directly to XRDRetriever.

3. 安装环境

建议使用 Python 3.9 或更高版本,并创建一个独立环境。

git clone https://github.com/Bin-Cao/XMatcher.git
cd XMatcher

python -m venv .venv
source .venv/bin/activate

python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

如果你要运行 notebook、测试或代码检查:

pip install -r requirements-dev.txt
pytest

3. Install The Environment

Use Python 3.9 or newer and create an isolated environment.

git clone https://github.com/Bin-Cao/XMatcher.git
cd XMatcher

python -m venv .venv
source .venv/bin/activate

python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -e .

For notebooks, tests, and lint tooling:

pip install -r requirements-dev.txt
pytest

4. 从 Releases 下载数据库

打开项目 GitHub Releases 页面。现在有两种下载方式:

  • MP500_xrd_database.pkl:推荐下载。它是已经构建好的 XMatcher 理论峰数据库,可以直接用于检索。
  • MP500.db:原始 ASE 晶体结构数据库。只有当你想自己重新构建、修改峰数或 2θ 范围等构建参数时才需要下载。

如果你选择直接下载 MP500_xrd_database.pkl,把它放到项目根目录即可:

XMatcher/
  MP500_xrd_database.pkl
  exp_data/
  XMatcher/
直接检索: 预构建文件可以直接这样读取:XRDRetriever(database_path="MP500_xrd_database.pkl")
不要把 MP500.db 传给检索器: XRDRetriever(database_path=...) 需要的是 MP500_xrd_database.pklMP500.db 只能作为构建输入。

4. Download Databases From Releases

Open the project's GitHub Releases page. There are now two download paths:

  • MP500_xrd_database.pkl: recommended. This is the prebuilt XMatcher theoretical peak database and can be used directly for retrieval.
  • MP500.db: the raw ASE crystal structure database. Download it only if you want to rebuild the peak database yourself or change build parameters such as peak count or 2θ range.

If you download MP500_xrd_database.pkl directly, place it in the project root:

XMatcher/
  MP500_xrd_database.pkl
  exp_data/
  XMatcher/
Direct retrieval: The prebuilt file can be loaded with XRDRetriever(database_path="MP500_xrd_database.pkl").
Do not pass MP500.db to the retriever: XRDRetriever(database_path=...) expects MP500_xrd_database.pkl. MP500.db is only a build input.

5. 构建 XMatcher 理论峰数据库

build_database_parallel.py 的作用是读取 MP500.db 中的晶体结构,调用 pymatgen 计算每个结构的理论 XRD 图谱,提取最强峰,并保存成 XMatcher 检索用的特征数据库。

小规模测试构建

python build_database_parallel.py \
  --db-path MP500.db \
  --output MP500_xrd_database.pkl \
  --workers 4 \
  --max-entries 500 \
  --batch-size 200 \
  --n-peaks 30 \
  --two-theta-min 10 \
  --two-theta-max 90

完整构建

python build_database_parallel.py \
  --db-path MP500.db \
  --output MP500_xrd_database.pkl \
  --workers 8 \
  --batch-size 1000 \
  --save-interval 1000 \
  --n-peaks 30 \
  --two-theta-min 10 \
  --two-theta-max 90

构建完成后,项目目录里会出现 MP500_xrd_database.pkl。检索代码必须读取这个输出文件:

retriever = XRDRetriever(database_path="MP500_xrd_database.pkl")
建议: 第一次构建先使用 --max-entries 500 验证环境和速度,再移除该参数构建全量库。

5. Build The XMatcher Theoretical Peak Database

build_database_parallel.py reads crystal structures from MP500.db, uses pymatgen to calculate theoretical XRD patterns, extracts the strongest peaks, and saves the searchable feature database used by XMatcher.

Small Test Build

python build_database_parallel.py \
  --db-path MP500.db \
  --output MP500_xrd_database.pkl \
  --workers 4 \
  --max-entries 500 \
  --batch-size 200 \
  --n-peaks 30 \
  --two-theta-min 10 \
  --two-theta-max 90

Full Build

python build_database_parallel.py \
  --db-path MP500.db \
  --output MP500_xrd_database.pkl \
  --workers 8 \
  --batch-size 1000 \
  --save-interval 1000 \
  --n-peaks 30 \
  --two-theta-min 10 \
  --two-theta-max 90

After the build finishes, MP500_xrd_database.pkl will appear in the project directory. Retrieval must read this output file:

retriever = XRDRetriever(database_path="MP500_xrd_database.pkl")
Recommendation: Start with --max-entries 500 to verify the environment and speed, then remove it for a full build.

6. 运行第一次匹配

下面的脚本会读取示例实验数据,检测峰,然后在构建好的 MP500_xrd_database.pkl 里检索最相近的结构。

from XMatcher import XRDRetriever

retriever = XRDRetriever(
    database_path="MP500_xrd_database.pkl",
    n_peaks=20,
    position_tolerance=0.2,
    scoring_method="hybrid",
)

results = retriever.retrieve_from_file(
    "exp_data/BTc.csv",
    elements=["B", "Tc"],
    element_filter_mode="exact",
    top_n=10,
)

retriever.print_results(results)

best = results[0]
print(best["formula"], best["score"], best["estimated_shift"])
print(best["peak_matches"][:3])

保存为 quick_start.py 后运行:

python quick_start.py

6. Run The First Search

The script below reads an example experimental pattern, detects peaks, and retrieves the closest structures from the built MP500_xrd_database.pkl.

from XMatcher import XRDRetriever

retriever = XRDRetriever(
    database_path="MP500_xrd_database.pkl",
    n_peaks=20,
    position_tolerance=0.2,
    scoring_method="hybrid",
)

results = retriever.retrieve_from_file(
    "exp_data/BTc.csv",
    elements=["B", "Tc"],
    element_filter_mode="exact",
    top_n=10,
)

retriever.print_results(results)

best = results[0]
print(best["formula"], best["score"], best["estimated_shift"])
print(best["peak_matches"][:3])

Save it as quick_start.py and run:

python quick_start.py

7. Jupyter API 说明

如果你希望在 notebook 里逐步导入实验 XRD、调整寻峰参数、执行全库检索并查看匹配峰对,请打开根目录下的 API 示例:

XMatcher_Jupyter_API_Guide_CN_EN.ipynb

这个 notebook 默认使用 MP500_xrd_database.pkl,并把 QUERY_ELEMENTS = None 作为全库检索设置。你可以直接修改实验数据路径、峰检测参数和匹配参数。

API 地址: XMatcher_Jupyter_API_Guide_CN_EN.ipynb

7. Jupyter API Guide

For a step-by-step notebook workflow covering experimental XRD import, peak-detection tuning, full-database retrieval, and peak-match inspection, open the API example in the project root:

XMatcher_Jupyter_API_Guide_CN_EN.ipynb

The notebook uses MP500_xrd_database.pkl by default and sets QUERY_ELEMENTS = None for full-database retrieval. You can edit the experimental data path, peak-detection parameters, and matching parameters directly.

API path: XMatcher_Jupyter_API_Guide_CN_EN.ipynb

模块二:App 使用

图形界面完整操作手册:上传实验谱、自动识别、候选判断、绘图编辑、PDF 全峰对比和结果导出。

点击展开阅读模块二

Module 2: App Usage

Complete graphical interface manual: upload patterns, identify phases, judge candidates, edit plots, compare PDF peaks, and export results.

Click to expand Module 2

模块三:常见问题

实验数据格式、参数建议和常见故障处理。把原来的 7/8/9 独立整理为本模块的 1/2/3。

点击展开阅读模块三

Module 3: FAQ

Experimental data format, parameter suggestions, and troubleshooting. The former 7/8/9 sections are reorganized as 1/2/3 here.

Click to expand Module 3

1. 实验 XRD 数据格式

实验文件至少需要两列:第一列为 2θ,第二列为强度。支持逗号、制表符、空格和分号分隔。

two_theta,intensity
10.00,5.2
10.02,5.5
10.04,5.1

读取器会自动排序、去除非法值、合并重复 2θ 点,并过滤负强度。

1. Experimental XRD Input Format

The file needs at least two columns: 2θ in the first column and intensity in the second. Comma, tab, whitespace, and semicolon delimiters are supported.

two_theta,intensity
10.00,5.2
10.02,5.5
10.04,5.1

The reader sorts values, removes invalid rows, merges duplicate 2θ points, and filters negative intensities.

2. 常用参数建议

  • elements:已知元素组成时一定要填,能显著减少误匹配。
  • element_filter_mode="exact":候选相必须只包含给定元素时使用。
  • element_filter_mode="contains":允许掺杂、杂质或额外元素时使用。
  • position_tolerance=0.2:常规实验数据的起点;仪器误差更大时可尝试 0.3 到 0.5。
  • n_peaks=20:一般足够;复杂多相或低信噪数据可提高到 30。
  • intensity_weight=0.15:默认偏重峰位,适合实验强度不完全可靠的场景。

2. Parameter Suggestions

  • elements: provide known elements whenever possible to reduce false positives.
  • element_filter_mode="exact": use when candidates must contain exactly the given elements.
  • element_filter_mode="contains": use when dopants, impurities, or extra elements are possible.
  • position_tolerance=0.2: a good starting point; try 0.3 to 0.5 for noisier calibration.
  • n_peaks=20: usually enough; increase to 30 for complex or noisy patterns.
  • intensity_weight=0.15: keeps the score position-focused when experimental intensities are less reliable.

3. 常见问题

找不到 MP500.db

确认文件已经从 Releases 下载,并且 build_database_parallel.py --db-path 指向正确路径。相对路径以你运行命令的目录为准。

找不到 MP500_xrd_database.pkl

说明还没有完成构建,或者输出文件不在当前目录。先运行构建命令,并让 XRDRetriever(database_path=...) 指向构建后的 .pkl 文件。

没有匹配结果

先尝试放宽 element_filter_modecontains,或增大 position_tolerance。同时检查实验文件的 2θ 单位和范围是否正确。

App 显示 API 未连接

确认已经在项目根目录运行 python xmatcher_local_api.py --database MP500_xrd_database.pkl,并且终端没有报错。如果修改过 API 地址,确认界面中的地址仍为 http://127.0.0.1:8765 或实际服务地址。

点击自动识别后没有候选

优先减小“峰数量”。峰数量越大,软件要求越多实验强峰同时被候选解释;对于噪声较多、多相或弱峰明显的数据,过多峰会让检索过严。也可以适当降低“最小峰高”和“最小显著性”,检查是否漏检主要峰。

PDF 模块提示 Unknown endpoint

这通常表示正在运行旧版本本地 API,里面还没有 /api/cif-xrd 接口。关闭旧终端,重新运行最新的 xmatcher_local_api.py,或重新打开封装后的 App。

PDF 计算失败

先检查 CIF 文件是否有效,再确认本地环境包含 pymatgen。如果 2θ 范围设置错误,例如最小值大于最大值,也会导致计算失败。

PDF 图和主图看起来不完全一样

主图用于快速识别,会显示用于匹配的峰和当前候选理论峰;PDF 模块用于完整验证,会显示去背底实验谱、完整理论峰底部标记和多相混合谱。两者用途不同,最终确认以 PDF 全峰对比为准。

构建数据库很慢

理论 XRD 计算本身比较重。先用 --max-entries 小规模测试,再根据机器核心数调整 --workers--batch-size

pickle 安全警告

这是针对构建后 .pkl 特征数据库的安全边界提醒。只加载自己构建的输出文件或可信来源提供的预构建特征库。

3. Troubleshooting

MP500.db cannot be found

Make sure the file was downloaded from Releases and build_database_parallel.py --db-path points to the correct location. Relative paths are resolved from the directory where you run the command.

MP500_xrd_database.pkl cannot be found

The build has not finished, or the output file is not in the current directory. Run the build command first, then point XRDRetriever(database_path=...) to the built .pkl file.

No matches returned

Try element_filter_mode="contains" or increase position_tolerance. Also verify the 2θ unit and range in the experimental file.

The App Shows API Not Connected

Make sure python xmatcher_local_api.py --database MP500_xrd_database.pkl is running in the project root and the terminal has no error. If you changed the API URL, confirm it matches http://127.0.0.1:8765 or the actual service address.

No Candidate After Running Identification

Reduce Peak count first. A larger peak count requires more experimental strong peaks to be explained simultaneously; noisy, multiphase, or weak-peak patterns can become too strict. Also check whether Min height and Min prominence missed important peaks.

PDF Module Reports Unknown Endpoint

This usually means an older local API is still running and does not expose /api/cif-xrd. Stop the old terminal and restart the latest xmatcher_local_api.py, or reopen the packaged App.

PDF Calculation Fails

Check that each CIF is valid, then confirm the local environment includes pymatgen. Invalid 2θ ranges, such as a minimum larger than the maximum, can also fail.

The PDF Plot Looks Different From The Main Plot

The main plot is for fast identification and selected candidate overlay; the PDF module is for full validation with background-removed experiment, bottom markers for all theoretical peaks, and multiphase mixture profiles. Use the PDF full-peak comparison for final confirmation.

Database building is slow

Theoretical XRD calculation is computationally heavy. Start with --max-entries, then tune --workers and --batch-size for your machine.

Pickle security warning

This safety boundary applies to the built .pkl feature database. Only load outputs built by yourself or trusted prebuilt feature databases.