Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit f37f2a99 authored by Olga Stamati's avatar Olga Stamati
Browse files

add helper function for extracting a padded slice and test

parent ac2e202e
Pipeline #49771 passed with stages
in 23 minutes and 21 seconds
......@@ -99,11 +99,50 @@ class testAll(unittest.TestCase):
im = numpy.random.rand(5, 5, 5)
spam.helpers.multipleShifts(im, [1, 2, 3])
# EA: function removed 2020-03-10 for now
#EA: function removed 2020-03-10 for now
#def test_binarisation(self):
#im = numpy.random.rand(10, 10, 10)
#spam.helpers.binarisation(im, threshold=0.5, mask=im > 0.2)
def test_slicePadded(self):
# case 1: inside
im = numpy.random.rand(100, 100, 100)
startStop = numpy.array([70, 90, 70, 90, 70, 90])
imSliced = spam.helpers.slicePadded(im, startStop)
self.assertEqual(numpy.subtract(im[startStop[0]:startStop[1], startStop[2]:startStop[3], startStop[4]:startStop[5]], imSliced).sum(), 0)
self.assertEqual(list(imSliced.shape), [20, 20, 20])
## case 2: hit top
startStop = numpy.array([-10, 10, -10, 10, -10, 10])
imSliced = spam.helpers.slicePadded(im, startStop)
self.assertEqual(imSliced[0:10, 0:10, 0:10].sum(), 0)
self.assertEqual(list(imSliced.shape), [20, 20, 20])
self.assertEqual(numpy.subtract(im[0:10, 0:10, 0:10], imSliced[10:, 10:, 10:]).sum(), 0)
## case 3: hit bot
startStop = numpy.array([90, 110, 90, 110, 90, 110])
imSliced = spam.helpers.slicePadded(im, startStop)
self.assertEqual(imSliced[10:, 10:, 10:].sum(), 0)
self.assertEqual(list(imSliced.shape), [20, 20, 20])
self.assertEqual(numpy.subtract(im[90:, 90:, 90:], imSliced[0:10, 0:10, 0:10]).sum(), 0)
## case 4: hit top/bot
startStop = numpy.array([-10, 110, -10, 110, -10, 110])
imSliced = spam.helpers.slicePadded(im, startStop)
self.assertEqual(list(imSliced.shape), [120, 120, 120])
self.assertEqual(imSliced[0:10, 0:10, 0:10].sum(), 0)
self.assertEqual(imSliced[110:, 110:, 110:].sum(), 0)
self.assertEqual(numpy.subtract(im, imSliced[10:110, 10:110, 10:110]).sum(), 0)
## case 5: hit top/bot with mask
startStop = numpy.array([-10, 110, -10, 110, -10, 110])
imSliced, mask = spam.helpers.slicePadded(im, startStop, createMask = True)
self.assertEqual(list(imSliced.shape), [120, 120, 120])
self.assertEqual(list(mask.shape), [120, 120, 120])
self.assertEqual(imSliced[0:10, 0:10, 0:10].sum(), 0)
self.assertEqual(imSliced[110:, 110:, 110:].sum(), 0)
self.assertEqual(numpy.subtract(im, imSliced[10:110, 10:110, 10:110]).sum(), 0)
self.assertEqual(mask[10:110, 10:110, 10:110].sum(), 100**3)
if __name__ == '__main__':
unittest.main()
......@@ -456,6 +456,49 @@ def _binarisation(im, threshold=0.0, boolean=False, op='>', mask=None):
return phases
def slicePadded(im, startStop, createMask = False):
"""
Extract padded slice from im, which is always of the dimensions asked (given from startStop)
Parameters
----------
im : 3D numpy array
The image to be sliced
startStop : 6 component list of ints
This array contains:
[Zmin, Zmax, Ymin, Ymax, Xmin, Xmax]
createMask : bool, optional
If True, return a of the padded slice, which is False when the slice falls outside im
Default = False
Returns
-------
imSliced : 3D numpy array
The sliced image
mask : 3D numpy array of bools
The 3D mask, only returned if createMask is True
"""
imSliced = numpy.zeros((startStop[1]-startStop[0], startStop[3]-startStop[2], startStop[5]-startStop[4]), dtype=im.dtype)
start = numpy.array([startStop[0], startStop[2], startStop[4]])
stop = numpy.array([startStop[1], startStop[3], startStop[5]])
startOffset = numpy.array([max(0, start[0]), max(0, start[1]), max(0, start[2])])
stopOffset = numpy.array([min(im.shape[0], stop[0]), min(im.shape[1], stop[1]), min(im.shape[2], stop[2])])
startLocal = startOffset - start
stopLocal = startLocal + stopOffset - startOffset
imSliced[startLocal[0]:stopLocal[0], startLocal[1]:stopLocal[1], startLocal[2]:stopLocal[2]] = im[startOffset[0]:stopOffset[0], startOffset[1]:stopOffset[1], startOffset[2]:stopOffset[2]]
if createMask:
mask = numpy.zeros_like(imSliced, dtype=bool)
mask[startLocal[0]:stopLocal[0], startLocal[1]:stopLocal[1], startLocal[2]:stopLocal[2]] = 1
return imSliced, mask
return imSliced
# private functions
#def _mask2D(im, erosion=False, structure=None, ):
#"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment