Skip to content

Commit

Permalink
Merge pull request #257 from oesteban/fix/templateflow
Browse files Browse the repository at this point in the history
[RF] Updates for templateflow
  • Loading branch information
effigies authored Oct 26, 2018
2 parents 603dbad + 34d440a commit 1658e4c
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 247 deletions.
13 changes: 7 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ WORKDIR /root/
COPY . niworkflows/
RUN find /root/niworkflows/ -name "test*.py" -exec chmod a-x '{}' \;
RUN cd niworkflows && \
pip install -e .[all] && \
python -c 'from niworkflows.data.getters import get_mni_icbm152_linear; get_mni_icbm152_linear()' && \
python -c 'from niworkflows.data.getters import get_mni_template_ras; get_mni_template_ras()' && \
python -c 'from niworkflows.data.getters import get_ds003_downsampled; get_ds003_downsampled()' && \
python -c 'from niworkflows.data.getters import get_ants_oasis_template_ras; get_ants_oasis_template_ras()' && \
python -c 'from niworkflows.data.getters import get_mni_icbm152_nlin_asym_09c; get_mni_icbm152_nlin_asym_09c()'
pip install -e .[all]

# Pre-install templates and data
RUN python -c 'from niworkflows.data.getters import get_template; get_template("MNI152Lin")' && \
python -c 'from niworkflows.data.getters import get_template; get_template("MNI152NLin2009cAsym")' && \
python -c 'from niworkflows.data.getters import get_template; get_template("OASIS")' && \
python -c 'from niworkflows.data.getters import get_ds003_downsampled; get_ds003_downsampled()'
12 changes: 6 additions & 6 deletions niworkflows/anat/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import os
from collections import OrderedDict
from multiprocessing import cpu_count
from pathlib import Path
from pkg_resources import resource_filename as pkgr_fn
from packaging.version import parse as parseversion, Version
from pathlib import Path

# nipype
from nipype.pipeline import engine as pe
Expand All @@ -24,7 +24,7 @@
from nipype.interfaces.ants import N4BiasFieldCorrection, Atropos, MultiplyImages

