Pure-Python crystallography, CIF I/O, and differentiable structure factors for X-ray diffraction.
Project description
midas-hkls
Crystallography toolkit for MIDAS: HKL list generation (sginfo-equivalent), CIF I/O, and differentiable structure factors in PyTorch for intensity-aware peak fitting in pf-HEDM, ff-HEDM, and powder diffraction.
What it provides
Always-on (numpy only)
SpaceGroup— load by number, Hermann-Mauguin symbol, or Hall symbol; expose symmetry operations, systematic absences, equivalent reflections, multiplicities, Laue class, centering. All 230 SGs.Lattice— direct/reciprocal metric tensors, d-spacings, Bragg 2θ, with per-crystal-system convenience constructors.generate_hkls()— enumerate Laue-unique allowed reflections within a d-spacing or 2θ cutoff, sorted by d-descending, with multiplicities.Atom,Crystal— asymmetric-unit description with symmetry expansion to the full unit cell (special-position dedupe).- Cromer-Mann (IT92) form factors
f(s)for 98 neutral elements. - CLI:
midas-hkls gen|info|list(drop-in forGetHKLList).
Optional: [cif] (gemmi) or [cif-pure] (pycifrw)
read_cif(path) -> Crystalandwrite_cif(crystal, path)— full CIF1.1 with anisotropic ADPs (gemmi) or isotropic-only (pycifrw fallback).- Origin-choice and rhombohedral/hexagonal settings handled correctly via the resolved Hall symbol.
Optional: [torch] — differentiable structure factors
structure_factors(crystal_t, hkl, *, anomalous=False)returns complexF_hkltensor, differentiable through:- atomic fractional coordinates,
- occupancies,
- isotropic B-factors and anisotropic U-tensors,
- the six lattice parameters,
- wavelength (when
anomalous=True).
intensity_from_crystal(...)andpowder_intensity(F, m, 2θ)for Lorentz-polarization-weighted powder I_hkl.anomalous_correction(elements, wavelength_A)for f', f'' (Cromer-Liberman tables, 92 elements × 401 log-spaced energies, 100 eV–200 keV).- Symmetry expansion is exact integer arithmetic; the autograd graph is rebuilt each forward call so gradients flow through ASU handles to UC atoms.
Quick start
1. Generate HKL list
from midas_hkls import SpaceGroup, Lattice, generate_hkls
sg = SpaceGroup.from_number(225) # CeO₂ / Cu / Au / NaCl (Fm-3m)
lat = Lattice.for_system("cubic", a=5.411)
refs = generate_hkls(sg, lat, wavelength_A=0.173, two_theta_max_deg=15.0)
for r in refs:
print(r.ring_nr, (r.h, r.k, r.l), r.d_spacing, r.two_theta_deg, r.multiplicity)
2. Read a structure & compute differentiable F_hkl
import torch
from midas_hkls import read_cif, generate_hkls, structure_factors, intensity_from_crystal
xt = read_cif("ceo2.cif")
xt_t = xt.to_torch(requires_grad={"B_iso": True}) # mark B-factors trainable
refs = generate_hkls(xt.space_group, xt.lattice,
wavelength_A=0.173, two_theta_max_deg=20.0)
F, I = intensity_from_crystal(xt_t, refs, wavelength_A=0.173, polarization=0.5)
# Fit B-factors against an experimental I_obs (log-space residual)
opt = torch.optim.Adam([xt_t.B_iso_asu], lr=0.05)
for _ in range(300):
opt.zero_grad()
_, I = intensity_from_crystal(xt_t, refs, wavelength_A=0.173)
loss = ((torch.log(I + 1e-3) - torch.log(I_obs + 1e-3)) ** 2).mean()
loss.backward()
opt.step()
3. Anomalous scattering (resonant f', f'')
from midas_hkls import structure_factors, anomalous_correction
# Add f', f'' from Cromer-Liberman tables at the experimental wavelength
F_anomalous = structure_factors(xt_t, hkls,
wavelength_A=1.5418, anomalous=True)
# Or get f', f'' directly per element
fp, fpp = anomalous_correction(["Fe", "O"], wavelength_A=1.5418)
CLI
midas-hkls gen --sg 225 --lat 5.411 5.411 5.411 90 90 90 --wavelength 0.173 \
--two-theta-max 15.0 -o ceo2.csv
midas-hkls info --sg "Fm-3m" --ops
midas-hkls list
Install
pip install midas-hkls # base: numpy only
pip install "midas-hkls[cif]" # + gemmi (CIF I/O)
pip install "midas-hkls[torch]" # + torch (structure factors)
pip install "midas-hkls[all]" # all of the above
Parity & validation
- HKL generation: byte-for-byte parity vs. MIDAS's
GetHKLList(sginfo) on CeO₂, LaB₆, Si, α-Fe, α-Ti, calcite, Pnma, P21/c. - Structure factors: |F| matches
gemmi.StructureFactorCalculatorXto <0.01% on CeO₂, Si, α-Fe, LaB₆, calcite (after applying gemmi'schange_occupancies_to_crystallographicto align conventions). - Anomalous f', f'' matches
gemmi.cromer_libermanexactly on grid energies and within 0.05 between grid points. torch.autograd.gradcheckverified on |F|² w.r.t. lattice parameters and atomic positions in float64.
Conventions
- Lengths in Å; angles in degrees.
- B-factor B = 8π² U (Ų); CIF U_ij stored in fractional basis.
- Wavelengths in Å; energies in eV (
E_eV = 12398.4 / λ_Å). - Symmetry operations stored as integer Seitz matrices over translation base STBF=12 — exact-arithmetic absence detection, no float fuzz.
- Equivalent HKLs include Friedel pairs (centric structure factor under X-ray Laue symmetry).
Roadmap (post v0.2.0)
- Wyckoff special-position constraints during refinement.
- Aspherical / multipole atomic form factors.
- Magnetic structure factors.
- Expanded ion form factors (currently only neutral atoms).
Origin
The 530-entry Hall-symbol table is extracted verbatim from sginfo
(© 1994-96 Ralf W. Grosse-Kunstleve, public domain). IT92 form factors and
Cromer-Liberman anomalous tables are exported from gemmi at packaging time
and ship as JSON.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file midas_hkls-0.3.0.tar.gz.
File metadata
- Download URL: midas_hkls-0.3.0.tar.gz
- Upload date:
- Size: 367.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e1f055ffa8ccb8ab9afed89ad1cdb2ede4600229a0897f1fa40d250486f7c24
|
|
| MD5 |
224e812397c53181046ded7157c38c54
|
|
| BLAKE2b-256 |
11ef93fa0bec4399ed6f8a95e9f9ac6780707399d5f4fdedc18ecc446f7a586b
|
Provenance
The following attestation bundles were made for midas_hkls-0.3.0.tar.gz:
Publisher:
python-packages.yml on marinerhemant/MIDAS
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
midas_hkls-0.3.0.tar.gz -
Subject digest:
9e1f055ffa8ccb8ab9afed89ad1cdb2ede4600229a0897f1fa40d250486f7c24 - Sigstore transparency entry: 1422236786
- Sigstore integration time:
-
Permalink:
marinerhemant/MIDAS@6772803a986642fada95093dc69f98b4e742701b -
Branch / Tag:
refs/tags/midas-hkls-v0.3.0 - Owner: https://github.com/marinerhemant
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-packages.yml@6772803a986642fada95093dc69f98b4e742701b -
Trigger Event:
release
-
Statement type:
File details
Details for the file midas_hkls-0.3.0-py3-none-any.whl.
File metadata
- Download URL: midas_hkls-0.3.0-py3-none-any.whl
- Upload date:
- Size: 361.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0bd181d65058dc2bb8ac82dfbe86b8ee65b7a856bae0c21908c3e0fc06cdab9b
|
|
| MD5 |
869ec88170531837a1b84e16346f6e28
|
|
| BLAKE2b-256 |
2eeec69acf11cf1e77b2bf776bafa66aacd99cbb9e2c127c2b661568695f38b4
|
Provenance
The following attestation bundles were made for midas_hkls-0.3.0-py3-none-any.whl:
Publisher:
python-packages.yml on marinerhemant/MIDAS
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
midas_hkls-0.3.0-py3-none-any.whl -
Subject digest:
0bd181d65058dc2bb8ac82dfbe86b8ee65b7a856bae0c21908c3e0fc06cdab9b - Sigstore transparency entry: 1422236874
- Sigstore integration time:
-
Permalink:
marinerhemant/MIDAS@6772803a986642fada95093dc69f98b4e742701b -
Branch / Tag:
refs/tags/midas-hkls-v0.3.0 - Owner: https://github.com/marinerhemant
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-packages.yml@6772803a986642fada95093dc69f98b4e742701b -
Trigger Event:
release
-
Statement type: