Skip to content

Commit

Permalink
Merge pull request #106 from mforets/mforets/104
Browse files Browse the repository at this point in the history
Fix some compatibility issues
  • Loading branch information
yuyichao committed Dec 15, 2018
2 parents 5baca58 + ed58d9a commit 1c3675c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
20 changes: 13 additions & 7 deletions src/MAT_HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ using HDF5
using Compat
using Compat.SparseArrays

@static if VERSION < v"0.7-"
_finalizer(f, x) = finalizer(x, f)
else
_finalizer = finalizer
end

import Base: read, write, close
import HDF5: names, exists, HDF5ReferenceObj, HDF5BitsKind

Expand All @@ -47,7 +53,7 @@ mutable struct MatlabHDF5File <: HDF5.DataFile
function MatlabHDF5File(plain, toclose::Bool=true, writeheader::Bool=false, refcounter::Int=0)
f = new(plain, toclose, writeheader, refcounter)
if toclose
finalizer(f, close)
_finalizer(close, f)
end
f
end
Expand Down Expand Up @@ -130,9 +136,9 @@ function read_complex(dtype::HDF5Datatype, dset::HDF5Dataset, ::Type{Array{T}})
HDF5.h5d_read(dset.id, memtype.id, HDF5.H5S_ALL, HDF5.H5S_ALL, HDF5.H5P_DEFAULT, buf)

if T == Float32
d = reinterpret(Complex64, dbuf, sz)
d = reinterpret(ComplexF32, dbuf, sz)
elseif T == Float64
d = reinterpret(Complex128, dbuf, sz)
d = reinterpret(ComplexF64, dbuf, sz)
else
d = slicedim(dbuf, 1, 1) + im * slicedim(dbuf, 1, 2)
end
Expand All @@ -156,7 +162,7 @@ function m_read(dset::HDF5Dataset)
end
else
T = mattype == "canonical empty" ? Union{} : str2eltype_matlab[mattype]
return Array{T}(dims...)
return Array{T}(undef, dims...)
end
end

