Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAMF MR Brain tumor segmentation #93

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6146429
MR Brain tumor segmentation
jithenece Jul 9, 2024
22636ff
add custom segdb entries
jithenece Jul 11, 2024
afcda3e
use gcc11.4 in synch to training code base
jithenece Jul 20, 2024
029fa7f
remove files
jithenece Jul 24, 2024
0bdc69f
change working dir
jithenece Jul 24, 2024
03679eb
add meta
jithenece Jul 30, 2024
2acda92
set order
jithenece Jul 30, 2024
c1b29fd
fix dockerfile
jithenece Jul 31, 2024
f11aeb0
MHub compliance rule fixes
jithenece Jul 31, 2024
75050b2
MHub compliance rule fixes
jithenece Jul 31, 2024
b927a85
Dockerfile replace copy with build
jithenece Aug 1, 2024
f158a7d
separate single processor into multiple modules
jithenece Aug 7, 2024
43a08df
format label name and value
jithenece Aug 7, 2024
ef4621f
fix review comments
jithenece Aug 21, 2024
be2e7a0
fix review comment
jithenece Aug 22, 2024
d382e5f
remove custom seg entries
jithenece Aug 23, 2024
1126c9d
fix code comments
jithenece Aug 23, 2024
57b1f56
fix meta comments
jithenece Aug 23, 2024
16196f7
add limitations
jithenece Aug 23, 2024
49bdb90
fix workflow
jithenece Aug 23, 2024
044d076
fix meta
jithenece Aug 23, 2024
5be31ea
update model name
jithenece Aug 26, 2024
996d543
update process flag
jithenece Aug 27, 2024
628022d
Merge branch 'MHubAI:main' into bamf_mr_brain_tumor
jithenece Aug 27, 2024
f40185f
Merge branch 'MHubAI:main' into bamf_mr_brain_tumor
jithenece Sep 9, 2024
8a4967d
Merge branch 'MHubAI:main' into bamf_mr_brain_tumor
jithenece Oct 1, 2024
489d518
update python dependencies
jithenece Oct 2, 2024
224131a
/test validation
jithenece Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions models/bamf_mr_brain_tumor/config/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
general:
data_base_dir: /app/data
version: 1.0
description: Default configuration for Bamf NNUnet Brain tumor segmentation on MR scans (dicom to dicom)

segdb:
triplets:
T_EDEMA_MABNORMALITY:
code: 79654002
meaning: Edema
scheme_designator: SCT
T_NECROSIS_MABNORMALITY:
code: 6574001
meaning: Necrosis
scheme_designator: SCT
T_ENHANCING_MABNORMALITY:
code: C113842
meaning: Enhancing Lesion
scheme_designator: NCIt
segments:
EDEMA:
name: Edema
category: C_MORPHOLOGICALLY_ABNORMAL_STRUCTURE
type: T_EDEMA_MABNORMALITY
color: [140, 224, 228]
NECROSIS:
name: Necrosis
category: C_MORPHOLOGICALLY_ABNORMAL_STRUCTURE
type: T_EDEMA_MABNORMALITY
LennyN95 marked this conversation as resolved.
Show resolved Hide resolved
color: [216, 191, 216]
ENHANCING:
name: Enhancing Lesion
category: C_MORPHOLOGICALLY_ABNORMAL_STRUCTURE
type: T_ENHANCING_MABNORMALITY
color: [128, 174, 128]
LennyN95 marked this conversation as resolved.
Show resolved Hide resolved

execute:
- FileStructureImporter
- NiftiConverter
- ReOrientationRunner
- BiasCorrectionRunner
- FLIRTRegistrationRunner
- SkullStripRunner
- StdRegistrationRunner
- NNUnetRunnerV2
- InverseStdRegistrationRunner
- InverseRegistrationRunner
- module: DsegConverter
target_dicom: dicom:mod=mr:type=t1
source_segs: nifti:mod=mr:type=t1:task=inverse
- module: DsegConverter
target_dicom: dicom:mod=mr:type=t1ce
source_segs: nifti:mod=mr:type=t1ce:task=inverse
- module: DsegConverter
target_dicom: dicom:mod=mr:type=t2
source_segs: nifti:mod=mr:type=t2:task=inverse
- module: DsegConverter
target_dicom: dicom:mod=mr:type=flair
source_segs: nifti:mod=mr:type=flair:task=inverse
- DataOrganizer

