Skip to content

Support for h5read #637

Open
Open
@mtfishman

Description

@mtfishman

@emstoudenmire probably you have investigated this but I was interested in the current state of h5read when looking over the docs you moved over here: #634.

Unfortunately, the more compact notation for reading using h5read doesn't work for some (maybe all?) of our types:

julia> using ITensors, ITensors.HDF5

julia> i = Index(2)
(dim=2|id=826)

julia> h5read("myfile.h5", "i" => Index)
ERROR: MethodError: no method matching h5read(::String, ::Pair{String, UnionAll})
Closest candidates are:
  h5read(::Any, ::AbstractString; pv...) at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:487
  h5read(::Any, ::Pair{var"#s53", DataType} where var"#s53"<:AbstractString; pv...) at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:500
  h5read(::Any, ::AbstractString, ::Tuple{Vararg{Union{Colon, Int64, AbstractRange{Int64}}, N} where N}; pv...) at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:513
Stacktrace:
 [1] top-level scope
   @ REPL[18]:1

julia> h5read("myfile.h5", "i" => Index{Int})
ERROR: MethodError: no method matching read(::HDF5.Group, ::Type{Index{Int64}})
Closest candidates are:
  read(::Union{HDF5.Attribute, HDF5.Dataset}, ::Type{T}, ::Any...) where T at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:1193
  read(::Union{HDF5.File, HDF5.Group}, ::Pair{var"#s53", DataType} where var"#s53"<:AbstractString; pv...) at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:1165
  read(::Union{HDF5.File, HDF5.Group}, ::AbstractString; pv...) at /home/mfishman/.julia/packages/HDF5/iH4LA/src/HDF5.jl:1158
  ...
Stacktrace:
 [1] h5read(filename::String, name_type_pair::Pair{String, DataType}; pv::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{0 Indices
, Tuple{}}})
   @ HDF5 ~/.julia/packages/HDF5/iH4LA/src/HDF5.jl:505
 [2] h5read(filename::String, name_type_pair::Pair{String, DataType})
   @ HDF5 ~/.julia/packages/HDF5/iH4LA/src/HDF5.jl:501
 [3] top-level scope
   @ REPL[19]:1

There are two issues going on here. In the first issue, I believe it is more of a limitation on the HDF5.jl side, where Index is technically an abstract type Index{T} where {T} with typeof(Index) === UnionAll, while h5read only accepts a concrete type like Index{Int} with typeof(Index{Int}) === DataType. In the second issue, the problem is more on our side, since we don't allow specifying a concrete type in this case, so we could expand the definition here to accept a more general Type{<: Index}. However, this fix is still inconvenient, since we don't want people having to specify the particular concrete Index{Int} type.

The version without the explicit type runs:

julia> h5read("myfile.h5", "i")
Dict{String, Any} with 5 entries:
  "dim"  => 2
  "id"   => 0x1432306146b0bb12
  "plev" => 0
  "tags" => Dict{String, Any}("tags"=>"")
  "dir"  => 0

but of course doesn't output an Index, just some default data structure. It is difficult to find information about how to make this work for custom types, but my understanding from this comment is that you may need to define a custom file format. This makes a certain amount of sense, since then the file format can be determined and dispatched on, and without that there doesn't seem to be a way to tell HDF5.jl to do some special parsing of the files to search for ITensor-specific type information. @emstoudenmire, have you investigated this? That seems like the strategy the packages like JLD.jl take for saving and loading custom types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions