1
0
Fork 0
mirror of https://gitlab.rlp.net/mobitar/ReCo.jl.git synced 2024-11-08 22:21:08 +00:00
ReCo.jl/analysis/mean_kappa.jl

114 lines
3 KiB
Julia
Raw Permalink Normal View History

2022-01-31 16:14:24 +00:00
using CairoMakie
using LaTeXStrings: @L_str
using Statistics: Statistics
using ReCo: ReCo
2022-04-06 13:37:01 +00:00
include("../visualization/common_CairoMakie.jl")
2022-01-31 16:14:24 +00:00
2022-02-08 22:16:38 +00:00
"""
plot_mean_κ(; rl_dir::String, n_last_episodes::Int64)
Plot the mean of `κ` of the learning process at the directory `rl_dir`.
`κ` is the ratio of the eigenvalues of the gyration tensor. `n_last_episodes` is the number of the last episodes of the learning process to average over.
The output is `rl_dir/mean_kappa.pdf`.
Return `nothing`.
"""
function plot_mean_κ(; rl_dir::String, n_last_episodes::Int64)
2022-01-31 16:14:24 +00:00
dir_content = readdir(rl_dir; join=true, sort=true)
n_content = length(dir_content)
sim_dirs = Vector{String}(undef, n_last_episodes)
sim_dir_counter = 1
# Skip first sim_dir for the case that the simulation is still running
skipped_first_sim_dir = false
for file_or_dir_ind in n_content:-1:1
file_or_dir = dir_content[file_or_dir_ind]
if isdir(file_or_dir)
if skipped_first_sim_dir
sim_dirs[sim_dir_counter] = file_or_dir
sim_dir_counter += 1
if sim_dir_counter > n_last_episodes
break
end
else
skipped_first_sim_dir = true
end
end
end
if sim_dir_counter < n_last_episodes
error("The rl_dir does not have n_last_episodes + 1 dirs!")
end
sim_consts = ReCo.load_sim_consts(sim_dirs[1])
half_box_len = sim_consts.half_box_len
total_n_snapshots = ReCo.BundlesInfo(sim_dirs[1]).total_n_snapshots
snapshot_κs = zeros(Float64, total_n_snapshots)
for sim_dir in sim_dirs
bundles_info = ReCo.BundlesInfo(sim_dir)
total_n_snapshots = bundles_info.total_n_snapshots
for snapshot_ind in 1:total_n_snapshots
bundle, bundle_snapshot = ReCo.get_bundle_to_snapshot(
bundles_info, snapshot_ind
)
cs_view = view(bundle.c, :, bundle_snapshot)
κ = ReCo.gyration_tensor_eigvals_ratio(cs_view, half_box_len)
snapshot_κs[snapshot_ind] += κ
end
end
snapshot_κs ./= n_last_episodes
mean_κ = Statistics.mean(snapshot_κs)
init_cairomakie!()
fig = gen_figure(; padding=9)
if n_last_episodes > 1
title = "Averaged over last $n_last_episodes episodes"
else
title = "Result of only one episode"
end
2022-01-31 16:14:24 +00:00
ax = Axis(
fig[1, 1];
xlabel="Frame",
ylabel=L"\kappa",
limits=(1, total_n_snapshots, 0.0, 1.04),
title=title,
2022-01-31 16:14:24 +00:00
)
lines!(ax, 1:total_n_snapshots, snapshot_κs; label=L"\kappa")
rounded_mean_κ = round(mean_κ; digits=2)
lines!(
ax,
[1, total_n_snapshots],
[mean_κ, mean_κ];
label=L"Mean $\tilde{\kappa} = %$rounded_mean_κ$",
linestyle=:dash,
color=:red,
)
axislegend(ax; position=:lb, padding=3, rowgap=-3)
set_gaps!(fig)
save_fig("mean_kappa.pdf", fig; parent_dir=rl_dir)
return nothing
2022-03-19 22:11:03 +00:00
end