Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ArviZPythonPlots"
uuid = "4a6e88f0-2c8e-11ee-0601-e94153f0eada"
authors = ["Seth Axen <seth@sethaxen.com>"]
version = "0.1.11"
version = "0.1.12"

[deps]
CondaPkg = "992eb4ea-22a4-4c89-a5bb-47a3300528ab"
Expand All @@ -22,7 +22,7 @@ DimensionalData = "0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29"
InferenceObjects = "0.4.13"
Markdown = "1"
OrderedCollections = "1"
PosteriorStats = "0.3"
PosteriorStats = "0.3, 0.4"
PythonCall = "0.9"
PythonPlot = "1"
Random = "1"
Expand Down
50 changes: 27 additions & 23 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ function PythonCall.Py(d::PSISLOOResult)
return arviz.stats.ELPDData(; data, index)
end

function PythonCall.Py(d::WAICResult)
estimates = elpd_estimates(d)
pointwise = elpd_estimates(d; pointwise=true)
ds = convert_to_dataset((waic_i=pointwise.elpd,))
pyds = PythonCall.Py(ds)
entries = (
elpd_waic=estimates.elpd,
se=estimates.se_elpd,
p_waic=estimates.p,
n_samples="unknown",
n_data_points=length(pointwise.elpd),
warning=false,
waic_i=pyds.waic_i,
scale="log",
)
data = pylist(values(entries))
index = pylist(map(pystr, keys(entries)))
return arviz.stats.ELPDData(; data, index)
@static if isdefined(PosteriorStats, :WAICResult)
function PythonCall.Py(d::WAICResult)
estimates = elpd_estimates(d)
pointwise = elpd_estimates(d; pointwise=true)
ds = convert_to_dataset((waic_i=pointwise.elpd,))
pyds = PythonCall.Py(ds)
entries = (
elpd_waic=estimates.elpd,
se=estimates.se_elpd,
p_waic=estimates.p,
n_samples="unknown",
n_data_points=length(pointwise.elpd),
warning=false,
waic_i=pyds.waic_i,
scale="log",
)
data = pylist(values(entries))
index = pylist(map(pystr, keys(entries)))
return arviz.stats.ELPDData(; data, index)
end
end

function rekey(nt::NamedTuple, old_new_keys::Pair...)
Expand All @@ -48,14 +50,16 @@ end
function PythonCall.Py(mc::ModelComparisonResult)
table = Tables.columntable(mc)
se_pairs = (:se_elpd => :se, :se_elpd_diff => :dse)
est_pairs = if eltype(mc.elpd_result) <: PSISLOOResult
(:elpd => :elpd_loo, :p => :p_loo)
elseif eltype(mc.elpd_result) <: WAICResult
(:elpd => :elpd_waic, :p => :p_waic)
end
est_pairs = _estimate_name_map(eltype(mc.elpd_result))
nrows = Tables.rowcount(table)
new_cols = (warning=fill(false, nrows), scale=fill("log", nrows))
table_new = merge(rekey(table, est_pairs..., se_pairs...), new_cols)
pdf = topandas(Val(:DataFrame), table_new; index_name="name")
return pdf
end

_estimate_name_map(::Type{<:PSISLOOResult}) = (:elpd => :elpd_loo, :p => :p_loo)

@static if isdefined(PosteriorStats, :WAICResult)
_estimate_name_map(::Type{<:WAICResult}) = (:elpd => :elpd_waic, :p => :p_waic)
end
5 changes: 2 additions & 3 deletions test/test_conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using Test
@test pyconvert(Array{Float64}, py_loo_result.pareto_k.values) ≈
pyconvert(Array{Float64}, loo_py_result.pareto_k.values) rtol = 1e-1
end
@testset "WAICResult" begin
isdefined(PosteriorStats, :WAICResult) && @testset "WAICResult" begin
idata = load_example_data("centered_eight")
waic_result = waic(idata)
waic_py_result = ArviZPythonPlots.arviz.waic(idata; pointwise=true)
Expand All @@ -36,8 +36,7 @@ using Test
)
@test pyconvert(Float64, py_waic_result.elpd_waic) ≈
pyconvert(Float64, waic_py_result.elpd_waic) rtol = 1e-3
@test pyconvert(Float64, py_waic_result.se) ≈ pyconvert(Float64, waic_py_result.se) rtol =
1e-1
@test pyconvert(Float64, py_waic_result.se) ≈ pyconvert(Float64, waic_py_result.se) rtol = 1e-1
@test pyconvert(Float64, py_waic_result.p_waic) ≈
pyconvert(Float64, waic_py_result.p_waic) rtol = 1e-3
@test pyconvert(Array{Float64}, py_waic_result.waic_i.values) ≈
Expand Down
Loading