def compare_jacobian(self, fit_function, ndim, isotropic, n, groups=None, custom_param_mode=None): ff = FitFunctions(fit_function, ndim, isotropic) param_mode = {param: 'var' for param in ff.params} param_mode['background'] = 'cluster' if custom_param_mode is not None: param_mode.update(custom_param_mode) ff = FitFunctions(fit_function, ndim, isotropic, param_mode=param_mode) params = np.random.random((n, len(ff.params))) * 10 residual, jacobian = self.get_residual(ff, n, params, groups) vect = vect_from_params(params, ff.modes, groups, operation=np.mean) actual = jacobian(vect) expected = approx_jacobian(vect, residual, EPSILON)[0] assert_allclose(actual, expected, rtol=RTOL, atol=ATOL)
def test_custom_no_jac(self): fit_function = dict(name='parabola', params=['a'], fun=lambda r2, p: p[0]*r2) ff = FitFunctions(fit_function, ndim=2, isotropic=True) params = np.random.random((1, len(ff.params))) * 10 residual, jacobian = self.get_residual(ff, 1, params) assert jacobian is None
def test_2D_gauss(self): def gauss(p, y, x): background, signal, yc, xc, size = p return background + signal*np.exp(-((y - yc)**2/size**2 + (x - xc)**2/size**2)) ff = FitFunctions('gauss', ndim=2, isotropic=True) params = np.array([[5, 200, 4, 5, 6]], dtype=np.float) image = np.random.random(self.repeats) * 200 mesh = np.random.random((ff.ndim, self.repeats)) * 10 masks = np.full((1, self.repeats), True, dtype=np.bool) residual, jacobian = ff.get_residual([image], [mesh], [masks], params) vect = vect_from_params(params, ff.modes, groups=None, operation=np.mean) actual = residual(vect) expected = np.sum((image - gauss(params[0], *mesh))**2) / self.repeats assert_allclose(actual, expected, atol=1E-7)