@@ -242,6 +242,41 @@ def test_linear_fit_model_set(self):
242242 assert_allclose (fitted_model (x , model_set_axis = False ), y_expected ,
243243 rtol = 1e-1 )
244244
245+ def test_linear_fit_model_set_common_weight (self ):
246+ """Tests fitting multiple models simultaneously."""
247+
248+ init_model = models .Polynomial1D (degree = 2 , c0 = [1 , 1 ], n_models = 2 )
249+ x = np .arange (10 )
250+ y_expected = init_model (x , model_set_axis = False )
251+ assert y_expected .shape == (2 , 10 )
252+
253+ # Add a bit of random noise
254+ with NumpyRNGContext (_RANDOM_SEED ):
255+ y = y_expected + np .random .normal (0 , 0.01 , size = y_expected .shape )
256+
257+ fitter = LinearLSQFitter ()
258+ fitted_model = fitter (init_model , x , y , weights = np .ones (10 ))
259+ assert_allclose (fitted_model (x , model_set_axis = False ), y_expected ,
260+ rtol = 1e-1 )
261+
262+ def test_linear_fit_model_set_weights (self ):
263+ """Tests fitting multiple models simultaneously."""
264+
265+ init_model = models .Polynomial1D (degree = 2 , c0 = [1 , 1 ], n_models = 2 )
266+ x = np .arange (10 )
267+ y_expected = init_model (x , model_set_axis = False )
268+ assert y_expected .shape == (2 , 10 )
269+
270+ # Add a bit of random noise
271+ with NumpyRNGContext (_RANDOM_SEED ):
272+ y = y_expected + np .random .normal (0 , 0.01 , size = y_expected .shape )
273+
274+ weights = [np .ones (10 ), np .ones (10 ) / 2 ]
275+ fitter = LinearLSQFitter ()
276+ fitted_model = fitter (init_model , x , y , weights = weights )
277+ assert_allclose (fitted_model (x , model_set_axis = False ), y_expected ,
278+ rtol = 1e-1 )
279+
245280 def test_linear_fit_2d_model_set (self ):
246281 """Tests fitted multiple 2-D models simultaneously."""
247282
@@ -291,6 +326,35 @@ def test_linear_fit_model_set_fixed_parameter(self):
291326 assert_allclose (fitted_model .c1 , [1. , - 2. ], atol = 1e-14 )
292327 assert_allclose (fitted_model .c2 , [0.5 , 0. ], atol = 1e-14 )
293328
329+ def test_linear_fit_2d_model_set_common_weight (self ):
330+ init_model = models .Polynomial2D (degree = 2 , c1_0 = [1 , 2 ], c0_1 = [- 0.5 , 1 ],
331+ n_models = 2 ,
332+ fixed = {'c1_0' : True , 'c0_1' : True })
333+
334+ x , y = np .mgrid [0 :5 , 0 :5 ]
335+ zz = np .array ([1 + x - 0.5 * y + 0.1 * x * x , 2 * x + y - 0.2 * y * y ])
336+
337+ fitter = LinearLSQFitter ()
338+ fitted_model = fitter (init_model , x , y , zz , weights = np .ones ((5 , 5 )))
339+
340+ assert_allclose (fitted_model (x , y , model_set_axis = False ), zz ,
341+ atol = 1e-14 )
342+
343+ def test_linear_fit_2d_model_set_weights (self ):
344+ init_model = models .Polynomial2D (degree = 2 , c1_0 = [1 , 2 ], c0_1 = [- 0.5 , 1 ],
345+ n_models = 2 ,
346+ fixed = {'c1_0' : True , 'c0_1' : True })
347+
348+ x , y = np .mgrid [0 :5 , 0 :5 ]
349+ zz = np .array ([1 + x - 0.5 * y + 0.1 * x * x , 2 * x + y - 0.2 * y * y ])
350+
351+ fitter = LinearLSQFitter ()
352+ weights = [np .ones ((5 , 5 )), np .ones ((5 , 5 ))]
353+ fitted_model = fitter (init_model , x , y , zz , weights = weights )
354+
355+ assert_allclose (fitted_model (x , y , model_set_axis = False ), zz ,
356+ atol = 1e-14 )
357+
294358 def test_linear_fit_2d_model_set_fixed_parameters (self ):
295359 """
296360 Tests fitting a 2d polynomial model set with fixed parameters (#6135).
@@ -812,6 +876,17 @@ def test_1d_set_with_common_weights_with_sigma_clip(self):
812876 fit , filtered = fitter (model , self .x1d , z1d , weights = self .weights1d )
813877 assert_allclose (fit .parameters , [0.8 , 0.8 ], atol = 1e-14 )
814878
879+ def test_1d_set_with_weights_with_sigma_clip (self ):
880+ """1D model set with separate weights"""
881+ model = models .Polynomial1D (0 , n_models = 2 )
882+ fitter = FittingWithOutlierRemoval (LinearLSQFitter (), sigma_clip ,
883+ niter = 3 , sigma = 3. )
884+ z1d = np .array ([self .z1d , self .z1d ])
885+ weights = np .array ([self .weights1d , self .weights1d ])
886+
887+ fit , filtered = fitter (model , self .x1d , z1d , weights = weights )
888+ assert_allclose (fit .parameters , [0.8 , 0.8 ], atol = 1e-14 )
889+
815890 def test_2d_without_weights_without_sigma_clip (self ):
816891 model = models .Polynomial2D (0 )
817892 fitter = LinearLSQFitter ()
@@ -835,8 +910,15 @@ def test_2d_with_weights_without_sigma_clip(self):
835910 fit = fitter (model , self .x , self .y , self .z , weights = self .weights )
836911 assert (fit .parameters [0 ] > 1.0 ) # outliers pulled it high
837912
913+ def test_2d_linear_with_weights_without_sigma_clip (self ):
914+ model = models .Polynomial2D (0 )
915+ fitter = LinearLSQFitter () # LinearLSQFitter doesn't handle weights properly in 2D
916+ fit = fitter (model , self .x , self .y , self .z , weights = self .weights )
917+ assert (fit .parameters [0 ] > 1.0 ) # outliers pulled it high
918+
838919 def test_2d_with_weights_with_sigma_clip (self ):
839- """smoke test for #7020 - fails without fitting.py patch because weights does not propagate"""
920+ """smoke test for #7020 - fails without fitting.py patch because
921+ weights does not propagate"""
840922 model = models .Polynomial2D (0 )
841923 fitter = FittingWithOutlierRemoval (LevMarLSQFitter (), sigma_clip ,
842924 niter = 3 , sigma = 3. )
@@ -847,6 +929,16 @@ def test_2d_with_weights_with_sigma_clip(self):
847929 assert (fit .parameters [0 ] > 10 ** (- 2 )) # weights pulled it > 0
848930 assert (fit .parameters [0 ] < 1.0 ) # outliers didn't pull it out of [-1:1] because they had been removed
849931
932+ def test_2d_linear_with_weights_with_sigma_clip (self ):
933+ """same as test above with a linear fitter."""
934+ model = models .Polynomial2D (0 )
935+ fitter = FittingWithOutlierRemoval (LinearLSQFitter (), sigma_clip ,
936+ niter = 3 , sigma = 3. )
937+ fit , filtered = fitter (model , self .x , self .y , self .z ,
938+ weights = self .weights )
939+ assert (fit .parameters [0 ] > 10 ** (- 2 )) # weights pulled it > 0
940+ assert (fit .parameters [0 ] < 1.0 ) # outliers didn't pull it out of [-1:1] because they had been removed
941+
850942
851943@pytest .mark .skipif ('not HAS_SCIPY' )
852944def test_fitters_with_weights ():
@@ -873,6 +965,20 @@ def test_fitters_with_weights():
873965 assert_allclose (pmod .parameters , p2 .parameters , atol = 10 ** (- 2 ))
874966
875967
968+ def test_linear_fitters_with_weights ():
969+ """Regression test for #7035"""
970+ Xin , Yin = np .mgrid [0 :21 , 0 :21 ]
971+ fitter = LinearLSQFitter ()
972+
973+ zsig = np .random .normal (0 , 0.01 , size = Xin .shape )
974+
975+ p2 = models .Polynomial2D (3 )
976+ p2 .parameters = np .arange (10 )/ 1.2
977+ z = p2 (Xin , Yin )
978+ pmod = fitter (models .Polynomial2D (3 ), Xin , Yin , z + zsig , weights = zsig ** (- 2 ))
979+ assert_allclose (pmod .parameters , p2 .parameters , atol = 10 ** (- 2 ))
980+
981+
876982@pytest .mark .skipif ('not HAS_SCIPY' )
877983@pytest .mark .filterwarnings ('ignore:The fit may be unsuccessful' )
878984def test_fitters_interface ():
0 commit comments