Skip to content

Commit

Permalink
v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Mar 9, 2021
1 parent 277d9a4 commit 5697660
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 20 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClinicalTrialUtilities"
uuid = "535c2557-d7d0-564d-8ff9-4ae146c18cfe"
authors = ["Vladimir Arnautov (mail@pharmcat.net)"]
version = "0.4.1"
version = "0.5.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down Expand Up @@ -29,6 +29,7 @@ julia = "1.0, 1.1, 1.2, 1.3, 1.4, 1.5"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"

[targets]
test = ["CSV", "Test", "Plots"]
test = ["CSV", "Test", "Plots", "StableRNGs"]
6 changes: 5 additions & 1 deletion cange.log
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
v 0.5.0
- StableRNGs test
- rng keyword for randomtable, randomseq
- auc_sparse for PKSubject

v0.4.1
- cox samplen
- datatable
- printdesigns


v0.4.0
- remove DataFrames interim
Expand Down
5 changes: 5 additions & 0 deletions docs/src/pk.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
```@docs
ClinicalTrialUtilities.pkimport
```

#### auc_sparse
```@docs
ClinicalTrialUtilities.auc_sparse
```
1 change: 1 addition & 0 deletions src/ClinicalTrialUtilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ applyelimrange!,
keldata,
setkelauto!,
getkelauto,
auc_sparse,
datatable,
# Simulation
ctsim,
Expand Down
27 changes: 27 additions & 0 deletions src/pk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,30 @@ function datatable_st(data) where T
end
function datatable_unst(data)
end

"""
auc_sparse(data::PKSubject)
AUC for sparse data.
```math
w_1 = (t_2 - t_1) / 2
w_j = (t_{j+1} - t_{j-1}) / 2 (2 \\leq j \\leq J - 1)
w_J = (t_J - t_{J-1}) / 2
AUC = \\sum_{j=1}^J \\mu_j w_j
```
where `math \\mu_j` is the mean drug concentration at time t.
"""
function auc_sparse(data::PKSubject)
wts = Vector{Float64}(undef, length(data))
wts[1] = (data.time[2] - data.time[1])/2.0
wts[end] = (data.time[end] - data.time[end-1])/2.0
if length(data) > 2
for i = 2:length(data)-1
wts[i] = (data.time[i+1] - data.time[i-1])/2.0
end
end
return data.obs'*wts
end
24 changes: 13 additions & 11 deletions src/randomization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ struct RandomTable
t
end
"""
randomseq(;blocksize = 4, subject = 10, group = 2, ratio = [1,1], seed = 0)
randomseq(;blocksize = 4, subject = 10, group = 2, ratio = [1,1], seed = 0, rng = Random.GLOBAL_RNG)
Randomization list generaton.
- ``blocksize`` - size of block;
- ``subject`` - subject number;
- ``group`` - number of groups;
- ``ratio`` - group ration, [1,1] means 1:1, [1,2] means 1:2, ets. ``length(ratio)`` should be equal ``group``;
- ``seed`` - RNG seed.
- ``seed`` - RNG seed;
- ``rng`` - random number generator.
"""
function randomseq(;blocksize::Int = 4, subject = 10, group = 2, ratio = [1,1], seed = 0)
function randomseq(;blocksize::Int = 4, subject = 10, group = 2, ratio = [1,1], seed = 0, rng = Random.GLOBAL_RNG)

#rng = MersenneTwister()
if seed != 0 Random.seed!(seed) end
if seed != 0 Random.seed!(rng, seed) end

r = sum(ratio)

