Wed, 22 Dec 2021 11:14:38 +0200
Add metaprogramming tools and fast multidimensional loops.
34
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # This module implements logging of intermediate computational results for |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # generating convergence graphs, etc. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | # |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | Logging routines for intermediate computational results. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | Includes `Log`, `log!`, `if_log!`, and `write_log`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | module Logger |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
11 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | import DelimitedFiles: writedlm |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | ############## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | # Our exports |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | ############## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | export Log, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | log!, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | if_log!, |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | write_log |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | ########## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | # Logging |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | ########## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | `struct Log{T}` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | A log of items of type `T` along with log configuration. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | The constructor takes no arguments; create the log with `Log{T}()` for `T` your |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | data type, e.g., `Float64`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | struct Log{T} |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | log :: Dict{Int, T} |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | Log{T}() where T = new{T}(Dict{Int, T}()) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | `if_log!(f :: Functdion, log :: Log{T}, i :: Int)` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
42 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | If based on the log settings, `i` is a verbose iteration, store the value of `f()` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | in the log at index `i`. Typically to be used with `do`-notation: |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | ```julia |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | if_log!(log, iteration) do |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | # calculate value to be logged |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | ``` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | function if_log!(f :: Function, log :: Log{T}, i :: Int) where T |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | if mod(i, log.verbose_iterations)==0 |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | log!(f, log, i) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | #printstyled("$(i): $(val)\n", color=:light_black) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
57 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
60 | `log!(f :: Function, log :: Log{T}, i :: Int) ` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
61 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
62 | Store the value `f()` in the log at index `i`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
63 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
64 | function log!(f :: Function, log :: Log{T}, i :: Int) where T |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
65 | val = f() |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
66 | push!(log.log, i => val) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
67 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
68 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | ############## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | # Data export |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | ############## |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
74 | `write_log(filename :: AbstractString, log :: Log{T})` |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
75 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
76 | Write a `Log{T}` as a CSV file using `DelimitedFiles`. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
77 | If T is a structural type, the field names are used as header fields. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
78 | Otherwise a single `value` field is attempted to be written. |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
79 | """ |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | function write_log(filename :: AbstractString, log :: Log{T}) where T |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | k = fieldnames(T) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | @assert(:iter ∉ k) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
83 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
84 | open(filename, "w") do io |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
85 | # Write header |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
86 | writedlm(io, isempty(k) ? [:iter :value] : ((:iter, k...),)) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
87 | # Write values |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
88 | iters = sort(collect(keys(log.log))) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
89 | for i ∈ iters |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
90 | v = log.log[i] |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
91 | writedlm(io, isempty(k) ? [i v] : ((i, (getfield(v, j) for j ∈ k)...),)) |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
92 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
93 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
94 | end |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | |
22a64e826ee7
New logger and iteration interface
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | end # module |