Skip to content

Commit

Permalink
Fixes to the commandline etc
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuegler committed Apr 19, 2024
1 parent 950ae6d commit b572bae
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 160 deletions.
6 changes: 3 additions & 3 deletions CerebNet/run_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
from pathlib import Path

from FastSurferCNN.utils import logging, parser_defaults, Plane, PLANES
from CerebNet.utils.load_config import get_config
from CerebNet.inference import Inference
from FastSurferCNN.utils.checkpoint import (
get_checkpoints,
load_checkpoint_config_defaults,
YAML_DEFAULT as CHECKPOINT_PATHS_FILE,
)
from FastSurferCNN.utils.common import assert_no_root, SubjectList
from CerebNet.inference import Inference
from CerebNet.utils.checkpoint import YAML_DEFAULT as CHECKPOINT_PATHS_FILE
from CerebNet.utils.load_config import get_config

logger = logging.get_logger(__name__)
DEFAULT_CEREBELLUM_STATSFILE = Path("stats/cerebellum.CerebNet.stats")
Expand Down
39 changes: 6 additions & 33 deletions CerebNet/utils/load_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@


# IMPORTS
import argparse
import sys
from typing import TYPE_CHECKING

if TYPE_CHECKING:
import yacs.config

from CerebNet.config import get_cfg_cerebnet
from FastSurferCNN.utils import PLANES


def get_config(args) -> "yacs.CfgNode":
def get_config(args) -> "yacs.config.CfgNode":
"""
Given the arguments, load and initialize the config_files.
"""
Expand All @@ -35,8 +37,8 @@ def get_config(args) -> "yacs.CfgNode":
if hasattr(args, "rng_seed"):
cfg.RNG_SEED = args.rng_seed
if hasattr(args, "out_dir"):
cfg.LOG_DIR = str(args.out_dir)

cfg.LOG_DIR = args.out_dir
path_ax, path_sag, path_cor = [
getattr(args, name) for name in ["ckpt_ax", "ckpt_sag", "ckpt_cor"]
]
Expand All @@ -50,32 +52,3 @@ def get_config(args) -> "yacs.CfgNode":
cfg.TEST.BATCH_SIZE = batch_size

return cfg


def setup_options():
"""
Set up the command-line options for the segmentation.
Returns
-------
argparse.Namespace
The configured argument parser.
"""
parser = argparse.ArgumentParser(description="Segmentation")
parser.add_argument(
"--cfg",
dest="cfg_file",
help="Path to the config file",
default="config_files/CerebNet.yaml",
type=str,
)
parser.add_argument(
"opts",
help="See CerebNet/config/cerebnet.py for all options",
default=None,
nargs=argparse.REMAINDER,
)

if len(sys.argv) == 1:
parser.print_help()
return parser.parse_args()
60 changes: 33 additions & 27 deletions FastSurferCNN/run_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,13 @@ def main(
*,
orig_name: Path | str,
out_dir: Path,
segfile: str,
pred_name: str,
ckpt_ax: Path,
ckpt_sag: Path,
ckpt_cor: Path,
cfg_ax: Path,
cfg_sag: Path,
cfg_cor: Path,
seg_log: Path,
qc_log: str = "",
log_name: str = "",
allow_root: bool = False,
Expand All @@ -631,7 +630,7 @@ def main(
threads: int = -1,
conform_to_1mm_threshold: float = 0.95,
**kwargs,
):
) -> Literal[0] | str:
# Warning if run as root user
allow_root or assert_no_root()