Expand All @@ -35,7 +36,7 @@ function randomseq(;blocksize::Int = 4, subject = 10, group = 2, ratio = [1,1],
fbn = subject÷blocksize #block number
rand = Vector{Int}(undef, 0) #rand list
for i = 1:fbn
append!(rand, block[sample(1:blocksize, blocksize, replace=false)]) #generate and append random block
append!(rand, block[sample(rng, 1:blocksize, blocksize, replace=false)]) #generate and append random block
end

last = subject%blocksize #not full block
Expand All @@ -45,15 +46,15 @@ function randomseq(;blocksize::Int = 4, subject = 10, group = 2, ratio = [1,1],
for i = 1:group
append!(block, fill(i, Int(rm*ratio[i])))
end
append!(rand, block[sample(1:last, last, replace=false)])
append!(rand, block[sample(rng, 1:last, last, replace=false)])
end
return rand

end

"""
randomtable(;blocksize = 4, subject = 10, group = 2,
ratio = [1,1], grseq = ["AB", "BA"], seed = 0)
ratio = [1,1], grseq = ["AB", "BA"], seed = 0, rng = Random.GLOBAL_RNG)
Randomization table generaton. Return NamedTuple of vectors.
Expand All @@ -63,13 +64,14 @@ Randomization table generaton. Return NamedTuple of vectors.
- ``group`` - number of groups;
- ``ratio`` - group ration, [1,1] means 1:1, [1,2] means 1:2, ets. ``length(ratio)`` should be equal ``group``;
- ``grseq`` - treatment sequence in each group;
- ``seed`` - RNG seed.
- ``seed`` - RNG seed;
- ``rng`` - random number generator.
"""
function randomtable(;blocksize::Int = 4, subject::Int = 10, group::Int = 2, ratio::Vector = [1,1], grseq::Vector{String} = ["AB", "BA"], seed = 0)
function randomtable(;blocksize::Int = 4, subject::Int = 10, group::Int = 2, ratio::Vector = [1,1], grseq::Vector{String} = ["AB", "BA"], seed = 0, rng = Random.GLOBAL_RNG)

if length(unique(length.(grseq))) != 1 throw(ArgumentError("Unequal grseq")) end

r = randomseq(;blocksize = blocksize, subject = subject, group = group, ratio = ratio, seed = seed)
r = randomseq(;blocksize = blocksize, subject = subject, group = group, ratio = ratio, seed = seed, rng = rng)
subj = collect(1:length(r))
seqrand = vcat(grseq[r]...)
nl = length(grseq[1]) + 3
Expand All @@ -83,7 +85,7 @@ function randomtable(;blocksize::Int = 4, subject::Int = 10, group::Int = 2, rat
np[3] = seqrand
for i=1:length(grseq[1])
nm[i+3] = Symbol("Period_"*string(i))
np[i+3] = getindex.(seqrand, i)
np[i+3] = map(x-> x[i:i], seqrand)
end
return NamedTuple{Tuple(nm)}(Tuple(np))
end
7 changes: 6 additions & 1 deletion test/pktest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,6 @@ println(" ---------------------------------- ")
@test unca[1][:volume] 11.0
p = ClinicalTrialUtilities.pkplot(upk, ylabel = "Excretion")
end

println(" ---------------------------------- ")
@testset "#12 Pharmacodynamics " begin

Expand Down Expand Up @@ -1041,3 +1040,9 @@ println(" ---------------------------------- ")
print(io, pd)
print(io, pd[1])
end
println(" ---------------------------------- ")
@testset "#13 Sparse PK " begin
pks = ClinicalTrialUtilities.PKSubject(sparse_pk; conc = :Concentration, time = :Time)
auc = ClinicalTrialUtilities.auc_sparse(pks)
@test auc 1.35 atol=1E-5
end
2 changes: 1 addition & 1 deletion test/sim.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
println(" ---------------------------------- ")
@testset " Simulations " begin
@testset "#14 Simulations " begin

t = ClinicalTrialUtilities.bepower(cv=0.2, n=20).task
result = ClinicalTrialUtilities.besim(t; nsim = 100, seed = 1234)
Expand Down
9 changes: 5 additions & 4 deletions test/test.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Clinical Trial Utilities
# Copyright © 2019 Vladimir Arnautov aka PharmCat (mail@pharmcat.net)
using Distributions, Random, DataFrames, CSV, Test, Plots, StatsBase
using Distributions, Random, DataFrames, CSV, Test, Plots, StatsBase, StableRNGs

path = dirname(@__FILE__)
io = IOBuffer();
Expand Down Expand Up @@ -223,9 +223,10 @@ include("freque.jl")

println(" ---------------------------------- ")
@testset "#9 Random " begin
@test ClinicalTrialUtilities.randomseq(seed = 1234) == [1,2,2,1,2,1,1,2,2,1]
rdf = ClinicalTrialUtilities.randomtable(seed = 1234)
@test rdf[:Group] == [1,2,2,1,2,1,1,2,2,1]
rng = StableRNG(0)
@test ClinicalTrialUtilities.randomseq(seed = 1234, rng = rng) == [1, 2, 1, 2, 2, 1, 1, 2, 2, 1]
rdf = ClinicalTrialUtilities.randomtable(seed = 1234, rng = rng)
@test rdf[:Group] == [1, 2, 1, 2, 2, 1, 1, 2, 2, 1]
end


Expand Down
1 change: 1 addition & 0 deletions test/testdata.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Simple PK DataFrame
pkdata = CSV.File(path*"/csv/pkdata.csv") |> DataFrame
sparse_pk = pkdata[2:7, :]
#Simple PK DataFrame
nanpkdata = CSV.File(path*"/csv/nanpk.csv") |> DataFrame
#Simple PD DataFrame
Expand Down

0 comments on commit 5697660

Please sign in to comment.