Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/OpenGATE/GateTools
Browse files Browse the repository at this point in the history
  • Loading branch information
David Boersma committed Nov 22, 2019
2 parents 77eb095 + 289d850 commit 34a0500
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
4 changes: 2 additions & 2 deletions bin/gt_image_crop
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ 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):
'''
\b
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_FILENAME> : input image
Expand Down
21 changes: 21 additions & 0 deletions gatetools/image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
47 changes: 47 additions & 0 deletions gatetools/image_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
18 changes: 10 additions & 8 deletions gatetools/phsp/phsp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 34a0500

Please sign in to comment.