Skip to main content

hklforge

Diffractometer angle calculations for any geometry, with solvers that are generated exactly from the geometry rather than hand-written for one instrument (diffcalc) or found by numerical search (libhkl, ad_hoc_diffractometer).

Status: the geometry model, the exact sub-problems, the planner (geometry + mode -> all solutions) and all of diffcalc's virtual angles (psi, naz, qaz, alpha/beta, betain/betaout, omega/bisect, ...) exist and are tested against diffcalc (all 194 modes), libhkl (all 13 E6C modes), xrayutilities and the Bernina two-reflection solver. For planning, there is crystallography (space-group absences with the reason, equivalents, structure factors from a CIF, reflection lists), tested against gemmi, and orientation helpers (U from reflections, pointing the detector along a beam).

Documentation: docs/ - virtual angles with 3D figures, modes, crystallography and orientation, verification, 3D view, geometry explorer, stored geometries. Example notebooks: examples/. Design notes and roadmap: CLAUDE.md.

Install

pip install hklforge                 # numpy + pyyaml only
pip install "hklforge[crystal]"      # + pandas, xraydb: reflection tables, structure factors
pip install "hklforge[draw]"         # + matplotlib: 3D drawings
pip install "hklforge[app]"          # + ipywidgets: the geometry explorer in a notebook

Use

from hklforge import Geometry

psic = Geometry.builtin("psic")              # You (1999) six-circle, as diffcalc
custom = Geometry.from_xrayutilities(        # xrayutilities' QConversion syntax
    sample=["x+", "z-", "y+", "z-"], detector=["x+", "z-"], r_i=(0, 1, 0))
psic.q_phi({"mu": 0, "eta": 10, "chi": 0, "phi": 0, "nu": 0, "delta": 20}, k=4.5)

from hklforge import Diffractometer
from hklforge.lattice import b_matrix
ub = b_matrix(5.431, 5.431, 5.431)                                   # phi frame, q = UB h (U = 1)
d = Diffractometer(psic, ub, energy=9000, surface_hkl=(0, 0, 1), reference_hkl=(0, 0, 1))
d.solve((1, 1, 2), {"eta": 0, "chi": 0, "phi": 0})          # every branch, as dicts of degrees
d.solve((1, 1, 2), {"chi": 10, "phi": 20, "betain": 5})
d.solve((1, 1, 1), {"mu": 0, "nu": 0, "h2": (2, 2, 0)})     # double diffraction, detector on h1
d.solve((1, 1, 1), {"eta": 0, "chi": 0, "h2": (2, 2, 0)}, detector="between")
d.solve((1, 1, 2), {"mu": 0, "nu": 0, "psi": 30})           # needs reference= / reference_hkl=
d.solve((1, 1, 2), {"nu": 0, "bisect": True, "omega": 0})   # bisecting
print(d.plan({"chi": 10, "phi": 20, "betain": 5}).describe())
angles = d.solve((1, 1, 2), {"mu": 0, "nu": 0, "betain": 2})[0]
d.pseudo(angles)                                            # all virtual angles of a position

from hklforge.draw import draw                              # 3D drawing (matplotlib)
draw(d, angles, show=("ttheta", "psi"))

Geometry.builtins()             # psic, fourcv, e4ch, k4cv, k6c (libhkl's names and frames)
from hklforge.view import page, message                     # interactive three.js view, any geometry
open("k6c.html", "w").write(page(Geometry.builtin("k6c")))
from hklforge.view.explorer import GeometryExplorer         # how a geometry is built, step by step
GeometryExplorer().widget
d.scan((1, 1, 2), {"mu": 0, "nu": 0, "phi": 0}, "phi", range(-180, 181, 5))   # solutions vs a held value

For planning (numpy only at import; pandas for tables, xraydb for |F|):

from hklforge.crystal.spacegroups import SpaceGroup
from hklforge.crystal.structure import read_cif
from hklforge.crystal import reflections
SpaceGroup("P63/mmc").why_forbidden((0, 0, 1))   # 'forbidden ... by the 6₃ screw axis along [0 0 1] and a c-glide plane ...'
st = read_cif("Bi2Se3.cif")
st.structure_factor((0, 0, 3), energy=9000)      # |F| with f0, f' + i f'' and Debye-Waller
reflections.reflection_list(st.cell, 9000, structure=st, d_min=1.5)   # allowed, why not, mult, |F|, I rel
reflections.rotation_groups(hkls, ub, axis)      # reflections a turn about an axis maps onto each other

from hklforge.orient import u_from_two           # U from two (hkl, direction) pairs, Busing & Levy
psic.detector_towards(k_out_lab, near=angles)    # detector circles pointing along a beam
from hklforge.crystal.bz import special_points   # Γ, X, L, ... and the standard paths, in hkl
special_points(st.cell, "R-3m")

Tests (pytest) compare against diffcalc-core and xrayutilities, the crystallography against gemmi (every space-group setting, structure factors).

License

BSD 3-Clause, see LICENSE. The software is provided "as is", without warranty of any kind. Check computed angles against your instrument's limits before moving motors.

Metadata

Release files for hklforge 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hklforge 0.2.0
File Size Uploaded Provenance Actions
hklforge-0.2.0.tar.gz 923.4 kB With provenance Details for hklforge-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hklforge 0.2.0
File Interpreter ABI Platform Actions
hklforge-0.2.0-py3-none-any.whl Python 3 none any Details for hklforge-0.2.0-py3-none-any.whl

Total release size: 1.2 MB

Release files / hklforge-0.2.0.tar.gz

Download URL hklforge-0.2.0.tar.gz
Size 923.4 kB
Tags Source
SHA-256 checksum
How to use checksums
662fa99d2ff4d619a5ed215540fb392865ba9520c707356c3824b573f67cfa85
BLAKE2b-256 checksum
How to use checksums
1285eb7c7c4d19962344891fbee28c2b7c1c012a2c2ccc1af95916bc838aa3b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Release files / hklforge-0.2.0-py3-none-any.whl

Download URL hklforge-0.2.0-py3-none-any.whl
Size 274.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
684514a010ffaefed072ce410e87987ca44969f89b010b6995469efba9e3fc80
BLAKE2b-256 checksum
How to use checksums
cfd116a1cd36ec79733d233c14e4d8ef08b6e0fa539da90a59c9b52b6c90ff17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

Security for hklforge 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for hklforge-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page