示例#1
0
def test_numpy_patch():
    storage = gt_store.from_array(np.random.randn(5, 5, 5),
                                  default_origin=(1, 1, 1),
                                  backend="gtmc")

    class npsub(np.ndarray):
        pass

    numpy_array = np.ones((3, 3, 3))
    matrix = np.ones((3, 3)).view(npsub)

    assert isinstance(np.asarray(storage), np.ndarray)
    assert isinstance(np.asarray(numpy_array), np.ndarray)
    assert isinstance(np.asarray(matrix), np.ndarray)
    assert isinstance(np.asanyarray(storage), type(storage))
    assert isinstance(np.asanyarray(numpy_array), np.ndarray)
    assert isinstance(np.asanyarray(matrix), npsub)
    assert isinstance(np.array(storage), np.ndarray)
    assert isinstance(np.array(matrix), np.ndarray)
    assert isinstance(np.array(numpy_array), np.ndarray)

    # apply numpy patch
    gt_store.prepare_numpy()

    assert isinstance(np.asarray(storage), type(storage))
    assert isinstance(np.asarray(numpy_array), np.ndarray)
    assert isinstance(np.asarray(matrix), np.ndarray)

    assert isinstance(np.array(matrix), np.ndarray)
    assert isinstance(np.array(numpy_array), np.ndarray)
    try:
        np.array(storage)
    except RuntimeError:
        pass
    else:
        assert False

    # undo patch
    gt_store.restore_numpy()

    assert isinstance(np.asarray(storage), np.ndarray)
    assert isinstance(np.asarray(numpy_array), np.ndarray)
    assert isinstance(np.asarray(matrix), np.ndarray)

    assert isinstance(np.array(storage), np.ndarray)
    assert isinstance(np.array(matrix), np.ndarray)
    assert isinstance(np.array(numpy_array), np.ndarray)
示例#2
0
    def test_asarray(self):
        storage = gt_store.from_array(
            np.random.randn(5, 5, 5), default_origin=(1, 1, 1), backend="gtmc"
        )

        class NDArraySub(np.ndarray):
            pass

        numpy_array = np.ones((3, 3, 3))
        matrix = np.ones((3, 3)).view(NDArraySub)

        assert isinstance(np.asarray(storage), np.ndarray)
        assert isinstance(np.asarray(numpy_array), np.ndarray)
        assert isinstance(np.asarray(matrix), np.ndarray)
        assert isinstance(np.asanyarray(storage), type(storage))
        assert isinstance(np.asanyarray(numpy_array), np.ndarray)
        assert isinstance(np.asanyarray(matrix), NDArraySub)
        assert isinstance(np.array(storage), np.ndarray)
        assert isinstance(np.array(matrix), np.ndarray)
        assert isinstance(np.array(numpy_array), np.ndarray)

        # apply numpy patch
        gt_store.prepare_numpy()

        try:
            assert isinstance(np.asarray(storage), type(storage))
            assert isinstance(np.asarray(numpy_array), np.ndarray)
            assert isinstance(np.asarray(matrix), np.ndarray)

            assert isinstance(np.array(matrix), np.ndarray)
            assert isinstance(np.array(numpy_array), np.ndarray)
            with pytest.raises(RuntimeError):
                np.array(storage)
        finally:
            # undo patch
            gt_store.restore_numpy()

        assert isinstance(np.asarray(storage), np.ndarray)
        assert isinstance(np.asarray(numpy_array), np.ndarray)
        assert isinstance(np.asarray(matrix), np.ndarray)

        assert isinstance(np.array(storage), np.ndarray)
        assert isinstance(np.array(matrix), np.ndarray)
        assert isinstance(np.array(numpy_array), np.ndarray)