/
plots.jl
475 lines (431 loc) · 13.7 KB
/
plots.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
struct NoPageSort end
const PKPLOTSTYLE = (
(:solid, :blue, :circle, :blue),
(:solid, :red, :utriangle, :red),
(:solid, :green, :diamond, :green),
(:solid, :magenta, :pentagon, :magenta),
(:solid, :purple, :heptagon, :purple),
(:solid, :indigo, :octagon, :indigo),
(:solid, :gold, :star, :gold),
(:solid, :yellow, :rect, :yellow),
(:solid, :gray, :xcross, :gray),
(:solid, :cyan, :cross, :cyan),
(:dot, :blue, :utriangle, :blue),
(:dot, :red, :circle, :red),
(:dot, :green, :rect, :green),
(:dot, :yellow, :diamond, :yellow),
(:dot, :gray, :cross, :gray),
(:dot, :cyan, :xcross, :cyan),
(:dot, :gold, :pentagon, :gold),
(:dot, :magenta, :star, :magenta),
(:dot, :purple, :octagon, :purple),
(:dot, :indigo, :heptagon, :indigo),
)
function plotstyle(n)
if isnothing(n) n = 1 end
if n >= 1 && n <= 20
return PKPLOTSTYLE[n]
elseif n > 1900
return (:auto, :auto, :auto, :auto)
end
n -= 20
linestyle = (:dash, :dashdot, :dashdotdot)
linecolor = (:yellow, :gray, :cyan, :gold, :magenta, :purple, :indigo)
markershape = (:star4, :star6, :star7, :star8, :rect, :star5, :diamond, :hexagon, :cross, :xcross, :utriangle, :dtriangle, :rtriangle, :ltriangle, :pentagon, :heptagon, :octagon, :vline, :hline)
markercolor = (:gray, :gold, :magenta, :purple, :indigo)
a = n % 3 + 1
b = n % 7 + 1
c = n % 19 + 1
d = n % 5 + 1
(linestyle[a], linecolor[b], markershape[c], markercolor[d])
end
@userplot PKPlot
@userplot PKElimpPlot
@userplot PdHLine
function luceil(x)
fl = Int(floor(log10(x)))
if fl < 0 fl = 0 end
ceil(x/10^fl)*10^fl
end
@recipe function f(subj::PKPlot; lcd = :auto, tcd = :auto)
x, y = subj.args
if isa(lcd, Real)
lc = luceil(maximum(x->isnan(x) ? -Inf : x, y)/lcd)
yt = 0:lc:lc*lcd
elseif isa(lcd, StepRange)
yt = lcd
elseif lcd == :all
yt = y
end
if isa(tcd, Real)
tc = luceil(maximum(x)/tcd)
xt = 0:tc:tc*tcd
elseif isa(tcd, StepRange)
xt = tcd
elseif tcd == :all
xt = x
end
widen --> true
seriestype --> :line
xguide --> "Time"
link --> :both
legend --> true
grid --> true
gridstyle --> :auto
#ticks := [nothing :auto nothing]
#xlims --> (minimum(x), maximum(x)*1.1)
#ylims --> (0, maximum(y)*1.1)
if !isa(lcd, Symbol) || lcd != :auto
yticks --> yt
end
if !isa(tcd, Symbol) || tcd != :auto
xticks --> xt
end
seriescolor --> :blue
markershape --> :circle
markersize --> 3
markercolor --> :match
markerstrokealpha --> 0
(x, y)
end
@recipe function f(subj::PKElimpPlot)
x, y = subj.args
seriestype --> :line
legend --> false
markersize --> 0
markerstrokealpha --> 0
(x, y)
end
@recipe function f(subj::PdHLine)
x, y = subj.args
seriestype --> :straightline
(x, [y, y])
end
# Text label from ID
function plotlabel(d, ld = nothing)
title = ""
if isnothing(d) return title end
if length(d) > 0
for (k, v) in d
kv = k
if !isnothing(ld) && haskey(ld, kv)
kv = ld[kv]
end
title *= "$(kv) = $(v); "
end
end
return title
end
"""
pkplot(subj; ls = false, elim = false, xticksn = :auto, yticksn = :auto, kwargs...)
Plot for subject
* `ls` - concentration in log scale;
* `elim` - draw elimination curve;
* `xticksn` - number of ticks on x axis;
* `yticksn` - number of ticks on y axis;
*Other keywords:*
* `plotstyle` - predefined plot style from PKPLOTSTYLE;
* `drawbl` (`false`) - draw baseline, only for PDSubject;
* `drawth` (`false`) - draw threshold, only for PDSubject;
"""
function pkplot(subj::AbstractSubject; ls = false, elim = false, xticksn = :auto, yticksn = :auto, kwargs...)
time = subj.time
obs = subj.obs
kwargs = Dict{Symbol, Any}(kwargs)
k = keys(kwargs)
if !(:plotstyle in k)
kwargs[:linestyle], kwargs[:linecolor], kwargs[:markershape], kwargs[:markercolor] = PKPLOTSTYLE[1]
else
kwargs[:linestyle], kwargs[:linecolor], kwargs[:markershape], kwargs[:markercolor] = kwargs[:plotstyle]
end
if !(:title in k)
kwargs[:title] = plotlabel(subj.id)
end
if !(:drawbl in k)
kwargs[:drawbl] = false
end
if !(:drawth in k)
kwargs[:drawth] = false
end
if !(:title in k)
kwargs[:title] = plotlabel(subj.id)
end
if !(:legend in k)
kwargs[:legend] = true
end
if !(:ylabel in k)
kwargs[:ylabel] = "Concentration"
end
if !(:xlims in k)
kwargs[:xlims] = (minimum(subj.time), maximum(subj.time)*1.1)
end
if :yscale in k
if kwargs[:yscale] in [:ln, :log, :log2, :log10]
ls = false
if !(:minorticks in k) kwargs[:minorticks] = true end
inds = findall(x-> x > 0, subj.obs)
time = subj.time[inds]
obs = subj.obs[inds]
if !(:yticks in k)
if kwargs[:yscale] == :log10
b = 10
elseif kwargs[:yscale] == :log2
b = 2
elseif kwargs[:yscale] == :ln || kwargs[:yscale] == :log
b = ℯ
end
t = collect(floor(log(b, minimum(obs))):ceil(log(b, maximum(obs))))
pushfirst!(t, first(t) - 1)
kwargs[:yticks] = b .^ t
end
if !(:ylims in k)
kwargs[:ylims] = (minimum(obs)*0.5, maximum(obs)*2.)
else
if kwargs[:ylims][1] <= 0
kwargs[:ylims] = (minimum(obs)/b, kwargs[:ylims][2])
end
end
end
else
if !(:ylims in k)
kwargs[:ylims] = (minimum(subj.obs), maximum(subj.obs)*1.15)
end
end
if ls == true
inds = findall(x-> x > 0, subj.obs)
time = subj.time[inds]
obs = log.(subj.obs[inds])
if (:ylims in k)
kwargs[:ylims] = (0, log(kwargs[:ylims][2]))
end
end
p = pkplot(time, obs; lcd = yticksn, tcd = xticksn, kwargs...)
if elim
if length(subj.keldata) > 0
rsq, rsqn = findmax(subj.keldata.ar)
lz = subj.keldata.a[rsqn]
lzint = subj.keldata.b[rsqn]
ts = subj.keldata.s[rsqn]
te = subj.keldata.e[rsqn]
if ls true
x = [ts, te]
y = [lzint + lz * x[1], lzint + lz * x[2]]
else
x = collect(ts:(te-ts)/100:te)
y = exp.(lzint .+ lz .* x)
end
pkelimpplot!(p, x, y)
end
end
if isa(subj, PDSubject)
if kwargs[:drawth] == true
pdhline!(p, [minimum(subj.time), maximum(subj.time)], getth(subj), lc = :blue, label = "TH")
end
if kwargs[:drawbl] == true
pdhline!(p, [minimum(subj.time), maximum(subj.time)], getbl(subj), lc = :red, label = "BL")
end
end
return p
end
function pkplot!(subj; ls = false, elim = false, xticksn = :auto, yticksn = :auto, kwargs...)
time = subj.time
obs = subj.obs
kwargs = Dict{Symbol, Any}(kwargs)
k = keys(kwargs)
if !(:plotstyle in k)
kwargs[:linestyle], kwargs[:linecolor], kwargs[:markershape], kwargs[:markercolor] = PKPLOTSTYLE[1]
else
kwargs[:linestyle], kwargs[:linecolor], kwargs[:markershape], kwargs[:markercolor] = kwargs[:plotstyle]
end
if !(:legend in k)
kwargs[:legend] = true
end
if !(:xlims in k)
kwargs[:xlims] = (minimum(subj.time), maximum(subj.time)*1.1)
end
if :yscale in k
if kwargs[:yscale] in [:ln, :log, :log2, :log10]
ls = false
if !(:minorticks in k) kwargs[:minorticks] = true end
inds = findall(x-> x > 0, subj.obs)
time = subj.time[inds]
obs = subj.obs[inds]
if !(:yticks in k)
if kwargs[:yscale] == :log10
b = 10
elseif kwargs[:yscale] == :log2
b = 2
elseif kwargs[:yscale] == :ln || kwargs[:yscale] == :log
b = ℯ
end
t = collect(floor(log(b, minimum(obs))):ceil(log(b, maximum(obs))))
pushfirst!(t, first(t) - 1)
kwargs[:yticks] = b .^ t
end
if !(:ylims in k)
kwargs[:ylims] = (minimum(obs)*0.5, maximum(obs)*2.)
else
if kwargs[:ylims][1] <= 0
kwargs[:ylims] = (minimum(obs)/b, kwargs[:ylims][2])
end
end
end
else
if !(:ylims in k)
kwargs[:ylims] = (minimum(subj.obs), maximum(subj.obs)*1.15)
end
end
if ls == true
inds = findall(x-> x > 0, subj.obs)
time = subj.time[inds]
obs = log.(subj.obs[inds])
if (:ylims in k)
kwargs[:ylims] = (0, log(kwargs[:ylims][2]))
end
end
p = pkplot!(time, obs; lcd = yticksn, tcd = xticksn, kwargs...)
return p
end
function pageplot(data, id, ulist; kwargs...)
kwargs = Dict{Symbol, Any}(kwargs)
k = keys(kwargs)
if !(:title in k)
kwargs[:title] = plotlabel(id, kwargs[:ldict])
end
fst = true
p = nothing
labvec = Vector{Int}(undef, 0)
# Make subdata by ID
isnothing(id) ? subdata = data : subdata = subset(data, id)
# Y lims
if !(:ylims in k) && length(subdata) > 1
ysc = :yscale in k
ylmin = findmin(x->minconc(x, ysc), getdata(subdata))[1]
ylmax = findmax(x->maxconc(x), getdata(subdata))[1]*1.15
if ysc
ylmax *= 5
end
kwargs[:ylims] = (ylmin, ylmax)
end
# Plotting subdata
if length(subdata) > 1 kwargs[:elim] = false end
for subj in subdata
if !isnothing(ulist)
num = findfirst(x-> x ⊆ subj.id, ulist)
if !isnothing(num)
style = plotstyle(num)
if num ∈ labvec
kwargs[:label] = nothing
else
kwargs[:label] = plotlabel(ulist[num], kwargs[:ldict])
push!(labvec, num)
end
else
style = plotstyle(1)
end
else
style = plotstyle(1)
end
if fst
p = pkplot(subj; plotstyle = style, kwargs...)
fst = false
else
pkplot!(subj; plotstyle = style, kwargs...)
end
end
p
end
"""
pkplot(data::DataSet{T};
typesort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing,
pagesort::Union{Nothing, Symbol, AbstractVector{Symbol}, NoPageSort} = nothing,
filter::Union{Nothing, Dict{Symbol}} = nothing,
uylims::Bool = false,
kwargs...) where T <: AbstractSubject
PK plot for subject set.
* `typesort` - sort on page by this id key;
* `pagesort` - different pages by this id key;
* `filter` - use only subjects if filter ⊆ subject id;
* `uylims` - same ylims for all dataset;
* `ldict` - Dict with labels for replace.
Use `pagesort = MetidaNCA.NoPageSort()` to prevent page plotting.
"""
function pkplot(data::DataSet{T};
typesort::Union{Nothing, Symbol, AbstractVector{Symbol}} = nothing,
pagesort::Union{Nothing, Symbol, AbstractVector{Symbol}, NoPageSort} = nothing,
filter::Union{Nothing, Dict{Symbol}} = nothing,
uylims::Bool = false,
ldict = nothing,
kwargs...) where T <: AbstractSubject
kwargs = Dict{Symbol, Any}(kwargs)
k = keys(kwargs)
if !(:ls in k)
kwargs[:ls] = false
end
if !(:elim in k)
kwargs[:elim] = false
end
if !(:drawbl in k)
kwargs[:drawbl] = false
end
if !(:drawth in k)
kwargs[:drawth] = false
end
if uylims && !(:ylims in k)
kwargs[:ylims] = (findmin(x -> minconc(x), getdata(data))[1], findmax(x -> maxconc(x), getdata(data))[1]*1.15)
end
if !isnothing(filter) data = subset(data, filter) end
if isnothing(typesort) && isnothing(pagesort)
p = []
printtitle = false
if !(:title in k)
printtitle = true
end
for subj in data
if printtitle
kwargs[:title] = plotlabel(subj.id, ldict)
end
if !(:legend in k)
kwargs[:legend] = false
end
push!(p, pkplot(subj; kwargs...))
end
return p
end
if !isnothing(typesort)
if isa(typesort, Symbol) typesort = [typesort] end
typelist = uniqueidlist(data, typesort)
else
typelist = nothing
if !(:legend in k)
kwargs[:legend] = false
end
end
if !isnothing(pagesort) && !isa(pagesort, NoPageSort)
if isa(pagesort, Symbol) pagesort = [pagesort] end
p = []
pagelist = uniqueidlist(data, pagesort)
for id in pagelist
push!(p, pageplot(data, id, typelist; ldict, kwargs...))
end
return p
else
if !(:title in k) && !isnothing(filter)
kwargs[:title] = plotlabel(filter)
end
return pageplot(data, nothing, typelist; ldict, kwargs...)
end
end
"""
pkplot(data::DataSet{T}; kwargs...) where T <: NCAResult
"""
function pkplot(data::DataSet{T}; kwargs...) where T <: NCAResult
ds = map(x-> x.data, data)
pkplot(ds; kwargs...)
end
"""
pkplot(data::NCAResult; kwargs...)
"""
function pkplot(data::NCAResult; kwargs...)
pkplot(data.data; kwargs...)
end