|
1 | 1 | # SPDX-License-Identifier: BSD-3-Clause |
2 | 2 | # Copyright (c) 2025 Scipp contributors (https://github.com/scipp) |
3 | 3 |
|
| 4 | +import numpy as np |
4 | 5 | import pytest |
5 | 6 | import scipp as sc |
6 | 7 | import scipp.testing |
@@ -484,3 +485,191 @@ def test_normalize_by_monitor_integrated_raises_if_monitor_range_too_narrow() -> |
484 | 485 | monitor=monitor, |
485 | 486 | uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
486 | 487 | ) |
| 488 | + |
| 489 | + |
| 490 | +def test_normalize_by_monitor_histogram_monitor_mask_aligned_bins() -> None: |
| 491 | + detector = sc.DataArray( |
| 492 | + sc.array(dims=['w'], values=[0, 10, 20, 30], unit='counts'), |
| 493 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 494 | + ).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 495 | + monitor = sc.DataArray( |
| 496 | + sc.array(dims=['w'], values=[5.0, 6.0, 7.0], unit='counts'), |
| 497 | + coords={'w': sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')}, |
| 498 | + masks={'m': sc.array(dims=['w'], values=[False, True, False])}, |
| 499 | + ) |
| 500 | + normalized = normalize_by_monitor_histogram( |
| 501 | + detector, |
| 502 | + monitor=monitor, |
| 503 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 504 | + ) |
| 505 | + |
| 506 | + expected = ( |
| 507 | + sc.DataArray( |
| 508 | + sc.array(dims=['w'], values=[0.0, 9.6, 0, 216 / 7], unit='counts'), |
| 509 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 510 | + ) |
| 511 | + .bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 512 | + .assign_masks(m=sc.array(dims=['w'], values=[False, True, False])) |
| 513 | + ) |
| 514 | + |
| 515 | + sc.testing.assert_allclose(normalized, expected) |
| 516 | + |
| 517 | + |
| 518 | +def test_normalize_by_monitor_histogram_monitor_mask_multiple() -> None: |
| 519 | + detector = sc.DataArray( |
| 520 | + sc.array(dims=['w'], values=[0, 10, 20, 30], unit='counts'), |
| 521 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 522 | + ).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 523 | + monitor = sc.DataArray( |
| 524 | + sc.array(dims=['w'], values=[5.0, 6.0, 7.0], unit='counts'), |
| 525 | + coords={'w': sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')}, |
| 526 | + masks={ |
| 527 | + 'm1': sc.array(dims=['w'], values=[False, True, False]), |
| 528 | + 'm2': sc.array(dims=['w'], values=[False, True, True]), |
| 529 | + }, |
| 530 | + ) |
| 531 | + normalized = normalize_by_monitor_histogram( |
| 532 | + detector, |
| 533 | + monitor=monitor, |
| 534 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 535 | + ) |
| 536 | + |
| 537 | + expected = ( |
| 538 | + sc.DataArray( |
| 539 | + sc.array(dims=['w'], values=[0.0, 10, 0, 0], unit='counts'), |
| 540 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 541 | + ) |
| 542 | + .bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 543 | + .assign_masks( |
| 544 | + m1=sc.array(dims=['w'], values=[False, True, False]), |
| 545 | + m2=sc.array(dims=['w'], values=[False, True, True]), |
| 546 | + ) |
| 547 | + ) |
| 548 | + |
| 549 | + sc.testing.assert_identical(normalized, expected) |
| 550 | + |
| 551 | + |
| 552 | +def test_normalize_by_monitor_histogram_monitor_mask_multiple_combine() -> None: |
| 553 | + detector = sc.DataArray( |
| 554 | + sc.array(dims=['w'], values=[0, 10, 20, 30], unit='counts'), |
| 555 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 556 | + ).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 557 | + monitor = sc.DataArray( |
| 558 | + sc.array(dims=['w'], values=[5.0, 6.0, 7.0], unit='counts'), |
| 559 | + coords={'w': sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')}, |
| 560 | + masks={ |
| 561 | + 'm1': sc.array(dims=['w'], values=[False, True, False]), |
| 562 | + 'm2': sc.array(dims=['w'], values=[False, True, True]), |
| 563 | + }, |
| 564 | + ) |
| 565 | + normalized = normalize_by_monitor_histogram( |
| 566 | + detector, |
| 567 | + monitor=monitor, |
| 568 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 569 | + combine_monitor_masks="MM", |
| 570 | + ) |
| 571 | + |
| 572 | + expected = ( |
| 573 | + sc.DataArray( |
| 574 | + sc.array(dims=['w'], values=[0.0, 10, 0, 0], unit='counts'), |
| 575 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 576 | + ) |
| 577 | + .bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 578 | + .assign_masks(MM=sc.array(dims=['w'], values=[False, True, True])) |
| 579 | + ) |
| 580 | + |
| 581 | + sc.testing.assert_identical(normalized, expected) |
| 582 | + |
| 583 | + |
| 584 | +def test_normalize_by_monitor_histogram_monitor_mask_unaligned_bins() -> None: |
| 585 | + detector = sc.DataArray( |
| 586 | + sc.array(dims=['w'], values=[0, 10, 20, 30], unit='counts'), |
| 587 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 588 | + ).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 589 | + monitor = sc.DataArray( |
| 590 | + sc.array(dims=['w'], values=[5.0, 6.0, 7.0, 8.0], unit='counts'), |
| 591 | + coords={'w': sc.array(dims=['w'], values=[0.0, 2, 3.5, 4, 7], unit='Å')}, |
| 592 | + masks={'m': sc.array(dims=['w'], values=[False, True, False, False])}, |
| 593 | + ) |
| 594 | + normalized = normalize_by_monitor_histogram( |
| 595 | + detector, |
| 596 | + monitor=monitor, |
| 597 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 598 | + ) |
| 599 | + |
| 600 | + expected = ( |
| 601 | + sc.DataArray( |
| 602 | + sc.array( |
| 603 | + dims=['w'], values=[0.0, 100 / 11, 200 / 11, 450 / 11], unit='counts' |
| 604 | + ), |
| 605 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 606 | + ) |
| 607 | + .bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 608 | + .assign_masks(m=sc.array(dims=['w'], values=[False, True, True, False])) |
| 609 | + ) |
| 610 | + |
| 611 | + sc.testing.assert_identical(normalized, expected) |
| 612 | + |
| 613 | + |
| 614 | +def test_normalize_by_monitor_histogram_monitor_mask_at_edge() -> None: |
| 615 | + detector = sc.DataArray( |
| 616 | + sc.array(dims=['w'], values=[0, 10, 30], unit='counts'), |
| 617 | + coords={'w': sc.arange('w', 3.0, unit='Å')}, |
| 618 | + ).bin(w=sc.array(dims=['w'], values=[0.0, 2, 3], unit='Å')) |
| 619 | + monitor = sc.DataArray( |
| 620 | + sc.array(dims=['w'], values=[5.0, 6.0], unit='counts'), |
| 621 | + coords={'w': sc.array(dims=['w'], values=[0.0, 2, 3], unit='Å')}, |
| 622 | + masks={'m': sc.array(dims=['w'], values=[False, True])}, |
| 623 | + ) |
| 624 | + normalized = normalize_by_monitor_histogram( |
| 625 | + detector, |
| 626 | + monitor=monitor, |
| 627 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 628 | + ) |
| 629 | + |
| 630 | + expected = ( |
| 631 | + sc.DataArray( |
| 632 | + sc.array(dims=['w'], values=[0, 10, 0], unit='counts'), |
| 633 | + coords={'w': sc.arange('w', 3.0, unit='Å')}, |
| 634 | + ) |
| 635 | + .bin(w=sc.array(dims=['w'], values=[0.0, 2, 3], unit='Å')) |
| 636 | + .assign_masks(m=sc.array(dims=['w'], values=[False, True])) |
| 637 | + ) |
| 638 | + |
| 639 | + sc.testing.assert_identical(normalized, expected) |
| 640 | + |
| 641 | + |
| 642 | +@pytest.mark.parametrize("nonfinite_value", [np.nan, np.inf]) |
| 643 | +def test_normalize_by_monitor_histogram_nonfinite_in_monitor_is_masked( |
| 644 | + nonfinite_value: float, |
| 645 | +) -> None: |
| 646 | + detector = sc.DataArray( |
| 647 | + sc.array(dims=['w'], values=[0, 10, 20, 30], unit='counts'), |
| 648 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 649 | + ).bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 650 | + monitor = sc.DataArray( |
| 651 | + sc.array(dims=['w'], values=[nonfinite_value, 6.0, 7.0], unit='counts'), |
| 652 | + coords={'w': sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')}, |
| 653 | + ) |
| 654 | + normalized = normalize_by_monitor_histogram( |
| 655 | + detector, |
| 656 | + monitor=monitor, |
| 657 | + uncertainty_broadcast_mode=UncertaintyBroadcastMode.fail, |
| 658 | + ) |
| 659 | + |
| 660 | + expected = ( |
| 661 | + sc.DataArray( |
| 662 | + sc.array( |
| 663 | + dims=['w'], values=[np.nan, np.nan, 65 / 6, 585 / 14], unit='counts' |
| 664 | + ), |
| 665 | + coords={'w': sc.arange('w', 1.0, 5.0, unit='Å')}, |
| 666 | + ) |
| 667 | + .bin(w=sc.array(dims=['w'], values=[1.0, 3, 4, 7], unit='Å')) |
| 668 | + .assign_masks( |
| 669 | + _scipp_nonfinite_monitor=sc.array( |
| 670 | + dims=['w'], values=[True, True, False, False] |
| 671 | + ) |
| 672 | + ) |
| 673 | + ) |
| 674 | + |
| 675 | + sc.testing.assert_identical(normalized, expected) |
0 commit comments