/
logbook.jl
116 lines (100 loc) · 3.42 KB
/
logbook.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Logbook()
Logbook(S::LittleDict)
A log for statistics intended for use on every iteration of an algorithm.
The logbook is constructed from a `LittleDict` ordered dictionary which maps stat names
(strings) to callables, such that _statname_ ``i`` can be computed from
_callable_ ``i``.
The resulting `Logbook` contains:
- `S::LittleDict`: The ordered dict of stat names and callables
- `records::AbstractVector`: A vector of NamedTuples where each field is a statistic.
If no argument is passed, the logbook is constructed with a set of commonly statistics such
as minimum, mean, median, maximum and standard deviation; in that order.
"""
mutable struct Logbook
S::LittleDict{AbstractString,Function}
records::AbstractVector
function Logbook(S::LittleDict)
fnames = [k for k in keys(S)]
d = Vector{namedtuple(fnames)}()
return new(S, d)
end
function Logbook()
fnames = [
"min_f",
"mean_f",
"median_f",
"max_f",
"std",
]
callables = [
minimum,
mean,
median,
maximum,
std,
]
S = LittleDict(fnames, callables)
d = Vector{namedtuple(fnames)}()
return new(S, d)
end
end
"""
compute!(logger::Logbook, data::AbstractVector)
compute!(notebooks::Vector{Logbook}, data::Vector{AbstractVector})
Computes statistics for `logger` (or a vector of `logger`s) using `data`,
which is usually a vector of fitnesses.
All calculations are done in place, so the `logger` records will be updated.
"""
function compute!(logger::Logbook, data::AbstractVector)
fnames = [s for s in keys(logger.S)]
callables = [f(data) for f in values(logger.S)]
record = namedtuple(fnames, callables)
push!(logger.records, record)
return nothing
end
function compute!(notebooks::Vector{Logbook}, data::AbstractVector)
for (logger, y) in zip(notebooks, data)
fnames = [s for s in keys(logger.S)]
callables = [f(y) for f in values(logger.S)]
record = namedtuple(fnames, callables)
push!(logger.records, record)
end
end
"""
summarise(logger::Logbook)
summarise(notebooks::Logbook)
Print and plot descriptive statistics for a given `logger` (or a vector of `logger`s).
"""
function summarise(logger::Logbook)
n = length(logger.records)
for (i, eachstat) in enumerate(keys(logger.S))
data = [logger.records[j][i] for j in 1:n]
printstyled("\n $(eachstat) \n"; bold=true)
print("max: $(maximum(data)) \n" *
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
plt = lineplot(data;
xlabel="it", ylabel=eachstat)
print(plt)
end
end
function summarise(notebooks::Vector{Logbook})
for eachnb in notebooks
n = length(eachnb.records)
for (i, eachstat) in enumerate(keys(eachnb.S))
data = [eachnb.records[j][i] for j in 1:n]
printstyled("\n $(eachstat) \n"; bold=true)
print("max: $(maximum(data)) \n" *
"avg: $(mean(data))\n" *
"median: $(median(data)) \n" *
"min: $(minimum(data))\n" *
"std: $(std(data))\n")
plt = lineplot(data;
xlabel="it", ylabel=eachstat)
print(plt)
end
end
end