Expand All @@ -165,7 +171,7 @@ function m_read(dset::HDF5Dataset)
if mattype == "cell"
# Cell arrays, represented as an array of refs
refs = read(dset, Array{HDF5ReferenceObj})
out = Array{Any}(size(refs))
out = Array{Any}(undef, size(refs))
f = file(dset)
for i = 1:length(refs)
dset = f[refs[i]]
Expand Down Expand Up @@ -466,7 +472,7 @@ function m_write(mfile::MatlabHDF5File, parent::HDF5Parent, name::String, data::
close(a)
end
# Write the items to the reference group
refs = Array{HDF5ReferenceObj}(size(data))
refs = Array{HDF5ReferenceObj}(undef, size(data))
for i = 1:length(data)
mfile.refcounter += 1
itemname = string(mfile.refcounter)
Expand All @@ -492,7 +498,7 @@ end

# Check that keys are valid for a struct, and convert them to an array of ASCIIStrings
function check_struct_keys(k::Vector)
asckeys = Vector{String}(length(k))
asckeys = Vector{String}(undef, length(k))
for i = 1:length(k)
key = k[i]
if !isa(key, AbstractString)
Expand Down
12 changes: 6 additions & 6 deletions src/MAT_v5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function read_data(f::IO, swap_bytes::Bool, ::Type{T}, dimensions::Vector{Int32}
end

function read_cell(f::IO, swap_bytes::Bool, dimensions::Vector{Int32})
data = Array{Any}(convert(Vector{Int}, dimensions)...)
data = Array{Any}(undef, convert(Vector{Int}, dimensions)...)
for i = 1:length(data)
(ignored_name, data[i]) = read_matrix(f, swap_bytes)
end
Expand All @@ -168,11 +168,11 @@ function read_struct(f::IO, swap_bytes::Bool, dimensions::Vector{Int32}, is_obje
n_fields = div(length(field_names), field_length)

# Get field names as strings
field_name_strings = Vector{String}(n_fields)
field_name_strings = Vector{String}(undef, n_fields)
n_el = prod(dimensions)
for i = 1:n_fields
sname = field_names[(i-1)*field_length+1:i*field_length]
index = findfirst(sname, 0)
index = something(findfirst(iszero, sname), 0)
field_name_strings[i] = String(index == 0 ? sname : sname[1:index-1])
end

Expand All @@ -190,7 +190,7 @@ function read_struct(f::IO, swap_bytes::Bool, dimensions::Vector{Int32}, is_obje
else
# Read multiple structs into a dict of arrays
for field_name in field_name_strings
data[field_name] = Array{Any}(dimensions...)
data[field_name] = Array{Any}(undef, dimensions...)
end
for i = 1:n_el
for field_name in field_name_strings
Expand Down Expand Up @@ -257,7 +257,7 @@ function read_string(f::IO, swap_bytes::Bool, dimensions::Vector{Int32})
if dimensions[1] <= 1
data = String(chars)
else
data = Vector{String}(dimensions[1])
data = Vector{String}(undef, dimensions[1])
for i = 1:dimensions[1]
data[i] = rstrip(String(chars[i:dimensions[1]:end]))
end
Expand Down Expand Up @@ -310,7 +310,7 @@ function read_matrix(f::IO, swap_bytes::Bool)
# a = {[], [], []}
# then MATLAB does not save the empty cells as zero-byte matrices. To avoid
# surprises, we produce an empty array in both cases.
return ("", Matrix{Union{}}(0, 0))
return ("", Matrix{Union{}}(undef, 0, 0))
end

flags = read_element(f, swap_bytes, UInt32)
Expand Down
18 changes: 12 additions & 6 deletions test/read.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
using MAT, Base.Test
using MAT

@static if VERSION < v"0.7-"
using Base.Test
else
using Test
end

function check(filename, result)
matfile = matopen(filename)
Expand Down Expand Up @@ -45,7 +51,7 @@ end

global format
for _format in ["v6", "v7", "v7.3"]
format = _format
global format = _format
cd(joinpath(dirname(@__FILE__), format))

result = Dict(
Expand All @@ -72,7 +78,7 @@ for _format in ["v6", "v7", "v7.3"]
end

result = Dict(
"imaginary" => Complex128[1 -1 1+im 1-im -1+im -1-im im]
"imaginary" => ComplexF64[1 -1 1+im 1-im -1+im -1-im im]
)
check("complex.mat", result)

Expand All @@ -89,7 +95,7 @@ for _format in ["v6", "v7", "v7.3"]
"a1x2" => [1.0 2.0],
"a2x1" => zeros(2, 1)+[1.0, 2.0],
"a2x2" => [1.0 3.0; 4.0 2.0],
"a2x2x2" => cat(3, [1.0 3.0; 4.0 2.0], [1.0 2.0; 3.0 4.0]),
"a2x2x2" => Compat.cat([1.0 3.0; 4.0 2.0], [1.0 2.0; 3.0 4.0], dims=3),
"empty" => zeros(0, 0),
"string" => "string"
)
Expand Down Expand Up @@ -127,8 +133,8 @@ for _format in ["v6", "v7", "v7.3"]
check("empty_cells.mat", result)

result = Dict(
"sparse_empty" => sparse(Matrix{Float64}(0, 0)),
"sparse_eye" => speye(20),
"sparse_empty" => sparse(Matrix{Float64}(undef, 0, 0)),
"sparse_eye" => sparse(1.0I, 20, 20),
"sparse_logical" => SparseMatrixCSC{Bool,Int64}(5, 5, [1:6;], [1:5;], fill(true, 5)),
"sparse_random" => sparse([0 6. 0; 8. 0 1.; 0 0 9.]),
"sparse_complex" => sparse([0 6. 0; 8. 0 1.; 0 0 9.]*(1. + 1.0im)),
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
using Compat

include("read.jl")
include("write.jl")
12 changes: 6 additions & 6 deletions test/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ test_write(Dict(
))

test_write(Dict(
"Complex128" => [1.0 -1.0 1.0+1.0im 1.0-1.0im -1.0+1.0im -1.0-1.0im 1.0im],
"ComplexF64" => [1.0 -1.0 1.0+1.0im 1.0-1.0im -1.0+1.0im -1.0-1.0im 1.0im],
"ComplexPair" => [1 2-3im 4+5im]
))
test_write(Dict("Complex128" => 1.0im, "ComplexPair" => 2-3im))
test_write(Dict("ComplexF64" => 1.0im, "ComplexPair" => 2-3im))

test_write(Dict(
"simple_string" => "the quick brown fox",
Expand All @@ -50,7 +50,7 @@ test_write(Dict(
"a1x2" => [1.0 2.0],
"a2x1" => zeros(2, 1)+[1.0, 2.0],
"a2x2" => [1.0 3.0; 4.0 2.0],
"a2x2x2" => cat(3, [1.0 3.0; 4.0 2.0], [1.0 2.0; 3.0 4.0]),
"a2x2x2" => Compat.cat([1.0 3.0; 4.0 2.0], [1.0 2.0; 3.0 4.0], dims=3),
"empty" => zeros(0, 0),
"string" => "string"
))
Expand All @@ -69,8 +69,8 @@ test_write(Dict(
))

test_write(Dict(
"sparse_empty" => sparse(Matrix{Float64}(0, 0)),
"sparse_eye" => speye(20),
"sparse_empty" => sparse(Matrix{Float64}(undef, 0, 0)),
"sparse_eye" => sparse(1.0I, 20, 20),
"sparse_logical" => SparseMatrixCSC{Bool,Int64}(5, 5, [1:6;], [1:5;], fill(true, 5)),
"sparse_random" => sparse([0 6. 0; 8. 0 1.; 0 0 9.]),
"sparse_complex" => sparse([0 6. 0; 8. 0 1.; 0 0 9.]*(1. + 1.0im)),
Expand Down Expand Up @@ -100,7 +100,7 @@ close(fid)
using DataStructures
sd = SortedDict(Dict(
"uint16" => UInt16(1),
"Complex128" => [1.0 -1.0 1.0+1.0im 1.0-1.0im -1.0+1.0im -1.0-1.0im 1.0im],
"ComplexF64" => [1.0 -1.0 1.0+1.0im 1.0-1.0im -1.0+1.0im -1.0-1.0im 1.0im],
"simple_string" => "the quick brown fox",
"a1x2" => [1.0 2.0],
"sparse_empty" => sparse(Matrix{Float64}(0, 0))
Expand Down

0 comments on commit 1c3675c

Please sign in to comment.