fhs / NPZ.jl

A Julia package that provides support for reading and writing Numpy .npy and .npz files
Other
117 stars 16 forks source link

Issue in writing dict type #38

Open Himscipy opened 3 years ago

Himscipy commented 3 years ago

Hi,

Thank you for the NPZ.jl package. I am using the package as follows

function SaveNPZ(loc,ROMParam, U)
    if ispath(loc) == false
        println("Current working directory \n", pwd() )
        # Make the directory....
        mkdir(loc)
    end
    tmp_file  =  joinpath(loc,"mydata.npz") #string( loc,"Zone_",string(j) )loc 
    println(tmp_file)
    npzwrite(tmp_file, Dict("ROMParam" => ROMParam, "U" => U) )
end

I am getting following error at the runtime not sure what I am doing wrong..novice to julia...!!

ERROR: LoadError: MethodError: no method matching npzwritearray(::ZipFile.WritableFile, ::Dict{Any,Any}) Closest candidates are: npzwritearray(::IO, !Matched::AbstractArray{UInt8,N} where N, !Matched::DataType, !Matched::Array{Int64,1}) at /Users/shar306/.julia/packages/NPZ/OBINJ/src/NPZ.jl:273 npzwritearray(::IO, !Matched::AbstractArray{T,N} where N) where T at /Users/shar306/.julia/packages/NPZ/OBINJ/src/NPZ.jl:298 npzwritearray(::IO, !Matched::T) where T<:Number at /Users/shar306/.julia/packages/NPZ/OBINJ/src/NPZ.jl:302

jishnub commented 3 years ago

Can you provide some example values of ROMParam and U that result in this error? This seems to work for me on master and julia 1.5.2:

julia> function SaveNPZ(loc,ROMParam, U)
           if ispath(loc) == false
               #println("Current working directory \n", pwd() )
               # Make the directory....
               mkdir(loc)
           end
           tmp_file  =  joinpath(loc,"mydata.npz") #string( loc,"Zone_",string(j) )loc 
           println(tmp_file)
           npzwrite(tmp_file, Dict("ROMParam" => ROMParam, "U" => U) )
       end
SaveNPZ (generic function with 1 method)

julia> SaveNPZ("data", 1, 1)
data/mydata.npz

julia> npzread("data/mydata.npz")
Dict{String,Int64} with 2 entries:
  "U"        => 1
  "ROMParam" => 1
Himscipy commented 3 years ago

Hi Jishnub, The ROMParam and U are dictionaries for me. The Julia version I am using is 1.4.2.

jishnub commented 3 years ago

I'm not sure if this package supports writing dictionaries or other non-array python objects. Meanwhile you may use PyCall to do this:

julia> np = pyimport("numpy");

julia> np.savez("temp.npz", U = Dict("a"=>4))

julia> f = np.load("temp.npz");

julia> f.get("U")
0-dimensional Array{PyObject,0}:
PyObject {'a': 4}

julia> f.get("U")[].get("a")
4
Himscipy commented 3 years ago

Hi Jishnub, Thank you for the inputs. I will take a note of it. I addressed the limitation by using hdf5 format though.