Skip to content

Commit

Permalink
Implemented requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
engrosamaali91 committed Feb 21, 2024
1 parent c9ac5e1 commit fde9211
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 71 deletions.
14 changes: 2 additions & 12 deletions CerebNet/apply_warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@ def save_nii_image(img_data, save_path, header, affine):
The header information for the NIfTI file.
affine : ndarray
The affine matrix for the NIfTI file.
Returns
-------
None
This function returns nothing.
"""

img_out = nib.Nifti1Image(img_data, header=header, affine=affine)
print(f"Saving {save_path}")
nib.save(img_out, save_path)


def store_warped_data(img_path, lbl_path, warp_path, result_path, patch_size):
def main(img_path, lbl_path, warp_path, result_path, patch_size):

"""
Load, warp, crop, and save both an image and its corresponding label based on a given warp field.
Expand All @@ -64,11 +59,6 @@ def store_warped_data(img_path, lbl_path, warp_path, result_path, patch_size):
Directory path where the warped and cropped images will be saved.
patch_size : tuple of int
The dimensions (height, width, depth) cropped images after warping.
Returns
-------
None
This function returns nothing.
"""

img, img_file = utils.load_reorient_rescale_image(img_path)
Expand Down Expand Up @@ -120,7 +110,7 @@ def store_warped_data(img_path, lbl_path, warp_path, result_path, patch_size):

args = parser.parse_args()
warp_path = join(args.result_path, args.warp_filename)
store_warped_data(args.img_path,
main(args.img_path,
args.lbl_path,
warp_path=warp_path,
result_path=args.result_path,
Expand Down
13 changes: 11 additions & 2 deletions CerebNet/run_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def setup_options():
def main(args):
"""
Main function to run the inference based on the given command line arguments.
This implementation is inspired by methods described in CerebNet for cerebellum sub-segmentation.
Parameters
----------
Expand All @@ -112,8 +113,16 @@ def main(args):
Returns
-------
str or None
A message indicating the failure reason in case of an exception, otherwise `None`.
int
Returns 0 upon successful execution to indicate success.
str
A message indicating the failure reason in case of an exception.
References
----------
Faber J, Kuegler D, Bahrami E, et al. CerebNet: A fast and reliable deep-learning
pipeline for detailed cerebellum sub-segmentation. NeuroImage 264 (2022), 119703.
https://doi.org/10.1016/j.neuroimage.2022.119703
"""
cfg = get_config(args)
cfg.TEST.ENABLE = True
Expand Down
6 changes: 0 additions & 6 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
import os
sys.path.append(os.path.dirname(__file__) + '/..')
sys.path.append(os.path.dirname(__file__) + '/../recon_surf')
# sys.path.append(os.path.dirname(__file__) + '/../CerebNet')

# sys.path.insert(0, '..') #after

# autodoc_mock_imports = ["torch", "yacs"]


project = 'FastSurfer'
copyright = '2023, Martin Reuter'
Expand Down
34 changes: 20 additions & 14 deletions recon_surf/N4_bias_correct.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@


# IMPORTS
import image_io as iio
# Group 1: Python native modules
import argparse
import sys
import logging
import SimpleITK as sitk
import numpy as np

import sys
from pathlib import Path
from typing import Optional, cast, Tuple

# Group 2: External modules
import SimpleITK as sitk
import numpy as np
from numpy import typing as npt

# Group 3: Internal modules
import image_io as iio

HELPTEXT = """
Script to call SITK N4 Bias Correction
Expand Down Expand Up @@ -104,7 +108,7 @@ def options_parse():
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = argparse.ArgumentParser(
description=HELPTEXT,
Expand Down Expand Up @@ -277,7 +281,8 @@ def normalize_wm_aseg(
target_bg: float = 3.
) -> sitk.Image:
"""
Normalize WM image [MISSING].
Normalize WM image so the white matter has a mean
intensity of target_wm and the backgroud has intensity target_bg.
Parameters
----------
Expand Down Expand Up @@ -323,8 +328,8 @@ def normalize_wm_aseg(
def normalize_img(
itk_image: sitk.Image,
itk_mask: Optional[sitk.Image],
source_intensity: Tuple[float, float],
target_intensity: Tuple[float, float]
source_intensity: tuple[float, float],
target_intensity: tuple[float, float]
) -> sitk.Image:
"""
Normalize image by source and target intensity values.
Expand All @@ -334,10 +339,11 @@ def normalize_img(
itk_image : sitk.Image
Input image to be normalized.
itk_mask : sitk.Image | None
[MISSING].
source_intensity : Tuple[float, float]
Brain mask, voxels inside the mask are guaranteed to be > 0,
None is optional.
source_intensity : tuple[float, float]
Source intensity range.
target_intensity : Tuple[float, float]
target_intensity : tuple[float, float]
Target intensity range.
Returns
Expand Down Expand Up @@ -474,12 +480,12 @@ def get_image_mean(image: sitk.Image, mask: Optional[sitk.Image] = None) -> floa

def get_brain_centroid(itk_mask: sitk.Image) -> np.ndarray:
"""
Get the brain centroid from the itk_mask
Get the brain centroid from a binary image.
Parameters
----------
itk_mask : sitk.Image
[MISSING].
Binary image to compute the centroid of its labeled region.
Returns
-------
Expand Down
4 changes: 2 additions & 2 deletions recon_surf/fs_balabels.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@

def options_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = optparse.OptionParser(
version="$Id:fs_balabels.py,v 1.0 2022/08/24 21:22:08 mreuter Exp $",
Expand Down
2 changes: 1 addition & 1 deletion recon_surf/lta.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def writeLTA(
dst_header: Dict
) -> None:
"""
Write linear transform array info to a .lta file.
An .lta file.
Parameters
----------
Expand Down
4 changes: 2 additions & 2 deletions recon_surf/map_surf_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@

def options_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = optparse.OptionParser(
version="$Id:map_surf_label.py,v 1.0 2022/08/24 21:22:08 mreuter Exp $",
Expand Down
17 changes: 10 additions & 7 deletions recon_surf/paint_cc_into_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@


# IMPORTS
import numpy as np
import nibabel as nib

import sys
import argparse

import numpy as np
import nibabel as nib
from numpy import typing as npt

HELPTEXT = """
Expand All @@ -44,12 +44,12 @@

def argument_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = argparse.ArgumentParser(usage=HELPTEXT)
parser.add_argument(
Expand Down Expand Up @@ -87,14 +87,14 @@ def paint_in_cc(pred: npt.ArrayLike, aseg_cc: npt.ArrayLike) -> npt.ArrayLike:
Parameters
----------
pred : npt.ArrayLike
asegdt : npt.ArrayLike
Deep-learning prediction.
aseg_cc : npt.ArrayLike
Aseg segmentation with CC.
Returns
-------
pred
asegdt
Prediction with added CC.
"""
cc_mask = (aseg_cc >= 251) & (aseg_cc <= 255)
Expand All @@ -116,3 +116,6 @@ def paint_in_cc(pred: npt.ArrayLike, aseg_cc: npt.ArrayLike) -> npt.ArrayLike:
pred_with_cc_fin.to_filename(options.output)

sys.exit(0)


# TODO: Rename the file (paint_cc_into_asegdkt or similar) and functions.
4 changes: 2 additions & 2 deletions recon_surf/rewrite_mc_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

def options_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = optparse.OptionParser(
version="$Id: rewrite_mc_surface,v 1.1 2020/06/23 15:42:08 henschell $",
Expand Down
11 changes: 6 additions & 5 deletions recon_surf/rotate_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

# IMPORTS
import optparse
import numpy as np
import sys

import numpy as np
import nibabel.freesurfer.io as fs
import align_points as align

import align_points as align
from numpy import typing as npt


Expand Down Expand Up @@ -68,12 +69,12 @@

def options_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = optparse.OptionParser(
version="$Id: rotate_sphere.py,v 1.0 2022/03/18 21:22:08 mreuter Exp $",
Expand Down Expand Up @@ -125,7 +126,7 @@ def align_aparc_centroids(
Returns
-------
R
R : npt.NDArray[float]
Rotation Matrix.
"""
#nferiorparietal, inferiortemporal, lateraloccipital, postcentral, posteriorsingulate
Expand Down
12 changes: 7 additions & 5 deletions recon_surf/sample_parc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
# IMPORTS
import optparse
import sys

import numpy as np
import nibabel.freesurfer.io as fs
import nibabel as nib
from scipy import sparse
from scipy.sparse.csgraph import connected_components

from lapy import TriaMesh
from smooth_aparc import smooth_aparc
from numpy import typing as npt
Expand Down Expand Up @@ -65,12 +67,12 @@

def options_parse():
"""
Command line option parser.
Create a command line interface and return command line options.
Returns
-------
options
Object holding options.
Namespace object holding options.
"""
parser = optparse.OptionParser(
version="$Id: smooth_aparc,v 1.0 2018/06/24 11:34:08 mreuter Exp $",
Expand Down Expand Up @@ -102,7 +104,7 @@ def options_parse():

def construct_adj_cluster(tria, annot):
"""
Adjacency matrix of edges from same annotation label only.
Compute adjacency matrix of edges from same annotation label only.
Operates only on triangles and removes edges that cross annotation
label boundaries.
Expand Down Expand Up @@ -268,7 +270,7 @@ def sample_img(surf, img, cortex=None, projmm=0.0, radius=None):
Filename of cortex label or np.array with cortex indices.
projmm : float
Sample projmm mm along the surface vertex normals (default=0).
radius : float [optional] | None
radius : float, optional
If given and if the sample is equal to zero, then consider
all voxels inside this radius to find a non-zero value.
Expand Down Expand Up @@ -389,7 +391,7 @@ def sample_parc (surf, seg, imglut, surflut, outaparc, cortex=None, projmm=0.0,
Filename of cortex label or np.ndarray with cortex indices.
projmm : float
Sample projmm mm along the surface vertex normals (default=0).
radius : float [optional] | None
radius : float, optional
If given and if the sample is equal to zero, then consider
all voxels inside this radius to find a non-zero value.
"""
Expand Down
Loading

0 comments on commit fde9211

Please sign in to comment.