Skip to content

Commit

Permalink
Correct reg aladin values (#260)
Browse files Browse the repository at this point in the history
* changed remove_fake_values to pad bigger and reg_resample...
* working on flist average (forcing int16 type in average)
  • Loading branch information
davidmeunier79 authored May 22, 2024
1 parent 47449ae commit 95d5d84
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 117 deletions.
25 changes: 22 additions & 3 deletions macapype/nodes/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,17 @@ def average_align(list_img, reorient=False):
img_0 = nib.load(list_img[0])
path, fname, ext = split_f(list_img[0])

list_data = [img_0.get_data()]
data_0 = img_0.get_fdata()

data_0_shape = data_0.shape
list_data = [data_0]

for i, img in enumerate(list_img[1:]):
data_orig_shape = nib.load(img).get_fdata().shape
if data_orig_shape != data_0_shape:
print(f"**** Warning , original image shape \
{data_orig_shape} \
have a different shape than ref {data_0_shape}")

print("running flirt on {}".format(img))
flirt = fsl.FLIRT(dof=6)
Expand All @@ -306,11 +315,21 @@ def average_align(list_img, reorient=False):
out_file = flirt.run().outputs.out_file
print(out_file)

data = nib.load(out_file).get_data()
data = nib.load(out_file).get_fdata()
assert data.shape == data_0_shape, \
f"**** Error, flirt results {out_file} \
have a different shape {data.shape} \
than ref {data_0_shape}"

list_data.append(data)

avg_data = np.mean(np.array(list_data), axis=0)
print(avg_data.shape)

assert data_0_shape == avg_data.shape, \
f"Error with average shape {avg_data.shape} != {data_0_shape}"

# after casting type
avg_data = avg_data.astype(np.int16)

av_img_file = os.path.abspath("avg_" + fname + ext)
nib.save(nib.Nifti1Image(avg_data, header=img_0.header,
Expand Down
30 changes: 22 additions & 8 deletions macapype/nodes/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,14 @@ class IterREGBETInputSpec(CommandLineInputSpec):
mandatory=False, argstr="-refw %s")

k = traits.Bool(
False, usedefault=True,
position=3, argstr="-k",
False, usedefault=True, argstr="-k",
desc="Will keep temporary files",
mandatory=False)

p = traits.String(
desc="Prefix for running FSL functions\
(can be a path or just a prefix)",
position=3, argstr="-p %s")
argstr="-p %s")


class IterREGBETOutputSpec(TraitedSpec):
Expand Down Expand Up @@ -749,11 +748,26 @@ def pad_zero_mri(img_file, pad_val=10, const=0):
img_arr = np.array(img.dataobj)
img_arr_copy = np.copy(img_arr)

img_arr_copy_padded = np.pad(
img_arr_copy,
pad_width=pad_val,
mode='constant',
constant_values=const)
if isinstance(pad_val, int):
print("running with pad_val")

img_arr_copy_padded = np.pad(
img_arr_copy,
pad_width=pad_val,
mode='constant',
constant_values=const)

elif pad_val == 'double':

pad_val = np.max(img_arr.shape)

print(f"running with double pad_val = {pad_val}")

img_arr_copy_padded = np.pad(
img_arr_copy,
pad_width=pad_val,
mode='constant',
constant_values=const)

img_padded = nib.Nifti1Image(img_arr_copy_padded,
header=img.header,
Expand Down
167 changes: 87 additions & 80 deletions macapype/pipelines/full_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,111 +514,118 @@ def create_brain_segment_from_mask_pipe(
"prob_csf", "gen_5tt"]),
name='outputnode')

if "register_NMT_pipe" in params:
if "use_priors" in params["segment_atropos_pipe"].keys():

# register NMT template, template mask and priors to subject T1
register_NMT_pipe = create_register_NMT_pipe(
params_template=params_template,
params=parse_key(params, "register_NMT_pipe"))

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_NMT_pipe, "inputnode.T1")

brain_segment_pipe.connect(
inputnode, 'indiv_params',
register_NMT_pipe, "inputnode.indiv_params")
if "register_NMT_pipe" in params:

elif "reg" in params:
# Iterative registration to the INIA19 template
reg = NodeParams(
IterREGBET(),
params=parse_key(params, "reg"),
name='reg')
# register NMT template, template mask and priors to subject T1
register_NMT_pipe = create_register_NMT_pipe(
params_template=params_template,
params=parse_key(params, "register_NMT_pipe"))

