示例#1
0
 def test_resize(self):
     data = np.zeros(10)
     B = DataBuffer(data=data)
     data = np.zeros(20)
     B.set_data(data)
     assert B.nbytes == data.nbytes
     assert B._need_resize is True
示例#2
0
 def test_set_data_base(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data, store=True)
     B.set_data(data)
     assert len(B._pending_data) == 1
示例#3
0
 def test_set_data_offset(self):
     data = np.zeros(100, np.float32)
     subdata = data[:10]
     
     B = DataBuffer(data)
     B.set_subdata(subdata, offset=10)
     offset = B._pending_data[-1][2]
     assert offset == 10*4
示例#4
0
 def test_set_data_base(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data, store=True, copy=False)
     B.set_data(data)
     assert len(B._pending_data) == 1
示例#5
0
 def test_invalid_view_after_set_data(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     Z = B[5:]
     B.set_data(np.zeros(15, dtype=dtype))
     assert Z._valid is False
示例#6
0
 def test_invalid_view_after_set_data(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     Z = B[5:]
     B.set_data(np.zeros(15, dtype=dtype))
     assert Z._valid is False
示例#7
0
 def test_set_data_offset(self):
     data = np.zeros(100, np.float32)
     subdata = data[:10]
     
     B = DataBuffer(data)
     B.set_subdata(subdata, offset=10)
     last_cmd = B._context.glir.clear()[-1]
     offset = last_cmd[2]
     assert offset == 10*4
示例#8
0
    def test_set_data_offset(self):
        data = np.zeros(100, np.float32)
        subdata = data[:10]

        B = DataBuffer(data)
        B.set_subdata(subdata, offset=10)
        last_cmd = B._glir.clear()[-1]
        offset = last_cmd[2]
        assert offset == 10 * 4
示例#9
0
 def test_invalid_view_after_resize(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color', np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     Y = B['position']
     Z = B[5:]
     B.resize_bytes(5)
     assert Y._valid is False
     assert Z._valid is False
示例#10
0
 def test_invalid_view_after_resize(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     Y = B['position']
     Z = B[5:]
     B.resize_bytes(5)
     assert Y._valid is False
     assert Z._valid is False
示例#11
0
 def test_set_data_base(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     B.set_data(data)
     last_cmd = B._context.glir.clear()[-1]
     assert last_cmd[0] == 'DATA'
     
     # Extra kwargs are caught
     self.assertRaises(TypeError, B.set_data, data, foo=4)
示例#12
0
    def test_set_data_base(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color', np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)
        B.set_data(data)
        last_cmd = B._glir.clear()[-1]
        assert last_cmd[0] == 'DATA'

        # Extra kwargs are caught
        self.assertRaises(TypeError, B.set_data, data, foo=4)
示例#13
0
 def test_non_contiguous_storage(self):
     # Ask to have CPU storage and to use data as storage
     # Not possible since data[::2] is not contiguous
     data = np.ones(100, np.float32)
     data_given = data[::2]
     
     with use_log_level('warning', record=True, print_msg=False) as l:
         B = DataBuffer(data_given, store=True)
     assert len(l) == 1
     assert B._data is not data_given
     assert B.stride == 4
     
     B = DataBuffer(data_given, store=False)
     assert B._data is not data_given
     assert B.stride == 4*2
示例#14
0
    def test_getitem_field(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color',    np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)

        Z = B["position"]
        assert Z.nbytes == 10 * 3 * np.dtype(np.float32).itemsize
        assert Z.offset == 0
        assert Z.size == 10
        assert Z.itemsize == 3 * np.dtype(np.float32).itemsize
        assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.dtype == (np.float32, 3)

        Z = B["texcoord"]
        assert Z.nbytes == 10 * 2 * np.dtype(np.float32).itemsize
        assert Z.offset == 3 * np.dtype(np.float32).itemsize
        assert Z.size == 10
        assert Z.itemsize == 2 * np.dtype(np.float32).itemsize
        assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.dtype == (np.float32, 2)

        Z = B["color"]
        assert Z.nbytes == 10 * 4 * np.dtype(np.float32).itemsize
        assert Z.offset == (2 + 3) * np.dtype(np.float32).itemsize
        assert Z.size == 10
        assert Z.itemsize == 4 * np.dtype(np.float32).itemsize
        assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.dtype == (np.float32, 4)
示例#15
0
 def test_no_resize_ellipsis(self):
     data = np.zeros(10)
     B = DataBuffer(data=data)
     data = np.zeros(30)
     # with self.assertRaises(ValueError):
     #    B[...] = data
     self.assertRaises(ValueError, B.__setitem__, Ellipsis, data)
示例#16
0
 def test_broadcast_ellipsis(self):
     data = np.zeros(10)
     B = DataBuffer(data=data)
     data = np.zeros(5)
     B[Ellipsis] = data
     glir_cmd = B._glir.clear()[-1]
     assert glir_cmd[-1].shape == (10, )
示例#17
0
 def test_setitem_field_no_storage(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color', np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     self.assertRaises(ValueError, B.__setitem__, 'position', (1, 2, 3))
示例#18
0
    def test_setitem(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color', np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)
        B[1] = data[0]
        B[-1] = data[0]
        B[:5] = data[:5]
        B[5:0] = data[:5]  # Weird, but we apparently support this
        # the below is no longer supported starting with NumPy 1.14.0
        # this used to be converted to a single element array of the above
        # dtype filled with 0s. This is no longer supported.
        # B[1] = b''  # Gets converted into array of dtype. Lists do not work
        # the below doesn't work on all systems (I guess?)
        # B[1] = 0
        B[1] = (
            [0, 0, 0],
            [0, 0],
            [0, 0, 0, 0],
        )

        self.assertRaises(IndexError, B.__setitem__, +999, data[0])
        self.assertRaises(IndexError, B.__setitem__, -999, data[0])
        self.assertRaises(TypeError, B.__setitem__, [], data[0])
示例#19
0
    def test_non_contiguous_storage(self):
        # Ask to have CPU storage and to use data as storage
        # Not possible since data[::2] is not contiguous
        data = np.ones(100, np.float32)
        data_given = data[::2]

        B = DataBuffer(data_given)
        assert B.stride == 4 * 2
示例#20
0
 def test_setitem_broadcast(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data, store=True)
     B['position'] = 1, 2, 3
     assert np.allclose(data['position'].ravel(), np.resize([1, 2, 3], 30))
示例#21
0
 def test_setitem_strided(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color', np.float32, 4)])
     data1 = np.zeros(10, dtype=dtype)
     data2 = np.ones(10, dtype=dtype)
     B = DataBuffer(data1)
     s = slice(None, None, 2)
     self.assertRaises(ValueError, B.__setitem__, s, data2[::2])
示例#22
0
 def test_every_two_item_no_storage(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data, store=False)
     # with self.assertRaises(ValueError):
     #    B[::2] = data[::2]
     s = slice(None, None, 2)
     self.assertRaises(ValueError, B.__setitem__, s, data[::2])
示例#23
0
 def test_setitem_strided(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data1 = np.zeros(10, dtype=dtype)
     data2 = np.ones(10, dtype=dtype)
     B = DataBuffer(data1, store=True)
     B[::2] = data2[::2]
     assert np.allclose(data1['position'][::2], data2['position'][::2])
     assert np.allclose(data1['texcoord'][::2], data2['texcoord'][::2])
     assert np.allclose(data1['color'][::2], data2['color'][::2])
示例#24
0
 def test_setitem_half(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data1 = np.zeros(10, dtype=dtype)
     data2 = np.ones(10, dtype=dtype)
     B = DataBuffer(data1, store=True)
     B[:5] = data2[:5]
     assert np.allclose(data1['position'][:5], data2['position'][:5])
     assert np.allclose(data1['texcoord'][:5], data2['texcoord'][:5])
     assert np.allclose(data1['color'][:5], data2['color'][:5])
     assert len(B._pending_data) == 2
示例#25
0
 def test_default_init(self):
     # Check default storage and copy flags
     data = np.ones(100)
     B = DataBuffer(data)
     assert B._store is True
     assert B._copied is False
     assert B.nbytes == data.nbytes
     assert B.offset == 0
     assert B.size == 100
     assert B.itemsize == data.itemsize
     assert B.stride == data.itemsize
     assert B.dtype == data.dtype
示例#26
0
    def test_set_data_base_view(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color',    np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data, store=True)
        # set_data on field is not allowed because set_data
        # can result in a buffer resize

        # with self.assertRaises(ValueError):
        #    B['position'].set_data(data)
        Z = B['position']
        self.assertRaises(ValueError, Z.set_data, data)
示例#27
0
 def test_getitem_index(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     Z = B[0:1]
     assert Z.nbytes == 1 * (3 + 2 + 4) * np.dtype(np.float32).itemsize
     assert Z.offset == 0
     assert Z.size == 1
     assert Z.itemsize == (3 + 2 + 4) * np.dtype(np.float32).itemsize
     assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize
     assert Z.dtype == B.dtype
示例#28
0
 def test_structured_init(self):
     # Check structured type
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color',    np.float32, 4)])
     data = np.zeros(10, dtype=dtype)
     B = DataBuffer(data)
     assert B.nbytes == data.nbytes
     assert B.offset == 0
     assert B.size == 10
     assert B.itemsize == data.itemsize
     assert B.stride == data.itemsize
     assert B.dtype == data.dtype
示例#29
0
    def test_getitem(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color', np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)
        assert B[1].dtype == dtype
        assert B[1].size == 1
        assert B[-1].dtype == dtype
        assert B[-1].size == 1

        self.assertRaises(IndexError, B.__getitem__, +999)
        self.assertRaises(IndexError, B.__getitem__, -999)
示例#30
0
    def test_default_init(self):
        # Check default storage and copy flags
        data = np.ones(100)
        B = DataBuffer(data)
        assert B.nbytes == data.nbytes
        assert B.offset == 0
        assert B.size == 100
        assert B.itemsize == data.itemsize
        assert B.stride == data.itemsize
        assert B.dtype == data.dtype

        # Given data must be actual numeric data
        self.assertRaises(TypeError, DataBuffer, 'this is not nice data')
示例#31
0
 def test_setitem_half(self):
     dtype = np.dtype([('position', np.float32, 3),
                       ('texcoord', np.float32, 2),
                       ('color', np.float32, 4)])
     data1 = np.zeros(10, dtype=dtype)
     data2 = np.ones(10, dtype=dtype)
     B = DataBuffer(data1)
     B._glir.clear()
     B[:5] = data2[:5]
     glir_cmds = B._glir.clear()
     assert len(glir_cmds) == 1
     set_data = glir_cmds[0][-1]
     assert np.allclose(set_data['position'], data2['position'][:5])
     assert np.allclose(set_data['texcoord'][:5], data2['texcoord'][:5])
     assert np.allclose(set_data['color'][:5], data2['color'][:5])
示例#32
0
    def test_setitem(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color', np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)
        B[1] = data[0]
        B[-1] = data[0]
        B[:5] = data[:5]
        B[5:0] = data[:5]  # Weird, but we apparently support this
        B[1] = b''  # Gets conveted into array of dtype. Lists do not work

        self.assertRaises(IndexError, B.__setitem__, +999, data[0])
        self.assertRaises(IndexError, B.__setitem__, -999, data[0])
        self.assertRaises(TypeError, B.__setitem__, [], data[0])
示例#33
0
    def test_init_view(self):
        data = np.zeros(10)
        B = DataBuffer(data=data)

        V = DataBufferView(B, 1)
        assert V.size == 1

        V = DataBufferView(B, slice(0, 5))
        assert V.size == 5

        V = DataBufferView(B, slice(5, 0))
        assert V.size == 5

        V = DataBufferView(B, Ellipsis)
        assert V.size == 10

        self.assertRaises(TypeError, DataBufferView, B, [])
        self.assertRaises(ValueError, DataBufferView, B, slice(0, 10, 2))
示例#34
0
    def test_getitem_index(self):
        dtype = np.dtype([('position', np.float32, 3),
                          ('texcoord', np.float32, 2),
                          ('color', np.float32, 4)])
        data = np.zeros(10, dtype=dtype)
        B = DataBuffer(data)
        Z = B[0:1]
        assert Z.base == B
        assert Z.id == B.id
        assert Z.nbytes == 1 * (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.offset == 0
        assert Z.size == 1
        assert Z.itemsize == (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.stride == (3 + 2 + 4) * np.dtype(np.float32).itemsize
        assert Z.dtype == B.dtype
        assert 'DataBufferView' in repr(Z)

        # There's a few things we cannot do with a view
        self.assertRaises(RuntimeError, Z.set_data, data)
        self.assertRaises(RuntimeError, Z.set_subdata, data)
        self.assertRaises(RuntimeError, Z.resize_bytes, 20)
        self.assertRaises(RuntimeError, Z.__getitem__, 3)
        self.assertRaises(RuntimeError, Z.__setitem__, 3, data)
示例#35
0
 def test_resize(self):
     data = np.zeros(10)
     B = DataBuffer(data=data)
     data = np.zeros(20)
     B.set_data(data)
     assert B.nbytes == data.nbytes
示例#36
0
 def test_no_storage_copy(self):
     data = np.ones(100, np.float32)
     B = DataBuffer(data, store=False)
     assert B.data is None
     assert B.stride == 4