Skip to content

Commit

Permalink
Make current Context a global variable in CompasToolkit.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnh committed Feb 12, 2024
1 parent e1c1630 commit 7491164
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 105 deletions.
8 changes: 4 additions & 4 deletions CompasToolkit.jl/examples/fisp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ComputationalResources
using LinearAlgebra
using StaticArrays

context = CompasToolkit.make_context(0)
context = CompasToolkit.init_context(0)

# First we assemble a Shepp Logan phantom with homogeneous T₁ and T₂
# but non-constant proton density and B₀
Expand All @@ -24,7 +24,7 @@ nvoxels = N*N

# Finally we assemble the phantom as an array of `T₁T₂B₀ρˣρʸxy` values
parameters_ref = map(T₁T₂B₀ρˣρʸxy, T₁, T₂, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(context, nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)

nTR = N; # nr of TRs used in the simulation
RF_train = LinRange(1,90,nTR) |> collect .|> complex; # flip angle train
Expand All @@ -40,7 +40,7 @@ fisp_ref = FISP(RF_train, sliceprofiles, TR, TE, max_state, TI);
RF_train = RF_train .|> ComplexF32 # constant flip angle train
sliceprofiles = collect(sliceprofiles) .|> ComplexF32 # z locations

fisp = CompasToolkit.FispSequence(context, RF_train, sliceprofiles, Float32(TR), Float32(TE), max_state, Float32(TI))
fisp = CompasToolkit.FispSequence(RF_train, sliceprofiles, Float32(TR), Float32(TE), max_state, Float32(TI))


# isochromat model
Expand All @@ -50,7 +50,7 @@ echos_ref = simulate(CUDALibs(), fisp_ref, parameters_ref);
echos_ref = collect(echos_ref)

echos = zeros(ComplexF32, nvoxels, nTR)
CompasToolkit.simulate_magnetization(context, echos, parameters, fisp)
CompasToolkit.simulate_magnetization(parameters, fisp)
echos = transpose(echos)

println("fraction equal: ", sum(echos .≈ echos_ref) / length(echos))
Expand Down
11 changes: 5 additions & 6 deletions CompasToolkit.jl/examples/pssfp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ComputationalResources
using LinearAlgebra
using StaticArrays

context = CompasToolkit.make_context(0)
context = CompasToolkit.init_context(0)

# First we assemble a Shepp Logan phantom with homogeneous T₁ and T₂
# but non-constant proton density and B₀
Expand All @@ -24,7 +24,7 @@ nvoxels = N*N

# Finally we assemble the phantom as an array of `T₁T₂B₀ρˣρʸxy` values
parameters_ref = map(T₁T₂B₀ρˣρʸxy, T₁, T₂, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(context, nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)

# Next, we assemble a balanced sequence with constant flip angle of 60 degrees,
nTR = N
Expand All @@ -47,17 +47,16 @@ pssfp_ref = pSSFP(RF_train, TR, γΔtRF, Δt, γΔtGRz, z)
Δt = (Δt.ex, Δt.inv, Δt.pr) # time intervals during TR
γΔtGRz = (γΔtGRz.ex, γΔtGRz.inv, γΔtGRz.pr) # slice select gradient strengths during TR

pssfp = CompasToolkit.pSSFPSequence(context, RF_train, Float32(TR), γΔtRF, Δt, γΔtGRz, z)
pssfp = CompasToolkit.pSSFPSequence(RF_train, Float32(TR), γΔtRF, Δt, γΔtGRz, z)

# isochromat model
pssfp_ref = gpu(f32(pssfp_ref))
parameters_ref = gpu(f32(parameters_ref))
echos_ref = simulate(CUDALibs(), pssfp_ref, parameters_ref);
echos_ref = collect(echos_ref)

echos = zeros(ComplexF32, nvoxels, nTR)
CompasToolkit.simulate_magnetization(context, echos, parameters, pssfp)
echos = transpose(echos)
echos = CompasToolkit.simulate_magnetization(parameters, pssfp)
echos = transpose(collect(echos))

println("fraction equal: ", sum(echos .≈ echos_ref) / length(echos))

Expand Down
7 changes: 3 additions & 4 deletions CompasToolkit.jl/examples/signal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using ComputationalResources
using LinearAlgebra
using StaticArrays

context = CompasToolkit.make_context(0)
context = CompasToolkit.init_context(0)

# First we assemble a Shepp Logan phantom with homogeneous T₁ and T₂
# but non-constant proton density and B₀
Expand All @@ -24,7 +24,7 @@ nvoxels = N*N

# Finally we assemble the phantom as an array of `T₁T₂B₀ρˣρʸxy` values
parameters_ref = map(T₁T₂B₀ρˣρʸxy, T₁, T₂, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(context, nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)
parameters = CompasToolkit.TissueParameters(nvoxels, T₁, T₂, B₁, B₀, real.(ρ), imag.(ρ), X, Y)

# Next, we assemble a balanced sequence with constant flip angle of 60 degrees,
nTR = N
Expand Down Expand Up @@ -60,7 +60,7 @@ k0 = [(-ns/2 * Δkˣ) + im * (py[mod1(r,N)] * Δkʸ) for r in 1:nr]; # starting
Δk = [Δkˣ + 0.0im for r in 1:nr]; # k-space steps per sample point for each readout

trajectory_ref = CartesianTrajectory(nr,ns,Δt_adc,k0,Δk,py);
trajectory = CompasToolkit.CartesianTrajectory(context, nr, ns, Float32(Δt_adc), ComplexF32.(k0), ComplexF32.(Δk[1]));
trajectory = CompasToolkit.CartesianTrajectory(nr, ns, Float32(Δt_adc), ComplexF32.(k0), ComplexF32.(Δk[1]));

# We use two different receive coils
ncoils = 4
Expand All @@ -80,7 +80,6 @@ signal_ref = collect(signal_ref)
signal_ref = reshape(signal_ref, ns, nr)

signal = CompasToolkit.magnetization_to_signal(
context,
echos,
parameters,
trajectory,
Expand Down
Loading

0 comments on commit 7491164

Please sign in to comment.