Beispiel #1
0
def test_restruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(restruct(aref.ravel(), np.zeros((3, 5, 2))), aref), \
        "Numpy ndarray restruct"

    # Numeric (preserves floating points)
    assert restruct(np.array(2.2), 0) == 2.2, "Integer restruct"
    assert restruct(np.array(-5.1), 0.0) == -5.1, "Float restruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'b': np.ones(2),
        'c': np.linspace(0, 1, 6).reshape(3, 2)
    }
    dzeros = {
        'a': np.zeros((1, 5)),
        'b': np.zeros(2),
        'c': np.zeros((3, 2))
    }
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    for key, val in list(restruct(darray, dzeros).items()):
        assert np.allclose(dref[key], val), "Dict restruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    lzeros = [np.zeros((2, 2)), np.zeros(2), np.zeros((2, 2))]
    for idx, val in enumerate(restruct(larray, lzeros)):
        assert np.allclose(lref[idx], val), "List restruct"

    # Tuple
    for idx, val in enumerate(restruct(larray, tuple(lzeros))):
        assert np.allclose(lref[idx], val), "Tuple restruct"

    # Composed / mixed types
    lref = [np.eye(2), {'a': np.array([-1, 1]), 'b': np.arange(3)}, 7]
    lzeros = [np.zeros((2, 2)), {'a': np.zeros(2), 'b': np.zeros(3)}, 0]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(destruct(restruct(larray, lzeros)), destruct(lref)), "List of mixed types"

    dref = {
        'a': [3.5, 1.2, -33],
        'b': np.arange(6).reshape(2, 3),
        'c': 7
    }
    dzeros = {
        'a': [0, 0, 0],
        'b': np.zeros((2, 3)),
        'c': 0
    }
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(destruct(restruct(darray, dzeros)), destruct(dref)), "Dictionary of mixed types"
Beispiel #2
0
def test_restruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(restruct(aref.ravel(), np.zeros((3, 5, 2))), aref), \
        "Numpy ndarray restruct"

    # Numeric (preserves floating points)
    assert restruct(np.array(2.2), 0) == 2.2, "Integer restruct"
    assert restruct(np.array(-5.1), 0.0) == -5.1, "Float restruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'b': np.ones(2),
        'c': np.linspace(0, 1, 6).reshape(3, 2)
    }
    dzeros = {'a': np.zeros((1, 5)), 'b': np.zeros(2), 'c': np.zeros((3, 2))}
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    for key, val in list(restruct(darray, dzeros).items()):
        assert np.allclose(dref[key], val), "Dict restruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    lzeros = [np.zeros((2, 2)), np.zeros(2), np.zeros((2, 2))]
    for idx, val in enumerate(restruct(larray, lzeros)):
        assert np.allclose(lref[idx], val), "List restruct"

    # Tuple
    for idx, val in enumerate(restruct(larray, tuple(lzeros))):
        assert np.allclose(lref[idx], val), "Tuple restruct"

    # Composed / mixed types
    lref = [np.eye(2), {'a': np.array([-1, 1]), 'b': np.arange(3)}, 7]
    lzeros = [np.zeros((2, 2)), {'a': np.zeros(2), 'b': np.zeros(3)}, 0]
    larray = np.array([1, 0, 0, 1, -1, 1, 0, 1, 2, 7])
    assert np.allclose(destruct(restruct(larray, lzeros)),
                       destruct(lref)), "List of mixed types"

    dref = {'a': [3.5, 1.2, -33], 'b': np.arange(6).reshape(2, 3), 'c': 7}
    dzeros = {'a': [0, 0, 0], 'b': np.zeros((2, 3)), 'c': 0}
    darray = np.array([3.5, 1.2, -33, 0, 1, 2, 3, 4, 5, 7])
    assert np.allclose(destruct(restruct(darray, dzeros)),
                       destruct(dref)), "Dictionary of mixed types"
Beispiel #3
0
def test_restruct():
    """Tests the destruct utility function"""

    # numpy ndarray
    aref = np.linspace(0, 1, 30).reshape(2, 5, 3).T
    assert np.allclose(restruct(aref.ravel(), np.zeros((3, 5, 2))), aref), \
        "Numpy ndarray restruct"

    # Dictionary
    dref = {
        'a': np.arange(5).reshape(1, 5),
        'b': np.ones(2),
        'c': np.linspace(0, 1, 6).reshape(3, 2)
    }
    dzeros = {
        'a': np.zeros((1, 5)),
        'b': np.zeros(2),
        'c': np.zeros((3, 2))
    }
    darray = np.array([0, 1, 2, 3, 4, 1, 1, 0, 0.2, 0.4, 0.6, 0.8, 1])
    for key, val in list(restruct(darray, dzeros).items()):
        assert np.allclose(dref[key], val), "Dict restruct"

    # List
    lref = [np.eye(2), np.array([-1, 1]), np.array([[10, 20], [30, 40]])]
    larray = np.array([1, 0, 0, 1, -1, 1, 10, 20, 30, 40])
    lzeros = [np.zeros((2, 2)), np.zeros(2), np.zeros((2, 2))]
    for idx, val in enumerate(restruct(larray, lzeros)):
        assert np.allclose(lref[idx], val), "List restruct"

    # Tuple
    for idx, val in enumerate(restruct(larray, tuple(lzeros))):
        assert np.allclose(lref[idx], val), "Tuple restruct"

    # Numeric
    assert restruct(np.array(2.0), 0) == 2.0, "Integer restruct"
    assert restruct(np.array(-5.0), 0.0) == -5.0, "Float restruct"
Beispiel #4
0
 def theta(self):
     """Gets the dictionary of parameters"""
     return restruct(self.opt.xk, self.theta_init)