modules:
FileStructureImporter:
input_dir: 'input_data'
structures:
- $patientID@instance/t1@dicom:mod=mr:type=t1
- $patientID/t1ce@dicom:mod=mr:type=t1ce
- $patientID/$type@dicom:mod=mr:type=t2
- $patientID/flair@dicom:mod=mr:type=flair
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you use a $type meta variable in the 3rd structure directive. I assume this is not on purpose. Instead, either use a specific declaration:

structures:
      - $patientID@instance/t1@dicom:mod=mr:type=t1
      - $patientID/t1ce@dicom:mod=mr:type=t1ce
      - $patientID/t2@dicom:mod=mr:type=t2
      - $patientID/flair@dicom:mod=mr:type=flair

This is equivalent to the use of a variable as in the example below, because the$type variable will read the folder name and add it to the type meta key for all files imported under the folder. However, in this case it is not clear from the import directive how the folder structure needs to look like, hence the first option should be used.

structures:
      - $patientID@instance/$type@dicom:mod=mr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jithenece I see you already updated this to the latter example. However, for the transparency with we should stick with the first, more verbose variant.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explains. I have reverted this to earlier verbose variant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jithenece Can you check if you pushed this already, I still see the simplified version in the most recent commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed it now. please check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still a $type variable (instead of t2).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed it. I have updated now.

import_id: patientID

NiftiConverter:
allow_multi_input: True
out_datas: nifti:mod=mr:task=conversion
engine: dcm2niix

ReOrientationRunner:
in_datas: nifti:mod=mr

BiasCorrectionRunner:
in_datas: nifti:mod=mr:task=reorientation

FLIRTRegistrationRunner:
in_datas: nifti:mod=mr:task=bias_corrected
reference_data: nifti:mod=mr:task=bias_corrected:type=t1ce

SkullStripRunner:
in_datas: nifti:mod=mr:task=registration

StdRegistrationRunner:
in_datas: nifti:mod=mr:task=skull_stripped

NNUnetRunnerV2:
in_t1_data: nifti:mod=mr:task=std_registration:type=t1
in_t1ce_data: nifti:mod=mr:task=std_registration:type=t1ce
in_t2_data: nifti:mod=mr:task=std_registration:type=t2
in_flair_data: nifti:mod=mr:task=std_registration:type=flair
nnunet_dataset: Dataset002_BRATS19
nnunet_config: 3d_fullres

InverseStdRegistrationRunner:
in_seg_data: nifti:mod=seg:model=nnunet:nnunet_dataset=Dataset002_BRATS19
in_mat_datas: txt:task=std_registration_transform_mat
in_registration_datas: nifti:mod=mr:task=registration

InverseRegistrationRunner:
in_seg_datas: nifti:mod=mr:task=std_inverse
in_mat_datas: txt:task=registration_transform_mat
in_registration_datas: nifti:mod=mr:task=bias_corrected

DsegConverter:
model_name: Bamf NNUnet Brain segmentation
skip_empty_slices: True

DataOrganizer:
targets:
- dicomseg:type=t1ce-->[i:patientID]/t1ce.seg.dcm
- dicomseg:type=t1-->[i:patientID]/t1.seg.dcm
- dicomseg:type=t2-->[i:patientID]/t2.seg.dcm
- dicomseg:type=flair-->[i:patientID]/flair.seg.dcm
80 changes: 80 additions & 0 deletions models/bamf_mr_brain_tumor/dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
FROM mhubai/base:latest

ENV DEBIAN_FRONTEND "noninteractive"
ENV LANG "en_GB.UTF-8"

# 1. Install gcc-14 and build ants
RUN apt-get update && apt-get install -y cmake make ninja-build git bzip2 flex manpages-dev g++ wget unzip file
RUN wget https://github.com/gcc-mirror/gcc/archive/refs/tags/releases/gcc-11.4.0.zip \
&& unzip gcc-11.4.0.zip && cd gcc-releases-gcc-11.4.0 && ./contrib/download_prerequisites \
&& mkdir /app/gcc-build && cd /app/gcc-build \
&& ../gcc-releases-gcc-11.4.0/configure -v --target=x86_64-linux-gnu --prefix=/usr/local/gcc-11.4.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-11.4 \
&& make -j$(nproc) && make install-strip \
&& update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-11.4.0/bin/gcc-11.4 11 --slave /usr/bin/g++ g++ /usr/local/gcc-11.4.0/bin/g++-11.4 \
&& rm -rf /app/gcc-*

