@@ -713,75 +713,176 @@ def test_copy_object(do_minimal):
713
713
714
714
715
715
@pytest .mark .parametrize (
716
- "starting_all_arrays, scalar_to_add, expected_all_arrays " ,
716
+ "operation, starting_yarray, scalar_value, expected_yarray " ,
717
717
[
718
- # Test scalar addition to yarray values (intensity) and expect no change to xarrays (q, tth, d)
719
- ( # C1: Add integer of 5, expect yarray to increase by by 5
720
- np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [2.0 , 1.0 , 60.0 , 6.28318531 ]]),
718
+ # Test scalar addition, subtraction, multiplication, and division to y-values by adding a scalar value
719
+ # C1: Test scalar addition to y-values (intensity), expect no change to x-values (q, tth, d)
720
+ ( # 1. Add 5
721
+ "add" ,
722
+ np .array ([1.0 , 2.0 ]),
721
723
5 ,
722
- np .array ([[ 6.0 , 0.51763809 , 30.0 , 12.13818192 ], [ 7.0 , 1.0 , 60.0 , 6.28318531 ] ]),
724
+ np .array ([6.0 , 7.0 ]),
723
725
),
724
- ( # C2: Add float of 5.1, expect yarray to be added by 5.1
725
- np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [2.0 , 1.0 , 60.0 , 6.28318531 ]]),
726
+ ( # 2. Add 5.1
727
+ "add" ,
728
+ np .array ([1.0 , 2.0 ]),
726
729
5.1 ,
727
- np .array ([[6.1 , 0.51763809 , 30.0 , 12.13818192 ], [7.1 , 1.0 , 60.0 , 6.28318531 ]]),
730
+ np .array ([6.1 , 7.1 ]),
731
+ ),
732
+ # C2: Test scalar subtraction to y-values (intensity), expect no change to x-values (q, tth, d)
733
+ ( # 1. Subtract 1
734
+ "sub" ,
735
+ np .array ([1.0 , 2.0 ]),
736
+ 1 ,
737
+ np .array ([0.0 , 1.0 ]),
738
+ ),
739
+ ( # 2. Subtract 0.5
740
+ "sub" ,
741
+ np .array ([1.0 , 2.0 ]),
742
+ 0.5 ,
743
+ np .array ([0.5 , 1.5 ]),
744
+ ),
745
+ # C3: Test scalar multiplication to y-values (intensity), expect no change to x-values (q, tth, d)
746
+ ( # 1. Multiply by 2
747
+ "mul" ,
748
+ np .array ([1.0 , 2.0 ]),
749
+ 2 ,
750
+ np .array ([2.0 , 4.0 ]),
751
+ ),
752
+ ( # 2. Multiply by 2.5
753
+ "mul" ,
754
+ np .array ([1.0 , 2.0 ]),
755
+ 2.5 ,
756
+ np .array ([2.5 , 5.0 ]),
757
+ ),
758
+ # C4: Test scalar division to y-values (intensity), expect no change to x-values (q, tth, d)
759
+ ( # 1. Divide by 2
760
+ "div" ,
761
+ np .array ([1.0 , 2.0 ]),
762
+ 2 ,
763
+ np .array ([0.5 , 1.0 ]),
764
+ ),
765
+ ( # 2. Divide by 2.5
766
+ "div" ,
767
+ np .array ([1.0 , 2.0 ]),
768
+ 2.5 ,
769
+ np .array ([0.4 , 0.8 ]),
728
770
),
729
771
],
730
772
)
731
- def test_addition_operator_by_scalar ( starting_all_arrays , scalar_to_add , expected_all_arrays , do_minimal_tth ):
773
+ def test_scalar_operations ( operation , starting_yarray , scalar_value , expected_yarray , do_minimal_tth ):
732
774
do = do_minimal_tth
733
- assert np .allclose (do .all_arrays , starting_all_arrays )
734
- do_scalar_right_sum = do + scalar_to_add
735
- assert np .allclose (do_scalar_right_sum .all_arrays , expected_all_arrays )
736
- do_scalar_left_sum = scalar_to_add + do
737
- assert np .allclose (do_scalar_left_sum .all_arrays , expected_all_arrays )
775
+ expected_xarray_constant = np .array ([[0.51763809 , 30.0 , 12.13818192 ], [1.0 , 60.0 , 6.28318531 ]])
776
+ assert np .allclose (do .all_arrays [:, [1 , 2 , 3 ]], expected_xarray_constant )
777
+ assert np .allclose (do .all_arrays [:, 0 ], starting_yarray )
778
+ if operation == "add" :
779
+ do_right_op = do + scalar_value
780
+ do_left_op = scalar_value + do
781
+ elif operation == "sub" :
782
+ do_right_op = do - scalar_value
783
+ do_left_op = scalar_value - do
784
+ elif operation == "mul" :
785
+ do_right_op = do * scalar_value
786
+ do_left_op = scalar_value * do
787
+ elif operation == "div" :
788
+ do_right_op = do / scalar_value
789
+ do_left_op = scalar_value / do
790
+ assert np .allclose (do_right_op .all_arrays [:, 0 ], expected_yarray )
791
+ assert np .allclose (do_left_op .all_arrays [:, 0 ], expected_yarray )
792
+ # Ensure x-values are unchanged
793
+ assert np .allclose (do_right_op .all_arrays [:, [1 , 2 , 3 ]], expected_xarray_constant )
794
+ assert np .allclose (do_left_op .all_arrays [:, [1 , 2 , 3 ]], expected_xarray_constant )
738
795
739
796
740
797
@pytest .mark .parametrize (
741
- "do_1_all_arrays, "
742
- "do_2_all_arrays, "
743
- "expected_do_1_all_arrays_with_y_summed, "
744
- "expected_do_2_all_arrays_with_y_summed" ,
798
+ "operation, " "expected_do_1_all_arrays_with_y_modified, " "expected_do_2_all_arrays_with_y_modified" ,
745
799
[
746
- # Test addition of two DO objects, expect combined yarray values and no change to xarrays ((q, tth, d)
747
- ( # C1: Add two DO objects, expect sum of yarray values
748
- (np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [2.0 , 1.0 , 60.0 , 6.28318531 ]]),),
749
- (np .array ([[1.0 , 6.28318531 , 100.70777771 , 1 ], [2.0 , 3.14159265 , 45.28748053 , 2.0 ]]),),
750
- (np .array ([[2.0 , 0.51763809 , 30.0 , 12.13818192 ], [4.0 , 1.0 , 60.0 , 6.28318531 ]]),),
751
- (np .array ([[2.0 , 6.28318531 , 100.70777771 , 1 ], [4.0 , 3.14159265 , 45.28748053 , 2.0 ]]),),
800
+ # Test addition, subtraction, multiplication, and division of two DO objects
801
+ ( # Test addition of two DO objects, expect combined yarray values
802
+ "add" ,
803
+ np .array ([[2.0 , 0.51763809 , 30.0 , 12.13818192 ], [4.0 , 1.0 , 60.0 , 6.28318531 ]]),
804
+ np .array ([[2.0 , 6.28318531 , 100.70777771 , 1 ], [4.0 , 3.14159265 , 45.28748053 , 2.0 ]]),
805
+ ),
806
+ ( # Test subtraction of two DO objects, expect differences in yarray values
807
+ "sub" ,
808
+ np .array ([[0.0 , 0.51763809 , 30.0 , 12.13818192 ], [0.0 , 1.0 , 60.0 , 6.28318531 ]]),
809
+ np .array ([[0.0 , 6.28318531 , 100.70777771 , 1 ], [0.0 , 3.14159265 , 45.28748053 , 2.0 ]]),
810
+ ),
811
+ ( # Test multiplication of two DO objects, expect multiplication in yarray values
812
+ "mul" ,
813
+ np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [4.0 , 1.0 , 60.0 , 6.28318531 ]]),
814
+ np .array ([[1.0 , 6.28318531 , 100.70777771 , 1 ], [4.0 , 3.14159265 , 45.28748053 , 2.0 ]]),
815
+ ),
816
+ ( # Test division of two DO objects, expect division in yarray values
817
+ "div" ,
818
+ np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [1.0 , 1.0 , 60.0 , 6.28318531 ]]),
819
+ np .array ([[1.0 , 6.28318531 , 100.70777771 , 1 ], [1.0 , 3.14159265 , 45.28748053 , 2.0 ]]),
752
820
),
753
821
],
754
822
)
755
- def test_addition_operator_by_another_do (
756
- do_1_all_arrays ,
757
- do_2_all_arrays ,
758
- expected_do_1_all_arrays_with_y_summed ,
759
- expected_do_2_all_arrays_with_y_summed ,
823
+ def test_binary_operator_on_do (
824
+ operation ,
825
+ expected_do_1_all_arrays_with_y_modified ,
826
+ expected_do_2_all_arrays_with_y_modified ,
760
827
do_minimal_tth ,
761
828
do_minimal_d ,
762
829
):
763
830
do_1 = do_minimal_tth
764
- assert np .allclose (do_1 .all_arrays , do_1_all_arrays )
765
831
do_2 = do_minimal_d
766
- assert np .allclose (do_2 .all_arrays , do_2_all_arrays )
767
- assert np .allclose ((do_1 + do_2 ).all_arrays , expected_do_1_all_arrays_with_y_summed )
768
- assert np .allclose ((do_2 + do_1 ).all_arrays , expected_do_2_all_arrays_with_y_summed )
769
-
832
+ assert np .allclose (
833
+ do_1 .all_arrays , np .array ([[1.0 , 0.51763809 , 30.0 , 12.13818192 ], [2.0 , 1.0 , 60.0 , 6.28318531 ]])
834
+ )
835
+ assert np .allclose (
836
+ do_2 .all_arrays , np .array ([[1.0 , 6.28318531 , 100.70777771 , 1 ], [2.0 , 3.14159265 , 45.28748053 , 2.0 ]])
837
+ )
770
838
771
- def test_addition_operator_invalid_type (do_minimal_tth , invalid_add_type_error_msg ):
772
- # Add a string to a DO object, expect TypeError, only scalar (int, float) allowed for addition
839
+ if operation == "add" :
840
+ do_1_y_modified = do_1 + do_2
841
+ do_2_y_modified = do_2 + do_1
842
+ elif operation == "sub" :
843
+ do_1_y_modified = do_1 - do_2
844
+ do_2_y_modified = do_2 - do_1
845
+ elif operation == "mul" :
846
+ do_1_y_modified = do_1 * do_2
847
+ do_2_y_modified = do_2 * do_1
848
+ elif operation == "div" :
849
+ do_1_y_modified = do_1 / do_2
850
+ do_2_y_modified = do_2 / do_1
851
+
852
+ assert np .allclose (do_1_y_modified .all_arrays , expected_do_1_all_arrays_with_y_modified )
853
+ assert np .allclose (do_2_y_modified .all_arrays , expected_do_2_all_arrays_with_y_modified )
854
+
855
+
856
+ def test_operator_invalid_type (do_minimal_tth , invalid_add_type_error_msg ):
857
+ # Add a string to a DiffractionObject, expect TypeError
773
858
do = do_minimal_tth
774
- with pytest .raises (TypeError , match = re .escape (invalid_add_type_error_msg )):
775
- do + "string_value"
776
- with pytest .raises (TypeError , match = re .escape (invalid_add_type_error_msg )):
777
- "string_value" + do
778
-
779
-
780
- def test_addition_operator_invalid_yarray_length (do_minimal , do_minimal_tth , y_grid_size_mismatch_error_msg ):
781
- # Combine two DO objects, one with empty xarrays (do_minimal) and the other with non-empty xarrays
859
+ invalid_value = "string_value"
860
+ operations = [
861
+ (lambda x , y : x + y ), # Test addition
862
+ (lambda x , y : x - y ), # Test subtraction
863
+ (lambda x , y : x * y ), # Test multiplication
864
+ (lambda x , y : x / y ), # Test division
865
+ ]
866
+ for operation in operations :
867
+ with pytest .raises (TypeError , match = re .escape (invalid_add_type_error_msg )):
868
+ operation (do , invalid_value )
869
+ with pytest .raises (TypeError , match = re .escape (invalid_add_type_error_msg )):
870
+ operation (invalid_value , do )
871
+
872
+
873
+ @pytest .mark .parametrize ("operation" , ["add" , "sub" , "mul" , "div" ])
874
+ def test_operator_invalid_yarray_length (operation , do_minimal , do_minimal_tth , y_grid_size_mismatch_error_msg ):
875
+ # Add two DO objects with different yarray lengths, expect ValueError
782
876
do_1 = do_minimal
783
877
do_2 = do_minimal_tth
784
878
assert len (do_1 .all_arrays [:, 0 ]) == 0
785
879
assert len (do_2 .all_arrays [:, 0 ]) == 2
786
880
with pytest .raises (ValueError , match = re .escape (y_grid_size_mismatch_error_msg )):
787
- do_1 + do_2
881
+ if operation == "add" :
882
+ do_1 + do_2
883
+ elif operation == "sub" :
884
+ do_1 - do_2
885
+ elif operation == "mul" :
886
+ do_1 * do_2
887
+ elif operation == "div" :
888
+ do_1 / do_2
0 commit comments