Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in writing dict type #38

Open
Himscipy opened this issue Aug 21, 2020 · 4 comments
Open

Issue in writing dict type #38

Himscipy opened this issue Aug 21, 2020 · 4 comments

Comments

@Himscipy
Copy link

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
Copy link
Contributor

jishnub commented Nov 15, 2020

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
Copy link
Author

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

@jishnub
Copy link
Contributor

jishnub commented Nov 18, 2020

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants