Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More permissive masking for kinematic constraints #267

Merged
merged 5 commits into from
Aug 31, 2023

Conversation

ajjackson
Copy link
Collaborator

Currently the masking logic for kinematic constrains includes only energy bins that fall entirely within the accessible energy range at the Q-bin mid-point. For narrow angle ranges, this can lead to rather ugly/inconvenient gaps in the spectrum -- and for the special case of an exact angle, no bins are included at all.

Here I have implemented a more permissive logic that unmasks the bin if any part of the energy range is accessible at this Q-bin midpoint.

euphonic-mask-finite

euphonic-mask-exact

At the moment of opening this PR it is implemented with an optional argument, so the existing behaviour remains default. However, I am inclined to declare the existing behaviour "a bug" and drop it; I can't see many situations where it would be preferable and you can always reduce the bin widths for more accuracy.

This new behaviour is required in order to perform numerical simulations of 1-D instruments such as TOSCA including dispersion. (The existing method in AbINS makes a DOS-like approximation).

@ajjackson ajjackson requested a review from mducle March 6, 2023 18:20
@ajjackson
Copy link
Collaborator Author

@mducle @davidvoneshen This PR is "draft" because it lacks testing/docs, but I'd appreciate input on the principle of the thing ;-)

@ajjackson
Copy link
Collaborator Author

Here is the script to produce the above plot, for anyone interested in playing around. You need to point it at a Spectrum2D .json file; you can make these with euphonic-powder-map --save-json

#! /usr/bin/env python3

# This requires a build of Euphonic in which the "strict" argument is available
# for euphonic.spectra.apply_kinematic_constraints, e.g. d068b5c

from argparse import ArgumentParser
from pathlib import Path

from euphonic import ureg, Spectrum2D
from euphonic.plot import plot_2d_to_axis
from euphonic.spectra import apply_kinematic_constraints

import matplotlib.pyplot as plt

def get_parser() -> ArgumentParser:
    parser = ArgumentParser(
        description='Demonstrate different masking rules for 2-D data in Euphonic')
    parser.add_argument('spectrum', type=Path, help='Euphonic Spectrum2D data file')
    parser.add_argument('--angle-range', nargs=2, default=(134.5, 135.5),
                        dest='angle_range', type=float)
    return parser

def main():
    args = get_parser().parse_args()

    spectrum = Spectrum2D.from_json_file(args.spectrum)

    fig, axes = plt.subplots(ncols=2)

    for ax, strict in zip(axes, [True, False]):
        masked_spectrum = apply_kinematic_constraints(
            spectrum,
            angle_range=args.angle_range,
            e_f = 32 * ureg('1/cm'),
            strict=strict)
        plot_2d_to_axis(masked_spectrum, ax)
        ax.set_title('Existing' if strict else 'Relaxed')

    fig.suptitle(f'Angle range: {args.angle_range[0]}°-{args.angle_range[1]}°')

    plt.show()
    

if __name__ == '__main__':
    main()

@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2023

Codecov Report

❗ No coverage uploaded for pull request base (master@d502f49). Click here to learn what that means.
The diff coverage is 100.00%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more.

@@            Coverage Diff            @@
##             master     #267   +/-   ##
=========================================
  Coverage          ?   95.78%           
=========================================
  Files             ?       28           
  Lines             ?     3958           
  Branches          ?      798           
=========================================
  Hits              ?     3791           
  Misses            ?       98           
  Partials          ?       69           
Files Changed Coverage Δ
euphonic/spectra.py 95.12% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

Test Results

     19 files  ±0       19 suites  ±0   39m 55s ⏱️ - 4m 51s
1 040 tests ±0  1 034 ✔️ ±0    6 💤 ±0  0 ±0 
7 238 runs  ±0  7 193 ✔️ ±0  45 💤 ±0  0 ±0 

Results for commit b71af0f. ± Comparison against base commit d502f49.

♻️ This comment has been updated with latest results.

@ajjackson
Copy link
Collaborator Author

Given that no strong opinions have been expressed I will make executive decision to consider the existing behaviour a bug; in this case we can lose some extra logic here.

The PR remains draft because of its dependence on #245 ; when that is merged this can be polished and opened for review.

@davidvoneshen
Copy link

Hi Adam, terribly sorry I forgot to look at this. Having now done so I agree.

@ajjackson ajjackson added this to the v1.3.0 milestone Jun 19, 2023
@ajjackson ajjackson force-pushed the permissive-masking branch from d068b5c to 09ba918 Compare July 25, 2023 14:52
Instead of requiring that bins lie entirely in accessible energy
range, require that any part of bin is in range. This avoids creating
a jagged line with gaps when using a very narrow angle range (e.g. for a
single detector bank.)
@ajjackson
Copy link
Collaborator Author

Updating test data: as we can see the underlying data looks the same, but the new outputs mask away slightly less of it

permissive-masking-testdata

@ajjackson ajjackson marked this pull request as ready for review August 25, 2023 15:44
A few NaN values become other values as the masking approach has been
slightly relaxed.
@ajjackson
Copy link
Collaborator Author

Updated the script test data: as expected the only changes are that some NaN become values.

@ajjackson ajjackson merged commit 7f58868 into master Aug 31, 2023
6 checks passed
@ajjackson ajjackson deleted the permissive-masking branch December 12, 2024 16:42
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.

3 participants