Skip to content

Commit

Permalink
Added --save-phase and --suffix parameters to entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
astewartau committed Feb 7, 2024
1 parent 77c414f commit 668a62c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ if __name__ == "__main__":
tissue_params = qsm_forward.TissueParams(root_dir="~/data", chi="ChiModelMIX.nii.gz")

recon_params_all = [
qsm_forward.ReconParams(voxel_size=voxel_size, session=session, TEs=TEs, TR=TR, flip_angle=flip_angle, suffix=suffix, export_phase=export_phase)
for (voxel_size, session, TEs, TR, flip_angle, suffix, export_phase) in [
qsm_forward.ReconParams(voxel_size=voxel_size, session=session, TEs=TEs, TR=TR, flip_angle=flip_angle, suffix=suffix, save_phase=save_phase)
for (voxel_size, session, TEs, TR, flip_angle, suffix, save_phase) in [
(np.array([0.64, 0.64, 0.64]), "0p64", np.array([3.5e-3]), 7.5e-3, 40, "T1w", False),
(np.array([0.64, 0.64, 0.64]), "0p64", np.array([0.004, 0.012, 0.02, 0.028]), 0.05, 15, "T2starw", True),
]
Expand Down
4 changes: 2 additions & 2 deletions qsm_forward/examples/head_phantom_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
tissue_params = qsm_forward.TissueParams(root_dir="~/data")

recon_params_all = [
qsm_forward.ReconParams(voxel_size=voxel_size, TEs=TEs, TR=TR, flip_angle=flip_angle, suffix=suffix, export_phase=export_phase)
for (voxel_size, TEs, TR, flip_angle, suffix, export_phase) in [
qsm_forward.ReconParams(voxel_size=voxel_size, TEs=TEs, TR=TR, flip_angle=flip_angle, suffix=suffix, save_phase=save_phase)
for (voxel_size, TEs, TR, flip_angle, suffix, save_phase) in [
(np.array([1.0, 1.0, 1.0]), np.array([0.004, 0.012, 0.02, 0.028]), 0.05, 15, "T2starw", True),
(np.array([1.0, 1.0, 1.0]), np.array([0.0035]), 0.0075, 40, "T1w", False)
]
Expand Down
8 changes: 6 additions & 2 deletions qsm_forward/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ def argparse_bool(user_in):
sub_parser.add_argument('--run', default=None),
sub_parser.add_argument('--TR', default=50e-3, type=float),
sub_parser.add_argument('--TEs', default=[ 4e-3, 12e-3, 20e-3, 28e-3 ], type=float, nargs='+')
sub_parser.add_argument('--flip_angle', default=15)
sub_parser.add_argument('--flip_angle', default=15, type=float)
sub_parser.add_argument('--B0', default=7, type=float)
sub_parser.add_argument('--B0-dir', default=[0., 0., 1.], type=float, nargs=3)
sub_parser.add_argument('--suffix', default='MEGRE')
sub_parser.add_argument('--generate-phase-offset', nargs='?', type=argparse_bool, const=True, default=True)
sub_parser.add_argument('--generate-shim-field', nargs='?', type=argparse_bool, const=True, default=True)
sub_parser.add_argument('--voxel-size', default=[1., 1., 1.], type=float, nargs=3)
sub_parser.add_argument('--peak-snr', default=np.inf, type=float)
sub_parser.add_argument('--random-seed', default=None, type=int)
sub_parser.add_argument('--save-phase', nargs='?', type=argparse_bool, const=True, default=None)
sub_parser.add_argument('--save-chi', nargs='?', type=argparse_bool, const=True, default=True)
sub_parser.add_argument('--save-mask', nargs='?', type=argparse_bool, const=True, default=True)
sub_parser.add_argument('--save-segmentation', nargs='?', type=argparse_bool, const=True, default=True)
Expand Down Expand Up @@ -93,7 +95,9 @@ def argparse_bool(user_in):
generate_shim_field=True,
voxel_size=np.array(args.voxel_size),
peak_snr=args.peak_snr,
random_seed=args.random_seed
random_seed=args.random_seed,
save_phase=args.save_phase,
suffix=args.suffix
)

qsm_forward.generate_bids(
Expand Down
16 changes: 8 additions & 8 deletions qsm_forward/qsm_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class ReconParams:
Random seed to use for noise.
suffix : string
The BIDS-compliant suffix that defines the weighting of the images (e.g. T1w, T2starw, PD).
export_phase : bool
Boolean to control whether phase images are exported.
save_phase : bool
Boolean to control whether phase images are saved.
"""

def __init__(
Expand All @@ -166,7 +166,7 @@ def __init__(
peak_snr=np.inf,
random_seed=None,
suffix="T2starw",
export_phase=True
save_phase=True
):
self.subject = subject
self.session = session
Expand All @@ -184,7 +184,7 @@ def __init__(
self.peak_snr = peak_snr
self.random_seed = random_seed
self.suffix = suffix
self.export_phase = export_phase
self.save_phase = save_phase

def rotation_matrix_from_vectors(vec1, vec2):
""" Compute the rotation matrix that aligns vec1 to vec2 """
Expand Down Expand Up @@ -328,10 +328,10 @@ def generate_bids(tissue_params: TissueParams, recon_params: ReconParams, bids_d
del sigHR_cropped

# save nifti images
mag_filename = f"{recon_name_i}" + ("_part-mag" if recon_params.export_phase else "") + f"_{recon_params.suffix}"
phs_filename = f"{recon_name_i}" + ("_part-phase" if recon_params.export_phase else "") + f"_{recon_params.suffix}"
mag_filename = f"{recon_name_i}" + ("_part-mag" if recon_params.save_phase else "") + f"_{recon_params.suffix}"
phs_filename = f"{recon_name_i}" + ("_part-phase" if recon_params.save_phase else "") + f"_{recon_params.suffix}"
nib.save(nib.Nifti1Image(dataobj=np.abs(sigHR_cropped_noisy), affine=chi_downsampled_nii.affine, header=chi_downsampled_nii.header), filename=os.path.join(subject_dir, "anat", f"{mag_filename}.nii"))
if recon_params.export_phase: nib.save(nib.Nifti1Image(dataobj=np.angle(sigHR_cropped_noisy), affine=chi_downsampled_nii.affine, header=chi_downsampled_nii.header), filename=os.path.join(subject_dir, "anat", f"{phs_filename}.nii"))
if recon_params.save_phase: nib.save(nib.Nifti1Image(dataobj=np.angle(sigHR_cropped_noisy), affine=chi_downsampled_nii.affine, header=chi_downsampled_nii.header), filename=os.path.join(subject_dir, "anat", f"{phs_filename}.nii"))

# json header
print(f"Creating JSON headers...")
Expand All @@ -357,7 +357,7 @@ def generate_bids(tissue_params: TissueParams, recon_params: ReconParams, bids_d

with open(os.path.join(subject_dir, "anat", f"{mag_filename}.json"), 'w') as mag_json_file:
json.dump(json_dict_mag, mag_json_file)
if recon_params.export_phase:
if recon_params.save_phase:
with open(os.path.join(subject_dir, "anat", f"{phs_filename}.json"), 'w') as phs_json_file:
json.dump(json_dict_phs, phs_json_file)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='qsm-forward',
version='0.19',
version='0.20',
packages=find_packages(),
url='https://github.com/astewartau/qsm-forward',
author='Ashley Stewart',
Expand Down

0 comments on commit 668a62c

Please sign in to comment.