-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow custom kernels
- Loading branch information
Korbinian Eckstein
committed
Mar 15, 2024
1 parent
adf437a
commit d523c5d
Showing
4 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using DICOM, QuantitativeSusceptibilityMappingTGV, CUDA, MriResearchTools, NPZ | ||
|
||
|
||
fn_phase = "/home/korbi/data/MRI/data/josef_streaking/P141_3depi.dicom.non-interpolated/EnhancedMRImage_EPI3D_sag_0.67mm_iso_Pha_00001.dcm" | ||
fn_mag = "/home/korbi/data/MRI/data/josef_streaking/P141_3depi.dicom.non-interpolated/EnhancedMRImage_EPI3D_sag_0.67mm_iso_Mag_00003.dcm" | ||
fn_mask = "/home/korbi/data/MRI/data/josef_streaking/P141_3depi.dicom.non-interpolated/wip_QSM_20240222T074855_Mask.npy" | ||
|
||
phase = dcm_parse(fn_phase)[tag"PixelData"] .* 2π ./ 4096 .- π | ||
mag = dcm_parse(fn_mag)[tag"PixelData"] | ||
mask = npzread(fn_mask) .!= 0 | ||
|
||
savenii(mask, "mask", ".") | ||
|
||
voxel_size = [0.67, 0.67, 0.67] # in [mm] | ||
TE = 0.035 | ||
B0 = 2.8949 | ||
B0_dir = [1, 0, 0] | ||
|
||
chi = qsm_tgv(phase, mask, voxel_size; TE=TE, fieldstrength=B0, B0_dir=B0_dir, gpu=CUDA) | ||
savenii(chi, "chi", ".") | ||
|
||
|
||
|
||
|
||
|
||
## | ||
|
||
|
||
phase = dcm_parse("/home/korbi/data/MRI/Siemens_QSM/data/EnhancedMRImage_3depi_0.65iso_te27_ef17_00001.dcm")[tag"PixelData"] .* 2π ./ 4096 .- π | ||
mag = dcm_parse("/home/korbi/data/MRI/Siemens_QSM/data/EnhancedMRImage_3depi_0.65iso_te27_ef17_00002.dcm")[tag"PixelData"] | ||
mask = dcm_parse("/home/korbi/data/MRI/Siemens_QSM/data/EnhancedMRImage_3depi_0.65iso_te27_ef17_Qsm_00002.dcm")[tag"PixelData"] .!= 0x0800 | ||
|
||
minp, maxp = extrema(phase) | ||
|
||
savenii(mask, "mask", ".") | ||
|
||
voxel_size = [0.325521, 0.325521, 0.65] # in [mm] | ||
TE = 0.027 # in [s] | ||
fieldstrength = 2.89497 # in [T] | ||
B0_dir = [0, 0, 1] | ||
|
||
chi_orig = qsm_tgv(phase, mask, voxel_size; iterations=1000:1000:8000, TE=TE, fieldstrength=fieldstrength, gpu=CUDA, B0_dir=B0_dir, orig_kernel=true); | ||
|
||
for (i, chi) in enumerate(chi_orig) | ||
savenii(chi, "chi_orig_$(i*1000)", ".") | ||
end | ||
|
||
# chi_orig = qsm_tgv(phase, mask, voxel_size; iterations=500, TE=TE, fieldstrength=fieldstrength, gpu=CUDA, B0_dir=B0_dir); | ||
# savenii(chi_orig, "chi_orig_500", ".") | ||
|
||
## First time install NPZ for reading npy files | ||
# import Pkg | ||
# Pkg.add("NPZ") | ||
|
||
|
||
using QuantitativeSusceptibilityMappingTGV, MriResearchTools, NPZ, CUDA | ||
|
||
mask = npzread("/home/korbi/data/MRI/data/josef_ice_run/wip_iSWI_20220913T080705_Mask_2.npy") .!= 0; # convert to boolean | ||
phase = npzread("/home/korbi/data/MRI/data/josef_ice_run/wip_iSWI_20220913T080705_Pha_2.npy") | ||
|
||
voxel_size = [1.0, 1.0, 1.0] # in [mm] | ||
TE = 0.021 # in [s] | ||
fieldstrength = 3 # in [T] | ||
B0_dir = [0, 0, 1] | ||
|
||
chi = qsm_tgv(phase, mask, voxel_size; TE=TE, fieldstrength=fieldstrength, gpu=CUDA, B0_dir=B0_dir); | ||
chi_orig = qsm_tgv(phase, mask, voxel_size; TE=TE, fieldstrength=fieldstrength, gpu=CUDA, B0_dir=B0_dir, orig_kernel=true); | ||
|
||
savenii(chi, "chi", ".") | ||
savenii(chi_orig, "chi_orig", ".") | ||
savenii((chi .- chi_orig) ./ chi_orig, "chi_relative", ".") # relative difference | ||
|
||
# Change regularization strength (1-4) | ||
# chi = qsm_tgv(phase, mask, res; TE, fieldstrength, regularization=1); | ||
|
||
## | ||
|
||
using MriResearchTools, QuantitativeSusceptibilityMappingTGV, CUDA | ||
|
||
phase = readphase("/home/korbi/data/MRI/data/ashley_qsm_test/bids-3depi/sub-1/ses-064mm-epi/anat/sub-1_ses-064mm-epi_run-1_part-phase_T2starw.nii") | ||
mask = niread("/home/korbi/data/MRI/data/ashley_qsm_test/result/mask.nii") .!= 0 | ||
|
||
voxel_size = [1.0, 1.0, 1.0] # in [mm] | ||
TE = 0.021 # in [s] | ||
fieldstrength = 3 # in [T] | ||
B0_dir = [0, 0, 1] | ||
|
||
chi_orig = qsm_tgv(phase, mask, voxel_size; TE, fieldstrength, orig_kernel=true, gpu=CUDA); | ||
chi_st = qsm_tgv(phase, mask, voxel_size; TE, fieldstrength, orig_kernel=:test, gpu=CUDA); | ||
|
||
|
||
savenii(chi_orig, "chi_orig", ".") | ||
savenii(chi_st, "chi_st", ".") | ||
|
||
|
||
savenii((chi_st .- chi_orig) ./ abs.(chi_orig), "chi_st_relative", ".") # relative differenceswapdims(phase, 1, 3) | ||
savenii(chi_st .- chi_orig, "chi_diff", ".") | ||
|
||
## Rotation | ||
|
||
# Rotate 90 degrees around y-axis | ||
phase_x = permutedims(phase, [3, 2, 1]) | ||
mask_x = permutedims(mask, [3, 2, 1]) | ||
chi_x = qsm_tgv(phase_x, mask_x, voxel_size; TE, fieldstrength, orig_kernel=dipole_kernel_orig_x(voxel_size), gpu=CUDA); | ||
savenii(chi_x, "chi_x", ".") | ||
|
||
## | ||
|
||
function dipole_kernel_orig_z(res) | ||
dipole_kernel = zeros(Float32, 3, 3, 3) | ||
dipole_kernel[3, 2, 2] = 1/3 / res[1]^2 | ||
dipole_kernel[2, 3, 2] = 1/3 / res[2]^2 | ||
dipole_kernel[2, 2, 3] = -2/3 / res[3]^2 | ||
dipole_kernel[2, 2, 1] = -2/3 / res[3]^2 | ||
dipole_kernel[2, 1, 2] = 1/3 / res[2]^2 | ||
dipole_kernel[1, 2, 2] = 1/3 / res[1]^2 | ||
dipole_kernel[2, 2, 2] = -sum(dipole_kernel) | ||
return dipole_kernel | ||
end | ||
|
||
function dipole_kernel_orig_x(res) | ||
dipole_kernel = zeros(Float32, 3, 3, 3) | ||
dipole_kernel[3, 2, 2] = -2/3 / res[1]^2 | ||
dipole_kernel[2, 3, 2] = 1/3 / res[2]^2 | ||
dipole_kernel[2, 2, 3] = 1/3 / res[3]^2 | ||
dipole_kernel[2, 2, 1] = 1/3 / res[3]^2 | ||
dipole_kernel[2, 1, 2] = 1/3 / res[2]^2 | ||
dipole_kernel[1, 2, 2] = -2/3 / res[1]^2 | ||
dipole_kernel[2, 2, 2] = -sum(dipole_kernel) | ||
return dipole_kernel | ||
end | ||
|
||
function dipole_kernel_orig_y(res) | ||
dipole_kernel = zeros(Float32, 3, 3, 3) | ||
dipole_kernel[3, 2, 2] = 1/3 / res[1]^2 | ||
dipole_kernel[2, 3, 2] = -2/3 / res[2]^2 | ||
dipole_kernel[2, 2, 3] = 1/3 / res[3]^2 | ||
dipole_kernel[2, 2, 1] = 1/3 / res[3]^2 | ||
dipole_kernel[2, 1, 2] = -2/3 / res[2]^2 | ||
dipole_kernel[1, 2, 2] = 1/3 / res[1]^2 | ||
dipole_kernel[2, 2, 2] = -sum(dipole_kernel) | ||
return dipole_kernel | ||
end |