ENV PATH=/usr/local/gcc-11.4.0/bin:$PATH
ENV LD_LIBRARY_PATH="/usr/local/gcc-11.4.0/lib64:$LD_LIBRARY_PATH"

RUN git clone https://github.com/ANTsX/ANTs.git /usr/local/src/ants \
&& mkdir /app/build && cd /app/build && cmake -DBUILD_TESTING=ON \
-DRUN_LONG_TESTS=OFF \
-DRUN_SHORT_TESTS=ON \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_INSTALL_PREFIX=/opt/ants \
/usr/local/src/ants \
&& cmake --build . --parallel \
&& cd /app/build/ANTS-build && cmake --build . --target test \
&& cmake --install . && rm -rf /app/build

# 2. Install mri_convert
ENV FREESURFER_HOME="/freesurfer"
RUN git clone https://github.com/freesurfer/freesurfer.git && cd freesurfer/mri_synthstrip \
git+https://github.com/freesurfer/surfa.git@0d83332351083b33c4da221e9d10a63a93ae7f52 \
&& mkdir -p $FREESURFER_HOME/models/ \
&& git remote add datasrc https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/repo/annex.git \
&& apt-get update -y && apt-get install -y git-annex \
&& git fetch datasrc && git-annex get . \
&& cp mri_synthstrip $FREESURFER_HOME \
&& cp synthstrip.*.pt $FREESURFER_HOME/models/

# 3. Install fsl
RUN wget https://fsl.fmrib.ox.ac.uk/fsldownloads/fslconda/releases/fslinstaller.py && \
python3 ./fslinstaller.py -V 6.0.7.11 -d /usr/local/fsl/ && rm fslinstaller.py

ENV PATH="/opt/ants/bin:$PATH:/$FREESURFER_HOME:/usr/local/fsl/bin" \
LD_LIBRARY_PATH="/opt/ants/lib:$LD_LIBRARY_PATH"

# 3. Install nnunet
# FIXME: set this environment variable as a shortcut to avoid nnunet crashing the build
# by pulling sklearn instead of scikit-learn
# N.B. this is a known issue:
# https://github.com/MIC-DKFZ/nnUNet/issues/1281
# https://github.com/MIC-DKFZ/nnUNet/pull/1209
ENV SKLEARN_ALLOW_DEPRECATED_SKLEARN_PACKAGE_INSTALL=True

# 4. Install nnunet and surfa
RUN pip3 install --no-cache-dir torch surfa==0.6
RUN apt-get update && apt-get install -y python3.9 python3-pip && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN python3.9 -m pip install nnunetv2==2.4.1
RUN apt-get update && apt-get install -y bc

# Clone the main branch of MHubAI/models
ARG MHUB_MODELS_REPO
RUN buildutils/import_mhub_model.sh bamf_mr_brain_tumor ${MHUB_MODELS_REPO}

# Pull nnUNet model weights into the container
ENV WEIGHTS_DIR=/root/.nnunet/nnUNet_models/
RUN mkdir -p $WEIGHTS_DIR
ENV WEIGHTS_FN=Dataset002_BRATS19.zip
ENV WEIGHTS_URL=https://zenodo.org/records/11582627/files/$WEIGHTS_FN
RUN wget --directory-prefix ${WEIGHTS_DIR} ${WEIGHTS_URL}
RUN unzip ${WEIGHTS_DIR}${WEIGHTS_FN} -d ${WEIGHTS_DIR}
RUN rm ${WEIGHTS_DIR}${WEIGHTS_FN}

# specify nnunet specific environment variables
ENV WEIGHTS_FOLDER=$WEIGHTS_DIR

# Default run script
ENTRYPOINT ["mhub.run"]
CMD ["--config", "/app/models/bamf_mr_brain_tumor/config/default.yml"]
Loading
Loading