Skip to content

Commit

Permalink
Added offset
Browse files Browse the repository at this point in the history
  • Loading branch information
clementpoiret committed Sep 27, 2021
1 parent 4c3d0f7 commit b7c5355
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
15 changes: 14 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions roiloc/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions roiloc/locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion roiloc/roiloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit b7c5355

Please sign in to comment.