This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
create_cmd_line.jl
92 lines (81 loc) · 2.78 KB
/
create_cmd_line.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
"""
# cmdline
Recursively parse the model to construct command line.
### Method
```julia
cmdline(m)
```
### Required arguments
```julia
* `m::Stanmodel` : Stanmodel
```
### Related help
```julia
?Stanmodel : Create a StanModel
```
"""
function cmdline(m)
cmd = ``
if isa(m, Stanmodel)
# Handle the model name field for unix and windows
cmd = @static Sys.isunix() ? `./$(getfield(m, :name))` : `cmd /c $(getfield(m, :name)).exe`
# Method (sample, optimize, variational and diagnose) specific portion of the model
cmd = `$cmd $(cmdline(getfield(m, :method)))`
# Common to all models
cmd = `$cmd $(cmdline(getfield(m, :random)))`
# Init file required?
if length(m.init_file) > 0 && isfile(m.init_file)
cmd = `$cmd init=$(m.init_file)`
end
# Data file required?
if length(m.data_file) > 0 && isfile(m.data_file)
cmd = `$cmd id=$(m.id) data file=$(m.data_file)`
end
# Output options
cmd = `$cmd output`
if length(getfield(m, :output).file) > 0
cmd = `$cmd file=$(string(getfield(m, :output).file))`
end
if length(getfield(m, :output).diagnostic_file) > 0
cmd = `$cmd diagnostic_file=$(string(getfield(m, :output).diagnostic_file))`
end
cmd = `$cmd refresh=$(string(getfield(m, :output).refresh))`
else
# The 'recursive' part
#println(lowercase(string(typeof(m))))
if isa(m, SamplingAlgorithm) || isa(m, OptimizeAlgorithm)
cmd = `$cmd algorithm=$(split(lowercase(string(typeof(m))), '.')[end])`
elseif isa(m, Engine)
cmd = `$cmd engine=$(split(lowercase(string(typeof(m))), '.')[end])`
elseif isa(m, Diagnostics)
cmd = `$cmd test=$(split(lowercase(string(typeof(m))), '.')[end])`
else
cmd = `$cmd $(split(lowercase(string(typeof(m))), '.')[end])`
end
#println(cmd)
for name in fieldnames(typeof(m))
#println("$(name) = $(getfield(m, name)) ($(typeof(getfield(m, name))))")
if isa(getfield(m, name), String) || isa(getfield(m, name), Tuple)
cmd = `$cmd $(name)=$(getfield(m, name))`
elseif length(fieldnames(typeof(getfield(m, name)))) == 0
if isa(getfield(m, name), Bool)
cmd = `$cmd $(name)=$(getfield(m, name) ? 1 : 0)`
else
if name == :metric || isa(getfield(m, name), DataType)
cmd = `$cmd $(name)=$(split(lowercase(string(typeof(getfield(m, name)))), '.')[end])`
else
if name == :algorithm && typeof(getfield(m, name)) == CmdStan.Fixed_param
cmd = `$cmd $(name)=fixed_param`
else
cmd = `$cmd $(name)=$(getfield(m, name))`
end
end
end
else
cmd = `$cmd $(cmdline(getfield(m, name)))`
end
end
end
#println(cmd)
cmd
end