Esempio n. 1
0
def test_array_from_file():
    shape = (2,3,4)
    dtype = np.dtype(np.float32)
    in_arr = np.arange(24, dtype=dtype).reshape(shape)
    # Check on string buffers
    offset = 0
    yield assert_true, buf_chk(in_arr, StringIO(), None, offset)
    offset = 10
    yield assert_true, buf_chk(in_arr, StringIO(), None, offset)
    # check on real file
    fd, fname = tempfile.mkstemp()
    try:
        # fortran ordered
        out_buf = file(fname, 'wb')
        in_buf = file(fname, 'rb')
        yield assert_true, buf_chk(in_arr, out_buf, in_buf, offset)
        # Drop offset to check that shape's not coming from file length
        out_buf.seek(0)
        in_buf.seek(0)
        offset = 5
        yield assert_true, buf_chk(in_arr, out_buf, in_buf, offset)
    finally:
        os.remove(fname)
    # Make sure empty shape, and zero length, give empty arrays
    arr = array_from_file((), np.dtype('f8'), StringIO())
    yield assert_equal, len(arr), 0
    arr = array_from_file((0,), np.dtype('f8'), StringIO())
    yield assert_equal, len(arr), 0
Esempio n. 2
0
def buf_chk(in_arr, out_buf, in_buf, offset):
    ''' Write contents of in_arr into fileobj, read back, check same '''
    instr = ' ' * offset + in_arr.tostring(order='F')
    out_buf.write(instr)
    out_buf.flush()
    if in_buf is None: # we're using in_buf from out_buf
        out_buf.seek(0)
        in_buf = out_buf
    arr = array_from_file(
        in_arr.shape,
        in_arr.dtype,
        in_buf,
        offset)
    return np.allclose(in_arr, arr)
Esempio n. 3
0
def write_return(data, fileobj, out_dtype, *args, **kwargs):
    fileobj.truncate(0)
    array_to_file(data, out_dtype, fileobj, *args, **kwargs)
    data = array_from_file(data.shape, out_dtype, fileobj)
    return data