Skip to content

Commit

Permalink
Recon_surf and CerebNet API's
Browse files Browse the repository at this point in the history
  • Loading branch information
engrosamaali91 authored and dkuegler committed Feb 21, 2024
1 parent cc7eedc commit f6aefb9
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 206 deletions.
38 changes: 35 additions & 3 deletions CerebNet/apply_warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,52 @@
# limitations under the License.

# IMPORTS
from os.path import join
import numpy as np
import nibabel as nib

from os.path import join
from CerebNet.datasets import utils


def save_nii_image(img_data, save_path, header, affine):
"""
Save an image data array as a NIfTI file.
Parameters
----------
img_data : ndarray
The image data to be saved.
save_path : str
The path (including file name) where the image will be saved.
header : nibabel.Nifti1Header
The header information for the NIfTI file.
affine : ndarray
The affine matrix for the NIfTI file.
"""

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.
Parameters
----------
img_path : str
Path to the T1-weighted MRI image to be warped.
lbl_path : str
Path to the label image corresponding to the T1 image, to be warped similarly.
warp_path : str
Path to the warp field file used to warp the images.
result_path : str
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.
"""

img, img_file = utils.load_reorient_rescale_image(img_path)

Expand Down Expand Up @@ -78,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
12 changes: 7 additions & 5 deletions CerebNet/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@

# IMPORTS
import time
import nibabel as nib
import numpy as np
import torch

from os import makedirs
from os.path import join, dirname, isfile
from typing import Dict, List, Tuple, Optional
from concurrent.futures import Future, ThreadPoolExecutor

import nibabel as nib
import numpy as np
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm

from FastSurferCNN.utils import logging
from FastSurferCNN.utils.threads import get_num_threads
from FastSurferCNN.utils.mapper import JsonColorLookupTable, TSVLookupTable
Expand All @@ -44,6 +43,9 @@


class Inference:
"""
Manages inference operations, including batch processing, data loading, and model predictions for neuroimaging data.
"""
def __init__(
self,
cfg: "yacs.ConfigNode",
Expand Down
30 changes: 30 additions & 0 deletions CerebNet/run_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@


def setup_options():
"""
Configure and return an argument parser for the segmentation script.
Returns
-------
argparse.ArgumentParser
The configured argument parser.
"""
# Training settings
parser = argparse.ArgumentParser(description="Segmentation")

Expand Down Expand Up @@ -94,6 +102,28 @@ 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
----------
args : argparse.Namespace
Command line arguments parsed by `argparse.ArgumentParser`.
Returns
-------
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
cfg.TRAIN.ENABLE = False
Expand Down
2 changes: 1 addition & 1 deletion doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ FastSurfer API
FastSurferCNN.utils.rst
FastSurferCNN.data_loader.rst
recon_surf.rst

CerebNet.rst
11 changes: 11 additions & 0 deletions doc/api/recon_surf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ API recon_surf References
align_points
align_seg
create_annotation
fs_balabels
lta
map_surf_label
N4_bias_correct
paint_cc_into_pred
rewrite_mc_surface
rotate_sphere
sample_parc
smooth_aparc
spherically_project_wrapper




4 changes: 0 additions & 4 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
import os
sys.path.append(os.path.dirname(__file__) + '/..')
sys.path.append(os.path.dirname(__file__) + '/../recon_surf')
# sys.path.insert(0, '..') #after

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


project = 'FastSurfer'
copyright = '2023, Martin Reuter'
Expand Down
Loading

0 comments on commit f6aefb9

Please sign in to comment.