예제 #1
0
def test_adjointness(size=4):
    """Tests for adjointness between gradient and div operators"""
    rng = check_random_state(42)
    for _ in range(3):
        image_1 = rng.rand(size, size, size)
        image_2 = rng.rand(3, size, size, size)
        Axdoty = np.dot((_gradient(image_1).ravel()), image_2.ravel())
        xdotAty = np.dot((_div(image_2).ravel()), image_1.ravel())
        assert_almost_equal(Axdoty, - xdotAty)
예제 #2
0
def test_adjointness(size=4):
    """Tests for adjointness between gradient and div operators"""
    rng = check_random_state(42)
    for _ in range(3):
        image_1 = rng.rand(size, size, size)
        image_2 = rng.rand(3, size, size, size)
        Axdoty = np.dot((_gradient(image_1).ravel()), image_2.ravel())
        xdotAty = np.dot((_div(image_2).ravel()), image_1.ravel())
        assert_almost_equal(Axdoty, -xdotAty)
예제 #3
0
def test_grad_matrix():
    """Test for matricial form of gradient"""
    rng = check_random_state(42)
    G = get_gradient_matrix(w.size, mask)
    image_buffer = np.zeros(mask.shape)
    grad_mask = np.array([mask for _ in range(mask.ndim)])
    for _ in range(10):
        v = rng.rand(w.size) * rng.randint(1000)
        image_buffer[mask] = v
        assert_almost_equal(_gradient(image_buffer)[grad_mask], np.dot(G, v))
예제 #4
0
def test_grad_matrix():
    """Test for matricial form of gradient"""
    rng = check_random_state(42)
    G = get_gradient_matrix(w.size, mask)
    image_buffer = np.zeros(mask.shape)
    grad_mask = np.array([mask for _ in range(mask.ndim)])
    for _ in range(10):
        v = rng.rand(w.size) * rng.randint(1000)
        image_buffer[mask] = v
        assert_almost_equal(_gradient(image_buffer)[grad_mask], np.dot(G, v))
예제 #5
0
def get_gradient_matrix(w_size, mask):
    """
    Given a number of features and a mask (which has the property
    mask[mask==True].size == w_size) computes a matrix G such that for
    a w vector we have np.dot(G, w) == gradient(w_masked)[mask]
    """
    grad_matrix = np.zeros((mask.ndim * w_size, w_size))
    grad_mask = np.array([mask for _ in range(mask.ndim)])
    image_buffer = np.zeros(mask.shape)

    for i in range(w_size):
        base_vector = np.zeros(w_size)
        base_vector[i] = 1
        image_buffer[mask] = base_vector
        gradient_column = _gradient(image_buffer)[grad_mask]
        grad_matrix[:, i] = gradient_column

    return grad_matrix
예제 #6
0
def get_gradient_matrix(w_size, mask):
    """
    Given a number of features and a mask (which has the property
    mask[mask==True].size == w_size) computes a matrix G such that for
    a w vector we have np.dot(G, w) == gradient(w_masked)[mask]
    """
    grad_matrix = np.zeros((mask.ndim * w_size, w_size))
    grad_mask = np.array([mask for _ in range(mask.ndim)])
    image_buffer = np.zeros(mask.shape)

    for i in range(w_size):
        base_vector = np.zeros(w_size)
        base_vector[i] = 1
        image_buffer[mask] = base_vector
        gradient_column = _gradient(image_buffer)[grad_mask]
        grad_matrix[:, i] = gradient_column

    return grad_matrix