Skip to content

Check peak-range files in one place and set d0 in autoreduction (#1147) - #1158

Merged
fanchercm merged 3 commits into
nextfrom
fix/1147-peak-range-d0
Nov 25, 2026
Merged

fanchercm merged 3 commits into
nextfrom
fix/1147-peak-range-d0

Conversation

@fanchercm

Copy link
Copy Markdown
Collaborator

Refs #1147.

Problem

Autoreduction fits peaks from a peak-range JSON but never applied its d0, so the project file stored a NaN d-reference. The strain/stress viewer then loaded it without error and showed NaN strain everywhere, with no message. The peak fitting tool already mapped an empty d0 list to 1.0; autoreduction did not.

What this changes

  • pyrs/peaks/peak_ranges.py (new): one checked reader, read_peak_range_json, and writer, write_peak_range_json. An empty or wrong-length d0 list becomes DEFAULT_D0 = 1.0 (a placeholder, not a measured d0; a warning naming the file and peak is logged). Anything else malformed raises PeakRangeFileError.
  • apply_d0(peak_collection, peak): stores the d0 on a fitted PeakCollection, so a caller cannot forget the step that set_d_reference's NaN default hides.
  • Never fail on a bad file: every bad path or content raises only PeakRangeFileError. This covers a FIFO or directory, an oversized or non-UTF-8 file, deeply nested JSON, huge numbers, a repeated key, a bad or duplicate label, and a bad window or d0. Messages name the file and peak and carry no raw control characters.
  • Peak fitting and texture fitting tools read and save through these, and report a bad file or table instead of failing, leaving the table and disk unchanged. The texture reader used to turn a null d0 into 1.0.
  • docs/ground_truths.md: root causes of Explore why peak-fit autoreduced data cannot be loaded into the stress/strain visualization #1147; docs/deferred_defects.md: DD-34 to DD-36.

Not in this PR

  • The other cause found, NaN sample positions in 47% of autoreduced files (calculate_sub_run_time_average), is only proposed in ground_truths.md and recorded as DD-34.
  • The HB2B autoreduce script is a separate repository; a patch for it is kept outside this repo. This PR must be deployed before that patch, which imports peak_ranges.
  • Fitted parameters are unaffected; only the stored d reference changes (NaN to 1.0) for files fitted through apply_d0.

Review

Design, test and security reviews were run on the first commits; their bugs are fixed here (see the second and third commits). Findings left as design choices are noted in the commit messages.

Testing

  • pixi run test-unit: 1391 passed.
  • tests/ui/test_peak_fitting.py, tests/ui/test_texture_fitting.py: 35 passed (offscreen).
  • ruff, ruff format and mypy (CI scope) pass via pre-commit.

Companion PRs carry the same change to qa and main.

Assisted-by: Claude

Autoreduction fitted peaks without applying the d0 of the peak-range file,
so its project files stored a NaN d-reference and the strain/stress viewer
showed all-NaN strain with no message. The peak fitting tool already maps an
empty d0 list to 1.0; that rule now lives in pyrs.peaks.peak_ranges.

- add read_peak_range_json(): empty or wrong-length d0 list -> DEFAULT_D0
  (1.0, a placeholder, not a measured d0); anything else malformed raises
  PeakRangeFileError naming the file and peak
- PeakFittingModel.load_peak_range_json delegates to it (valid files load
  as before)
- the peak fitting window reports a bad file and keeps the ranges on screen
  instead of failing
- docs/ground_truths.md: root causes of #1147 (NaN sample positions in 47%
  of autoreduced files; NaN d-reference) and proposed, unapplied fixes

Fitted parameters are unaffected; only the stored d reference changes
(NaN -> 1.0) for files fitted through the new function.

Defects recorded, not fixed:
- DD-34: calculate_sub_run_time_average returns NaN for vx/vy/vz when the
  raw log starts with a NaN placeholder
- DD-35: strain/stress viewer selectedPeak setter lets StrainField errors
  escape a Qt slot and abort the process

Assisted-by: Claude
Review of the first #1147 commit found inputs that escaped the one-type
contract the peak fitting window and autoreduction rely on:

- a FIFO path hung the caller; a device or huge file was read without bound
- deeply nested JSON raised RecursionError; a 5000-digit number raised
  ValueError; a 400-digit number raised OverflowError
- a None, int, bytes or NUL-containing path raised TypeError or ValueError
- a repeated key silently dropped the first peak
- a null or non-printable peak label became the label "None" or was accepted
- keys and values were echoed raw into messages (control characters, no limit)

The file is now opened without blocking, must be a regular file under 1 MB,
is decoded as UTF-8 (a BOM is accepted) and parsed with a duplicate-key check;
every failure is a PeakRangeFileError. A file with no peaks is an error.

Tests: every bad-input case now expects PeakRangeFileError (not ValueError),
adds a regression test for each input above, and the GUI test asserts that
nothing on screen changed.

Not done, left for a decision: the texture-fitting reader and the writers share
this file format but not its checks; the 1.0 default is not flagged to callers.

Assisted-by: Claude
Follow-up to the review of the first #1147 commits:

- the texture fitting tool's from_json/to_json and the peak fitting tool's
  save now use the same checked reader and a new checked writer,
  write_peak_range_json, so a file PyRS writes is one it reads; a bad file or
  table is reported through the existing failure paths and leaves the table
  and disk unchanged (the texture reader used to turn a null d0 into 1.0)
- a defaulted d0 (empty or wrong-length list) logs a warning naming the file
  and peak, since 1.0 is a placeholder
- apply_d0() stores a peak's d0 on its fitted PeakCollection, so a caller
  cannot forget the step that set_d_reference's NaN default hides
- two peaks may not share a label (the label is the peak tag)
- error messages keep the file name when the path is long

Tests: writer, apply_d0, warning, duplicate labels, the texture model's
fit-range files, the peak fitting model's save, and a GUI test for a bad save.

Defect recorded, not fixed:
- DD-36: TextureFittingViewer.load calls TextureFittingModel.from_json with
  one argument instead of two, so the "Load Stress state" action raises
  TypeError out of a Qt slot

Assisted-by: Claude
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

  • Trigger review

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

Run configuration

  • 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

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.28571% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.23%. Comparing base (82a9eba) to head (67f5631).
⚠️ Report is 2 commits behind head on next.

Files with missing lines Patch % Lines
pyrs/peaks/peak_ranges.py 98.48% 2 Missing ⚠️
...interface/texture_fitting/texture_fitting_model.py 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #1158      +/-   ##
==========================================
+ Coverage   87.12%   87.23%   +0.11%     
==========================================
  Files         106      107       +1     
  Lines       16655    16786     +131     
==========================================
+ Hits        14510    14644     +134     
+ Misses       2145     2142       -3     
Flag Coverage Δ
gui 69.52% <74.85%> (+<0.01%) ⬆️
integration 57.24% <18.85%> (-0.27%) ⬇️
ring-calibration 32.34% <18.85%> (-0.07%) ⬇️
unit 56.30% <93.14%> (+0.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fanchercm

Copy link
Copy Markdown
Collaborator Author

The other cause found in #1147, NaN sample positions written by the converter, is not part of this PR; it is tracked in #1164 (recorded as DD-34 in docs/ground_truths.md on next).

Assisted-by: Claude

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