Skip to content

Commit

Permalink
Apply JuliaFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
notZaki committed Jul 30, 2020
1 parent a43f319 commit 76b9b03
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 222 deletions.
29 changes: 13 additions & 16 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
using Documenter, Perfusion

makedocs(
;
makedocs(;
modules = [Perfusion],
format = Documenter.HTML(
prettyurls = get(ENV, "CI", nothing) == "true"
),
pages=[
format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
pages = [
"Introduction" => "index.md",
"Pre-processing" => Any[
"Loading DICOM" => "preprocessing/loading.md",
"T1 mapping" => "preprocessing/T1mapping.md",
"Signal to Concentration" => "preprocessing/signal2concentration.md"
"Loading DICOM"=>"preprocessing/loading.md",
"T1 mapping"=>"preprocessing/T1mapping.md",
"Signal to Concentration"=>"preprocessing/signal2concentration.md",
],
"AIF-based Models" => Any[
"AIF" => "models/aif.md",
"Tofts" => "models/tofts.md",
"Extended Tofts" => "models/extendedtofts.md",
"Exchange" => "models/exchange.md",
"Uptake" => "models/uptake.md",
"Filtration" => "models/filtration.md"
"AIF"=>"models/aif.md",
"Tofts"=>"models/tofts.md",
"Extended Tofts"=>"models/extendedtofts.md",
"Exchange"=>"models/exchange.md",
"Uptake"=>"models/uptake.md",
"Filtration"=>"models/filtration.md",
],
"Functions" => "functions.md"
"Functions" => "functions.md",
],
repo = "https://github.com/notZaki/Perfusion.jl/blob/{commit}{path}#L{line}",
sitename = "Perfusion.jl",
Expand Down
10 changes: 5 additions & 5 deletions src/Perfusion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ using LsqFit
using Statistics: mean
include("relaxation.jl")
export signal_to_concentration,
concentration_to_signal,
signal_to_R1,
R1_to_signal,
concentration_to_R1,
R1_to_concentration
concentration_to_signal,
signal_to_R1,
R1_to_signal,
concentration_to_R1,
R1_to_concentration
export fit_relaxation, fit_relaxation_nls, fit_relaxation_despot, fit_relaxation_novifast

using SpecialFunctions: gamma
Expand Down
47 changes: 16 additions & 31 deletions src/aif.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ The hematocrit `hct` is used to convert the concentration in blood to plasma and
"""
function aif_parker(t; params = _parker_params, hct = 0.42)
@extract (A, T, σ, α, β, s, τ) params
cb = [(A[1] / (σ[1] * sqrt(2 * pi))) * exp(-(t - T[1])^2 / (2 * (σ[1])^2)) +
(A[2] / (σ[2] * sqrt(2 * pi))) * exp(-(t - T[2])^2 / (2 * (σ[2])^2)) +
α * exp(-β * t) / (1.0 + exp(-s * (t - τ))) for t in t]
cb[t .<= 0] .= 0
cb = [
(A[1] / (σ[1] * sqrt(2 * pi))) * exp(-(t - T[1])^2 / (2 * (σ[1])^2)) +
(A[2] / (σ[2] * sqrt(2 * pi))) * exp(-(t - T[2])^2 / (2 * (σ[2])^2)) +
α * exp(-β * t) / (1.0 + exp(-s * (t - τ))) for t in t
]
cb[t.<=0] .= 0
ca = cb ./ (1 - hct)
return ca
end

const _georgiou_params = (
A = [0.37, 0.33, 10.06],
m = [0.11, 1.17, 16.02],
α = 5.26,
β = 0.032,
τ = 0.129,
)
const _georgiou_params =
(A = [0.37, 0.33, 10.06], m = [0.11, 1.17, 16.02], α = 5.26, β = 0.032, τ = 0.129)

"""
aif_georgiou(t; params, hct = 0.35)
Expand Down Expand Up @@ -61,7 +58,7 @@ function aif_georgiou(t; params = _georgiou_params, hct = 0.35)
end
end
cb = exponential_part .* gammavariate_part
cb[t .<= 0] .= 0
cb[t.<=0] .= 0
ca = cb ./ (1 - hct)
return ca
end
Expand All @@ -88,7 +85,7 @@ The default value is set to to zero, i.e. no conversion takes place.
function aif_biexponential(t; params, hct = 0)
@extract (D, a, m) params
cb = [D * (a[1] * exp(-m[1] * t) + a[2] * exp(-m[2] * t)) for t in t]
cb[t .<= 0] .= 0
cb[t.<=0] .= 0
ca = cb ./ (1 - hct)
return ca
end
Expand Down Expand Up @@ -129,16 +126,12 @@ The timepoints `t` are in minutes with the bolus arriving at 0.
The model parameters from the paper will be used by default unless an optional `params` input is provided.
The `hct` is used to convert the concentration in blood to plasma and has default value of 0.42.
"""
function aif_orton1(
t;
params = (aB = 10.4, μB = 12.0, aG = 1.24, μG = 0.169),
hct = 0.42,
)
function aif_orton1(t; params = (aB = 10.4, μB = 12.0, aG = 1.24, μG = 0.169), hct = 0.42)
@extract (aB, μB, aG, μG) params
AB = aB - aB * aG / (μB - μG)
AG = aB * aG / (μB - μG)
cb = [AB * exp(-μB * t) + AG * exp(-μG * t) for t in t]
cb[t .< 0] .= 0
cb[t.<0] .= 0
ca = cb ./ (1 - hct)
return ca
end
Expand All @@ -152,16 +145,12 @@ The timepoints `t` are in minutes with the bolus arriving at 0.
The model parameters from the paper will be used by default unless an optional `params` input is provided.
The `hematocrit` is used to convert the concentration in blood to plasma and has default value of 0.42.
"""
function aif_orton2(
t;
params = (aB = 344, μB = 20.2, aG = 1.24, μG = 0.172),
hct = 0.42,
)
function aif_orton2(t; params = (aB = 344, μB = 20.2, aG = 1.24, μG = 0.172), hct = 0.42)
@extract (aB, μB, aG, μG) params
AB = aB - aB * aG / (μB - μG)
AG = aB * aG / (μB - μG)^2
cb = [AB * t * exp(-μB * t) + AG * (exp(-μG * t) - exp(-μB * t)) for t in t]
cb[t .<= 0] .= 0
cb[t.<=0] .= 0
ca = cb ./ (1 - hct)
return ca
end
Expand All @@ -175,11 +164,7 @@ The timepoints `t` are in minutes with the bolus arriving at `t = 0`.
The model parameters from the paper will be used by default unless an optional `params` input is provided.
The `hct` is used to convert the concentration in blood to plasma and has default value of 0.42.
"""
function aif_orton3(
t;
params = (aB = 2.84, μB = 22.8, aG = 1.36, μG = 0.171),
hct = 0.42,
)
function aif_orton3(t; params = (aB = 2.84, μB = 22.8, aG = 1.36, μG = 0.171), hct = 0.42)
@extract (aB, μB, aG, μG) params
tb = 2 * pi / μB
cb = zeros(length(t))
Expand All @@ -190,7 +175,7 @@ function aif_orton3(
cb[i] = aB * aG * _orton3_f(tb, μG, μB) * exp(-μG * (t - tb))
end
end
cb[t .<= 0] .= 0
cb[t.<=0] .= 0
ca = cb ./ (1 - hct)
return ca
end
Expand Down
6 changes: 3 additions & 3 deletions src/dce_models/exchange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ function fit_exchange_nls(
continue
end
initialvalue = [init_fp[idx], init_ps[idx], init_ve[idx], init_vp[idx]]
fp[idx], ps[idx], ve[idx], vp[idx] = curve_fit(model, t, ct[idx, :], initialvalue).param
fp[idx], ps[idx], ve[idx], vp[idx] =
curve_fit(model, t, ct[idx, :], initialvalue).param
end
T = @. (vp + ve) / fp
Tp = @. vp / fp
Te = @. ve / ps
return (; est = (; fp, ps, ve, vp, T, Tp, Te))
end

function fit_exchange_lls(
;
function fit_exchange_lls(;
t::AbstractVector,
ca::AbstractVector,
ct::AbstractArray,
Expand Down
6 changes: 3 additions & 3 deletions src/dce_models/filtration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function fit_filtration_nls(
continue
end
initialvalue = [init_fp[idx], init_ps[idx], init_ve[idx], init_vp[idx]]
fp[idx], ps[idx], ve[idx], vp[idx] = curve_fit(model, t, ct[idx, :], initialvalue).param
fp[idx], ps[idx], ve[idx], vp[idx] =
curve_fit(model, t, ct[idx, :], initialvalue).param
end
T = @. (vp + ve) / fp
Tp = @. vp / fp
Expand All @@ -54,8 +55,7 @@ function fit_filtration_nls(
return (; est = (; fp, ps, ve, vp, T, Tp, Te))
end

function fit_filtration_lls(
;
function fit_filtration_lls(;
t::AbstractVector,
ca::AbstractVector,
ct::AbstractArray,
Expand Down
59 changes: 34 additions & 25 deletions src/dce_models/reference_region.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@ function fit_rrm_nls(
ct::AbstractArray,
mask = true,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca=crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
rel_kt, kep, kep_rr = (fill(NaN, volume_size...) for _ = 1:3)
lls_est = fit_rrm_lls(; t, crr, ct, mask).est
init_rel_kt, init_kep, init_kep_rr = select(lls_est, (:rel_kt, :kep, :kep_rr))
model(x, p) = model_referenceregion(conv; t = x, crr, params = (rel_kt = p[1], kep = p[2], kep_rr = p[3]))
model(x, p) = model_referenceregion(
conv;
t = x,
crr,
params = (rel_kt = p[1], kep = p[2], kep_rr = p[3]),
)
for idx in eachindex(IndexCartesian(), mask)
if mask[idx] == false
continue
end
initialvalues = [init_rel_kt[idx], init_kep[idx], init_kep_rr[idx]]
rel_kt[idx], kep[idx], kep_rr[idx] = curve_fit(model, t, ct[idx, :], initialvalues).param
rel_kt[idx], kep[idx], kep_rr[idx] =
curve_fit(model, t, ct[idx, :], initialvalues).param
end
rel_ve = @. rel_kt * kep_rr / kep
return (; est = (; rel_kt, rel_ve, kep, kep_rr))
end

function fit_rrm_lls(
;
function fit_rrm_lls(;
t::AbstractVector,
crr::AbstractVector,
ct::AbstractArray,
mask = true,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca = crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
x1, x2, x3 = (fill(NaN, volume_size...) for _ = 1:3)

M = zeros(num_timepoints, 3)
Expand Down Expand Up @@ -68,9 +75,10 @@ function fit_crrm_nls(
mask = true,
kep_rr = 0.0,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca = crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
rel_kt, kep = (fill(NaN, volume_size...) for _ = 1:2)

if kep_rr <= 0
rrm_est = fit_rrm_nls(conv; t, crr, ct, mask).est
viable_est = positive_only_mask(rrm_est)
Expand All @@ -79,10 +87,11 @@ function fit_crrm_nls(
lls_est = fit_crrm_lls(; t, crr, ct, kep_rr, mask = mask).est
init_rel_kt, init_kep = select(lls_est, (:rel_kt, :kep))
model(x, p) = model_referenceregion(
conv;
t = x,
crr,
params = (rel_kt = p[1], kep = p[2], kep_rr = kep_rr))
conv;
t = x,
crr,
params = (rel_kt = p[1], kep = p[2], kep_rr = kep_rr),
)
for idx in eachindex(IndexCartesian(), mask)
if mask[idx] == false
continue
Expand All @@ -94,15 +103,15 @@ function fit_crrm_nls(
return (; est = (; rel_kt, rel_ve, kep, kep_rr))
end

function fit_crrm_lls(
;
function fit_crrm_lls(;
t::AbstractVector,
crr::AbstractVector,
ct::AbstractArray,
mask = true,
kep_rr = 0.0,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca = crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
rel_kt, kep = (fill(NaN, volume_size...) for _ = 1:2)

if kep_rr <= 0
Expand All @@ -124,14 +133,14 @@ function fit_crrm_lls(
return (est = (rel_kt = rel_kt, rel_ve = rel_ve, kep = kep, kep_rr = kep_rr),)
end

function fit_errm_lls(
;
function fit_errm_lls(;
t::AbstractVector,
crr::AbstractVector,
ct::AbstractArray,
mask = true,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca = crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
x1, x2, x3, x4 = (fill(NaN, volume_size...) for _ = 1:4)

M = zeros(num_timepoints, 4)
Expand Down Expand Up @@ -159,15 +168,15 @@ function fit_errm_lls(
return (; est = (; rel_kt, rel_ve, rel_vp, kep, kep_rr))
end

function fit_cerrm_lls(
;
function fit_cerrm_lls(;
t::AbstractVector,
crr::AbstractVector,
ct::AbstractArray,
mask = true,
kep_rr = 0.0,
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca = crr, ct, mask)
(t, ct, mask, num_timepoints, volume_size) =
resolve_fitting_inputs(; t, ca = crr, ct, mask)
x1, rel_vp, kep = (fill(NaN, volume_size...) for _ = 1:3)

if kep_rr <= 0
Expand Down Expand Up @@ -212,8 +221,7 @@ function fit_rrift_with_crrm(; crr, cp, t, ct, tail_start, kep_rr = 0.0, mask =
return (est = (; est..., kep_rr, kt_rr, ve_rr))
end

function fit_rrift(
;
function fit_rrift(;
crr::AbstractVector,
cp::AbstractVector,
t::AbstractVector,
Expand All @@ -225,8 +233,9 @@ function fit_rrift(
crr_tail = crr[tail_start:end]
cp_tail = cp[tail_start:end]
t_tail = t[tail_start:end]
numerator = crr_tail .- crr_tail[1] .+
kep_rr .* cumul_integrate(t_tail, crr_tail, TrapezoidalFast())
numerator =
crr_tail .- crr_tail[1] .+
kep_rr .* cumul_integrate(t_tail, crr_tail, TrapezoidalFast())
denominator = cumul_integrate(t_tail, cp_tail, TrapezoidalFast())
kt_rr = denominator \ numerator
return kt_rr
Expand Down
6 changes: 2 additions & 4 deletions src/dce_models/tofts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ function fit_extendedtofts_nls(
return (; est)
end

function fit_tofts_lls(
;
function fit_tofts_lls(;
t::AbstractVector,
ca::AbstractVector,
ct::AbstractArray,
Expand All @@ -150,8 +149,7 @@ function fit_tofts_lls(
return (; est)
end

function fit_extendedtofts_lls(
;
function fit_extendedtofts_lls(;
t::AbstractVector,
ca::AbstractVector,
ct::AbstractArray,
Expand Down
6 changes: 2 additions & 4 deletions src/dce_models/uptake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ function fit_uptake_nls(
)
(t, ct, mask, num_timepoints, volume_size) = resolve_fitting_inputs(; t, ca, ct, mask)
fp, ps, vp = (fill(NaN, volume_size...) for _ = 1:3)
model(x, p) =
model_uptake(; t = x, ca, params = (fp = p[1], ps = p[2], vp = p[3]))
model(x, p) = model_uptake(; t = x, ca, params = (fp = p[1], ps = p[2], vp = p[3]))
lls_est = fit_uptake_lls(; t, ca, ct, mask).est
init_fp, init_ps, init_vp = select(lls_est, (:fp, :ps, :vp))
for idx in eachindex(IndexCartesian(), mask)
Expand All @@ -38,8 +37,7 @@ function fit_uptake_nls(
return (; est = (; fp, ps, vp))
end

function fit_uptake_lls(
;
function fit_uptake_lls(;
t::AbstractVector,
ca::AbstractVector,
ct::AbstractArray,
Expand Down

0 comments on commit 76b9b03

Please sign in to comment.