Skip to content

Commit

Permalink
Remove read and write (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored Sep 17, 2020
1 parent f356e99 commit 55dc51f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
keywords = ["markov chain monte carlo", "probablistic programming"]
license = "MIT"
desc = "Chain types and utility functions for MCMC simulations."
version = "4.2.0"
version = "4.2.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,20 @@ corner(c::Chains, [:A, :B])

### Saving and Loading Chains

Chains objects can be serialized and deserialized using `read` and `write`.
Like any Julia object, a `Chains` object can be saved using `Serialization.serialize`
and loaded back by `Serialization.deserialize` as identical as possible.
Note, however, that in general
[this process will not work if the reading and writing are done by different versions of Julia, or an instance of Julia with a different system image](https://docs.julialang.org/en/v1/stdlib/Serialization/#Serialization-1).
You might want to consider [JLSO](https://github.com/invenia/JLSO.jl) for saving metadata
such as the Julia version and the versions of all packages installed as well.

```julia
# Save a chain.
write("chain-file.jls", chn)
using Serialization
serialize("chain-file.jls", chn)

# Read a chain.
chn2 = read("chain-file.jls", Chains)
chn2 = deserialize("chain-file.jls")
```

### Exporting Chains
Expand Down
26 changes: 25 additions & 1 deletion src/MCMCChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import TableTraits
import IteratorInterfaceExtensions

using LinearAlgebra: diag, dot, BlasReal
import Serialization: serialize, deserialize
import Random
import Serialization
import Statistics: std, cor, mean, var, mean!

export Chains, chains, chainscat
Expand Down Expand Up @@ -77,4 +77,28 @@ include("plot.jl")
include("tables.jl")
include("rstar.jl")

# deprecations
# TODO: Remove dependency on Serialization if this deprecation is removed
@static if VERSION < v"1.1"
Base.@deprecate read(
f::AbstractString,
::Type{T}
) where {T<:Chains} open(Serialization.deserialize, f, "r") false
Base.@deprecate write(
f::AbstractString,
c::Chains
) open(f, "w") do io
Serialization.serialize(io, c)
end false
else
Base.@deprecate read(
f::AbstractString,
::Type{T}
) where {T<:Chains} Serialization.deserialize(f) false
Base.@deprecate write(
f::AbstractString,
c::Chains
) Serialization.serialize(f, c) false
end

end # module
11 changes: 0 additions & 11 deletions src/fileio.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
#################### File I/O ####################

function Base.read(name::AbstractString, ::Type{T}) where {T<:Chains}
c = open(deserialize, name, "r")
isa(c, T) || throw(TypeError(:open, "read(\"$name\", $T)", T, c))
return c
end

function Base.write(name::AbstractString, c::Chains)
open(file -> serialize(file, c), name, "w")
end


function readcoda(output::AbstractString, index::AbstractString)
out = readdlm(output, Any)
ind = readdlm(index, Any)
Expand Down
9 changes: 7 additions & 2 deletions test/serialization_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using MCMCChains
using Distributions

using Random
using Serialization
using Test

Random.seed!(20)
Expand All @@ -27,8 +28,12 @@ ProjDir = mktempdir()
end
chn1 = Chains(vals, ["m", "s"])

write(joinpath(ProjDir, "chn1.jls"), chn1)
chn2 = read(joinpath(ProjDir, "chn1.jls"), Chains)
# Julia 1.0 doesn't support `serialize(::AbstractString, value)`
# and `deserialize(::AbstractString)`
open(joinpath(ProjDir, "chn1.jls"), "w") do io
serialize(io, chn1)
end
chn2 = open(deserialize, joinpath(ProjDir, "chn1.jls"), "r")

open(joinpath(ProjDir, "chn1.txt"), "w") do io
describe(io, chn1);
Expand Down

2 comments on commit 55dc51f

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21539

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.2.1 -m "<description of version>" 55dc51f12e89ed672809c1439ce81af19045232d
git push origin v4.2.1

Please sign in to comment.