1717 Gaussian ,
1818 Linear ,
1919 Lorentzian ,
20+ NegativeTrapezoid ,
2021 Polynomial ,
2122 SlitScan ,
2223 TopHat ,
@@ -599,7 +600,7 @@ def test_width_guess(self):
599600
600601 outp = TopHat .guess ()(x , y )
601602
602- assert outp ["width" ] == pytest .approx (1 .0 , rel = 1e-2 )
603+ assert outp ["width" ] == pytest .approx (2 .0 , rel = 1e-2 )
603604
604605 def test_guess_given_flat_data (self ):
605606 x = np .arange (- 5.0 , 5.0 , 1.0 , dtype = np .float64 )
@@ -688,15 +689,15 @@ def test_gradient_guess(self):
688689
689690 outp = Trapezoid .guess ()(x , y )
690691
691- assert outp ["gradient" ] == pytest .approx (10 .0 , rel = 1e-2 )
692+ assert outp ["gradient" ] == pytest .approx (8 .0 , rel = 1e-2 )
692693
693694 def test_y_offset_guess (self ):
694- x = np .arange (- 5.0 , 5.0 , 1.0 , dtype = np .float64 )
695+ x = np .linspace (- 5.0 , 5.0 , num = 11 , dtype = np .float64 )
695696 y = np .array ([1.0 , 1.0 , 1.0 , 2.0 , 3.0 , 3.0 , 3.0 , 2.0 , 1.0 , 1.0 , 1.0 ], dtype = np .float64 )
696697
697698 outp = Trapezoid .guess ()(x , y )
698699
699- x1 = np .arange (- 6.0 , 6.0 , 1.0 , dtype = np .float64 )
700+ x1 = np .linspace (- 6.0 , 6.0 , num = 13 , dtype = np .float64 )
700701 y1 = np .array (
701702 [1.0 , 1.0 , 1.0 , 2.0 , 3.0 , 3.0 , 3.0 , 3.0 , 3.0 , 2.0 , 1.0 , 1.0 , 1.0 ], dtype = np .float64
702703 )
@@ -713,3 +714,112 @@ def test_guess_given_flat_data(self):
713714 outp = Trapezoid .guess ()(x , y )
714715 # check that with flat data gradient guess is 0
715716 assert outp ["gradient" ] == pytest .approx (0.0 , rel = 1e-2 )
717+
718+
719+ class TestNegativeTrapezoid :
720+ class TestNegativeTrapezoidModel :
721+ def test_negative_trapezoid_model (self ):
722+ x = np .arange (- 5.0 , 6.0 , 1.0 , dtype = np .float64 )
723+ cen = 0
724+ y_offset = - 1
725+ height = 1
726+ background = 1
727+ gradient = 1
728+
729+ outp = NegativeTrapezoid .model ().func (
730+ x ,
731+ cen = cen ,
732+ y_offset = y_offset ,
733+ height = height ,
734+ background = background ,
735+ gradient = gradient ,
736+ )
737+
738+ assert background == pytest .approx (np .max (outp ), rel = 1e-2 )
739+ assert background - height == pytest .approx (np .min (outp ), rel = 1e-2 )
740+
741+ outp1 = NegativeTrapezoid .model ().func (
742+ x ,
743+ cen = cen + 3 ,
744+ y_offset = y_offset ,
745+ height = height ,
746+ background = background ,
747+ gradient = gradient - 0.5 ,
748+ )
749+
750+ # check centre moves when data is shifted
751+ assert np .mean (x [np .where (outp < background )]) < np .mean (
752+ x [np .where (outp1 < background )]
753+ )
754+
755+ # check gradient: a greater gradient means smaller average y values as wider
756+ assert np .mean (outp ) > np .mean (outp1 )
757+
758+ outp2 = NegativeTrapezoid .model ().func (
759+ x ,
760+ cen = cen ,
761+ y_offset = y_offset - 5 ,
762+ height = height ,
763+ background = background ,
764+ gradient = gradient ,
765+ )
766+
767+ # check y_offset: a smaller y_offset means smaller average y values as wider
768+ assert np .mean (outp ) > np .mean (outp2 )
769+
770+ class TestNegativeTrapezoidGuess :
771+ def test_background_guess (self ):
772+ x = np .array ([- 1.0 , 0.0 , 1.0 , 2.0 , 3.0 ], dtype = np .float64 )
773+ y = np .array ([- 1.0 , - 2.0 , - 2.0 , - 2.0 , - 1.0 ], dtype = np .float64 )
774+
775+ outp = NegativeTrapezoid .guess ()(x , y )
776+
777+ assert outp ["background" ] == pytest .approx (- 1.0 , rel = 1e-2 )
778+
779+ def test_cen_height_guess (self ):
780+ x = np .array ([- 1.0 , 0.0 , 1.0 , 2.0 , 3.0 ], dtype = np .float64 )
781+ y = np .array ([- 1.0 , - 1.0 , - 2.0 , - 1.0 , - 1.0 ], dtype = np .float64 )
782+
783+ outp = NegativeTrapezoid .guess ()(x , y )
784+
785+ assert outp ["cen" ] == pytest .approx (1.0 , rel = 1e-2 )
786+ assert outp ["height" ] == pytest .approx (1.0 , rel = 1e-2 )
787+
788+ def test_gradient_guess (self ):
789+ x = np .array ([- 4.0 , - 3.0 , - 2.0 , - 1.0 , 0.0 , 1.0 , 2.0 , 3.0 , 4.0 ], dtype = np .float64 )
790+ y = np .array ([1.0 , 2.0 , 4.0 , 8.0 , 16.0 , 8.0 , 4.0 , 2.0 , 1.0 ], dtype = np .float64 )
791+
792+ # Should choose x = -1.0 as x1 and x = -2.5 as x0
793+ # height = 16 - 1 = 15
794+ # gradient = 15 / (-1 - -2.5) = 10
795+
796+ outp = NegativeTrapezoid .guess ()(x , y )
797+
798+ assert outp ["gradient" ] == pytest .approx (8.0 , rel = 1e-2 )
799+
800+ def test_y_offset_guess (self ):
801+ x = np .linspace (- 5.0 , 5.0 , num = 11 , dtype = np .float64 )
802+ y = np .array (
803+ [- 1.0 , - 1.0 , - 1.0 , - 2.0 , - 3.0 , - 3.0 , - 3.0 , - 2.0 , - 1.0 , - 1.0 , - 1.0 ], dtype = np .float64
804+ )
805+
806+ outp = NegativeTrapezoid .guess ()(x , y )
807+
808+ x1 = np .linspace (- 6.0 , 6.0 , num = 13 , dtype = np .float64 )
809+ y1 = np .array (
810+ [- 1.0 , - 1.0 , - 1.0 , - 2.0 , - 3.0 , - 3.0 , - 3.0 , - 3.0 , - 3.0 , - 2.0 , - 1.0 , - 1.0 , - 1.0 ],
811+ dtype = np .float64 ,
812+ )
813+
814+ outp1 = NegativeTrapezoid .guess ()(x1 , y1 )
815+
816+ # Assert that with a greater top width, y_offset decreases
817+ assert outp ["y_offset" ] > outp1 ["y_offset" ]
818+
819+ def test_guess_given_flat_data (self ):
820+ x = np .arange (- 5.0 , 5.0 , 1.0 , dtype = np .float64 )
821+ y = np .zeros_like (x , dtype = np .float64 ) + 1
822+
823+ outp = NegativeTrapezoid .guess ()(x , y )
824+ # check that with flat data gradient guess is 0
825+ assert outp ["gradient" ] == pytest .approx (0.0 , rel = 1e-2 )
0 commit comments