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

Script dicom to dicom using Freesurfer docker image #2

Open
Nilser3 opened this issue Jan 30, 2024 · 5 comments · Fixed by #4
Open

Script dicom to dicom using Freesurfer docker image #2

Nilser3 opened this issue Jan 30, 2024 · 5 comments · Fixed by #4

Comments

@Nilser3
Copy link
Collaborator

Nilser3 commented Jan 30, 2024

Description

  • The strategy to implement pre-trained models in PARADIM is to do it via Docker and using a script.py that take a dicom folder as input and generate a dicom folder as output (segmentation) keeping the same metadata.

  • Pretrained model that we want to implement is WMH-SynthSeg and is implemented in freesurfer-linux-ubuntu22_x86_64-dev-20240112-88246c0

Scripting

This script.py does:

  • Dicom_1 to Nifti_1
  • Processing this Nifti_1 and output Nifti_2 (i.e. segmentation, reslicing)
  • Nifti_2 to Dicom_2

To test it on Dicom data, here is a subject from PPMI project, using:

python script.py --dicom_dir Dicom_1 --dicom_out_dir Dicom_2

But I need to improve the parser of several Dicom folders, as well as preserve the entire metadata in Dicom_2.

Dockerizing

I made a first Freesurfer docker image, available in:

docker pull nilser3/freesurfer-linux-ubuntu22_x86_64-dev-20240112-88246c0_nlm:20240130.v3.0

and for test scrpit.py with Dicom_1:

docker run -v /path:/mnt -it freesurfer-linux-ubuntu22_x86_64-dev-20240112-88246c0_nlm:20240130.v3.0 python3 script.py  --dicom_dir  ../../mnt/Dicom_1  --dicom_out_dir ../../mnt/Dicom_2

(here the Processing is just a mri_convert for reslicing, just to see the functionality of this docker image)

@NathanMolinier feedback please 😊

@NathanMolinier
Copy link
Contributor

We don't need everything to be gathered within only one script.py. Actually, for better comprehension, we should split the different functions (dicom2niftii, docker creation...)

@Nilser3
Copy link
Collaborator Author

Nilser3 commented Feb 10, 2024

Hi @NathanMolinier , some feedbacks

  • Using the pydicom-seg library, I managed to keep all the metadata of the input in the output .

  • Also, the issue of having different input output orientations was solved (tested on RAI and ASL orientations).

  • For preserve the colors and Label names (based on FreeSurferColorLUT.txt ), Label.json files have been created for each class (additional information is configured there), in the templates folder, based on qiicr.org.

Slicer 5.6.1 Visualization

image

Example of --dicom_out_dir after run script.py

  • As the WMH-SynthSeg model does a multi-class segmentation (up to 33 classes) this code update saves each class in a dicom file, and all dicom files in a output folder.

@NathanMolinier
Copy link
Contributor

Great work @Nilser3 ! Once you're done with your code, just create a Pull Request, I will check what you did !

@Nilser3
Copy link
Collaborator Author

Nilser3 commented Feb 13, 2024

The script.py has been updated with the following:

  1. If dicom_input_dir has several subfolders, the script will run in each of these subfolders.
  2. The script will only apply to 3D anatomical images (Localizers, DTI, GRE, fMRI images are excluded, ).
  3. The script will create in dicom_output_dir the same arborescence as dicom_input_dir, only that it will contain the segmentations.

dicom_output_dir

tree dcm_seg
dcm_seg
└── PPMI
    └── 100001
        ├── 3D_T2_FLAIR
        │   └── 2020-10-07_13_51_32.0
        │       └── I1473191
        │           └── 02_Left-Cerebral-White-Matter_WMH_SynthSeg.dcm
        │           └──        .
        │           └──        .
        │           └──        .
        └── SAG_3D_MPRAGE
            ├── 2020-10-07_13_51_32.0
            │       └── I1473172
            │           └── 02_Left-Cerebral-White-Matter_WMH_SynthSeg.dcm
            │           └──        .
            │           └──        .
            │           └──        .        
            └── 2022-11-29_14_47_02.0
            │      └── I1658546
            │           └── 02_Left-Cerebral-White-Matter_WMH_SynthSeg.dcm
            │           └──        .
            │           └──        .
            │           └──        .

@NathanMolinier , Ready for a PR 😊

@Nilser3
Copy link
Collaborator Author

Nilser3 commented Feb 15, 2024

Dockerizing

To create our container, we must have in a single folder containing :

Inside this folder we create a dockerfile with the following content.

First Docker image for freesurfer-wmhsynthseg

docker build -t freesurfer-wmhsyntheseg .

Second Docker image (add script)

Dockerfile

#  Use an Ubuntu image
FROM freesurfer-wmhsyntheseg

# Install git and other necessary dependencies
RUN apt-get update && \
    apt-get install -y git python3 python3-pip && \
    rm -rf /var/lib/apt/lists/*

# Sets the working directory to /app
WORKDIR /app
COPY . /app

# Update pip and setuptools
RUN pip3 install --upgrade pip setuptools

# Install other dependencies
RUN pip3 install -r requirements.txt
RUN pip3 install -e .

COPY run.sh /app/run.sh

# Sets the default command to run when the container starts up
ENTRYPOINT ["/app/run.sh"]

Docker image build

docker build -t registre.apps.ul-pca-pr-ul01.ulaval.ca/ul-val-prj-pim-pr/freesurfer-wmhsyntheseg:0.0.3 .

Push to Paradim Team

docker push registre.apps.ul-pca-pr-ul01.ulaval.ca/ul-val-prj-pim-pr/freesurfer-wmhsyntheseg:0.0.3

To use

docker run -v /USER_IN_PATH:/in:ro -v /USER_OUT_PATH:/out:rw registre.apps.ul-pca-pr-ul01.ulaval.ca/ul-val-prj-pim-pr/freesurfer-wmhsyntheseg:0.0.3 /in /out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants