Skip to content

Add pyrs.pole_figures.core: path-length-corrected pole figures (stage 1 of 4) - #1165

Open
jefrbunn wants to merge 4 commits into
neutrons:nextfrom
jefrbunn:feature/pole-figures-core
Open

jefrbunn wants to merge 4 commits into
neutrons:nextfrom
jefrbunn:feature/pole-figures-core

Conversation

@jefrbunn

@jefrbunn jefrbunn commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

This PR adds pyrs.pole_figures.core, the physics core of a tool that corrects HB-2B pole figures for the beam path through the sample. The sample shape comes from a closed STL mesh. The tool predicts the correction for each χ, φ before any data exist, and it corrects measured pole figures from reduced HB2B runs.

This is stage 1 of 4. It has no GUI, plots or file output. Those follow in later PRs (see below).

What is in this PR

  • stl_mesh: reads binary and ASCII STL files, checks that the mesh is closed and consistently wound, and traces beam paths through it.
  • rotation, geometry, projection: rotations, the HB-2B cradle (χ, φ, ω) with the sample mount and the pole for each orientation, and the equal-area projection.
  • gauge, correction: a Monte Carlo gauge volume, the attenuated intensity factor F and the correction F(0,0)/F. Prediction and measured runs share one correction step.
  • grid, prediction: the χ, φ grid with its solid-angle weights, and the predicted correction for each plane before any data exist.
  • runs, peaks, polefig: read reduced HB2B runs, measure peak rates for each sub-run, and build corrected pole figures. Points with a correction above a cutoff are flagged and left out.
  • neutron_xs, phase, reflections, hb2b: cross sections and μ from the composition, phases from presets or CIF files, allowed reflections, and HB-2B constants. The wavelengths come from MonoSetting.
  • table_scan, combine, parallel: scan planning, merging runs of the same hkl, and a process pool for the Monte Carlo work.
  • types, protocols: shared array and callback types, and the pole-figure interface the plotting stage will use.

Conventions and approved parameters

The new section "Path-length-corrected pole figures" in docs/ground_truths.md records the frames and angles. It also gives the exact mapping to PoleFigureCalculator. The two agree to 2e-8 with χ_PyRS = 90° − χ_HB-2B and PyRS (x, y, normal) = (vy, vz, −vx). This was checked at five 2θ, ω settings.

PoleFigureCalculator takes χ = 0 as φ along Q. On the HB-2B cradle the φ axis lies along Q at χ = 90°. This is recorded as DD-37 and is not changed here. Changing the texture window is a production change and needs a separate decision.

The empirical parameters (cutoff, peak windows, sub-run rejection and others) are listed in that section. The HB-2B instrument scientist approved them. Each one is a named constant or an argument with a default.

Dependencies

  • gemmi >=0.6,<0.8 in the package and every environment. It reads CIF files and gives structure factors.
  • trimesh and rtree in the test feature only. They give an independent check of the ray tracer.
  • pixi.lock adds only these packages.

Tests

  • Unit (tests/unit/pyrs/pole_figures, 327 tests): in-memory meshes, μ against Sears (1992) cross sections, analytic box and sphere chords, analytic attenuation, and geometry checks.
  • Integration (tests/integration/test_pole_figures_*.py, 188 tests): parity with the reference script's outputs, STL fixtures with the trimesh cross-check, and a 13-sub-run HB2B test file (0.26 MB) read in CI.
  • 40 tests on four full runs skip unless HIDRA_PF_TEST_DATA points to the reduced runs (about 20 MB each).
  • The test data in tests/data/pole_figures is anonymized.
  • Local runs: pre-commit, pixi run mypy pyrs scripts tests and the unit tier (1718 passed) are clean.

Later stages

  1. Plots and file output (the pole-figure figures and the per-run CSVs for MTEX).
  2. The GUI at pyrs/interface/pole_figures, opened from a standard button in the PyRS main window.
  3. The ODF step through MTEX for Python (pymtex), as an optional pixi feature.

