From b7c5355de4a45d3380ac0dd1a7ae8346fd3a4530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20POIRET?= Date: Mon, 27 Sep 2021 10:55:49 +0200 Subject: [PATCH] Added offset --- README.rst | 15 ++++++++++++++- roiloc/location.py | 16 +++++++++++----- roiloc/locator.py | 15 ++++++++++++--- roiloc/roiloc.py | 23 ++++++++++++++++++++++- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/README.rst b/README.rst index cc3fcff..0a3152f 100644 --- a/README.rst +++ b/README.rst @@ -25,7 +25,8 @@ CLI *** usage: roiloc [-h] -p PATH -i INPUTPATTERN [-r ROI [ROI ...]] -c CONTRAST [-b] - [-t TRANSFORM] [-m MARGIN [MARGIN ...]] [--mask MASK] + [-t TRANSFORM] [-m MARGIN [MARGIN ...]] [--rightoffset RIGHTOFFSET [RIGHTOFFSET ...]] + [--leftoffset LEFTOFFSET [LEFTOFFSET ...]] [--mask MASK] [--extracrops EXTRACROPS [EXTRACROPS ...]] [--savesteps] arguments:: @@ -52,6 +53,18 @@ arguments:: has to be a list of 3 integers, to control the margin in the three axis (0: left/right margin, 1: post/ant margin, 2: inf/sup margin). Default: [8,8,8] + --rightoffset RIGHTOFFSET [RIGHTOFFSET ...] + Offset to add to the bounding box of the right ROI in + voxels. It has to be a list of 3 integers, to control + the offset in the three axis (0: from left to right, + 1: from post to ant, 2: from inf to sup). + Default: [0,0,0] + --leftoffset LEFTOFFSET [LEFTOFFSET ...] + Offset to add to the bounding box of the left ROI in + voxels. It has to be a list of 3 integers, to control + the offset in the three axis (0: from left to right, + 1: from post to ant, 2: from inf to sup). + Default: [0,0,0] --mask MASK Pattern for brain tissue mask to improve registration (e.g.: `sub_*bet_mask.nii.gz`). If providing a BET mask, please also pass `-b` to use a BET MNI template. diff --git a/roiloc/location.py b/roiloc/location.py index af560f3..2a2d41f 100644 --- a/roiloc/location.py +++ b/roiloc/location.py @@ -7,12 +7,15 @@ from rich import print -def get_coords(x: np.ndarray, margin: list = [8, 8, 8]) -> list: - """Get coordinates of a given ROI, and apply a margin. +def get_coords(x: np.ndarray, + margin: list = [8, 8, 8], + offset: list = [0, 0, 0]) -> list: + """Get coordinates of a given ROI, and apply a margin with offset. Args: x (np.ndarray): ROI in binary format margin (list, optional): margin for xyz axes. Defaults to [8, 8, 8] + offset (list, optional): offset for xyz axes. Defaults to [0, 0, 0] Returns: list: Coordinates in xyzxyz format @@ -21,9 +24,12 @@ def get_coords(x: np.ndarray, margin: list = [8, 8, 8]) -> list: mask = np.where(x != 0) - minx, maxx = int(np.min(mask[0])), int(np.max(mask[0])) - miny, maxy = int(np.min(mask[1])), int(np.max(mask[1])) - minz, maxz = int(np.min(mask[2])), int(np.max(mask[2])) + minx, maxx = int(np.min(mask[0])) + offset[0], int(np.max( + mask[0])) + offset[0] + miny, maxy = int(np.min(mask[1])) + offset[1], int(np.max( + mask[1])) + offset[1] + minz, maxz = int(np.min(mask[2])) + offset[2], int(np.max( + mask[2])) + offset[2] minx = (minx - margin[0]) if (minx - margin[0]) > 0 else 0 miny = (miny - margin[1]) if (miny - margin[1]) > 0 else 0 diff --git a/roiloc/locator.py b/roiloc/locator.py index d5ef451..425e3b4 100644 --- a/roiloc/locator.py +++ b/roiloc/locator.py @@ -18,7 +18,9 @@ class RoiLocator: bet (bool, optional): Use brain extracted MNI template. Defaults to False. transform_type (str, optional): Type of transformation for the registration. Defaults to "AffineFast". - margin (list, optional): Margin to apply. Defaults to [4, 4, 4]. + margin (list, optional): Margin to apply. Defaults to [8, 8, 8]. + rightoffset (list, optional): Offset to apply to the right hippocampus. Defaults to [0, 0, 0]. + leftoffset (list, optional): Offset to apply to the left hippocampus. Defaults to [0, 0, 0]. mask (Optional[ANTsImage], optional): Brain mask to improve registration quality. Defaults to None. @@ -40,11 +42,15 @@ def __init__(self, roi: str, bet: bool = False, transform_type: str = "AffineFast", - margin: list = [4, 4, 4], + margin: list = [8, 8, 8], + rightoffset: list = [0, 0, 0], + leftoffset: list = [0, 0, 0], mask: Optional[ANTsImage] = None): self.transform_type = transform_type self.margin = margin + self.rightoffset = rightoffset + self.leftoffset = leftoffset self.mask = mask self._roi_idx = get_roi_indices(roi) @@ -90,7 +96,10 @@ def fit(self, image: ANTsImage): region = get_roi(registered_atlas=registered_atlas, idx=int(self._roi_idx[i]), save=False) - coords = get_coords(region.numpy(), margin=self.margin) + offset = self.rightoffset if side == "right" else self.leftoffset + coords = get_coords(region.numpy(), + margin=self.margin, + offset=offset) self.coords[side] = coords diff --git a/roiloc/roiloc.py b/roiloc/roiloc.py index 6c73ba2..f53cb46 100644 --- a/roiloc/roiloc.py +++ b/roiloc/roiloc.py @@ -89,7 +89,10 @@ def main(args): f"{image_stem}_{roi}_{side}_{args.transform}_mask.nii.gz", save=args.savesteps) - coords = get_coords(region.numpy(), margin=args.margin) + offset = args.rightoffset if side == "right" else args.leftoffset + coords = get_coords(region.numpy(), + margin=args.margin, + offset=offset) for file in files: fstem = file.stem.split(".")[0] @@ -167,6 +170,24 @@ def start(): required=False, default=[8, 8, 8]) + parser.add_argument( + "--rightoffset", + nargs='+', + type=int, + help= + "Offset to add to the bounding box of the right ROI in voxels. It has to be a list of 3 integers, to control the offset in the three axis (0: voxels from left to right, 1: voxels from post to ant, 2: voxels from inf to sup). Default: [0,0,0]", + required=False, + default=[0, 0, 0]) + + parser.add_argument( + "--leftoffset", + nargs='+', + type=int, + help= + "Offset to add to the bounding box of the left ROI in voxels. It has to be a list of 3 integers, to control the offset in the three axis (0: voxels from left to right, 1: voxels from post to ant, 2: voxels from inf to sup). Default: [0,0,0]", + required=False, + default=[0, 0, 0]) + parser.add_argument( "--mask", help=