Expand All @@ -658,7 +657,7 @@ def main(

config = SubjectDirectoryConfig(
orig_name=orig_name,
pred_name=segfile,
pred_name=pred_name,
conf_name=conf_name,
in_dir=in_dir,
csv_file=csv_file,
Expand All @@ -668,29 +667,36 @@ def main(
remove_suffix=remove_suffix,
out_dir=out_dir,
)
config.copy_org_name = "mri/orig/001.mgz"

# Get all subjects of interest
subjects = SubjectList(config, segfile="pred_name", copy_orig_name="copy_orig_name")
subjects.make_subjects_dir()

# Set Up Model
eval = RunModelOnData(
lut=lut,
ckpt_ax=ckpt_ax,
ckpt_sag=ckpt_sag,
ckpt_cor=ckpt_cor,
cfg_ax=cfg_ax,
cfg_sag=cfg_sag,
cfg_cor=cfg_cor,
device=device,
viewagg_device=viewagg_device,
threads=threads,
batch_size=batch_size,
vox_size=vox_size,
async_io=async_io,
conform_to_1mm_threshold=conform_to_1mm_threshold,
)
config.copy_orig_name = "mri/orig/001.mgz"

try:
# Get all subjects of interest
subjects = SubjectList(
config,
segfile="pred_name",
copy_orig_name="copy_orig_name",
)
subjects.make_subjects_dir()

# Set Up Model
eval = RunModelOnData(
lut=lut,
ckpt_ax=ckpt_ax,
ckpt_sag=ckpt_sag,
ckpt_cor=ckpt_cor,
cfg_ax=cfg_ax,
cfg_sag=cfg_sag,
cfg_cor=cfg_cor,
device=device,
viewagg_device=viewagg_device,
threads=threads,
batch_size=batch_size,
vox_size=vox_size,
async_io=async_io,
conform_to_1mm_threshold=conform_to_1mm_threshold,
)
except RuntimeError as e:
return e.args[0]

qc_failed_subject_count = 0

Expand Down
11 changes: 8 additions & 3 deletions FastSurferCNN/utils/parser_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class SubjectDirectoryConfig:
flags=("--csv_file",),
default=None,
help="Csv-file with subjects to analyze (alternative to --tag)",
),
)
sid: Optional[str] = field(
flags=("--sid",),
default=None,
Expand All @@ -191,7 +191,7 @@ class SubjectDirectoryConfig:
default="*",
help="Search tag to process only certain subjects. If a single image should be "
"analyzed, set the tag with its id. Default: processes all.",
),
)
brainmask_name: str = field(
default="mri/mask.mgz",
help="Name under which the brainmask image will be saved, in the same "
Expand Down Expand Up @@ -271,7 +271,12 @@ class SubjectDirectoryConfig:
"(no memory check will be done).",
),
"in_dir": __arg("--in_dir", dc=SubjectDirectoryConfig, fieldname="in_dir"),
"tag": __arg("--tag", type=unquote_str, dc=SubjectDirectoryConfig, fieldname="search_tag"),
"tag": __arg(
"--tag",
type=unquote_str,
dc=SubjectDirectoryConfig,
fieldname="search_tag",
),
"csv_file": __arg("--csv_file", dc=SubjectDirectoryConfig),
"batch_size": __arg(
"--batch_size",
Expand Down
74 changes: 37 additions & 37 deletions doc/overview/OUTPUT_FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

The segmentation module outputs the files shown in the table below. The two primary output files are the `aparc.DKTatlas+aseg.deep.mgz` file, which contains the FastSurfer segmentation of cortical and subcortical structures based on the DKT atlas, and the `aseg+DKT.stats` file, which contains summary statistics for these structures. Note, that the surface model (downstream) corrects these segmentations along the cortex with the created surfaces. So if the surface model is used, it is recommended to use the updated segmentations and stats (see below).

| directory | filename | module | description |
|:------------|-------------------------------|-----------|-------------|
| mri | aparc.DKTatlas+aseg.deep.mgz | asegdkt | cortical and subcortical segmentation|
| mri | aseg.auto_noCCseg.mgz | asegdkt | simplified subcortical segmentation without corpus callosum labels|
| mri | mask.mgz | asegdkt | brainmask|
| mri | orig.mgz | asegdkt | conformed image|
| mri | orig_nu.mgz | asegdkt | biasfield-corrected image|
| mri/orig | 001.mgz | asegdkt | original image|
| scripts | deep-seg.log | asegdkt | logfile|
| stats | aseg+DKT.stats | asegdkt | table of cortical and subcortical segmentation statistics|
| directory | filename | module | description |
|:----------|------------------------------|---------|--------------------------------------------------------------------|
| mri | aparc.DKTatlas+aseg.deep.mgz | asegdkt | cortical and subcortical segmentation |
| mri | aseg.auto_noCCseg.mgz | asegdkt | simplified subcortical segmentation without corpus callosum labels |
| mri | mask.mgz | asegdkt | brainmask |
| mri | orig.mgz | asegdkt | conformed image |
| mri | orig_nu.mgz | asegdkt | biasfield-corrected image |
| mri/orig | 001.mgz | asegdkt | original image |
| scripts | deep-seg.log | asegdkt | logfile |
| stats | aseg+DKT.stats | asegdkt | table of cortical and subcortical segmentation statistics |

## Cerebnet module

The cerebellum module outputs the files in the table shown below. Unless switched off by the `--no_cereb` argument, this module is automatically run whenever the segmentation module is run. It adds two files, an image with the sub-segmentation of the cerebellum and a text file with summary statistics.


| directory | filename | module | description |
|:------------|-------------------------------|-----------|-------------|
| mri | cerebellum.CerebNet.nii.gz | cerebnet | cerebellum sub-segmentation|
| stats | cerebellum.CerebNet.stats | cerebnet | table of cerebellum segmentation statistics|
| directory | filename | module | description |
|:----------|----------------------------|----------|---------------------------------------------|
| mri | cerebellum.CerebNet.nii.gz | cerebnet | cerebellum sub-segmentation |
| stats | cerebellum.CerebNet.stats | cerebnet | table of cerebellum segmentation statistics |

## HypVINN module

Expand Down Expand Up @@ -51,26 +51,26 @@ After running this module, some of the initial segmentations and corresponding v

The primary output files are pial, white, and inflated surface files, the thickness overlay files, and the cortical parcellation (annotation) files. The preferred way of assessing this output is the [FreeView](https://surfer.nmr.mgh.harvard.edu/fswiki/FreeviewGuide) software. Summary statistics for volume and thickness estimates per anatomical structure are reported in the stats files, in particular the `aseg.stats`, and the left and right `aparc.DKTatlas.mapped.stats` files.

| directory | filename | module | description |
|:------------|-------------------------------|-----------|-------------|
| mri | aparc.DKTatlas+aseg.deep.withCC.mgz| surface | cortical and subcortical segmentation incl. corpus callosum after running the surface module|
| mri | aparc.DKTatlas+aseg.mapped.mgz| surface | cortical and subcortical segmentation after running the surface module|
| mri | aparc.DKTatlas+aseg.mgz | surface | symlink to aparc.DKTatlas+aseg.mapped.mgz|
| mri | aparc+aseg.mgz | surface | symlink to aparc.DKTatlas+aseg.mapped.mgz|
| mri | aseg.mgz | surface | subcortical segmentation after running the surface module|
| mri | wmparc.DKTatlas.mapped.mgz | surface | white matter parcellation|
| mri | wmparc.mgz | surface | symlink to wmparc.DKTatlas.mapped.mgz|
| surf | lh.area, rh.area | surface | surface area overlay file|
| surf | lh.curv, rh.curv | surface | curvature overlay file|
| surf | lh.inflated, rh.inflated | surface | inflated cortical surface|
| surf | lh.pial, rh.pial | surface | pial surface|
| surf | lh.thickness, rh.thickness | surface | cortical thickness overlay file|
| surf | lh.volume, rh.volume | surface | gray matter volume overlay file|
| surf | lh.white, rh.white | surface | white matter surface|
| label | lh.aparc.DKTatlas.annot, rh.aparc.DKTatlas.annot| surface | symlink to lh.aparc.DKTatlas.mapped.annot|
| label | lh.aparc.DKTatlas.mapped.annot, rh.aparc.DKTatlas.mapped.annot| surface | annotation file for cortical parcellations, mapped from ASEGDKT segmentation to the surface|
| stats | aseg.stats | surface | table of cortical and subcortical segmentation statistics after running the surface module|
| stats | lh.aparc.DKTatlas.mapped.stats, rh.aparc.DKTatlas.mapped.stats| surface | table of cortical parcellation statistics, mapped from ASEGDKT segmentation to the surface|
| stats | lh.curv.stats, rh.curv.stats | surface | table of curvature statistics|
| stats | wmparc.DKTatlas.mapped.stats | surface | table of white matter segmentation statistics|
| scripts | recon-all.log | surface | logfile|
| directory | filename | module | description |
|:----------|----------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------|
| mri | aparc.DKTatlas+aseg.deep.withCC.mgz | surface | cortical and subcortical segmentation incl. corpus callosum after running the surface module |
| mri | aparc.DKTatlas+aseg.mapped.mgz | surface | cortical and subcortical segmentation after running the surface module |
| mri | aparc.DKTatlas+aseg.mgz | surface | symlink to aparc.DKTatlas+aseg.mapped.mgz |
| mri | aparc+aseg.mgz | surface | symlink to aparc.DKTatlas+aseg.mapped.mgz |
| mri | aseg.mgz | surface | subcortical segmentation after running the surface module |
| mri | wmparc.DKTatlas.mapped.mgz | surface | white matter parcellation |
| mri | wmparc.mgz | surface | symlink to wmparc.DKTatlas.mapped.mgz |
| surf | lh.area, rh.area | surface | surface area overlay file |
| surf | lh.curv, rh.curv | surface | curvature overlay file |
| surf | lh.inflated, rh.inflated | surface | inflated cortical surface |
| surf | lh.pial, rh.pial | surface | pial surface |
| surf | lh.thickness, rh.thickness | surface | cortical thickness overlay file |
| surf | lh.volume, rh.volume | surface | gray matter volume overlay file |
| surf | lh.white, rh.white | surface | white matter surface |
| label | lh.aparc.DKTatlas.annot, rh.aparc.DKTatlas.annot | surface | symlink to lh.aparc.DKTatlas.mapped.annot |
| label | lh.aparc.DKTatlas.mapped.annot, rh.aparc.DKTatlas.mapped.annot | surface | annotation file for cortical parcellations, mapped from ASEGDKT segmentation to the surface |
| stats | aseg.stats | surface | table of cortical and subcortical segmentation statistics after running the surface module |
| stats | lh.aparc.DKTatlas.mapped.stats, rh.aparc.DKTatlas.mapped.stats | surface | table of cortical parcellation statistics, mapped from ASEGDKT segmentation to the surface |
| stats | lh.curv.stats, rh.curv.stats | surface | table of curvature statistics |
| stats | wmparc.DKTatlas.mapped.stats | surface | table of white matter segmentation statistics |
| scripts | recon-all.log | surface | logfile |
Loading

0 comments on commit b572bae

Please sign in to comment.