From 95adaa452ee82d8eb9f46afcb782ac851887bd10 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Thu, 12 Oct 2017 09:56:03 +0100
Subject: [PATCH] Atlas mask query methods raise an error if the provided mask
 is not in the same space as the atlas. This might change in the future.

---
 fsl/data/atlases.py | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/fsl/data/atlases.py b/fsl/data/atlases.py
index 51f2adcb2..22ddbb7e7 100644
--- a/fsl/data/atlases.py
+++ b/fsl/data/atlases.py
@@ -711,12 +711,16 @@ class LabelAtlas(Atlas):
         # Use nearest neighbour interpolation
         # for resampling, as it is most likely
         # that the mask is binary.
-        mask     = mask.resample(self.shape[:3], dtype=np.float32, order=0)[0]
-        boolmask = mask > 0
+        mask, xform = mask.resample(self.shape[:3], dtype=np.float32, order=0)
+
+        if not fslimage.Image(mask, xform=xform).sameSpace(self):
+            raise ValueError('Mask is not in the same space as atlas')
+
 
         # Extract the labels that are in
         # the mask, and their corresponding
         # mask weights
+        boolmask  = mask > 0
         vals      = self[boolmask]
         weights   = mask[boolmask]
         weightsum = weights.sum()
@@ -851,7 +855,11 @@ class ProbabilisticAtlas(Atlas):
 
         # Make sure that the mask has the same
         # number of voxels as the atlas image
-        mask      = mask.resample(self.shape[:3], dtype=np.float32, order=0)[0]
+        mask, xform = mask.resample(self.shape[:3], dtype=np.float32, order=0)
+
+        if not fslimage.Image(mask, xform=xform).sameSpace(self):
+            raise ValueError('Mask is not in the same space as atlas')
+
         boolmask  = mask > 0
         weights   = mask[boolmask]
         weightsum = weights.sum()
-- 
GitLab