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

2d arrays get converted into string representation by write_file #135

Open
anchal-physics opened this issue Sep 25, 2023 · 0 comments
Open
Labels

Comments

@anchal-physics
Copy link

For 2d arrays, the YAML.write_file() function writes a string version of the julia data which is parsed wrongly when read back by the load_file function.

Specs

Package version:
YAML v0.4.9

Julia and machine:

julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e909 (2023-07-05 09:39 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 12 × Apple M2 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
  Threads: 1 on 8 virtual cores

To reproduce:

using YAML
a = rand(10, 10)
d = Dict([("2darray", a)])
println("Writing to file:")
println(typeof(d["2darray"]), " of size ", size(d["2darray"]))
YAML.write_file("2darray.yaml", d)

println("Reading from file:")
d_read = YAML.load_file("2darray.yaml")
println(d_read["2darray"])
println(typeof(d_read["2darray"]), " of length ", length(d_read["2darray"]))

Suggested workaround

using YAML
using JSON
a = rand(3, 3)
b = rand(3)
d = Dict([("2darray", JSON.sprint(a)), ("1darray", b)])
println("Writing to file:")
println("2d array:")
println(d["2darray"])
# println(typeof(d["2darray"]), " of size ", size(d["2darray"]))
YAML.write_file("2darray_json.yaml", d)
println("Reading from file:")
d_read = YAML.load_file("2darray_json.yaml")
for (key, val) in d_read
    if isa(val, String)
        if startswith(val, "[[")
            d_read[key] = mapreduce(permutedims, vcat, JSON.parse(val))
        end
    end
end
println("2d array:")
println(d_read["2darray"])
println(typeof(d_read["2darray"]), " of size ", size(d_read["2darray"]))
println("1d array:")
println(d_read["1darray"])
println(typeof(d_read["1darray"]), " of length ", length(d_read["1darray"]))
@kescobo kescobo added the bug label Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants