"""Roundtrip testing of npy-matlab.
Write data in NPY format to be read and rewritten using npy-matlab. Then
re-read the matlab written data again and check if it is still the same.
"""
import os
import os.path as op
from subprocess import call
import numpy as np
import skimage.data
# For numpy data types see:
# https://docs.scipy.org/doc/numpy-1.15.0/user/basics.types.html
SUPPORTED_DTYPES = ['uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16',
'int32', 'int64', 'float32', 'float64']
# Directory of this file
test_dir = op.dirname(op.realpath(__file__))
def _save_testing_data():
"""Write testing data to /tests/data.
Returns
-------
all_saved_data : list of tuples
all_saved_data[i][0] is the path to written data
all_saved_data[i][1] is actual data
"""
# Make a data directory
data_dir = op.join(test_dir, 'data')
if not op.exists(data_dir):
op.mkdir(data_dir)
# Test 1D data
n = 10000
t = np.linspace(-10., 10., n)
sine = (1 + np.sin(t)) * 64
# Test 3D data
chelsea = skimage.data.chelsea()
# Save all test data in NPY format
all_saved_data = list()
for dtype in SUPPORTED_DTYPES:
sine_tr = sine.astype(dtype)
fpath = op.join(data_dir, 'sine_{}.npy'.format(dtype))
np.save(fpath, sine_tr)
all_saved_data.append((fpath, sine_tr))
chelsea_tr = chelsea.astype(dtype)
fpath = op.join(data_dir, 'chelsea_{}.npy'.format(dtype))
np.save(fpath, chelsea_tr)
all_saved_data.append((fpath, chelsea_tr))
return all_saved_data
def test_roundtrip():
"""Test roundtrip."""
# Write testing data from Python
all_saved_data = _save_testing_data()
# General command structure for calling octave
# note, there is no closing `"` sign. Must be added later.
main_dir = os.sep.join(test_dir.split(os.sep)[:-1])
func_dir = op.join(main_dir, 'npy-matlab')
cmd_octave = 'octave --no-gui --eval "'
cmd_octave += "addpath('{}');".format(func_dir)
# Now for each data type and testing data:
# read with octave
# re-write with octave
# re-read with numpy and assert it is the same
for i in range(len(all_saved_data)):
# Get the original data
fpath = all_saved_data[i][0]
original_data = all_saved_data[i][1]
# Read into octave and write back from octave
new_fpath = op.join(op.split(fpath)[0], 'matlab_' + op.split(fpath)[1])
cmd_read = "new_data=readNPY('{}');".format(fpath)
cmd_write = "writeNPY(new_data, '{}');".format(new_fpath)
cmd_complete = cmd_octave + cmd_read + cmd_write + '"'
print(cmd_complete)
call(cmd_complete, shell=True)
# Read back with numpy and assert it's the same
new_data = np.load(new_fpath)
np.testing.assert_array_equal(original_data, new_data.squeeze())
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MATLAB工具箱-npy-matlab工具箱1.rar (53个子文件)
29.MATLAB工具箱-npy-matlab工具箱1
.DS_Store 6KB
.travis.yml 1KB
tests
data
matlab_sine_int64.npy 78KB
matlab_sine_int16.npy 20KB
matlab_sine_uint16.npy 20KB
chelsea_float32.npy 1.55MB
sine_uint64.npy 78KB
matlab_chelsea_float32.npy 1.55MB
chelsea_uint64.npy 3.1MB
sine_int64.npy 78KB
sine_float64.npy 78KB
matlab_chelsea_int8.npy 396KB
matlab_sine_int8.npy 10KB
chelsea_int32.npy 1.55MB
matlab_sine_uint64.npy 78KB
matlab_sine_uint32.npy 39KB
chelsea_uint16.npy 793KB
matlab_chelsea_uint64.npy 3.1MB
matlab_chelsea_int16.npy 793KB
sine_uint16.npy 20KB
matlab_chelsea_float64.npy 3.1MB
chelsea_int16.npy 793KB
matlab_sine_uint8.npy 10KB
chelsea_uint32.npy 1.55MB
chelsea_int8.npy 396KB
test.npy 160B
matlab_chelsea_int32.npy 1.55MB
chelsea_uint8.npy 396KB
sine_int32.npy 39KB
chelsea_float64.npy 3.1MB
matlab_sine_float32.npy 39KB
matlab_sine_float64.npy 78KB
sine_float32.npy 39KB
matlab_chelsea_int64.npy 3.1MB
sine_int16.npy 20KB
matlab_sine_int32.npy 39KB
matlab_chelsea_uint32.npy 1.55MB
matlab_chelsea_uint16.npy 793KB
chelsea_int64.npy 3.1MB
sine_uint8.npy 10KB
sine_uint32.npy 39KB
sine_int8.npy 10KB
matlab_chelsea_uint8.npy 396KB
test_readNPY.m 1KB
test_npy_roundtrip.py 3KB
npy.ipynb 2.34MB
examples
exampleMemmap.m 755B
.gitignore 38B
npy-matlab
constructNPYheader.m 3KB
datToNPY.m 1KB
writeNPY.m 470B
readNPY.m 878B
readNPYheader.m 2KB
共 53 条
- 1
资源评论
小正太浩二
- 粉丝: 196
- 资源: 5920
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功