Skip to content

Commit d161a24

Browse files
committed
Fixes for weights with linear fitter
Fix astropy#7035
1 parent aeabab1 commit d161a24

2 files changed

Lines changed: 141 additions & 8 deletions

File tree

‎astropy/modeling/fitting.py‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
382382

383383
has_fixed = any(model_copy.fixed.values())
384384

385+
if weights is not None:
386+
weights = np.asarray(weights, dtype=float)
387+
385388
if has_fixed:
386389

387390
# The list of fixed params is the complement of those being fitted:
@@ -399,6 +402,10 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
399402
if len(farg) == 2:
400403
x, y = farg
401404

405+
if weights is not None and weights.ndim > 1:
406+
_, weights = _convert_input(x, weights, n_models=len(model_copy),
407+
model_set_axis=model_copy.model_set_axis)
408+
402409
# map domain into window
403410
if hasattr(model_copy, 'domain'):
404411
x = self._map_domain_window(model_copy, x)
@@ -414,6 +421,10 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
414421
else:
415422
x, y, z = farg
416423

424+
if weights is not None and weights.ndim > 2:
425+
_, _, weights = _convert_input(x, y, weights, n_models=len(model_copy),
426+
model_set_axis=model_copy.model_set_axis)
427+
417428
# map domain into window
418429
if hasattr(model_copy, 'x_domain'):
419430
x, y = self._map_domain_window(model_copy, x, y)
@@ -446,8 +457,17 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
446457
# but z has a second axis for the model set. NB. This is
447458
# ~5-10x faster than using rollaxis.
448459
rhs = z.T if model_axis == 0 else z
460+
461+
if weights is not None:
462+
if weights.ndim > 2:
463+
weights = np.rollaxis(z, model_axis, z.ndim)
464+
weights = weights.reshape(-1, weights.shape[-1])
465+
else:
466+
weights = weights.flatten()
449467
else:
450468
rhs = z.flatten()
469+
if weights is not None:
470+
weights = weights.flatten()
451471

452472
# If the derivative is defined along rows (as with non-linear models)
453473
if model_copy.col_fit_deriv:
@@ -482,14 +502,18 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
482502
rhs = rhs - sum_of_implicit_terms
483503

484504
if weights is not None:
485-
weights = np.asarray(weights, dtype=float)
486-
if len(x) != len(weights):
505+
if len(lhs) != len(weights):
487506
raise ValueError("x and weights should have the same length")
507+
488508
if rhs.ndim == 2:
489-
lhs *= weights[:, np.newaxis]
490-
# Don't modify in-place in case rhs was the original dependent
491-
# variable array
492-
rhs = rhs * weights[:, np.newaxis]
509+
if weights is not None and weights.shape == rhs.shape:
510+
lhs = lhs[..., np.newaxis] * weights[:, np.newaxis]
511+
rhs = rhs * weights
512+
else:
513+
lhs *= weights[:, np.newaxis]
514+
# Don't modify in-place in case rhs was the original
515+
# dependent variable array
516+
rhs = rhs * weights[:, np.newaxis]
493517
else:
494518
lhs *= weights[:, np.newaxis]
495519
rhs = rhs * weights
@@ -499,7 +523,24 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
499523

500524
masked = np.any(np.ma.getmask(rhs))
501525

502-
if len(model_copy) == 1 or not masked:
526+
if weights is not None and weights.ndim > 1 and weights.shape == rhs.shape:
527+
528+
scl = scl.T
529+
lacoef = np.zeros(lhs.shape[-2:], dtype=rhs.dtype)
530+
531+
for model_lhs, model_rhs, model_lacoef in zip(lhs.T, rhs.T, lacoef.T):
532+
533+
# Cull masked points on both sides of the matrix equation:
534+
good = ~model_rhs.mask if masked else slice(None)
535+
model_lhs = model_lhs.T[good]
536+
model_rhs = model_rhs[good][..., np.newaxis]
537+
538+
# Solve for this model:
539+
t_coef, resids, rank, sval = np.linalg.lstsq(model_lhs,
540+
model_rhs, rcond)
541+
model_lacoef[:] = t_coef.T
542+
543+
elif len(model_copy) == 1 or not masked:
503544

504545
# If we're fitting one or more models over a common set of points,
505546
# we only have to solve a single matrix equation, which is an order

‎astropy/modeling/tests/test_fitters.py‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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')
852944
def test_fitters_with_weights():

0 commit comments

Comments
 (0)