Questions for the maintainers

  1. Stage 4 uses pymtex, which is GPL-2.0-only. PyRS's LICENSE is GPL-3.0, and its pyproject says MIT. Is an optional feature acceptable, or should the ODF step stay outside PyRS?
  2. Should a later PR change the texture window's χ zero (DD-37), or keep He's convention and document it?

🤖 Generated with Claude Code

jefrbunn and others added 3 commits November 26, 2026 11:05
gemmi (>=0.6,<0.8) reads CIF files and gives structure factors for
pyrs.pole_figures. trimesh and rtree go in the test feature only: the
pole-figure integration tests use them as an independent cross-check of
the STL ray tracer. pixi.lock adds only these packages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stage 1 of bringing the HB-2B path-length-corrected pole figure tool into
PyRS. Physics core only (no GUI, plots or file output):

- stl_mesh: closed STL mesh of the sample and a ray tracer for beam paths
- geometry: the HB-2B cradle (chi, phi, omega) and the sample mount
- gauge: gauge-volume Monte Carlo and the attenuated intensity factor F
- prediction: predicted correction F(0,0)/F on a chi/phi grid, per plane
- runs, peaks, polefig: read reduced HB2B runs, peak rates per sub-run,
  corrected pole figures with points above a cutoff ignored
- materials: phases (presets or CIF via gemmi), allowed reflections and
  mu; HB-2B wavelengths from MonoSetting
- table_scan, combine, parallel: scan planning, merging runs, worker pool

Tests: unit in tests/unit/pyrs/pole_figures (in-memory meshes; mu checked
against Sears 1992 cross sections; analytic sphere chords and
attenuation), integration in tests/integration/test_pole_figures_*.py
(parity with the reference script's outputs, STL fixtures with a trimesh
cross-check, a 13-sub-run HB2B test file; full runs when
HIDRA_PF_TEST_DATA is set). Fixtures in tests/data/pole_figures (test
data anonymized), helpers in tests/util/pole_figures_helpers.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ground_truths.md: the frames and angles of pyrs.pole_figures, the exact
mapping to PoleFigureCalculator (chi_PyRS = 90 - chi_HB2B, PyRS (x, y,
normal) = (vy, vz, -vx), checked at five 2theta/omega settings to 2e-8),
the attenuation model and the empirical parameters approved by the
instrument scientist.

Defects recorded, not fixed:
- DD-37: the texture window's chi zero differs from the HB-2B cradle's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: neutrons/PyRS
  • Branch: next
  • File: .coderabbit.yaml
  • Commit: e5b5024
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 448897c2-2a56-47f0-be41-cbe389d96f33
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@jefrbunn
jefrbunn marked this pull request as ready for review November 26, 2026 16:50
From the design review of this PR. Results are unchanged (the parity
tests pass).

- correction: Beams, check_reference and gauge_corrections, shared by
  prediction and polefig; GaugeCorrections groups the Monte Carlo
  results on PolePeak and PlanePrediction
- protocols.PoleFigureLike for the later plotting stage
- types: array aliases and progress callbacks in one place
- geometry split into rotation, projection and geometry; materials into
  neutron_xs, phase, reflections and hb2b; the grid into grid; peak
  measurement into peaks
- combine refuses runs with a different omega or mount
- text and table helpers moved out of core (they return in the file
  output stage); the reference brute-force tracer moved to the tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jefrbunn

Copy link
Copy Markdown
Author

@fanchercm @walshmm this is ready for review. It is stage 1 of 4 of the path-length-corrected pole figure tool for HB-2B (physics core only). Stage 2 (plots and file output) is #1166, stacked on this one, so please review this PR first. I could not request reviewers from my fork.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026

Copy link
Copy Markdown

Add Carrot credits or activate Agent usage billing to use Autofix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant