def test_numeric_undo_format_as():

    # Compare identity of Composite batches
    def assert_components(batch1, batch2):
        for e1, e2 in zip(batch1, batch2):
            if isinstance(e1, tuple) and isinstance(e2, tuple):
                assert_components(e1, e2)
            elif isinstance(e1, tuple) or isinstance(e2, tuple):
                raise ValueError('Composite batches do not match.')
            else:
                assert np.allclose(e1, e2)

    # VectorSpace and Conv2DSpace
    VS = VectorSpace(dim=27)
    VS_sparse = VectorSpace(dim=27, sparse=True)

    # VectorSpace to Sparse VectorSpace
    VS_batch = np.arange(270).reshape(10, 27)
    new_SVS_batch = VS.np_format_as(VS_batch, VS_sparse)
    new_VS_batch = VS.undo_np_format_as(new_SVS_batch, VS_sparse)
    assert np.allclose(new_VS_batch, VS_batch)

    # ConvSpace to ConvSpace
    CS = Conv2DSpace(shape=[3, 3],
                     num_channels=3,
                     axes=('b', 0, 1, 'c'),
                     dtype='float32')
    CS_non_default = Conv2DSpace(shape=[3, 3],
                                 num_channels=3,
                                 axes=('c', 'b', 0, 1),
                                 dtype='float64')
    CS_batch = np.arange(270).reshape(10, 3, 3, 3).astype('float32')
    new_ndCS_batch = CS.np_format_as(CS_batch, CS_non_default)
    new_CS_batch = CS.undo_np_format_as(new_ndCS_batch, CS_non_default)
    assert np.allclose(new_CS_batch, CS_batch)
    assert new_ndCS_batch.shape != CS_batch.shape
    assert new_ndCS_batch.dtype == 'float64'
    assert new_CS_batch.dtype == 'float32'

    ndCS_batch = np.arange(270).reshape(3, 10, 3, 3)
    new_CS_batch = CS_non_default.np_format_as(ndCS_batch, CS)
    new_ndCS_batch = CS_non_default.undo_np_format_as(new_CS_batch, CS)
    assert np.allclose(new_ndCS_batch, ndCS_batch)
    assert new_CS_batch.shape != ndCS_batch.shape
    assert new_ndCS_batch.dtype == 'float64'
    assert new_CS_batch.dtype == 'float32'

    # Start in VectorSpace
    VS_batch = np.arange(270).reshape(10, 27)
    new_CS_batch = VS.np_format_as(VS_batch, CS)
    new_VS_batch = VS.undo_np_format_as(new_CS_batch, CS)
    assert np.allclose(new_VS_batch, VS_batch)
    # Non-default axes
    new_CS_batch = VS.np_format_as(VS_batch, CS_non_default)
    new_VS_batch = VS.undo_np_format_as(new_CS_batch, CS_non_default)
    assert np.allclose(new_VS_batch, VS_batch)

    # Start in Conv2D with default axes
    CS_batch = np.arange(270).reshape(10, 3, 3, 3)
    new_VS_batch = CS.np_format_as(CS_batch, VS)
    new_CS_batch = CS.undo_np_format_as(new_VS_batch, VS)
    assert np.allclose(new_CS_batch, CS_batch)
    # Non-default axes
    CS_batch = np.arange(270).reshape(3, 10, 3, 3)
    new_VS_batch = CS_non_default.np_format_as(CS_batch, VS)
    new_CS_batch = CS_non_default.undo_np_format_as(new_VS_batch, VS)
    assert np.allclose(new_CS_batch, CS_batch)

    # Composite Space to VectorSpace
    VS = VectorSpace(dim=27)
    CS = Conv2DSpace(shape=[2, 2], num_channels=3, axes=('b', 0, 1, 'c'))
    CompS = CompositeSpace((CompositeSpace((VS, VS)), CS))
    VS_large = VectorSpace(dim=(2*27+12))
    VS_batch = np.arange(270).reshape(10, 27)
    VS_batch2 = 2*np.arange(270).reshape(10, 27)
    CS_batch = 3*np.arange(120).reshape(10, 2, 2, 3)
    CompS_batch = ((VS_batch, VS_batch2), CS_batch)
    new_VS_batch = CompS.np_format_as(CompS_batch, VS_large)
    new_CompS_batch = CompS.undo_np_format_as(new_VS_batch, VS_large)
    assert_components(CompS_batch, new_CompS_batch)

    # VectorSpace to Composite Space
    CompS = CompositeSpace((CompositeSpace((VS, VS)), CS))
    VS_batch = np.arange((2*27+12)*10).reshape(10, 2*27+12)
    new_CompS_batch = VS_large.np_format_as(VS_batch, CompS)
    new_VS_batch = VS_large.undo_np_format_as(new_CompS_batch, CompS)
    assert np.allclose(VS_batch, new_VS_batch)
    # Reorder CompositeSpace
    CompS = CompositeSpace((VS, CompositeSpace((VS, CS))))
    VS_batch = np.arange((2*27+12)*10).reshape(10, 2*27+12)
    new_CompS_batch = VS_large.np_format_as(VS_batch, CompS)
    new_VS_batch = VS_large.undo_np_format_as(new_CompS_batch, CompS)
    assert np.allclose(VS_batch, new_VS_batch)
    # Reorder CompositeSpace
    CompS = CompositeSpace((CompositeSpace((CompositeSpace((VS,)), CS)), VS))
    VS_batch = np.arange((2*27+12)*10).reshape(10, 2*27+12)
    new_CompS_batch = VS_large.np_format_as(VS_batch, CompS)
    new_VS_batch = VS_large.undo_np_format_as(new_CompS_batch, CompS)
    assert np.allclose(VS_batch, new_VS_batch)

    # CompositeSpace to CompositeSpace
    VS = VectorSpace(dim=27)
    CS = Conv2DSpace(shape=[3, 3], num_channels=3, axes=('b', 0, 1, 'c'))
    VS_batch = np.arange(270).reshape(10, 27)
    VS_batch2 = 2*np.arange(270).reshape(10, 27)
    VS_batch3 = 3*np.arange(270).reshape(10, 27)
    CompS_VS = CompositeSpace((CompositeSpace((VS, VS)), VS))
    CompS_CS = CompositeSpace((CompositeSpace((CS, CS)), CS))
    CompS_VS_batch = ((VS_batch, VS_batch2), VS_batch3)
    new_CompS_CS_batch = CompS_VS.np_format_as(CompS_VS_batch, CompS_CS)
    new_CompS_VS_batch = CompS_VS.undo_np_format_as(new_CompS_CS_batch,
                                                    CompS_CS)
    assert_components(CompS_VS_batch, new_CompS_VS_batch)
Test functionality of undo_np_format_as on numeric batches.
dataset/ETHPy150Open lisa-lab/pylearn2/pylearn2/space/tests/test_space.py/test_numeric_undo_format_as