JuliaIO / BSON.jl

Other
158 stars 39 forks source link

FileIO interface #31

Closed JonasIsensee closed 5 years ago

JonasIsensee commented 5 years ago

Hi, I implemented the relevant functions for BSON to work with FileIO.

julia> using FileIO

julia> r = rand(10);

julia> str = "Hallo Welt!"
"Hallo Welt!"

julia> num = 42
42

julia> FileIO.save("test.bson", Dict(:r=>r, :str=>str))

julia> FileIO.save("test.bson", :r,r, :str, str, :num, num)

julia> d = FileIO.load("test.bson")
Dict{Symbol,Any} with 3 entries:
  :num => 42
  :str => "Hallo Welt!"
  :r   => [0.19408, 0.929891, 0.890522, 0.854719, 0.0294219, 0.418311, 0.391583, 0.845015, 0.312361, 0.63644]

julia> a,b = FileIO.load("test.bson", :str, :num)
("Hallo Welt!", 42)

The syntax bson("test.bson", a=1, b=2) sadly cannot be ported as this syntax is already used for working with |>. Instead I implemented it to look like FileIO.save("test.bson", :a, 1, :b, 2).

To add tests for this relatively simple functionality I would need to make FileIO a dependency so I haven't done this so far.

The relevant PR to add the format to FileIO as well is here: https://github.com/JuliaIO/FileIO.jl/pull/224

closes #24

Datseris commented 5 years ago

Tests pass, the fails are on nightly only.

@MikeInnes can you please have a look and merge if possible? It would make our scientific work much, much easier.

MikeInnes commented 5 years ago

Seems reasonable to me. Can I suggest using Dict-style syntax for the keyword arguments? e.g. FileIO.save("test.bson", :r=>r, :str=>tr, :num=>num)

JonasIsensee commented 5 years ago

Good idea. I added the syntax you suggested. There is however one odd quirk:

julia> save("test.bson", :a => "test1", :b => 2, :c => rand(5))

julia> load("test.bson")
Dict{Symbol,Any} with 3 entries:
  :a => "test1"
  :b => 2
  :c => [0.420764, 0.00832725, 0.478815, 0.875597, 0.170539]

julia> save("test.bson", :a => "test1", :b => 2, :c, rand(5))

julia> load("test.bson")
Dict{Any,Any} with 2 entries:
  :c          => [0.259777, 0.31934, 0.468751, 0.62964, 0.213068]
  :a=>"test1" => :b=>2

In the second case, the first pair is used as key for the second pair, because the third set was not given as a pair.

MikeInnes commented 5 years ago

Best to just remove the conflicting syntax there and only allow pairs.

JonasIsensee commented 5 years ago

I'm not a big fan of it either. I added it in the first place, as this is the syntax supported by both JLD and JLD2. If all support the same IO syntax, one just needs to exchange the file-type suffix to switch between data formats. It's up to you

MikeInnes commented 5 years ago

Maybe the JLD syntax predates pairs? It seems like an odd choice otherwise. Maybe the newer-style syntax can be added there (you can do it from your own code at worst).

Datseris commented 5 years ago

@MikeInnes since this is merged can you please tag a new release as well? it is necessary for working with FileIO.