Skip to content

ADD: Make template dimensions support T2w as well #879

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

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Changes from 1 commit
Commits
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
28 changes: 20 additions & 8 deletions niworkflows/interfaces/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@

CONFORMATION_TEMPLATE = """\t\t<h3 class="elem-title">Anatomical Conformation</h3>
\t\t<ul class="elem-desc">
\t\t\t<li>Input T1w images: {n_t1w}</li>
\t\t\t<li>Input {anat} images: {n_anat}</li>
\t\t\t<li>Output orientation: RAS</li>
\t\t\t<li>Output dimensions: {dims}</li>
\t\t\t<li>Output voxel size: {zooms}</li>
Expand All @@ -378,16 +378,23 @@


class _TemplateDimensionsInputSpec(BaseInterfaceInputSpec):
anat_type = traits.Enum("T1w", "T2w", usedefault=True, desc="Anatomical image type")
anat_list = InputMultiObject(
File(exists=True), xor="t1w_list", desc="input anatomical images"
)
t1w_list = InputMultiObject(
File(exists=True), mandatory=True, desc="input T1w images"
xor="anat_list",
deprecated="1.14.0",
new_name="anat_list",
)
max_scale = traits.Float(
3.0, usedefault=True, desc="Maximum scaling factor in images to accept"
)


class _TemplateDimensionsOutputSpec(TraitedSpec):
t1w_valid_list = OutputMultiObject(exists=True, desc="valid T1w images")
t1w_valid_list = OutputMultiObject(exists=True, deprecated="1.14.0", desc="valid T1w images")
anat_valid_list = OutputMultiObject(exists=True, desc="valid anatomical images")
target_zooms = traits.Tuple(
traits.Float, traits.Float, traits.Float, desc="Target zoom information"
)
Expand All @@ -399,8 +406,8 @@

class TemplateDimensions(SimpleInterface):
"""
Finds template target dimensions for a series of T1w images, filtering low-resolution images,
if necessary.
Finds template target dimensions for a series of anatomical images, filtering low-resolution
images, if necessary.

Along each axis, the minimum voxel size (zoom) and the maximum number of voxels (shape) are
found across images.
Expand All @@ -426,7 +433,8 @@
)
zoom_fmt = "{:.02g}mm x {:.02g}mm x {:.02g}mm".format(*zooms)
return CONFORMATION_TEMPLATE.format(
n_t1w=len(self.inputs.t1w_list),
anat=self.inputs.anat_type,
n_anat=len(self.inputs.anat_list),
dims="x".join(map(str, dims)),
zooms=zoom_fmt,
n_discards=len(discards),
Expand All @@ -435,7 +443,10 @@

def _run_interface(self, runtime):
# Load images, orient as RAS, collect shape and zoom data
in_names = np.array(self.inputs.t1w_list)
if not self.inputs.anat_list: # Deprecate: 1.14.0
self.inputs.anat_list = self.inputs.t1w_list

Check warning on line 447 in niworkflows/interfaces/images.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/images.py#L447

Added line #L447 was not covered by tests

in_names = np.array(self.inputs.anat_list)

Check warning on line 449 in niworkflows/interfaces/images.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/images.py#L449

Added line #L449 was not covered by tests
orig_imgs = np.vectorize(nb.load)(in_names)
reoriented = np.vectorize(nb.as_closest_canonical)(orig_imgs)
all_zooms = np.array([img.header.get_zooms()[:3] for img in reoriented])
Expand All @@ -452,7 +463,8 @@

# Ignore dropped images
valid_fnames = np.atleast_1d(in_names[valid]).tolist()
self._results["t1w_valid_list"] = valid_fnames
self._results["anat_valid_list"] = valid_fnames
self._results["t1w_valid_list"] = valid_fnames # Deprecate: 1.14.0

Check warning on line 467 in niworkflows/interfaces/images.py

View check run for this annotation

Codecov / codecov/patch

niworkflows/interfaces/images.py#L466-L467

Added lines #L466 - L467 were not covered by tests

# Set target shape information
target_zooms = all_zooms[valid].min(axis=0)
Expand Down
Loading