demo + utils venv
This commit is contained in:
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,4 @@
|
||||
function [a, b] = afunc(c, d)
|
||||
% A function
|
||||
a = c + 1;
|
||||
b = d + 10;
|
||||
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,5 @@
|
||||
Japanese:
|
||||
すべての人間は、生まれながらにして自由であり、
|
||||
かつ、尊厳と権利と について平等である。
|
||||
人間は、理性と良心とを授けられており、
|
||||
互いに同胞の精神をもって行動しなければならない。
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
% Generates mat files for loadmat unit tests
|
||||
% Uses save_matfile.m function
|
||||
% This is the version for matlab 4
|
||||
|
||||
% work out matlab version and file suffix for test files
|
||||
global FILEPREFIX FILESUFFIX
|
||||
sepchar = '/';
|
||||
if strcmp(computer, 'PCWIN'), sepchar = '\'; end
|
||||
FILEPREFIX = [pwd sepchar 'data' sepchar];
|
||||
mlv = version;
|
||||
FILESUFFIX = ['_' mlv '_' computer '.mat'];
|
||||
|
||||
% basic double array
|
||||
theta = 0:pi/4:2*pi;
|
||||
save_matfile('testdouble', theta);
|
||||
|
||||
% string
|
||||
save_matfile('teststring', '"Do nine men interpret?" "Nine men," I nod.')
|
||||
|
||||
% complex
|
||||
save_matfile('testcomplex', cos(theta) + 1j*sin(theta));
|
||||
|
||||
% asymmetric array to check indexing
|
||||
a = zeros(3, 5);
|
||||
a(:,1) = [1:3]';
|
||||
a(1,:) = 1:5;
|
||||
|
||||
% 2D matrix
|
||||
save_matfile('testmatrix', a);
|
||||
|
||||
% minus number - tests signed int
|
||||
save_matfile('testminus', -1);
|
||||
|
||||
% single character
|
||||
save_matfile('testonechar', 'r');
|
||||
|
||||
% string array
|
||||
save_matfile('teststringarray', ['one '; 'two '; 'three']);
|
||||
|
||||
% sparse array
|
||||
save_matfile('testsparse', sparse(a));
|
||||
|
||||
% sparse complex array
|
||||
b = sparse(a);
|
||||
b(1,1) = b(1,1) + j;
|
||||
save_matfile('testsparsecomplex', b);
|
||||
|
||||
% Two variables in same file
|
||||
save([FILEPREFIX 'testmulti' FILESUFFIX], 'a', 'theta')
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
% Generates mat files for loadmat unit tests
|
||||
% This is the version for matlab 5 and higher
|
||||
% Uses save_matfile.m function
|
||||
|
||||
% work out matlab version and file suffix for test files
|
||||
global FILEPREFIX FILESUFFIX
|
||||
FILEPREFIX = [fullfile(pwd, 'data') filesep];
|
||||
temp = ver('MATLAB');
|
||||
mlv = temp.Version;
|
||||
FILESUFFIX = ['_' mlv '_' computer '.mat'];
|
||||
|
||||
% basic double array
|
||||
theta = 0:pi/4:2*pi;
|
||||
save_matfile('testdouble', theta);
|
||||
|
||||
% string
|
||||
save_matfile('teststring', '"Do nine men interpret?" "Nine men," I nod.')
|
||||
|
||||
% complex
|
||||
save_matfile('testcomplex', cos(theta) + 1j*sin(theta));
|
||||
|
||||
% asymmetric array to check indexing
|
||||
a = zeros(3, 5);
|
||||
a(:,1) = [1:3]';
|
||||
a(1,:) = 1:5;
|
||||
|
||||
% 2D matrix
|
||||
save_matfile('testmatrix', a);
|
||||
|
||||
% minus number - tests signed int
|
||||
save_matfile('testminus', -1);
|
||||
|
||||
% single character
|
||||
save_matfile('testonechar', 'r');
|
||||
|
||||
% string array
|
||||
save_matfile('teststringarray', ['one '; 'two '; 'three']);
|
||||
|
||||
% sparse array
|
||||
save_matfile('testsparse', sparse(a));
|
||||
|
||||
% sparse complex array
|
||||
b = sparse(a);
|
||||
b(1,1) = b(1,1) + j;
|
||||
save_matfile('testsparsecomplex', b);
|
||||
|
||||
% Two variables in same file
|
||||
save([FILEPREFIX 'testmulti' FILESUFFIX], 'a', 'theta')
|
||||
|
||||
|
||||
% struct
|
||||
save_matfile('teststruct', ...
|
||||
struct('stringfield','Rats live on no evil star.',...
|
||||
'doublefield',[sqrt(2) exp(1) pi],...
|
||||
'complexfield',(1+1j)*[sqrt(2) exp(1) pi]));
|
||||
|
||||
% cell
|
||||
save_matfile('testcell', ...
|
||||
{['This cell contains this string and 3 arrays of increasing' ...
|
||||
' length'], 1., 1.:2., 1.:3.});
|
||||
|
||||
% scalar cell
|
||||
save_matfile('testscalarcell', {1})
|
||||
|
||||
% Empty cells in two cell matrices
|
||||
save_matfile('testemptycell', {1, 2, [], [], 3});
|
||||
|
||||
% 3D matrix
|
||||
save_matfile('test3dmatrix', reshape(1:24,[2 3 4]))
|
||||
|
||||
% nested cell array
|
||||
save_matfile('testcellnest', {1, {2, 3, {4, 5}}});
|
||||
|
||||
% nested struct
|
||||
save_matfile('teststructnest', struct('one', 1, 'two', ...
|
||||
struct('three', 'number 3')));
|
||||
|
||||
% array of struct
|
||||
save_matfile('teststructarr', [struct('one', 1, 'two', 2) ...
|
||||
struct('one', 'number 1', 'two', 'number 2')]);
|
||||
|
||||
% matlab object
|
||||
save_matfile('testobject', inline('x'))
|
||||
|
||||
% array of matlab objects
|
||||
%save_matfile('testobjarr', [inline('x') inline('x')])
|
||||
|
||||
% unicode test
|
||||
if str2num(mlv) > 7 % function added 7.0.1
|
||||
fid = fopen([FILEPREFIX 'japanese_utf8.txt']);
|
||||
from_japan = fread(fid, 'uint8')';
|
||||
fclose(fid);
|
||||
save_matfile('testunicode', native2unicode(from_japan, 'utf-8'));
|
||||
end
|
||||
|
||||
% func
|
||||
if str2num(mlv) > 7 % function pointers added recently
|
||||
func = @afunc;
|
||||
save_matfile('testfunc', func);
|
||||
end
|
||||
@@ -0,0 +1,6 @@
|
||||
function save_matfile(test_name, v)
|
||||
% saves variable passed in m with filename from prefix
|
||||
|
||||
global FILEPREFIX FILESUFFIX
|
||||
eval([test_name ' = v;']);
|
||||
save([FILEPREFIX test_name FILESUFFIX], test_name)
|
||||
@@ -0,0 +1,31 @@
|
||||
''' Tests for byteorder module '''
|
||||
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
from numpy.testing import assert_
|
||||
from pytest import raises as assert_raises
|
||||
|
||||
import scipy.io.matlab.byteordercodes as sibc
|
||||
|
||||
|
||||
def test_native():
|
||||
native_is_le = sys.byteorder == 'little'
|
||||
assert_(sibc.sys_is_le == native_is_le)
|
||||
|
||||
|
||||
def test_to_numpy():
|
||||
if sys.byteorder == 'little':
|
||||
assert_(sibc.to_numpy_code('native') == '<')
|
||||
assert_(sibc.to_numpy_code('swapped') == '>')
|
||||
else:
|
||||
assert_(sibc.to_numpy_code('native') == '>')
|
||||
assert_(sibc.to_numpy_code('swapped') == '<')
|
||||
assert_(sibc.to_numpy_code('native') == sibc.to_numpy_code('='))
|
||||
assert_(sibc.to_numpy_code('big') == '>')
|
||||
for code in ('little', '<', 'l', 'L', 'le'):
|
||||
assert_(sibc.to_numpy_code(code) == '<')
|
||||
for code in ('big', '>', 'b', 'B', 'be'):
|
||||
assert_(sibc.to_numpy_code(code) == '>')
|
||||
assert_raises(ValueError, sibc.to_numpy_code, 'silly string')
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,185 @@
|
||||
""" Testing mio5_utils Cython module
|
||||
|
||||
"""
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
from io import BytesIO
|
||||
cStringIO = BytesIO
|
||||
|
||||
import numpy as np
|
||||
|
||||
from numpy.testing import assert_array_equal, assert_equal, assert_
|
||||
from pytest import raises as assert_raises
|
||||
|
||||
from scipy._lib.six import u
|
||||
|
||||
import scipy.io.matlab.byteordercodes as boc
|
||||
import scipy.io.matlab.streams as streams
|
||||
import scipy.io.matlab.mio5_params as mio5p
|
||||
import scipy.io.matlab.mio5_utils as m5u
|
||||
|
||||
|
||||
def test_byteswap():
|
||||
for val in (
|
||||
1,
|
||||
0x100,
|
||||
0x10000):
|
||||
a = np.array(val, dtype=np.uint32)
|
||||
b = a.byteswap()
|
||||
c = m5u.byteswap_u4(a)
|
||||
assert_equal(b.item(), c)
|
||||
d = m5u.byteswap_u4(c)
|
||||
assert_equal(a.item(), d)
|
||||
|
||||
|
||||
def _make_tag(base_dt, val, mdtype, sde=False):
|
||||
''' Makes a simple matlab tag, full or sde '''
|
||||
base_dt = np.dtype(base_dt)
|
||||
bo = boc.to_numpy_code(base_dt.byteorder)
|
||||
byte_count = base_dt.itemsize
|
||||
if not sde:
|
||||
udt = bo + 'u4'
|
||||
padding = 8 - (byte_count % 8)
|
||||
all_dt = [('mdtype', udt),
|
||||
('byte_count', udt),
|
||||
('val', base_dt)]
|
||||
if padding:
|
||||
all_dt.append(('padding', 'u1', padding))
|
||||
else: # is sde
|
||||
udt = bo + 'u2'
|
||||
padding = 4-byte_count
|
||||
if bo == '<': # little endian
|
||||
all_dt = [('mdtype', udt),
|
||||
('byte_count', udt),
|
||||
('val', base_dt)]
|
||||
else: # big endian
|
||||
all_dt = [('byte_count', udt),
|
||||
('mdtype', udt),
|
||||
('val', base_dt)]
|
||||
if padding:
|
||||
all_dt.append(('padding', 'u1', padding))
|
||||
tag = np.zeros((1,), dtype=all_dt)
|
||||
tag['mdtype'] = mdtype
|
||||
tag['byte_count'] = byte_count
|
||||
tag['val'] = val
|
||||
return tag
|
||||
|
||||
|
||||
def _write_stream(stream, *strings):
|
||||
stream.truncate(0)
|
||||
stream.seek(0)
|
||||
for s in strings:
|
||||
stream.write(s)
|
||||
stream.seek(0)
|
||||
|
||||
|
||||
def _make_readerlike(stream, byte_order=boc.native_code):
|
||||
class R(object):
|
||||
pass
|
||||
r = R()
|
||||
r.mat_stream = stream
|
||||
r.byte_order = byte_order
|
||||
r.struct_as_record = True
|
||||
r.uint16_codec = sys.getdefaultencoding()
|
||||
r.chars_as_strings = False
|
||||
r.mat_dtype = False
|
||||
r.squeeze_me = False
|
||||
return r
|
||||
|
||||
|
||||
def test_read_tag():
|
||||
# mainly to test errors
|
||||
# make reader-like thing
|
||||
str_io = BytesIO()
|
||||
r = _make_readerlike(str_io)
|
||||
c_reader = m5u.VarReader5(r)
|
||||
# This works for StringIO but _not_ cStringIO
|
||||
assert_raises(IOError, c_reader.read_tag)
|
||||
# bad SDE
|
||||
tag = _make_tag('i4', 1, mio5p.miINT32, sde=True)
|
||||
tag['byte_count'] = 5
|
||||
_write_stream(str_io, tag.tostring())
|
||||
assert_raises(ValueError, c_reader.read_tag)
|
||||
|
||||
|
||||
def test_read_stream():
|
||||
tag = _make_tag('i4', 1, mio5p.miINT32, sde=True)
|
||||
tag_str = tag.tostring()
|
||||
str_io = cStringIO(tag_str)
|
||||
st = streams.make_stream(str_io)
|
||||
s = streams._read_into(st, tag.itemsize)
|
||||
assert_equal(s, tag.tostring())
|
||||
|
||||
|
||||
def test_read_numeric():
|
||||
# make reader-like thing
|
||||
str_io = cStringIO()
|
||||
r = _make_readerlike(str_io)
|
||||
# check simplest of tags
|
||||
for base_dt, val, mdtype in (('u2', 30, mio5p.miUINT16),
|
||||
('i4', 1, mio5p.miINT32),
|
||||
('i2', -1, mio5p.miINT16)):
|
||||
for byte_code in ('<', '>'):
|
||||
r.byte_order = byte_code
|
||||
c_reader = m5u.VarReader5(r)
|
||||
assert_equal(c_reader.little_endian, byte_code == '<')
|
||||
assert_equal(c_reader.is_swapped, byte_code != boc.native_code)
|
||||
for sde_f in (False, True):
|
||||
dt = np.dtype(base_dt).newbyteorder(byte_code)
|
||||
a = _make_tag(dt, val, mdtype, sde_f)
|
||||
a_str = a.tostring()
|
||||
_write_stream(str_io, a_str)
|
||||
el = c_reader.read_numeric()
|
||||
assert_equal(el, val)
|
||||
# two sequential reads
|
||||
_write_stream(str_io, a_str, a_str)
|
||||
el = c_reader.read_numeric()
|
||||
assert_equal(el, val)
|
||||
el = c_reader.read_numeric()
|
||||
assert_equal(el, val)
|
||||
|
||||
|
||||
def test_read_numeric_writeable():
|
||||
# make reader-like thing
|
||||
str_io = cStringIO()
|
||||
r = _make_readerlike(str_io, '<')
|
||||
c_reader = m5u.VarReader5(r)
|
||||
dt = np.dtype('<u2')
|
||||
a = _make_tag(dt, 30, mio5p.miUINT16, 0)
|
||||
a_str = a.tostring()
|
||||
_write_stream(str_io, a_str)
|
||||
el = c_reader.read_numeric()
|
||||
assert_(el.flags.writeable is True)
|
||||
|
||||
|
||||
def test_zero_byte_string():
|
||||
# Tests hack to allow chars of non-zero length, but 0 bytes
|
||||
# make reader-like thing
|
||||
str_io = cStringIO()
|
||||
r = _make_readerlike(str_io, boc.native_code)
|
||||
c_reader = m5u.VarReader5(r)
|
||||
tag_dt = np.dtype([('mdtype', 'u4'), ('byte_count', 'u4')])
|
||||
tag = np.zeros((1,), dtype=tag_dt)
|
||||
tag['mdtype'] = mio5p.miINT8
|
||||
tag['byte_count'] = 1
|
||||
hdr = m5u.VarHeader5()
|
||||
# Try when string is 1 length
|
||||
hdr.set_dims([1,])
|
||||
_write_stream(str_io, tag.tostring() + b' ')
|
||||
str_io.seek(0)
|
||||
val = c_reader.read_char(hdr)
|
||||
assert_equal(val, u(' '))
|
||||
# Now when string has 0 bytes 1 length
|
||||
tag['byte_count'] = 0
|
||||
_write_stream(str_io, tag.tostring())
|
||||
str_io.seek(0)
|
||||
val = c_reader.read_char(hdr)
|
||||
assert_equal(val, u(' '))
|
||||
# Now when string has 0 bytes 4 length
|
||||
str_io.seek(0)
|
||||
hdr.set_dims([4,])
|
||||
val = c_reader.read_char(hdr)
|
||||
assert_array_equal(val, [u(' ')] * 4)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
''' Jottings to work out format for __function_workspace__ matrix at end
|
||||
of mat file.
|
||||
|
||||
'''
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
import os.path
|
||||
import sys
|
||||
import io
|
||||
|
||||
from numpy.compat import asstr
|
||||
|
||||
from scipy.io.matlab.mio5 import (MatlabObject, MatFile5Writer,
|
||||
MatFile5Reader, MatlabFunction)
|
||||
|
||||
test_data_path = os.path.join(os.path.dirname(__file__), 'data')
|
||||
|
||||
|
||||
def read_minimat_vars(rdr):
|
||||
rdr.initialize_read()
|
||||
mdict = {'__globals__': []}
|
||||
i = 0
|
||||
while not rdr.end_of_stream():
|
||||
hdr, next_position = rdr.read_var_header()
|
||||
name = asstr(hdr.name)
|
||||
if name == '':
|
||||
name = 'var_%d' % i
|
||||
i += 1
|
||||
res = rdr.read_var_array(hdr, process=False)
|
||||
rdr.mat_stream.seek(next_position)
|
||||
mdict[name] = res
|
||||
if hdr.is_global:
|
||||
mdict['__globals__'].append(name)
|
||||
return mdict
|
||||
|
||||
|
||||
def read_workspace_vars(fname):
|
||||
fp = open(fname, 'rb')
|
||||
rdr = MatFile5Reader(fp, struct_as_record=True)
|
||||
vars = rdr.get_variables()
|
||||
fws = vars['__function_workspace__']
|
||||
ws_bs = io.BytesIO(fws.tostring())
|
||||
ws_bs.seek(2)
|
||||
rdr.mat_stream = ws_bs
|
||||
# Guess byte order.
|
||||
mi = rdr.mat_stream.read(2)
|
||||
rdr.byte_order = mi == b'IM' and '<' or '>'
|
||||
rdr.mat_stream.read(4) # presumably byte padding
|
||||
mdict = read_minimat_vars(rdr)
|
||||
fp.close()
|
||||
return mdict
|
||||
|
||||
|
||||
def test_jottings():
|
||||
# example
|
||||
fname = os.path.join(test_data_path, 'parabola.mat')
|
||||
ws_vars = read_workspace_vars(fname)
|
||||
@@ -0,0 +1,46 @@
|
||||
""" Testing
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
import numpy as np
|
||||
|
||||
from numpy.testing import assert_array_equal, assert_array_almost_equal, \
|
||||
assert_
|
||||
|
||||
from scipy.io.matlab.mio_utils import squeeze_element, chars_to_strings
|
||||
|
||||
|
||||
def test_squeeze_element():
|
||||
a = np.zeros((1,3))
|
||||
assert_array_equal(np.squeeze(a), squeeze_element(a))
|
||||
# 0d output from squeeze gives scalar
|
||||
sq_int = squeeze_element(np.zeros((1,1), dtype=float))
|
||||
assert_(isinstance(sq_int, float))
|
||||
# Unless it's a structured array
|
||||
sq_sa = squeeze_element(np.zeros((1,1),dtype=[('f1', 'f')]))
|
||||
assert_(isinstance(sq_sa, np.ndarray))
|
||||
|
||||
|
||||
def test_chars_strings():
|
||||
# chars as strings
|
||||
strings = ['learn ', 'python', 'fast ', 'here ']
|
||||
str_arr = np.array(strings, dtype='U6') # shape (4,)
|
||||
chars = [list(s) for s in strings]
|
||||
char_arr = np.array(chars, dtype='U1') # shape (4,6)
|
||||
assert_array_equal(chars_to_strings(char_arr), str_arr)
|
||||
ca2d = char_arr.reshape((2,2,6))
|
||||
sa2d = str_arr.reshape((2,2))
|
||||
assert_array_equal(chars_to_strings(ca2d), sa2d)
|
||||
ca3d = char_arr.reshape((1,2,2,6))
|
||||
sa3d = str_arr.reshape((1,2,2))
|
||||
assert_array_equal(chars_to_strings(ca3d), sa3d)
|
||||
# Fortran ordered arrays
|
||||
char_arrf = np.array(chars, dtype='U1', order='F') # shape (4,6)
|
||||
assert_array_equal(chars_to_strings(char_arrf), str_arr)
|
||||
# empty array
|
||||
arr = np.array([['']], dtype='U1')
|
||||
out_arr = np.array([''], dtype='U1')
|
||||
assert_array_equal(chars_to_strings(arr), out_arr)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
""" Testing miobase module
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
|
||||
from numpy.testing import assert_equal
|
||||
from pytest import raises as assert_raises
|
||||
|
||||
from scipy.io.matlab.miobase import matdims
|
||||
|
||||
|
||||
def test_matdims():
|
||||
# Test matdims dimension finder
|
||||
assert_equal(matdims(np.array(1)), (1, 1)) # numpy scalar
|
||||
assert_equal(matdims(np.array([1])), (1, 1)) # 1d array, 1 element
|
||||
assert_equal(matdims(np.array([1,2])), (2, 1)) # 1d array, 2 elements
|
||||
assert_equal(matdims(np.array([[2],[3]])), (2, 1)) # 2d array, column vector
|
||||
assert_equal(matdims(np.array([[2,3]])), (1, 2)) # 2d array, row vector
|
||||
# 3d array, rowish vector
|
||||
assert_equal(matdims(np.array([[[2,3]]])), (1, 1, 2))
|
||||
assert_equal(matdims(np.array([])), (0, 0)) # empty 1d array
|
||||
assert_equal(matdims(np.array([[]])), (0, 0)) # empty 2d
|
||||
assert_equal(matdims(np.array([[[]]])), (0, 0, 0)) # empty 3d
|
||||
# Optional argument flips 1-D shape behavior.
|
||||
assert_equal(matdims(np.array([1,2]), 'row'), (1, 2)) # 1d array, 2 elements
|
||||
# The argument has to make sense though
|
||||
assert_raises(ValueError, matdims, np.array([1,2]), 'bizarre')
|
||||
# Check empty sparse matrices get their own shape
|
||||
from scipy.sparse import csr_matrix, csc_matrix
|
||||
assert_equal(matdims(csr_matrix(np.zeros((3, 3)))), (3, 3))
|
||||
assert_equal(matdims(csc_matrix(np.zeros((2, 2)))), (2, 2))
|
||||
@@ -0,0 +1,35 @@
|
||||
""" Test reading of files not conforming to matlab specification
|
||||
|
||||
We try and read any file that matlab reads, these files included
|
||||
"""
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
from os.path import dirname, join as pjoin
|
||||
|
||||
from numpy.testing import assert_
|
||||
from pytest import raises as assert_raises
|
||||
|
||||
from scipy.io.matlab.mio import loadmat
|
||||
|
||||
TEST_DATA_PATH = pjoin(dirname(__file__), 'data')
|
||||
|
||||
|
||||
def test_multiple_fieldnames():
|
||||
# Example provided by Dharhas Pothina
|
||||
# Extracted using mio5.varmats_from_mat
|
||||
multi_fname = pjoin(TEST_DATA_PATH, 'nasty_duplicate_fieldnames.mat')
|
||||
vars = loadmat(multi_fname)
|
||||
funny_names = vars['Summary'].dtype.names
|
||||
assert_(set(['_1_Station_Q', '_2_Station_Q',
|
||||
'_3_Station_Q']).issubset(funny_names))
|
||||
|
||||
|
||||
def test_malformed1():
|
||||
# Example from gh-6072
|
||||
# Contains malformed header data, which previously resulted into a
|
||||
# buffer overflow.
|
||||
#
|
||||
# Should raise an exception, not segfault
|
||||
fname = pjoin(TEST_DATA_PATH, 'malformed1.mat')
|
||||
with open(fname, 'rb') as f:
|
||||
assert_raises(ValueError, loadmat, f)
|
||||
@@ -0,0 +1,184 @@
|
||||
""" Testing
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import division, print_function, absolute_import
|
||||
|
||||
import os
|
||||
import sys
|
||||
import zlib
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
cStringIO = BytesIO
|
||||
else:
|
||||
from cStringIO import StringIO as cStringIO
|
||||
|
||||
from tempfile import mkstemp
|
||||
from contextlib import contextmanager
|
||||
|
||||
import numpy as np
|
||||
|
||||
from numpy.testing import assert_, assert_equal
|
||||
from pytest import raises as assert_raises
|
||||
|
||||
from scipy.io.matlab.streams import (make_stream,
|
||||
GenericStream, cStringStream, FileStream, ZlibInputStream,
|
||||
_read_into, _read_string)
|
||||
|
||||
IS_PYPY = ('__pypy__' in sys.modules)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def setup_test_file():
|
||||
val = b'a\x00string'
|
||||
fd, fname = mkstemp()
|
||||
|
||||
with os.fdopen(fd, 'wb') as fs:
|
||||
fs.write(val)
|
||||
with open(fname, 'rb') as fs:
|
||||
gs = BytesIO(val)
|
||||
cs = cStringIO(val)
|
||||
yield fs, gs, cs
|
||||
os.unlink(fname)
|
||||
|
||||
|
||||
def test_make_stream():
|
||||
with setup_test_file() as (fs, gs, cs):
|
||||
# test stream initialization
|
||||
assert_(isinstance(make_stream(gs), GenericStream))
|
||||
if sys.version_info[0] < 3 and not IS_PYPY:
|
||||
assert_(isinstance(make_stream(cs), cStringStream))
|
||||
assert_(isinstance(make_stream(fs), FileStream))
|
||||
|
||||
|
||||
def test_tell_seek():
|
||||
with setup_test_file() as (fs, gs, cs):
|
||||
for s in (fs, gs, cs):
|
||||
st = make_stream(s)
|
||||
res = st.seek(0)
|
||||
assert_equal(res, 0)
|
||||
assert_equal(st.tell(), 0)
|
||||
res = st.seek(5)
|
||||
assert_equal(res, 0)
|
||||
assert_equal(st.tell(), 5)
|
||||
res = st.seek(2, 1)
|
||||
assert_equal(res, 0)
|
||||
assert_equal(st.tell(), 7)
|
||||
res = st.seek(-2, 2)
|
||||
assert_equal(res, 0)
|
||||
assert_equal(st.tell(), 6)
|
||||
|
||||
|
||||
def test_read():
|
||||
with setup_test_file() as (fs, gs, cs):
|
||||
for s in (fs, gs, cs):
|
||||
st = make_stream(s)
|
||||
st.seek(0)
|
||||
res = st.read(-1)
|
||||
assert_equal(res, b'a\x00string')
|
||||
st.seek(0)
|
||||
res = st.read(4)
|
||||
assert_equal(res, b'a\x00st')
|
||||
# read into
|
||||
st.seek(0)
|
||||
res = _read_into(st, 4)
|
||||
assert_equal(res, b'a\x00st')
|
||||
res = _read_into(st, 4)
|
||||
assert_equal(res, b'ring')
|
||||
assert_raises(IOError, _read_into, st, 2)
|
||||
# read alloc
|
||||
st.seek(0)
|
||||
res = _read_string(st, 4)
|
||||
assert_equal(res, b'a\x00st')
|
||||
res = _read_string(st, 4)
|
||||
assert_equal(res, b'ring')
|
||||
assert_raises(IOError, _read_string, st, 2)
|
||||
|
||||
|
||||
class TestZlibInputStream(object):
|
||||
def _get_data(self, size):
|
||||
data = np.random.randint(0, 256, size).astype(np.uint8).tostring()
|
||||
compressed_data = zlib.compress(data)
|
||||
stream = BytesIO(compressed_data)
|
||||
return stream, len(compressed_data), data
|
||||
|
||||
def test_read(self):
|
||||
block_size = 131072
|
||||
|
||||
SIZES = [0, 1, 10, block_size//2, block_size-1,
|
||||
block_size, block_size+1, 2*block_size-1]
|
||||
|
||||
READ_SIZES = [block_size//2, block_size-1,
|
||||
block_size, block_size+1]
|
||||
|
||||
def check(size, read_size):
|
||||
compressed_stream, compressed_data_len, data = self._get_data(size)
|
||||
stream = ZlibInputStream(compressed_stream, compressed_data_len)
|
||||
data2 = b''
|
||||
so_far = 0
|
||||
while True:
|
||||
block = stream.read(min(read_size,
|
||||
size - so_far))
|
||||
if not block:
|
||||
break
|
||||
so_far += len(block)
|
||||
data2 += block
|
||||
assert_equal(data, data2)
|
||||
|
||||
for size in SIZES:
|
||||
for read_size in READ_SIZES:
|
||||
check(size, read_size)
|
||||
|
||||
def test_read_max_length(self):
|
||||
size = 1234
|
||||
data = np.random.randint(0, 256, size).astype(np.uint8).tostring()
|
||||
compressed_data = zlib.compress(data)
|
||||
compressed_stream = BytesIO(compressed_data + b"abbacaca")
|
||||
stream = ZlibInputStream(compressed_stream, len(compressed_data))
|
||||
|
||||
stream.read(len(data))
|
||||
assert_equal(compressed_stream.tell(), len(compressed_data))
|
||||
|
||||
assert_raises(IOError, stream.read, 1)
|
||||
|
||||
def test_seek(self):
|
||||
compressed_stream, compressed_data_len, data = self._get_data(1024)
|
||||
|
||||
stream = ZlibInputStream(compressed_stream, compressed_data_len)
|
||||
|
||||
stream.seek(123)
|
||||
p = 123
|
||||
assert_equal(stream.tell(), p)
|
||||
d1 = stream.read(11)
|
||||
assert_equal(d1, data[p:p+11])
|
||||
|
||||
stream.seek(321, 1)
|
||||
p = 123+11+321
|
||||
assert_equal(stream.tell(), p)
|
||||
d2 = stream.read(21)
|
||||
assert_equal(d2, data[p:p+21])
|
||||
|
||||
stream.seek(641, 0)
|
||||
p = 641
|
||||
assert_equal(stream.tell(), p)
|
||||
d3 = stream.read(11)
|
||||
assert_equal(d3, data[p:p+11])
|
||||
|
||||
assert_raises(IOError, stream.seek, 10, 2)
|
||||
assert_raises(IOError, stream.seek, -1, 1)
|
||||
assert_raises(ValueError, stream.seek, 1, 123)
|
||||
|
||||
stream.seek(10000, 1)
|
||||
assert_raises(IOError, stream.read, 12)
|
||||
|
||||
def test_all_data_read(self):
|
||||
compressed_stream, compressed_data_len, data = self._get_data(1024)
|
||||
stream = ZlibInputStream(compressed_stream, compressed_data_len)
|
||||
assert_(not stream.all_data_read())
|
||||
stream.seek(512)
|
||||
assert_(not stream.all_data_read())
|
||||
stream.seek(1024)
|
||||
assert_(stream.all_data_read())
|
||||
|
||||
Reference in New Issue
Block a user