@@ -461,39 +461,57 @@ def test_normalize_by_monitor_integrated_assigns_mask_if_monitor_range_too_narro
461461 )
462462
463463
464- def random_variable (
465- rng : np .random .Generator , dim : str , n : int , unit : str , with_variances : bool = False
466- ) -> sc .Variable :
467- values = rng .uniform (0.1 , 2.0 , n )
468- variances = values * rng .uniform (0.1 , 0.5 , n ) if with_variances else None
469- return sc .array (dims = [dim ], values = values , variances = variances , unit = unit )
470-
471-
472- def random_binned_data (
473- rng : np .random .Generator ,
474- dim : str ,
475- n_events : int ,
476- n_bins : int ,
477- unit : str ,
478- coord_unit : str ,
479- with_variances : bool = False ,
480- ) -> sc .DataArray :
481- return sc .DataArray (
482- random_variable (rng , 'event' , n_events , unit , with_variances = with_variances ),
483- coords = {dim : random_variable (rng , 'event' , n_events , coord_unit )},
484- ).bin ({dim : n_bins })
485-
486-
487- def make_sample_and_vanadium () -> tuple [sc .DataArray , sc .DataArray ]:
488- rng = np .random .default_rng (seed = 495 )
489- sample = random_binned_data (rng , 'dspacing' , 84 , 35 , 'counts' , 'Å' , True )
490- vanadium = random_binned_data (rng , 'dspacing' , 146 , 79 , 'counts' , 'Å' , True )
491- return sample , vanadium
464+ class TestNormalizeByVanadium :
465+ def random_variable (
466+ self ,
467+ rng : np .random .Generator ,
468+ dim : str ,
469+ n : int ,
470+ unit : str ,
471+ with_variances : bool = False ,
472+ ) -> sc .Variable :
473+ values = rng .uniform (0.1 , 2.0 , n )
474+ variances = values * rng .uniform (0.1 , 0.5 , n ) if with_variances else None
475+ return sc .array (dims = [dim ], values = values , variances = variances , unit = unit )
476+
477+ def random_binned_data (
478+ self ,
479+ rng : np .random .Generator ,
480+ n_events : int ,
481+ unit : str ,
482+ with_variances : bool = False ,
483+ * coords : tuple [str , int , str ],
484+ ) -> sc .DataArray :
485+ return sc .DataArray (
486+ self .random_variable (
487+ rng , 'event' , n_events , unit , with_variances = with_variances
488+ ),
489+ coords = {
490+ dim : self .random_variable (rng , 'event' , n_events , coord_unit )
491+ for (dim , _ , coord_unit ) in coords
492+ },
493+ ).bin ({dim : n_bins for (dim , n_bins , _ ) in coords })
494+
495+ def make_sample_and_vanadium_1d (self ) -> tuple [sc .DataArray , sc .DataArray ]:
496+ rng = np .random .default_rng (seed = 495 )
497+ sample = self .random_binned_data (rng , 84 , 'count' , True , ('dspacing' , 35 , 'Å' ))
498+ vanadium = self .random_binned_data (
499+ rng , 146 , 'count' , True , ('dspacing' , 79 , 'Å' )
500+ )
501+ return sample , vanadium
492502
503+ def make_sample_and_vanadium_2d (self ) -> tuple [sc .DataArray , sc .DataArray ]:
504+ rng = np .random .default_rng (seed = 3193 )
505+ sample = self .random_binned_data (
506+ rng , 138 , 'count' , True , ('dspacing' , 35 , 'Å' ), ('two_theta' , 13 , 'rad' )
507+ )
508+ vanadium = self .random_binned_data (
509+ rng , 170 , 'count' , True , ('dspacing' , 79 , 'Å' ), ('two_theta' , 14 , 'rad' )
510+ )
511+ return sample , vanadium
493512
494- class TestNormalizeByVanadium :
495513 def test_1d_binned_vanadium (self ) -> None :
496- sample , vanadium = make_sample_and_vanadium ()
514+ sample , vanadium = self . make_sample_and_vanadium_1d ()
497515 normed = normalize_by_vanadium_dspacing (
498516 FocussedDataDspacing [SampleRun ](sample ),
499517 FocussedDataDspacing [VanadiumRun ](vanadium ),
@@ -507,7 +525,7 @@ def test_1d_binned_vanadium(self) -> None:
507525 sc .testing .assert_allclose (normed , expected )
508526
509527 def test_1d_histogrammed_vanadium (self ) -> None :
510- sample , vanadium = make_sample_and_vanadium ()
528+ sample , vanadium = self . make_sample_and_vanadium_1d ()
511529 vanadium = vanadium .hist ()
512530 normed = normalize_by_vanadium_dspacing (
513531 FocussedDataDspacing [SampleRun ](sample ),
@@ -521,8 +539,8 @@ def test_1d_histogrammed_vanadium(self) -> None:
521539 expected = sample / sc .values (norm )
522540 sc .testing .assert_allclose (normed , expected )
523541
524- def test_binned_vanadium_binning_has_no_effect (self ) -> None :
525- sample , vanadium = make_sample_and_vanadium ()
542+ def test_1d_binned_vanadium_binning_has_no_effect (self ) -> None :
543+ sample , vanadium = self . make_sample_and_vanadium_1d ()
526544 vana_binned_like_sample = vanadium .bin (dspacing = sample .coords ['dspacing' ])
527545 normed_a = normalize_by_vanadium_dspacing (
528546 FocussedDataDspacing [SampleRun ](sample ),
@@ -537,7 +555,7 @@ def test_binned_vanadium_binning_has_no_effect(self) -> None:
537555 sc .testing .assert_allclose (normed_a , normed_b )
538556
539557 def test_1d_masks_zero_vanadium_bins (self ) -> None :
540- sample , vanadium = make_sample_and_vanadium ()
558+ sample , vanadium = self . make_sample_and_vanadium_1d ()
541559 vanadium ['dspacing' , 5 ] = sc .scalar (0.0 , variance = 0.0 , unit = 'counts' )
542560 normed = normalize_by_vanadium_dspacing (
543561 FocussedDataDspacing [SampleRun ](sample ),
@@ -551,4 +569,71 @@ def test_1d_masks_zero_vanadium_bins(self) -> None:
551569 normed .masks ['zero_vanadium' ], norm .data == sc .scalar (0.0 , unit = 'counts' )
552570 )
553571
554- # TODO vanadium and 2theta
572+ def test_2d_binned_vanadium (self ) -> None :
573+ sample , vanadium = self .make_sample_and_vanadium_2d ()
574+ normed = normalize_by_vanadium_dspacing_and_two_theta (
575+ FocussedDataDspacingTwoTheta [SampleRun ](sample ),
576+ FocussedDataDspacingTwoTheta [VanadiumRun ](vanadium ),
577+ UncertaintyBroadcastMode .drop ,
578+ )
579+ # we test masks separately
580+ normed = normed .drop_masks (list (normed .masks .keys ()))
581+
582+ norm = vanadium .hist (
583+ dspacing = sample .coords ['dspacing' ], two_theta = sample .coords ['two_theta' ]
584+ )
585+ expected = sample / sc .values (norm )
586+ sc .testing .assert_allclose (normed , expected )
587+
588+ def test_2d_histogrammed_vanadium (self ) -> None :
589+ sample , vanadium = self .make_sample_and_vanadium_2d ()
590+ vanadium = vanadium .hist ()
591+ normed = normalize_by_vanadium_dspacing_and_two_theta (
592+ FocussedDataDspacingTwoTheta [SampleRun ](sample ),
593+ FocussedDataDspacingTwoTheta [VanadiumRun ](vanadium ),
594+ UncertaintyBroadcastMode .drop ,
595+ )
596+ # we test masks separately
597+ normed = normed .drop_masks (list (normed .masks .keys ()))
598+
599+ norm = vanadium .rebin (
600+ dspacing = sample .coords ['dspacing' ], two_theta = sample .coords ['two_theta' ]
601+ )
602+ expected = sample / sc .values (norm )
603+ sc .testing .assert_allclose (normed , expected )
604+
605+ def test_2d_binned_vanadium_binning_has_no_effect (self ) -> None :
606+ sample , vanadium = self .make_sample_and_vanadium_2d ()
607+ vana_binned_like_sample = vanadium .bin (
608+ dspacing = sample .coords ['dspacing' ], two_theta = sample .coords ['two_theta' ]
609+ )
610+ normed_a = normalize_by_vanadium_dspacing_and_two_theta (
611+ FocussedDataDspacingTwoTheta [SampleRun ](sample ),
612+ FocussedDataDspacingTwoTheta [VanadiumRun ](vanadium ),
613+ UncertaintyBroadcastMode .drop ,
614+ )
615+ normed_b = normalize_by_vanadium_dspacing_and_two_theta (
616+ FocussedDataDspacingTwoTheta [SampleRun ](sample ),
617+ FocussedDataDspacingTwoTheta [VanadiumRun ](vana_binned_like_sample ),
618+ UncertaintyBroadcastMode .drop ,
619+ )
620+ sc .testing .assert_allclose (normed_a , normed_b )
621+
622+ def test_2d_masks_zero_vanadium_bins (self ) -> None :
623+ sample , vanadium = self .make_sample_and_vanadium_2d ()
624+ vanadium ['dspacing' , 5 ]['two_theta' , 7 ] = sc .scalar (
625+ 0.0 , variance = 0.0 , unit = 'counts'
626+ )
627+ normed = normalize_by_vanadium_dspacing_and_two_theta (
628+ FocussedDataDspacingTwoTheta [SampleRun ](sample ),
629+ FocussedDataDspacingTwoTheta [VanadiumRun ](vanadium ),
630+ UncertaintyBroadcastMode .drop ,
631+ )
632+
633+ norm = vanadium .hist (
634+ dspacing = sample .coords ['dspacing' ], two_theta = sample .coords ['two_theta' ]
635+ )
636+
637+ sc .testing .assert_allclose (
638+ normed .masks ['zero_vanadium' ], norm .data == sc .scalar (0.0 , unit = 'counts' )
639+ )
0 commit comments