Skip to content

Commit 0672929

Browse files
committed
Fixes for weights with linear fitter
Fix astropy#7035
1 parent 4bc5014 commit 0672929

2 files changed

Lines changed: 142 additions & 8 deletions

File tree

‎astropy/modeling/fitting.py‎

Lines changed: 49 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,10 @@ 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+
_, weights = _convert_input(x, weights, n_models=len(model_copy),
570+
model_set_axis=model_copy.model_set_axis)
571+
565572
# map domain into window
566573
if hasattr(model_copy, 'domain'):
567574
x = self._map_domain_window(model_copy, x)
@@ -577,6 +584,10 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
577584
else:
578585
x, y, z = farg
579586

587+
if weights is not None and weights.ndim > 2:
588+
_, _, weights = _convert_input(x, y, weights, n_models=len(model_copy),
589+
model_set_axis=model_copy.model_set_axis)
590+
580591
# map domain into window
581592
if hasattr(model_copy, 'x_domain'):
582593
x, y = self._map_domain_window(model_copy, x, y)
@@ -609,8 +620,17 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
609620
# but z has a second axis for the model set. NB. This is
610621
# ~5-10x faster than using rollaxis.
611622
rhs = z.T if model_axis == 0 else z
623+
624+
if weights is not None:
625+
if weights.ndim > 2:
626+
weights = np.rollaxis(z, model_axis, z.ndim)
627+
weights = weights.reshape(-1, weights.shape[-1])
628+
else:
629+
weights = weights.flatten()
612630
else:
613631
rhs = z.flatten()
632+
if weights is not None:
633+
weights = weights.flatten()
614634

615635
# If the derivative is defined along rows (as with non-linear models)
616636
if model_copy.col_fit_deriv:
@@ -645,14 +665,18 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
645665
rhs = rhs - sum_of_implicit_terms
646666

647667
if weights is not None:
648-
weights = np.asarray(weights, dtype=float)
649-
if len(x) != len(weights):
668+
if len(lhs) != len(weights):
650669
raise ValueError("x and weights should have the same length")
670+
651671
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]
672+
if weights is not None and weights.shape == rhs.shape:
673+
lhs = lhs[..., np.newaxis] * weights[:, np.newaxis]
674+
rhs = rhs * weights
675+
else:
676+
lhs *= weights[:, np.newaxis]
677+
# Don't modify in-place in case rhs was the original
678+
# dependent variable array
679+
rhs = rhs * weights[:, np.newaxis]
656680
else:
657681
lhs *= weights[:, np.newaxis]
658682
rhs = rhs * weights
@@ -663,7 +687,25 @@ def __call__(self, model, x, y, z=None, weights=None, rcond=None):
663687
masked = np.any(np.ma.getmask(rhs))
664688

665689
a = None # need for calculating covarience
666-
if len(model_copy) == 1 or not masked:
690+
691+
if weights is not None and weights.ndim > 1 and weights.shape == rhs.shape:
692+
693+
scl = scl.T
694+
lacoef = np.zeros(lhs.shape[-2:], dtype=rhs.dtype)
695+
696+
for model_lhs, model_rhs, model_lacoef in zip(lhs.T, rhs.T, lacoef.T):
697+
698+
# Cull masked points on both sides of the matrix equation:
699+
good = ~model_rhs.mask if masked else slice(None)
700+
model_lhs = model_lhs.T[good]
701+
model_rhs = model_rhs[good][..., np.newaxis]
702+
703+
# Solve for this model:
704+
t_coef, resids, rank, sval = np.linalg.lstsq(model_lhs,
705+
model_rhs, rcond)
706+
model_lacoef[:] = t_coef.T
707+
708+
elif len(model_copy) == 1 or not masked:
667709

668710
# If we're fitting one or more models over a common set of points,
669711
# 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)