Skip to content

Include wavelength component in Q resolution calculation#112

Open
welbournR wants to merge 19 commits into
nextfrom
resolution_fix
Open

Include wavelength component in Q resolution calculation#112
welbournR wants to merge 19 commits into
nextfrom
resolution_fix

Conversation

@welbournR
Copy link
Copy Markdown
Collaborator

Description of work:

Q-resolution calculation updated to include both lambda and theta components. Includes prior functions that weren't integrated into the workflow.

DRAFT - this needs tests to be updated. Request other eyes on the code to check the changes.

Check all that apply:

  • updated documentation
  • Source added/refactored
  • Added unit tests
  • Added integration tests
  • Verified that tests requiring the /SNS and /HFIR filesystems pass without fail

References:

  • Links to IBM EWM items:
  • Links to related issues or pull requests:

⚠️ Manual test for the reviewer

(Instructions for testing here)

Check list for the reviewer

  • best software practices
    • clearly named variables (better to be verbose in variable names)
    • code comments explaining the intent of code blocks
  • All the tests are passing
  • The documentation is up to date
  • code comments added when explaining intent

Comment thread src/lr_reduction/event_reduction.py
Comment thread src/lr_reduction/event_reduction.py Outdated
Comment thread pyproject.toml
Comment thread src/lr_reduction/event_reduction.py Outdated
Comment thread src/lr_reduction/event_reduction.py Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 88.34356% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.25%. Comparing base (d4c6005) to head (df24037).

Files with missing lines Patch % Lines
src/lr_reduction/time_resolved.py 66.66% 13 Missing ⚠️
src/lr_reduction/event_reduction.py 88.09% 5 Missing ⚠️
src/lr_reduction/output.py 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next     #112      +/-   ##
==========================================
+ Coverage   85.16%   85.25%   +0.09%     
==========================================
  Files          23       24       +1     
  Lines        2925     3032     +107     
==========================================
+ Hits         2491     2585      +94     
- Misses        434      447      +13     

☔ View full report in Codecov by Sentry.
📢 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.

@backmari backmari changed the title Update q resolution calculation Include wavelength component in Q resolution calculation Feb 25, 2026
@backmari backmari marked this pull request as ready for review February 25, 2026 19:01
@backmari backmari requested a review from Copilot February 25, 2026 19:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates LR’s Q-resolution calculation to include both wavelength (λ) and angular (θ) components, and wires the resulting per-bin resolution through the reduction/template/workflow outputs.

Changes:

  • Add new θ- and λ-based resolution helpers and return dq_over_q alongside reduced R(Q) results.
  • Introduce a SymPy-based UserDefinedFunction to evaluate the wavelength-resolution function string.
  • Update workflow/output plumbing and adjust unit/integration tests for the new return signatures and new resolution behavior.

Reviewed changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/lr_reduction/event_reduction.py Implements new θ/λ resolution computation and returns dq_over_q_bins from specular.
src/lr_reduction/template.py Propagates dq_over_q through process_from_template_ws and switches θ selection to CoordinateSystem.
src/lr_reduction/workflow.py Stores dq (absolute) in outputs and updates explorer return to include dq.
src/lr_reduction/user_defined_function.py Adds SymPy-based parser/evaluator for user-provided resolution functions.
src/lr_reduction/data_info.py Adds CoordinateSystem enum and refactors DataType.from_workspace to use it.
src/lr_reduction/gravity_correction.py Uses CoordinateSystem.from_workspace for θ-in selection.
src/lr_reduction/time_resolved.py Keeps legacy resolution for backwards compatibility; updates slice reductions/plotting to use dq.
src/lr_reduction/output.py Adds fwhm flag to ASCII header labeling for dq column.
tests/unit/lr_reduction/test_user_defined_function.py New unit tests for parsing/evaluating user-defined functions.
tests/unit/lr_reduction/test_event_reduction.py Adds tests for θ- and λ-resolution helpers; adjusts mock log exception type.
tests/unit/lr_reduction/test_data_info.py Rewrites tests to use real Mantid workspaces; adds CoordinateSystem tests.
tests/test_reduction.py Updates test expectations/unpacking for new dq_over_q return; removes obsolete wavelength test.
tests/test_dead_time.py Updates unpacking for new return shape.
pyproject.toml Adds sympy (runtime + env) and jupyter (env) dependencies.
pixi.lock Lockfile updates for new dependencies and pixi options.
docs/api/lr_reduction.rst Fixes API docs module reference from typing to types.
.gitattributes Marks pixi.lock as generated and binary-merged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

print("Could not find BL4B:Mot:xi.RBV: using supplied dQ/Q")
return default_dq
theta_deg = np.degrees(theta)
return theta_deg, default_dq
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the fallback branch when BL4B:Mot:xi.RBV is missing, compute_theta_resolution returns (theta_deg, default_dq), but default_dq is documented as a fractional dθ/θ (and historically was used as a dimensionless dQ/Q). Returning it as degrees makes the later (delta_th_deg / theta_deg) term scale as default_dq/theta_deg, which is incorrect. Convert the default fractional value into degrees (e.g., dtheta_deg = default_dq * theta_deg) or rename/retune the parameter so units are consistent across all return paths.

Suggested change
return theta_deg, default_dq
dtheta_deg = default_dq * theta_deg
return theta_deg, dtheta_deg

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@welbournR This is a good catch by copilot! Previously, when there was only a theta component, the fallback value if theta could not be computed would be dQ/Q = 0.027, but now this value is returned as the fallback dtheta, which is combined with the wavelength resolution.
What would be a reasonable default dtheta?

Comment on lines 1499 to 1505
Returns
-------
tuple of np.ndarray
(wavelength, d_lambda):
wavelength: the fitted wavelength values
d_lambda: the difference between wavelength and the fit

Raises
------
ValueError : if ws does not have exactly one spectrum
"""
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compute_wavelength_resolution’s docstring still describes the old Mantid-EvaluateFunction behavior ("fitted wavelength values" and "difference between wavelength and the fit"). The new implementation simply evaluates the provided resolution function on wl_list and returns (wl_list, d_lambda), so the return-value description should be updated to match what the function actually returns (and clarify expected units/meaning of d_lambda).

Copilot uses AI. Check for mistakes.
Comment thread tests/test_reduction.py
Comment on lines +63 to +65
# TODO: tighten the tolerance here once the equation has been updated
dq_over_q_expected = [0.014] * np.ones(43)
np.testing.assert_array_almost_equal(dq_over_q, dq_over_q_expected, decimal=3)
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions currently hard-code nearly-constant dq_over_q arrays with very loose/placeholder expectations (and TODOs to revisit). This weakens the regression protection for the new Q-resolution calculation. Prefer asserting against values computed from the underlying resolution model (theta+lambda terms) or against a known-good reference dataset so the test fails meaningfully if the resolution implementation changes.

Copilot uses AI. Check for mistakes.
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.

4 participants