# niworkflows
from ..data import TEMPLATE_MAP, get_dataset
from ..data import OSF_RESOURCES, TEMPLATE_ALIASES, get_template
from ..interfaces.ants import (
ImageMath,
ResampleImageBySpacing,
Expand Down Expand Up @@ -163,16 +163,16 @@ def init_brain_extraction_wf(name='brain_extraction_wf',
wf = pe.Workflow(name)

template_path = None
if in_template in TEMPLATE_MAP:
template_path = get_dataset(in_template)
if in_template in TEMPLATE_ALIASES or in_template in OSF_RESOURCES:
template_path = get_template(in_template)
else:
template_path = in_template
template_path = Path(in_template)

mod = ('%sw' % modality[:2].upper()
if modality.upper().startswith('T') else modality.upper())

# Append template modality
potential_targets = list(Path(template_path).glob('*_%s.nii.gz' % mod))
potential_targets = list(template_path.glob('*_%s.nii.gz' % mod))
if not potential_targets:
raise ValueError(
'No %s template was found under "%s".' % (mod, template_path))
Expand Down
14 changes: 1 addition & 13 deletions niworkflows/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,8 @@
from .getters import (
get_brainweb_1mm_normal,
get_ds003_downsampled,
get_mni_template,
get_mni_template_ras,
get_ants_oasis_template,
get_ants_oasis_template_ras,
get_mni_epi,
get_mni152_nlin_sym_las,
get_mni152_nlin_sym_ras,
get_mni_icbm152_linear,
get_mni_icbm152_nlin_asym_09c,
get_oasis_dkt31_mni152,
get_hcp32k_files,
get_conte69_mesh,
get_dataset,
get_template,
get_bids_examples,
TEMPLATE_MAP,
TEMPLATE_ALIASES,
)
156 changes: 8 additions & 148 deletions niworkflows/data/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@

OSF_PROJECT_URL = ('https://files.osf.io/v1/resources/fvuh8/providers/osfstorage/')
OSF_RESOURCES = {
'ants_nki_template_ras': ('59cd90f46c613b02b3d79782', 'e5debaee65b8f2c8971577db1327e314'),
'ants_oasis_template': ('57f32ae89ad5a101f977eb79', '34d39070b541c416333cc8b6c2fe993c'),
'ants_oasis_template_ras': ('584123a29ad5a1020913609d', 'afa21f99c66ae1672320d8aa0408229a'),
'brainweb': ('57f32b96b83f6901f194c3ca', '384263fbeadc8e2cca92ced98f224c4b'),
# Conte69-atlas meshes in 32k resolution
'conte69': ('5b198ec5ec24e20011b48548', 'bd944e3f9f343e0e51e562b440960529'),
'ds003_downsampled': ('57f328f6b83f6901ef94cf70', '5a558961c1eb5e5f162696d8afa956e8'),
'fMRIPrep': ('5bc12155ac011000176bff82', '1aec4d286bd89f4f90316ce2cde63218'),
# conversion between fsaverage5/6 and fs_LR(32k)
'hcpLR32k': ('5b198ec6b796ba000f3e4858', '0ba9adcaa42fa88616a4cea5a1ce0c5a'),
'mni_epi': ('57fa09cdb83f6901d93623a0', '9df727e1f742ec55213480434b4c4811'),
'mni_icbm152_linear': ('580705eb594d9001ed622649', '72be639e92532def7caad75cb4058e83'),
'mni_icbm152_nlin_asym_09c': ('580705089ad5a101f17944a9', '002f9bf24dc5c32de50c03f01fa539ec'),
'mni_template': ('57f32ab29ad5a101fb77fd89', 'debfa882b8c301cd6d75dd769e73f727'),
'mni_template_RAS': ('57f32a799ad5a101f977eb77', 'a4669f0e7acceae148bb39450b2b21b4'),
'mni152_nlin_sym_las': ('57fa7fc89ad5a101e635eeef', '9c4c0cad2a2e99d6799f01abf4107f5a'),
'mni152_nlin_sym_ras': ('57fa7fd09ad5a101df35eed0', '65d64ad5a980da86e7d07d95b3ed2ccb'),
'MNI152Lin': ('5bc984f8ccdb6b0018abb993', 'a1eb0a121d2aa68fc8a6e75ab148f1db'),
'MNI152NLin2009cAsym': ('5b0dbce20f461a000db8fa3d', '5d386d7db9c1dec30230623db25e05e1'),
'NKI': ('5bc3fad82aa873001bc5a553', '092e56fb3700f9f57b8917a0db887db6'),
'OASIS30ANTs': ('5b0dbce34c28ef0012c7f788', 'f625a0390eb32a7852c7b0d71ac428cd'),
# Mindboggle DKT31 label atlas in MNI152NLin2009cAsym space
'OASISTRT20': ('5b16f17aeca4a80012bd7542', '1b5389bc3a895b2bd5c0d47401107176'),
}

Expand All @@ -43,7 +37,8 @@
}

# Map names of templates to OSF_RESOURCES keys
TEMPLATE_MAP = {
TEMPLATE_ALIASES = {
'MNI152Lin': 'MNI152Lin',
'MNI152NLin2009cAsym': 'MNI152NLin2009cAsym',
'OASIS': 'OASIS30ANTs',
'NKI': 'NKI',
Expand All @@ -54,7 +49,6 @@ def get_dataset(dataset_name, dataset_prefix=None, data_dir=None,
url=None, resume=True, verbose=1):
"""Download and load the BIDS-fied brainweb 1mm normal
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
Expand All @@ -73,15 +67,14 @@ def get_template(template_name, data_dir=None, url=None, resume=True, verbose=1)
template_name = template_name[4:]

# An aliasing mechanism. Please avoid
template_name = TEMPLATE_MAP.get(template_name, template_name)
template_name = TEMPLATE_ALIASES.get(template_name, template_name)
return get_dataset(template_name, dataset_prefix='tpl-', data_dir=data_dir,
url=url, resume=resume, verbose=verbose)


def get_brainweb_1mm_normal(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the BIDS-fied brainweb 1mm normal
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
Expand All @@ -94,7 +87,6 @@ def get_brainweb_1mm_normal(data_dir=None, url=None, resume=True, verbose=1):
def get_ds003_downsampled(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the BIDS-fied ds003_downsampled
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
Expand All @@ -104,144 +96,12 @@ def get_ds003_downsampled(data_dir=None, url=None, resume=True, verbose=1):
resume=resume, verbose=verbose)


def get_mni_template(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni_template', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni_template_ras(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni_template_RAS', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni_epi(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni_epi', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_ants_oasis_template(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the ANTs template of the OASIS dataset.
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('ants_oasis_template', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_ants_oasis_template_ras(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the ANTs template of the OASIS dataset.
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('ants_oasis_template_ras', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_ants_nki_template_ras(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the ANTs template of the NKI dataset.
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('ants_nki_template_ras', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni152_nlin_sym_las(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni152_nlin_sym_las', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni152_nlin_sym_ras(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni152_nlin_sym_ras', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni_icbm152_nlin_asym_09c(data_dir=None, url=None, resume=True, verbose=1):
return get_dataset('mni_icbm152_nlin_asym_09c', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_mni_icbm152_linear(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the mni template
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_dataset('mni_icbm152_linear', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_bids_examples(data_dir=None, url=None, resume=True, verbose=1,
variant='BIDS-examples-1-1.0.0-rc3u5'):
"""
Download BIDS-examples-1
"""
"""Download BIDS-examples-1"""
variant = 'BIDS-examples-1-1.0.0-rc3u5' if variant not in BIDS_EXAMPLES else variant
if url is None:
url = BIDS_EXAMPLES[variant][0]
md5 = BIDS_EXAMPLES[variant][1]
return fetch_file(variant, url, data_dir, resume=resume, verbose=verbose,
md5sum=md5)


def get_oasis_dkt31_mni152(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files from the Mindboggle DKT31 label
atlas in MNI152NLin2009cAsym space
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_template('OASISTRT20', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_hcp32k_files(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load the necessary files for conversion between fsaverage5/6
and fs_LR(32k)
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_template('hcpLR32k', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)


def get_conte69_mesh(data_dir=None, url=None, resume=True, verbose=1):
"""Download and load Conte69-atlas meshes in 32k resolution
:param str data_dir: path of the data directory. Used to force data storage
in a non-standard location.
:param str url: download URL of the dataset. Overwrite the default URL.
"""
return get_template('conte69', data_dir=data_dir, url=url,
resume=resume, verbose=verbose)
46 changes: 40 additions & 6 deletions niworkflows/interfaces/masks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from nipype.interfaces.mixins import reporting
from nipype.algorithms import confounds
from . import report_base as nrc
from seaborn import color_palette


class BETInputSpecRPT(nrc.SVGReportCapableInputSpec,
Expand Down Expand Up @@ -250,6 +251,9 @@ class ROIsPlotInputSpecRPT(nrc.SVGReportCapableInputSpec):
masked = traits.Bool(False, usedefault=True, desc='mask in_file prior plotting')
colors = traits.Either(None, traits.List(Str), usedefault=True,
desc='use specific colors for contours')
levels = traits.Either(None, traits.List(traits.Float),
usedefault=True, desc='pass levels to nilearn.plotting')
mask_color = Str('r', usedefault=True, desc='color for mask')


class ROIsPlot(nrc.ReportingInterface):
Expand All @@ -258,21 +262,51 @@ class ROIsPlot(nrc.ReportingInterface):
def _generate_report(self):
from niworkflows.viz.utils import plot_segs, compose_view
seg_files = self.inputs.in_rois
mask_file = None
if isdefined(self.inputs.in_mask):
mask_file = self.inputs.in_mask
seg_files.insert(0, self.inputs.in_mask)
mask_file = None if not isdefined(self.inputs.in_mask) \
else self.inputs.in_mask

# Remove trait decoration and replace None with []
levels = [l for l in self.inputs.levels or []]
colors = [c for c in self.inputs.colors or []]

if len(seg_files) == 1: # in_rois is a segmentation
nsegs = len(levels)
if nsegs == 0:
levels = np.unique(np.round(
nb.load(seg_files[0]).get_data()).astype(int))
levels = (levels[levels > 0] - 0.5).tolist()
nsegs = len(levels)

levels = [levels]
missing = nsegs - len(colors)
if missing > 0:
colors = colors + color_palette("husl", missing)
colors = [colors]
else: # in_rois is a list of masks
nsegs = len(seg_files)
levels = [[0.5]] * nsegs
missing = nsegs - len(colors)
if missing > 0:
colors = [[c] for c in colors + color_palette("husl", missing)]

if mask_file:
seg_files.insert(0, mask_file)
if levels:
levels.insert(0, [0.5])
colors.insert(0, [self.inputs.mask_color])
nsegs += 1

self._out_report = os.path.abspath(self.inputs.out_report)
compose_view(
plot_segs(
image_nii=self.inputs.in_file,
seg_niis=seg_files,
bbox_nii=mask_file,
levels=levels,
colors=colors,
out_file=self.inputs.out_report,
masked=self.inputs.masked,
colors=self.inputs.colors,
compress=self.inputs.compress_report
compress=self.inputs.compress_report,
),
fg_svgs=None,
out_file=self._out_report
Expand Down
Loading

0 comments on commit 1658e4c

Please sign in to comment.