orlox / SideKicks.jl

Statistical Inference to DEtermine KICKS on compact objects
GNU General Public License v3.0
1 stars 0 forks source link

Saving results #27

Open orlox opened 1 week ago

orlox commented 1 week ago

One additional thing we need is the capacity to save results in a machine readable way. It would be ideal if an entire instance of KickMCMCResults could be saved in hdf5. Running the MCMC and analyzing it should be two different steps, so we can in principle just throw a large MCMC at a node in a cluster and then carefully analyze the outcome. We can also analyze more than one chain in the same session this way, without needing to run them there.

Here is a simple example that creates an HDF5 file with one dataset, storing a matrix and a set of column names:

# Create the file
using HDF5

A = zeros(100,5)
column_names = ["column one", "column two", "column three", "column four", "column 5"]
for i in 1:(100*5)
    A[i] = i
end

file = h5open("test.hdf5", "w")
dataset = create_dataset(file, "A", Float64, size(A))
write(dataset, A)
attrs(dataset)["column_names"] = column_names

close(file)

##
# Read the file
file = h5open("test.hdf5", "r")
A_loaded = file["A"][:,:]
@show A_loaded
@show attrs(file["A"])["column_names"]
close(file)
orlox commented 1 week ago

As a starting point, if we consider all the elements of KickMCMCResults:

@kwdef mutable struct KickMCMCResults
    mcmc_model_cauchy
    mcmc_model_normal
    observations::Observations
    results::Dict{Symbol, Matrix{Float64}}
    chains
    nuts_warmup_count::Int
    nuts_acceptance_rate::Float64
    nsamples::Int
end

Main critical thing is saving the results. We should anyhow save everything, as it documents what was done more precisely (and from the chains object one can get important metrics regarding convergence). But the rest can be done later. I did write up some code to save chains a while back but that has been lost to the aether now.