Skip to content

Commit 2bfdd65

Browse files
committed
Fixes for weights with linear fitter
Fix astropy#7035
1 parent b0e9df1 commit 2bfdd65

2 files changed

Lines changed: 168 additions & 8 deletions

File tree

‎astropy/modeling/fitting.py‎

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

546546
has_fixed = any(model_copy.fixed.values())
547547

548+
if weights is not None:
549+
weights = np.asarray(weights, dtype=float)
550+
548551
if has_fixed:
549552

550553
# The list of fixed params is the complement of those being fitted:
@@ -562,6 +565,12 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
562565
if len(farg) == 2:
563566
x, y = farg
564567

568+
if weights is not None and weights.ndim > 1:
569+
# If we have separate weights for multiple models we need
570+
# to apply the same conversion as for the data.
571+
_, weights = _convert_input(x, weights, n_models=len(model_copy),
572+
model_set_axis=model_copy.model_set_axis)
573+
565574
# map domain into window
566575
if hasattr(model_copy, 'domain'):
567576
x = self._map_domain_window(model_copy, x)
@@ -577,6 +586,12 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
577586
else:
578587
x, y, z = farg
579588

589+
if weights is not None and weights.ndim > 2:
590+
# If we have separate weights for multiple models we need
591+
# to apply the same conversion as for the data.
592+
_, _, weights = _convert_input(x, y, weights, n_models=len(model_copy),
593+
model_set_axis=model_copy.model_set_axis)
594+
580595
# map domain into window
581596
if hasattr(model_copy, 'x_domain'):
582597
x, y = self._map_domain_window(model_copy, x, y)
@@ -609,8 +624,18 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
609624
# but z has a second axis for the model set. NB. This is
610625
# ~5-10x faster than using rollaxis.
611626
rhs = z.T if model_axis == 0 else z
627+
628+
if weights is not None:
629+
# Same for weights
630+
if weights.ndim > 2:
631+
weights = np.rollaxis(z, model_axis, z.ndim)
632+
weights = weights.reshape(-1, weights.shape[-1])
633+
else:
634+
weights = weights.flatten()
612635
else:
613636
rhs = z.flatten()
637+
if weights is not None:
638+
weights = weights.flatten()
614639

615640
# If the derivative is defined along rows (as with non-linear models)
616641
if model_copy.col_fit_deriv:
@@ -645,14 +670,20 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
645670
rhs = rhs - sum_of_implicit_terms
646671

647672
if weights is not None:
648-
weights = np.asarray(weights, dtype=float)
649-
if len(x) != len(weights):
673+
if len(lhs) != len(weights):
650674
raise ValueError("x and weights should have the same length")
675+
651676
if rhs.ndim == 2:
652-
lhs *= weights[:, np.newaxis]
653-
# Don't modify in-place in case rhs was the original dependent
654-
# variable array
655-
rhs = rhs * weights[:, np.newaxis]
677+
if weights is not None and weights.shape == rhs.shape:
678+
# separate weights for multiple models case: broadcast
679+
# lhs to have more dimension (for each model)
680+
lhs = lhs[..., np.newaxis] * weights[:, np.newaxis]
681+
rhs = rhs * weights
682+
else:
683+
lhs *= weights[:, np.newaxis]
684+
# Don't modify in-place in case rhs was the original
685+
# dependent variable array
686+
rhs = rhs * weights[:, np.newaxis]
656687
else:
657688
lhs *= weights[:, np.newaxis]
658689
rhs = rhs * weights
@@ -663,7 +694,30 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
663694
masked = np.any(np.ma.getmask(rhs))
664695

665696
a = None # need for calculating covarience
666-
if len(model_copy) == 1 or not masked:
697+
698+
if weights is not None and weights.ndim > 1 and weights.shape == rhs.shape:
699+
700+
# separate weights for multiple models case: Numpy's lstsq
701+
# supports multiple dimensions only for rhs, so we need to loop
702+
# manually on the models. This may be fixed in the future with
703+
# https://github.com/numpy/numpy/pull/15777
704+
705+
scl = scl.T
706+
lacoef = np.zeros(lhs.shape[-2:], dtype=rhs.dtype)
707+
708+
for model_lhs, model_rhs, model_lacoef in zip(lhs.T, rhs.T, lacoef.T):
709+
710+
# Cull masked points on both sides of the matrix equation:
711+
good = ~model_rhs.mask if masked else slice(None)
712+
model_lhs = model_lhs.T[good]
713+
model_rhs = model_rhs[good][..., np.newaxis]
714+
715+
# Solve for this model:
716+
t_coef, resids, rank, sval = np.linalg.lstsq(model_lhs,
717+
model_rhs, rcond)
718+
model_lacoef[:] = t_coef.T
719+
720+
elif len(model_copy) == 1 or not masked:
667721

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

‎astropy/modeling/tests/test_fitters.py‎

Lines changed: 107 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():
@@ -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')
878984
def test_fitters_interface():

0 commit comments

Comments
 (0)