reg.inputs.refb_file = params_template["template_brain"]
brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_NMT_pipe, "inputnode.T1")

brain_segment_pipe.connect(
inputnode, 'debiased_T1',
reg, 'inw_file')
brain_segment_pipe.connect(
inputnode, 'indiv_params',
register_NMT_pipe, "inputnode.indiv_params")

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
reg, 'inb_file')
elif "reg" in params:
# Iterative registration to the template
reg = NodeParams(
IterREGBET(),
params=parse_key(params, "reg"),
name='reg')

brain_segment_pipe.connect(
inputnode, ('indiv_params', parse_key, "reg"),
reg, 'indiv_params')
reg.inputs.refb_file = params_template["template_brain"]

if "template_seg" in params_template.keys():
reg.inputs.refw_file = params_template["template_head"]
reg.inputs.k = True

# seg
register_seg_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_seg_to_nat")
register_seg_to_nat.inputs.interp = "nearestneighbour"
brain_segment_pipe.connect(
inputnode, 'debiased_T1',
reg, 'inw_file')

register_seg_to_nat.inputs.in_file = params_template[
"template_seg"]
brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_seg_to_nat, 'reference')
reg, 'inb_file')

brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_seg_to_nat, "in_matrix_file")
inputnode, ('indiv_params', parse_key, "reg"),
reg, 'indiv_params')

else:
# gm
register_gm_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_gm_to_nat")
register_gm_to_nat.inputs.output_type = "NIFTI" # for SPM segment
register_gm_to_nat.inputs.interp = "nearestneighbour"
if "template_seg" in params_template.keys():

register_gm_to_nat.inputs.in_file = params_template["template_gm"]
# seg
register_seg_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_seg_to_nat")
register_seg_to_nat.inputs.interp = "nearestneighbour"

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_gm_to_nat, 'reference')
register_seg_to_nat.inputs.in_file = params_template[
"template_seg"]
brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_seg_to_nat, 'reference')

brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_gm_to_nat, "in_matrix_file")
brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_seg_to_nat, "in_matrix_file")

# wm
register_wm_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_wm_to_nat")
register_wm_to_nat.inputs.output_type = "NIFTI" # for SPM segment
register_wm_to_nat.inputs.interp = "nearestneighbour"
else:
# gm
register_gm_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_gm_to_nat")
register_gm_to_nat.inputs.output_type = "NIFTI" # for SPM
register_gm_to_nat.inputs.interp = "nearestneighbour"

register_wm_to_nat.inputs.in_file = params_template["template_wm"]
register_gm_to_nat.inputs.in_file = \
params_template["template_gm"]

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_wm_to_nat, 'reference')
brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_gm_to_nat, 'reference')

brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_wm_to_nat, "in_matrix_file")
brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_gm_to_nat, "in_matrix_file")

# csf
register_csf_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_csf_to_nat")
register_csf_to_nat.inputs.output_type = "NIFTI" # for SPM segment
register_csf_to_nat.inputs.interp = "nearestneighbour"
# wm
register_wm_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_wm_to_nat")
register_wm_to_nat.inputs.output_type = "NIFTI" # for SPM
register_wm_to_nat.inputs.interp = "nearestneighbour"

register_csf_to_nat.inputs.in_file = params_template[
"template_csf"]
register_wm_to_nat.inputs.in_file = \
params_template["template_wm"]

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_csf_to_nat, 'reference')
brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_wm_to_nat, 'reference')

brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_csf_to_nat, "in_matrix_file")
else:
print("##### Error, no coregistration method is defined")
return brain_segment_pipe
brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_wm_to_nat, "in_matrix_file")

# csf
register_csf_to_nat = pe.Node(
fsl.ApplyXFM(), name="register_csf_to_nat")
register_csf_to_nat.inputs.output_type = "NIFTI" # for SPM
register_csf_to_nat.inputs.interp = "nearestneighbour"

register_csf_to_nat.inputs.in_file = \
params_template["template_csf"]

brain_segment_pipe.connect(
inputnode, 'masked_debiased_T1',
register_csf_to_nat, 'reference')

brain_segment_pipe.connect(
reg, 'inv_transfo_file',
register_csf_to_nat, "in_matrix_file")
else:
print("##### Error, no coregistration method is defined")
return brain_segment_pipe

# ants Atropos
if "template_seg" in params_template.keys():
Expand Down
Loading

0 comments on commit 95d5d84

Please sign in to comment.