From 184b32c6de5c27f59741ebfa2760740c288f99ea Mon Sep 17 00:00:00 2001 From: David AW Barton Date: Thu, 20 Dec 2018 23:13:09 +0000 Subject: [PATCH 1/5] Add stdlibs --- src/MAT.jl | 1 + src/MAT_v5.jl | 1 + test/runtests.jl | 2 ++ 3 files changed, 4 insertions(+) diff --git a/src/MAT.jl b/src/MAT.jl index 5b55d2a..5baac22 100644 --- a/src/MAT.jl +++ b/src/MAT.jl @@ -27,6 +27,7 @@ VERSION >= v"0.4.0-dev+6521" && __precompile__() module MAT using HDF5, Compat +using Compat.SparseArrays include("MAT_HDF5.jl") include("MAT_v5.jl") diff --git a/src/MAT_v5.jl b/src/MAT_v5.jl index 92b4ceb..7680a23 100644 --- a/src/MAT_v5.jl +++ b/src/MAT_v5.jl @@ -27,6 +27,7 @@ module MAT_v5 using Libz, BufferedStreams, HDF5, Compat +using Compat.SparseArrays import Base: read, write, close import HDF5: names, exists diff --git a/test/runtests.jl b/test/runtests.jl index 9ddf796..3cf30b4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,6 @@ using Compat +using Compat.SparseArrays +using Compat.LinearAlgebra include("read.jl") include("write.jl") From 3d22435e9d9e6daf68fc28cb278e8ac7a642b6a4 Mon Sep 17 00:00:00 2001 From: David AW Barton Date: Thu, 20 Dec 2018 23:14:29 +0000 Subject: [PATCH 2/5] Fixes to logical arrays --- src/MAT_HDF5.jl | 9 +++++++-- src/MAT_v5.jl | 17 +++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index d9707eb..dcdf005 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -618,8 +618,13 @@ function read(obj::HDF5Object, ::Type{Bool}) tf > 0 end function read(obj::HDF5Object, ::Type{Array{Bool}}) - tf = read(obj, Array{UInt8}) - reinterpret(Bool, tf) + if HDF5.isnull(obj) + return Bool[] + end + # Use the low-level HDF5 API to put the data directly into a Bool array + tf = Array{Bool}(undef, size(obj)) + HDF5.h5d_read(obj.id, HDF5.hdf5_type_id(UInt8), tf, obj.xfer) + tf end ## Utilities for handling complex numbers diff --git a/src/MAT_v5.jl b/src/MAT_v5.jl index 7680a23..5d5a57d 100644 --- a/src/MAT_v5.jl +++ b/src/MAT_v5.jl @@ -137,6 +137,9 @@ end function read_data(f::IO, swap_bytes::Bool, ::Type{T}, dimensions::Vector{Int32}) where {T} (dtype, nbytes, hbytes) = read_header(f, swap_bytes) read_type = READ_TYPES[dtype] + if (read_type == UInt8) && (T === Bool) + read_type = Bool + end read_array = any(dimensions .!= 1) if sizeof(read_type)*prod(dimensions) != nbytes @@ -329,12 +332,14 @@ function read_matrix(f::IO, swap_bytes::Bool) elseif class == mxCHAR_CLASS && length(dimensions) <= 2 data = read_string(f, swap_bytes, dimensions) else - convert_type = CONVERT_TYPES[class] - data = read_data(f, swap_bytes, convert_type, dimensions) - if (flags[1] & (1 << 11)) != 0 # complex - data = complex_array(data, read_data(f, swap_bytes, convert_type, dimensions)) - elseif (flags[1] & (1 << 9)) != 0 # logical - data = reinterpret(Bool, round_uint8(data)) + if (flags[1] & (1 << 9)) != 0 # logical + data = read_data(f, swap_bytes, Bool, dimensions) + else + convert_type = CONVERT_TYPES[class] + data = read_data(f, swap_bytes, convert_type, dimensions) + if (flags[1] & (1 << 11)) != 0 # complex + data = complex_array(data, read_data(f, swap_bytes, convert_type, dimensions)) + end end end From 7bb44adc413ee704cd1feefd2bf1145f5b6f5247 Mon Sep 17 00:00:00 2001 From: David AW Barton Date: Thu, 20 Dec 2018 23:14:53 +0000 Subject: [PATCH 3/5] Fixes to complex arrays --- src/MAT_HDF5.jl | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index dcdf005..98ca460 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -130,19 +130,9 @@ function read_complex(dtype::HDF5Datatype, dset::HDF5Dataset, ::Type{Array{T}}) end memtype = build_datatype_complex(T) sz = size(dset) - dbuf = Array{T}(2, sz...) - st = sizeof(T) - buf = reinterpret(UInt8, dbuf, (2 * st, sz...)) - HDF5.h5d_read(dset.id, memtype.id, HDF5.H5S_ALL, HDF5.H5S_ALL, HDF5.H5P_DEFAULT, buf) - - if T == Float32 - d = reinterpret(ComplexF32, dbuf, sz) - elseif T == Float64 - d = reinterpret(ComplexF64, dbuf, sz) - else - d = slicedim(dbuf, 1, 1) + im * slicedim(dbuf, 1, 2) - end - length(d) == 1 ? d[1] : d + dbuf = Array{Complex{T}}(undef, sz...) + HDF5.h5d_read(dset.id, memtype.id, HDF5.H5S_ALL, HDF5.H5S_ALL, HDF5.H5P_DEFAULT, vec(dbuf)) + length(dbuf) == 1 ? dbuf[1] : dbuf end function m_read(dset::HDF5Dataset) From 1d6495d18c663d73877915f94ee827f5b6308043 Mon Sep 17 00:00:00 2001 From: David AW Barton Date: Thu, 20 Dec 2018 23:22:11 +0000 Subject: [PATCH 4/5] Fixed string handling --- src/MAT_HDF5.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MAT_HDF5.jl b/src/MAT_HDF5.jl index 98ca460..952152c 100644 --- a/src/MAT_HDF5.jl +++ b/src/MAT_HDF5.jl @@ -596,9 +596,9 @@ function read(obj::HDF5Object, ::Type{MatlabString}) data = reshape(data, sz[2:end]) end if ndims(data) == 1 - return convert(String, convert(Vector{Char}, data)) + return String(convert(Vector{Char}, data)) elseif ndims(data) == 2 - return datap = String[rstrip(convert(String, convert(Vector{Char}, vec(data[i, :])))) for i = 1:size(data, 1)] + return datap = String[rstrip(String(convert(Vector{Char}, vec(data[i, :])))) for i = 1:size(data, 1)] else return data end From 55fb265f6368989731fa2635e4f66e5be5d5cb8d Mon Sep 17 00:00:00 2001 From: David Barton Date: Mon, 14 Jan 2019 15:46:51 +0000 Subject: [PATCH 5/5] Change == to === --- src/MAT_v5.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MAT_v5.jl b/src/MAT_v5.jl index 5d5a57d..a449146 100644 --- a/src/MAT_v5.jl +++ b/src/MAT_v5.jl @@ -137,7 +137,7 @@ end function read_data(f::IO, swap_bytes::Bool, ::Type{T}, dimensions::Vector{Int32}) where {T} (dtype, nbytes, hbytes) = read_header(f, swap_bytes) read_type = READ_TYPES[dtype] - if (read_type == UInt8) && (T === Bool) + if (read_type === UInt8) && (T === Bool) read_type = Bool end