Skip to content

Commit

Permalink
limri: update PR #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
AGrigis committed Oct 17, 2023
1 parent 02df411 commit adb3fd7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 65 deletions.
3 changes: 1 addition & 2 deletions limri/norm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
"""

from .hist import hist_matching
from .minmax import minmax_matching
from .from_mean import from_mean_matching
from .minmax import minmax_matching, norm
37 changes: 0 additions & 37 deletions limri/norm/from_mean.py

This file was deleted.

24 changes: 23 additions & 1 deletion limri/norm/minmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
##########################################################################

"""
Histogram matching.
MinMax matching.
"""

# Imports
Expand Down Expand Up @@ -51,3 +51,25 @@ def minmax_matching(source, template, mask, concentration=2.):
matched = (source * concentration) / ref_val

return matched


def norm(source, ref_val, concentration=2.):
""" Simply normalize the intensities from a reference intensity value and
the associated compartment concentration.
Parameters
----------
source: np.ndarray
the image to transform.
ref_val: int
reference value of phantom intensity for the corresponding site.
concentration: float, default 2.
the compartment concentration in milli mols / litre.
Returns
-------
matched: np.ndarray
the transformed source image.
"""
matched = (source * concentration) / ref_val
return matched
4 changes: 0 additions & 4 deletions limri/regtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,16 @@ def antsregister(template_file, li_file, lianat_file, hanat_file, outdir,
lianat2hanat = ants.apply_transforms(
fixed=hanat, moving=lianat, transformlist=lianat2h["fwdtransforms"],
interpolator="bSpline")
print(f"{lianat2hanat=}")
filename = os.path.join(outdir, "lianat2hanat.nii.gz")
lianat2hanat.to_filename(filename)
print_result(f"lianat2h T1: {filename}")
filename = os.path.join(outdir, "lianat2hanat.png")
lianat2hanat.plot_ortho(
hanat, flat=True, xyz_lines=False, orient_labels=False,
title="lianat2hanat", filename=filename, overlay_alpha=0.5)

print_subtitle("Rigid: li -> hanat...")
li2hanat = ants.apply_transforms(
fixed=hanat, moving=li, transformlist=lianat2h["fwdtransforms"],
interpolator="bSpline")
print(f"{li2hanat=}")
filename = os.path.join(outdir, "li2hanat.nii.gz")
li2hanat.to_filename(filename)
print_result(f"li2h T1: {filename}")
Expand Down
39 changes: 20 additions & 19 deletions limri/workflows/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,60 @@
# Imports
import os
import nibabel
from limri.norm import hist_matching, minmax_matching, from_mean_matching
from limri.norm import hist_matching, minmax_matching, norm
from limri.color_utils import print_title, print_result

# Global parameters
NORM_MAP = {
"hist": hist_matching,
"minmax": minmax_matching,
"mean": from_mean_matching
"norm": norm
}


def li2mninorm(li2mni_file, mask_file, outdir, ref_value=None,
li2mniref_file=None, norm="hist"):
def li2mninorm(li2mni_file, mask_file, outdir, norm="hist", ref_value=None,
li2mniref_file=None):
""" Normalize intensities using histogram matching.
Parameters
----------
li2mni_file: str
path to the Li image.
li2mniref_file: str
path to the reference Li image.
ref_value: int
reference value of phantom intensity for the corresponding site.
mask_file: str
the brain mask image.
outdir: str
path to the destination folder.
norm: str, default 'hist'
the normalization method, can be: hist, minmax, mean.
the normalization method, can be: 'hist', 'minmax', 'norm'.
ref_value: int, default None
reference value of phantom intensity: needed for 'norm' normlaization.
li2mniref_file: str, default None
path to the reference Li image: needed for 'hist' and 'minmax'
normalization.
"""
print_title("Load data...")
li2mni = nibabel.load(li2mni_file)
li2mni_arr = li2mni.get_fdata()
if li2mniref_file:
if li2mniref_file is not None:
li2mniref = nibabel.load(li2mni_file)
li2mniref_arr = li2mniref.get_fdata()
mask = nibabel.load(mask_file)
mask_arr = mask.get_fdata()
if norm == "hist" or norm == "minmax" and li2mniref_file is None:
raise ValueError("We need the path to the reference Li image with"
"li2mniref_file argument for this type of "
"normalization method.")

raise ValueError("We need the path to the reference Li image "
"specified throught the 'li2mniref_file' argument "
"for this type of normalization method.")
if norm == "norm" and ref_value is None:
raise ValueError("We need reference phantom intensity value image"
"specified through the 'ref_value' argument for this "
"type of normalization method.")
print_title("Normalization...")
norm_fn = NORM_MAP.get(norm)
if norm_fn is None:
raise ValueError("Normalization method not defined.")
if norm != "mean":
if norm != "norm":
norm_arr = norm_fn(li2mni_arr, li2mniref_arr, mask_arr)
elif norm == "mean":
if ref_value is None:
raise ValueError("We need the ref_value for this type of "
"normalization method")
else:
norm_arr = norm_fn(li2mni_arr, ref_value)
norm = nibabel.Nifti1Image(norm_arr, li2mni.affine)
norm_file = os.path.join(outdir, "li2mninorm.nii.gz")
Expand Down
4 changes: 2 additions & 2 deletions limri/workflows/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def li2mni(li_file, lianat_file, hanat_file, outdir, li2lianat=None):
if not is_generated:
antsregister(
template_file=ref_file, lianat_file=lianat_bcorr_file,
li_file=li_file, hanat_file=hanat_bcorr_file, outdir=outdir,
li_file=li_reo_file, hanat_file=hanat_bcorr_file, outdir=outdir,
mask_file=mask_file)
else:
print_warning("li2mni transformation already computed")
Expand All @@ -109,7 +109,7 @@ def li2mni(li_file, lianat_file, hanat_file, outdir, li2lianat=None):
li2mni_file = os.path.join(outdir, "li2mni.nii.gz")
if not os.path.isfile(li2mni_file):
li2lianat = li2lianat or (0, 0, 0)
apply_translation(image_file=li_file, translation=li2lianat,
apply_translation(image_file=li_reo_file, translation=li2lianat,
filename=li2mni_file)
apply_transforms(
fixed_file=ref_file, moving_file=li2mni_file,
Expand Down

0 comments on commit adb3fd7

Please sign in to comment.