Skip to content

Commit

Permalink
Reformat AtlasPack into a BIDS dataset and fix ANTS and AFNI (#35)
Browse files Browse the repository at this point in the history
* Reformat AtlasPack into a BIDS dataset.

* Update Dockerfile
  • Loading branch information
tsalo authored Oct 2, 2024
1 parent f1d2b3f commit eeba053
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
18 changes: 8 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ COPY --from=atlaspack /AtlasPack/tpl-MNI152NLin6Asym_*.nii.gz /AtlasPack/
COPY --from=atlaspack /AtlasPack/atlas-4S*.tsv /AtlasPack/
COPY --from=atlaspack /AtlasPack/*.json /AtlasPack/

# Write dataset_description.json
RUN echo '{"Name": "AtlasPack", "BIDSVersion": "1.0.0", "DatasetType": "atlas"}' >> /AtlasPack/dataset_description.json

# Install basic libraries
RUN apt-get update && \
apt-get install -y --no-install-recommends \
Expand Down Expand Up @@ -114,12 +111,9 @@ RUN apt-get update -qq \
| tar -xz -C /opt/afni-latest --strip-components 1

# Configure AFNI
ENV AFNI_MODELPATH="/opt/afni-latest/models" \
AFNI_IMSAVE_WARNINGS="NO" \
AFNI_TTATLAS_DATASET="/usr/share/afni/atlases" \
AFNI_PLUGINPATH="/opt/afni-latest/plugins"

ENV PATH="/opt/afni-latest/bin:$PATH"
ENV PATH="$PATH:/opt/afni-latest" \
AFNI_INSTALLDIR=/opt/afni-latest \
AFNI_IMSAVE_WARNINGS=NO

RUN echo "Downloading C3D ..." \
&& mkdir /opt/c3d \
Expand Down Expand Up @@ -170,7 +164,7 @@ RUN mkdir -p $ANTSPATH && \
curl -sSL "https://github.com/ANTsX/ANTs/releases/download/v2.5.3/ants-2.5.3-ubuntu-22.04-X64-gcc.zip" -o /tmp/ants.zip && \
unzip /tmp/ants.zip -d $ANTSPATH && \
rm /tmp/ants.zip
ENV PATH=$ANTSPATH:$PATH
ENV PATH=/usr/lib/ants/ants-2.5.3/bin:$PATH

# Install SVGO
RUN npm install -g svgo
Expand Down Expand Up @@ -200,6 +194,10 @@ RUN python fetch_templates.py && \
find $HOME/.cache/templateflow -type d -exec chmod go=u {} + && \
find $HOME/.cache/templateflow -type f -exec chmod go=u {} +

# Reformat AtlasPack into a BIDS dataset
COPY scripts/fix_atlaspack.py fix_atlaspack.py
RUN python fix_atlaspack.py && rm fix_atlaspack.py

# Install pandoc (for HTML/LaTeX reports)
RUN curl -o pandoc-2.2.2.1-1-amd64.deb -sSL "https://github.com/jgm/pandoc/releases/download/2.2.2.1/pandoc-2.2.2.1-1-amd64.deb" && \
dpkg -i pandoc-2.2.2.1-1-amd64.deb && \
Expand Down
51 changes: 51 additions & 0 deletions scripts/fix_atlaspack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Convert the AtlasPack folder into a BIDS dataset."""

import json
import os
import re
import shutil

if __name__ == "__main__":
# Where all the files are located
in_dir = "/AtlasPack"

with open(os.path.join(in_dir, "dataset_description.json"), "w") as fo:
json.dump(
{"Name": "AtlasPack", "BIDSVersion": "1.9.0", "DatasetType": "atlas"},
fo,
)

# Define patterns and corresponding target formats
patterns = [
(r"atlas-([a-zA-Z0-9+]+)_dseg.tsv", r"atlas-\1/atlas-\1_dseg.tsv"),
(
r"tpl-MNI152NLin6Asym_atlas-([a-zA-Z0-9+]+)_res-01_dseg.nii.gz",
r"atlas-\1/atlas-\1_space-MNI152NLin6Asym_res-01_dseg.nii.gz",
),
(
r"tpl-fsLR_atlas-([a-zA-Z0-9+]+)_den-91k_dseg.dlabel.nii",
r"atlas-\1/atlas-\1_space-fsLR_den-91k_dseg.dlabel.nii",
),
(
r"tpl-MNI152NLin6Asym_atlas-([a-zA-Z0-9+]+)_dseg.json",
r"atlas-\1/atlas-\1_space-MNI152NLin6Asym_res-01_dseg.json",
),
(
r"tpl-fsLR_atlas-([a-zA-Z0-9+]+)_dseg.json",
r"atlas-\1/atlas-\1_space-fsLR_den-91k_dseg.json",
),
]

for pattern, target_format in patterns:
files = [f for f in os.listdir(in_dir) if re.search(pattern, f)]
for filename in files:
atlas_name = re.findall(pattern, filename)[0]
target_dir = os.path.join(in_dir, f"atlas-{atlas_name}")
target_path = os.path.join(in_dir, target_format.replace(r"\1", atlas_name))

# Create target directory if it doesn't exist
os.makedirs(target_dir, exist_ok=True)

# Move and rename the file
shutil.move(os.path.join(in_dir, filename), target_path)
print(f"Moved {filename} to {target_path}")

0 comments on commit eeba053

Please sign in to comment.