Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for MRIBase.rawdata and kDataCart #218

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion MRIBase/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ NFFTTools = "0.2.4"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[targets]
test = ["Test"]
test = ["Test", "Random"]

82 changes: 82 additions & 0 deletions MRIBase/test/Utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Utils
using MRIBase

export get_default_cartesian_raw_acq_data

function get_default_cartesian_raw_acq_data_2d(n_profiles, n_samples_per_profile, data = nothing)
params = Dict{String, Any}()
params["trajectory"] = "Cartesian"
params["encodedSize"] = [n_samples_per_profile, n_profiles]
params["encodedFOV"] = [n_samples_per_profile, n_profiles, 1]

traj_nodes = kspaceNodes(trajectory(Float32, "Cartesian", n_profiles, n_samples_per_profile))
traj_nodes = reshape(traj_nodes, 2, n_samples_per_profile, n_profiles)
if data == nothing
data = rand(ComplexF32, n_samples_per_profile, n_profiles)
end

profiles = Vector{Profile}(undef, n_profiles)
for i_profile in 1:n_profiles
profile_head = AcquisitionHeader(idx = EncodingCounters(kspace_encode_step_1 = i_profile - 1), center_sample = div(n_samples_per_profile, 2))
profiles[i_profile] = Profile(
profile_head,
traj_nodes[:, :, i_profile],
reshape(data[:, i_profile], n_samples_per_profile, 1),
)
end

f = RawAcquisitionData(
params,
profiles,
)
return f
end


function get_default_cartesian_raw_acq_data_3d(n_slices, n_profiles, n_samples_per_profile, data = nothing, mask=nothing)
params = Dict{String, Any}()
params["trajectory"] = "Cartesian"
params["encodedSize"] = [n_samples_per_profile, n_profiles, n_slices]
params["encodedFOV"] = [n_samples_per_profile, n_profiles, n_slices]

traj_nodes = kspaceNodes(trajectory(Float32, "Cartesian3D", n_profiles, n_samples_per_profile, numSlices=n_slices))
traj_nodes = reshape(traj_nodes, 3, n_samples_per_profile, n_profiles, n_slices)
if data == nothing
data = rand(ComplexF32, n_samples_per_profile, n_profiles, n_slices)
end

n_elemens = mask == nothing ? n_profiles*n_slices : sum(mask)
profiles = Vector{Profile}(undef, n_elemens)
i = 1
for i_profile in 1:n_profiles
for i_slice in 1:n_slices
if mask != nothing && !mask[1, i_profile, i_slice]
continue
end

profile_head = AcquisitionHeader(
idx = EncodingCounters(
kspace_encode_step_1 = i_profile - 1,
kspace_encode_step_2 = i_slice - 1,
),
center_sample = div(n_samples_per_profile, 2),
trajectory_dimensions=3,
)
# profiles[(i_slice-1)*n_profiles+i_profile] = Profile(
profiles[i] = Profile(
profile_head,
traj_nodes[:, :, i_profile, i_slice],
reshape(data[:, i_profile, i_slice], n_samples_per_profile, 1),
)
i += 1
end
end

f = RawAcquisitionData(
params,
profiles,
)
return f
end

end
6 changes: 5 additions & 1 deletion MRIBase/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Test, MRIBase

include("Utils.jl")

include("testTrajectories.jl")
include("testFlags.jl")
include("testConversion.jl")
include("testRawAcqData.jl")
include("testAcqData.jl")
include("testConversion.jl")
34 changes: 34 additions & 0 deletions MRIBase/test/testAcqData.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using .Utils: get_default_cartesian_raw_acq_data_2d, get_default_cartesian_raw_acq_data_3d


@testset "kDataCart restores the order when profiles are shuffled" begin
n_profiles = 10
n_samples_per_profile = 12
data = rand(ComplexF32, n_samples_per_profile, n_profiles)
f = get_default_cartesian_raw_acq_data_2d(n_profiles, n_samples_per_profile, data)
f.profiles = shuffle(f.profiles)
acq = AcquisitionData(f)
kdata = kDataCart(acq)[:, :, 1, 1, 1, 1]
@test isapprox(kdata, data)
end


@testset "kDataCart restores the order when profiles are shuffled for 3D data" begin
n_profiles = 10
n_samples_per_profile = 12
n_slices = 3
data = rand(ComplexF32, n_samples_per_profile, n_profiles, n_slices)
elipsoid_mask = zeros(Bool, 1, n_profiles, n_slices)
for i in 1:n_profiles
for j in 1:n_slices
elipsoid_mask[1, i, j] = (i-5)^2/25 + (j-2)^2/4 < 0.5
end
end
data .*= elipsoid_mask
f = get_default_cartesian_raw_acq_data_3d(n_slices, n_profiles, n_samples_per_profile, data, elipsoid_mask)
f.profiles = shuffle(f.profiles)
acq = AcquisitionData(f)
kdata = kDataCart(acq)[:, :, :, 1, 1, 1]

@test isapprox(kdata, data)
end
26 changes: 26 additions & 0 deletions MRIBase/test/testRawAcqData.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Random
using MRIBase
using .Utils: get_default_cartesian_raw_acq_data_2d


@testset "MRIBase.rawdata with dense 2D cartesian data" begin
n_profiles = 10
n_samples_per_profile = 12
data = rand(ComplexF32, n_samples_per_profile, n_profiles)
f = get_default_cartesian_raw_acq_data_2d(n_profiles, n_samples_per_profile, data)
kdata = MRIBase.rawdata(f)
kdata = reshape(kdata, n_samples_per_profile, n_profiles)
@test isapprox(kdata, data)
end


@testset "MRIBase.rawdata is not supposed to handle shuffles profiles" begin
n_profiles = 10
n_samples_per_profile = 12
data = rand(ComplexF32, n_samples_per_profile, n_profiles)
f = get_default_cartesian_raw_acq_data_2d(n_profiles, n_samples_per_profile, data)
shuffle!(f.profiles)
kdata = MRIBase.rawdata(f)
kdata = reshape(kdata, n_samples_per_profile, n_profiles)
@test !isapprox(kdata, data)
end
Loading