def test_knot_weights_jacobian_is_correct(self, num_knots, degree, cyclical): """Tests that Jacobian is correct.""" positions_init = np.random.random_sample((10, 1)) scale = num_knots if cyclical else num_knots - degree positions_init *= scale # Wrap this in identity because some assert_* ops look at the constant # tensor value and mark it as unfeedable. positions = tf.identity(tf.convert_to_tensor(value=positions_init)) weights = bspline.knot_weights(positions=positions, num_knots=num_knots, degree=degree, cyclical=cyclical, sparse_mode=False) sparse_weights = bspline.knot_weights(positions=positions, num_knots=num_knots, degree=degree, cyclical=cyclical, sparse_mode=True)[0] with self.subTest(name="dense_mode"): self.assert_jacobian_is_correct(positions, positions_init, weights) with self.subTest(name="sparse_mode"): self.assert_jacobian_is_correct(positions, positions_init, sparse_weights)
def test_knot_weights_jacobian_is_correct(self, num_knots, degree, cyclical): """Tests that Jacobian is correct.""" positions_init = np.random.random_sample((10, 1)) scale = num_knots if cyclical else num_knots - degree positions_init *= scale positions = tf.convert_to_tensor(value=positions_init) weights = bspline.knot_weights(positions=positions, num_knots=num_knots, degree=degree, cyclical=cyclical, sparse_mode=False) sparse_weights = bspline.knot_weights(positions=positions, num_knots=num_knots, degree=degree, cyclical=cyclical, sparse_mode=True)[0] with self.subTest(name="dense_mode"): self.assert_jacobian_is_correct(positions, positions_init, weights) with self.subTest(name="sparse_mode"): self.assert_jacobian_is_correct(positions, positions_init, sparse_weights)
def test_full_degree_non_cyclical_knot_weights(self, positions): """Tests that noncyclical weights are correct when using max degree.""" cyclical_weights = bspline.knot_weights( positions=positions, num_knots=3, degree=2, cyclical=True) noncyclical_weights = bspline.knot_weights( positions=positions, num_knots=3, degree=2, cyclical=False) self.assertAllClose(cyclical_weights, noncyclical_weights)
def test_knot_weights_preset(self, position, weights, degree): """Tests that knot weights are correct when degree < num_knots - 1.""" self.assertAllClose( bspline.knot_weights(position, num_knots=3, degree=degree, cyclical=True), weights)
def sparse_mode_fn(positions): return bspline.knot_weights( positions=positions, num_knots=num_knots, degree=degree, cyclical=cyclical, sparse_mode=True)[0]
def test_interpolate_with_weights_preset(self, positions, knots): """Tests that interpolate_with_weights works correctly.""" degree = 1 cyclical = False interp1 = bspline.interpolate(knots, positions, degree, cyclical) weights = bspline.knot_weights(positions, 2, degree, cyclical) interp2 = bspline.interpolate_with_weights(knots, weights) self.assertAllClose(interp1, interp2)
def test_knot_weights_sparse_mode_preset(self, positions, gt_weights, gt_shifts, degree, cyclical): """Tests that sparse mode returns correct results.""" weights, shifts = bspline.knot_weights(positions, num_knots=3, degree=degree, cyclical=cyclical, sparse_mode=True) self.assertAllClose(weights, gt_weights) self.assertAllClose(shifts, gt_shifts)