diff --git a/bin/gt_image_crop b/bin/gt_image_crop index d18723b..4de6934 100755 --- a/bin/gt_image_crop +++ b/bin/gt_image_crop @@ -15,7 +15,7 @@ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) @click.option('--output', '-o', help='Output filename', required=True) @click.option('--bg', help='Value of the backgroup to autocrop', default=None) @click.option('--bb', default=None, - help="Bounding Box with 6 ints (voxel), separated with comma 'x1,y1,z1,x2,y2,z2'") + help="Bounding Box with 6 ints (voxel), separated with comma 'x1,x2,y1,y2,z1,z2'") @gt.add_options(gt.common_options) def gt_image_crop(input_filename, output, bg, bb, **kwargs): ''' @@ -23,7 +23,7 @@ def gt_image_crop(input_filename, output, bg, bb, **kwargs): Crop an image. If --bg : auto crop according to the given background value - If --bb : manual crop in mm. + If --bb : manual crop in voxel. \b : input image diff --git a/gatetools/image_convert.py b/gatetools/image_convert.py index 2bd2357..1fa87c7 100644 --- a/gatetools/image_convert.py +++ b/gatetools/image_convert.py @@ -190,3 +190,24 @@ def image_convert(inputImage, pixeltype=None): return castFilter return inputImage + +##################################################################################### +import unittest +import hashlib +import os +from .logging_conf import LoggedTestCase + +class Test_Convert(LoggedTestCase): + def test_convert(self): + x = np.arange(-10, 10, 1) + y = np.arange(-12, 15, 1) + z = np.arange(-13, 10, 1) + xx, yy, zz = np.meshgrid(x, y, z) + image = itk.image_from_array(np.float32(xx)) + convertedImage = image_convert(image, "unsigned char") + itk.imwrite(convertedImage, "testConvert.mha") + with open("testConvert.mha","rb") as fnew: + bytesNew = fnew.read() + new_hash = hashlib.sha256(bytesNew).hexdigest() + os.remove("testConvert.mha") + self.assertTrue("da57bf05ec62b9b5ee2aaa71aacbf7dbca8acbf2278553edc499a3af8007dd44" == new_hash) diff --git a/gatetools/image_crop.py b/gatetools/image_crop.py index 13cc796..c61aa02 100644 --- a/gatetools/image_crop.py +++ b/gatetools/image_crop.py @@ -117,3 +117,50 @@ def image_crop_with_bb(img, bb): cropper.SetRegionOfInterest(region) cropper.Update() return cropper.GetOutput() + +##################################################################################### +import unittest +from .logging_conf import LoggedTestCase + +class Test_Crop(LoggedTestCase): + def test_auto_crop(self): + Dimension = 3 + PixelType = itk.ctype('unsigned char') + ImageType = itk.Image[PixelType, Dimension] + + image = ImageType.New() + start = itk.Index[Dimension]() + start[0] = 0 + start[1] = 0 + start[2] = 0 + size = itk.Size[Dimension]() + size[0] = 200 + size[1] = 200 + size[2] = 200 + region = itk.ImageRegion[Dimension]() + region.SetSize(size) + region.SetIndex(start) + image.SetRegions(region) + image.Allocate() + image.FillBuffer(0) + npView = itk.array_from_image(image) + npView[10:52, 42:192, 124:147] =1 + image = itk.image_from_array(npView) + autoCrop = image_auto_crop(image) + autoCropSize = autoCrop.GetLargestPossibleRegion().GetSize() + self.assertTrue(np.allclose(autoCropSize[0], 23)) + self.assertTrue(np.allclose(autoCropSize[1], 150)) + self.assertTrue(np.allclose(autoCropSize[2], 42)) + self.assertTrue(np.allclose(itk.array_from_image(autoCrop)[0, 0, 0], 1)) + def test_crop(self): + x = np.arange(-10, 10, 0.1) + y = np.arange(-12, 15, 0.1) + z = np.arange(-13, 10, 0.1) + xx, yy, zz = np.meshgrid(x, y, z) + image = itk.image_from_array(np.float32(xx)) + croppedImage = image_crop_with_bb(image, gt.bounding_box(xyz=[10.0, 12.0, 0.0, 7.0, 6.0, 15.0])) + croppedImageSize = croppedImage.GetLargestPossibleRegion().GetSize() + self.assertTrue(np.allclose(croppedImageSize[0], 3)) + self.assertTrue(np.allclose(croppedImageSize[1], 8)) + self.assertTrue(np.allclose(croppedImageSize[2], 10)) + self.assertTrue(np.allclose(itk.array_from_image(croppedImage)[0, 0, 0], -10)) diff --git a/gatetools/phsp/phsp_helpers.py b/gatetools/phsp/phsp_helpers.py index 0f10044..385f0f1 100644 --- a/gatetools/phsp/phsp_helpers.py +++ b/gatetools/phsp/phsp_helpers.py @@ -377,24 +377,26 @@ def fig_histo2D(ax, data, keys, k, nbins, color='g'): ax.set_xlabel(k[0]) ax.set_ylabel(k[1]) -# ----------------------------------------------------------------------------- +##################################################################################### import unittest import hashlib -class Test_Phsp(unittest.TestCase): +from gatetools.logging_conf import LoggedTestCase + +class Test_Phsp(LoggedTestCase): def test_phsp_convert(self): - print('Test_Phsp test_phsp_convert') testfilesPath = os.path.dirname(os.path.realpath(__file__)) testfilesPath = os.path.join(testfilesPath, "testphsp") data, read_keys, m = load(os.path.join(testfilesPath, "phsp.root"), -1) save_npy(os.path.join(testfilesPath, "testphsp.npy"), data, read_keys) - fnew = open(os.path.join(testfilesPath, "testphsp.npy"),"rb") - bytesNew = fnew.read() - new_hash = hashlib.sha256(bytesNew).hexdigest() + with open(os.path.join(testfilesPath, "testphsp.npy"),"rb") as fnew: + bytesNew = fnew.read() + new_hash = hashlib.sha256(bytesNew).hexdigest() + self.assertTrue("cec796ec5764d039b02e15d504e80ccf2b2c35e0e5380985245262faf0ff0892" == new_hash) + dataNPY, read_keysNPY, mNPY = load(os.path.join(testfilesPath, "testphsp.npy"), -1) + self.assertTrue(np.allclose(131.69868, np.amax(dataNPY[:,2]))) os.remove(os.path.join(testfilesPath, "testphsp.npy")) - self.assertTrue("cec796ec5764d039b02e15d504e80ccf2b2c35e0e5380985245262faf0ff0892" == new_hash) def test_phsp_info(self): - print('Test_Phsp test_phsp_info') testfilesPath = os.path.dirname(os.path.realpath(__file__)) testfilesPath = os.path.join(testfilesPath, "testphsp") data, read_keys, m = load(os.path.join(testfilesPath, "phsp.root"), -1)