Skip to content

Commit

Permalink
update run_fastsurfer.sh for hypvinn
Browse files Browse the repository at this point in the history
formatting
  • Loading branch information
dkuegler committed Apr 18, 2024
1 parent efed6a8 commit f71d2fe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 27 deletions.
8 changes: 4 additions & 4 deletions FastSurferCNN/models/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
# limitations under the License.

# IMPORTS
from typing import Optional
from typing import Optional, TYPE_CHECKING

import numpy as np
import yacs.config
from torch import Tensor, nn
if TYPE_CHECKING:
import yacs.config

import FastSurferCNN.models.interpolation_layer as il
import FastSurferCNN.models.sub_module as sm



class FastSurferCNNBase(nn.Module):
"""
Network Definition of Fully Competitive Network network.
Expand Down Expand Up @@ -387,7 +387,7 @@ def forward(
}


def build_model(cfg: yacs.config.CfgNode) -> FastSurferCNN | FastSurferVINN:
def build_model(cfg: 'yacs.config.CfgNode') -> FastSurferCNN | FastSurferVINN:
"""
Build requested model.
Expand Down
10 changes: 5 additions & 5 deletions HypVINN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Run the HypVINN/run_pipeline.py which has the following arguments:
* `--seg_log` : Path to file in which run logs will be saved. If not set logs will be stored in /sd/sid/logs/hypvinn_seg.log
### Image processing options
* `--no_reg` : Deactivate registration of T2 to T1. If multi modal input is used; images need to be registered externally,
* `--reg_type` : Freesurfer Registration type to run. coreg : mri_coreg (Default) or robust : mri_robust_register.
* `--reg_mode` : Freesurfer Registration type to run. coreg : mri_coreg (Default) or robust : mri_robust_register.
* `--qc_snap`: Activate the creation of QC snapshots of the predicted HypVINN segmentation.
### FastSurfer Technical parameters (see FastSurfer documentation)
* `--device`
Expand Down Expand Up @@ -57,15 +57,15 @@ The pipeline can do all pre-processing by itself (step 1). This step can be skip
python HypVINN/run_pipeline.py --sid test_subject --sd /output \
--t1 /data/test_subject_t1.nii.gz \
--t2 /data/test_subject_t2.nii.gz \
--reg_type coreg \
--reg_mode coreg \
--seg_log /outdir/test_subject.log \
--batch_size 6
```
2. Run full pipeline only using a t1
```
python HypVINN/run_pipeline.py --sid test_subject --sd /output \
--t1 /data/test_subject_t1.nii.gz \
--reg_type coreg \
--reg_mode coreg \
--seg_log /outdir/test_subject.log \
--batch_size 6
```
Expand All @@ -75,7 +75,7 @@ The pipeline can do all pre-processing by itself (step 1). This step can be skip
python HypVINN/run_pipeline.py --sid test_subject --sd /output \
--t1 /data/test_subject_t1.nii.gz \
--t2 /data/test_subject_t2.nii.gz \
--reg_type coreg \
--reg_mode coreg \
--seg_log /outdir/test_subject.log \
--batch_size 6 --no_reg
```
Expand All @@ -85,7 +85,7 @@ The pipeline can do all pre-processing by itself (step 1). This step can be skip
python HypVINN/run_pipeline.py --sid test_subject --sd /output \
--t1 /data/test_subject_t1.nii.gz \
--t2 /data/test_subject_t2.nii.gz \
--reg_type coreg \
--reg_mode coreg \
--seg_log /outdir/test_subject.log \
--batch_size 6 --qc_snap
```
Expand Down
33 changes: 23 additions & 10 deletions HypVINN/run_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,31 @@ def option_parse():
parser = parser_defaults.add_arguments(
parser, ["t1"]
)

parser.add_argument('--t2', type=lambda x : None if x == 'None' else str(x), default=None,required=False, help="path to the T2 image to process")
parser.add_argument(
'--t2',
type=lambda x : None if x == 'None' else str(x),
default=None,
required=False,
help="Path to the T2 image to process.",
)

# 3. Image processing options
parser.add_argument("--no_reg", action='store_false', dest="registration", help="Deactivate registration of T2 to t1,"
"If multi mode is run images need to be register properly")

parser.add_argument("--qc_snap", action='store_true', dest="qc_snapshots",
help="Create qc snapshots")

parser.add_argument('--reg_type', type=str, default="coreg", choices=["coreg", "robust"],
help="Freesurfer Registration type to run. coreg: mri_coreg, robust : mri_robust_register ")
parser.add_argument(
"--qc_snap",
action='store_true',
dest="qc_snapshots",
help="Create qc snapshots",
)
parser.add_argument(
"--reg_mode",
type=str,
default="coreg",
choices=["none", "coreg", "robust"],
help="Freesurfer Registration type to run. coreg: mri_coreg, "
"robust : mri_robust_register, none: entirely deactivates "
"registration of T2 to T1, if both images are passed, "
"images need to be register properly externally.",
)



Expand Down
2 changes: 1 addition & 1 deletion HypVINN/utils/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def hyvinn_preproc(args):

LOGGER.info("Registering T1 to T2 ...")
args.t2 = t1_to_t2_registration(t1_path=args.t1, t2_path=args.t2, out_dir=args.out_dir,
registration_type=args.reg_type)
registration_type=args.reg_mode)
LOGGER.info("Registration finish in {:0.4f} seconds".format(time.time() - load_res))
else:
LOGGER.info(
Expand Down
1 change: 0 additions & 1 deletion recon_surf/N4_bias_correct.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ def main (options):


if __name__ == "__main__":

# Command Line options are error checking done here
options = options_parse()
sys.exit(main(options))
63 changes: 57 additions & 6 deletions run_fastsurfer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ reconsurfdir="$FASTSURFER_HOME/recon_surf"
# Regular flags defaults
subject=""
t1=""
# TODO include <T2_input>
t2=""
merged_segfile=""
cereb_segfile=""
asegdkt_segfile=""
asegdkt_segfile_default="\$SUBJECTS_DIR/\$SID/mri/aparc.DKTatlas+aseg.deep.mgz"
asegdkt_statsfile=""
cereb_statsfile=""
cereb_flags=()
hypo_segfile=""
hypo_statsfile=""
hypvinn_flags=()
conformed_name=""
seg_log=""
run_talairach_registration="false"
Expand All @@ -59,7 +62,7 @@ surf_flags=()
vox_size="min"
run_asegdkt_module="1"
run_cereb_module="1"
# TODO include <run_hypvinn_module>
run_hypvinn_module="1"
threads="1"
# python3.10 -s excludes user-directory package inclusion
python="python3.10 -s"
Expand Down Expand Up @@ -185,6 +188,23 @@ SEGMENTATION PIPELINE:
--no_biasfield Deactivate the calculation of partial volume-corrected
statistics.
HYPOTHALAMUS MODULE (HypVINN):
--no_hypvinn Skip the hypothalamus segmentation.
--no_biasfield This option implies --no_hypvinn, as the hypothalamus
sub-segmentation requires biasfield-corrected images.
--t2 <T2_input> *Optional* T2 full head input (does not have to be bias
corrected, a mandatory biasfield correction step is
performed). Requires an ABSOLUTE Path!
--reg_mode <none|coreg|robust>
Ignored, if no T2 image is passed.
Specifies the registration method used to register T1
and T2 images. Options are 'coreg' (default) for
mri_coreg, 'robust' for mri_robust_register, and 'none'
to skip registration (this requires T1 and T2 are
externally co-registered).
--qc_snap Create QC snapshots in \$SUBJECTS_DIR/\$sid/qc_snapshots
to simplify the QC process.
SURFACE PIPELINE:
--surf_only Run surface pipeline only. The segmentation input has
to exist already in this case.
Expand Down Expand Up @@ -246,15 +266,17 @@ Henschel L*, Kuegler D*, Reuter M. (*co-first). FastSurferVINN: Building
for HighRes Brain MRI. NeuroImage 251 (2022), 118933.
http://dx.doi.org/10.1016/j.neuroimage.2022.118933
For cerebellum sub-segmentation:
Faber J*, Kuegler D*, Bahrami E*, et al. (*co-first). 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
Estrada S, Kuegler D, Bahrami E, Xu P, Mousa D, Breteler MMB, Aziz NA, Reuter M. FastSurfer-HypVINN:
Automated sub-segmentation of the hypothalamus and adjacent structures on high-resolutional brain MRI.
Imaging Neuroscience 2023; 1 1–32.
https://doi.org/10.1162/imag_a_00034
For hypothalamus sub-segemntation:
Estrada S, Kuegler D, Bahrami E, Xu P, Mousa D, Breteler MMB, Aziz NA, Reuter M.
FastSurfer-HypVINN: Automated sub-segmentation of the hypothalamus and adjacent
structures on high-resolutional brain MRI. Imaging Neuroscience 2023; 1 1–32.
https://doi.org/10.1162/imag_a_00034
EOF
}
Expand Down Expand Up @@ -301,6 +323,11 @@ case $key in
shift # past argument
shift # past value
;;
--t2)
t2="$2"
shift # past argument
shift # past value
;;
--merged_segfile)
merged_segfile="$2"
shift # past argument
Expand Down Expand Up @@ -334,6 +361,26 @@ case $key in
shift # past argument
shift # past value
;;
--hypo_segfile)
hypo_segfile="$2"
shift # past argument
shift # past value
;;
--hypo_statsfile)
hypo_statsfile="$2"
shift # past argument
shift # past value
;;
--reg_mode)
mode=$(echo "$2" | tr "[:upper:]" "[:lower:]")
if [[ "$mode" =~ /^(none|coreg|robust)$/ ]] ; then
hypvinn_flags=("${hypvinn_flags[@]}" --regmode "$mode")
else
echo "Invalid --reg_mode option, must be 'none', 'coreg' or 'robust'."
fi
shift # past argument
shift # past value
;;
--mask_name)
mask_name="$2"
shift # past argument
Expand Down Expand Up @@ -400,6 +447,10 @@ case $key in
run_cereb_module="0"
shift # past argument
;;
--no_hypvinn)
run_hypvinn_module="0"
shift # past argument
;;
--tal_reg)
run_talairach_registration="true"
shift
Expand Down

0 comments on commit f71d2fe

Please sign in to comment.