MCPcopy Create free account
hub / github.com/SimpleITK/SimpleITK / test_4d

Function test_4d

Wrapping/Python/tests/sitkImageIndexingTest.py:402–448  ·  view source on GitHub ↗

testing __getitem__ for getting 4D sub-regions

()

Source from the content-addressed store, hash-verified

400 assert_image_ndarray_equals(img[..., 0], nda[0, ...])
401
402def test_4d():
403 """testing __getitem__ for getting 4D sub-regions"""
404
405 # Check if we have 4D Image support
406 try:
407 sitk.Image([2, 2, 2, 2], sitk.sitkFloat32)
408 except RuntimeError:
409 pytest.skip("4D Image not supported")
410
411 nda = np.linspace(0, 119, 120).reshape(2, 3, 4, 5)
412
413 img = sitk.GetImageFromArray(nda, isVector=False)
414
415 # check some exceptions
416 with pytest.raises(IndexError):
417 img[0, 1, 0, 0, 0]
418 with pytest.raises(IndexError):
419 img[0, 4, 0, 0]
420 with pytest.raises(IndexError):
421 img[0, 0, 0, 2]
422 with pytest.raises(IndexError):
423 img[-6, 0, 0, 0]
424 with pytest.raises(IndexError):
425 img[0, 0, 0, -3]
426
427 # check basic indexing
428 assert img[0, 0, 0, 0] == 0.0
429 assert img[(1, 0, 0, 0)] == 1.0
430 assert img[[2, 0, 0, 0]] == 2.0
431 assert img[3, 2, 1, 0] == 33.0
432 assert img[-4, -3, -2, -1] == 86.0
433 assert img[-4, -3, -2, -1] == nda[-1, -2, -3, -4]
434
435 assert_image_ndarray_equals(img[1:-1, 1:-1, 1:-1], nda[1:-1, 1:-1, 1:-1])
436 assert_image_ndarray_equals(img[1:-1, 1:-1, 1:-1], nda[1:-1, 1:-1, 1:-1, :])
437 assert_image_ndarray_equals(img[:, :, :, 1:2], nda[1:2, :, :, :])
438 assert_image_ndarray_equals(img[3:6, 1:5, 2:4, 0:2], nda[0:2, 2:4, 1:5, 3:6])
439 assert_image_ndarray_equals(img[1:, 2:, :2, :], nda[:, :2, 2:, 1:])
440
441 # some negative cases
442 assert_image_ndarray_equals(
443 img[::-1, ::-1, ::-1, ::-1], nda[::-1, ::-1, ::-1, ::-1]
444 )
445 assert_image_ndarray_equals(
446 img[:, ::-2, ::-1, ::-3], nda[::-3, ::-1, ::-2, :]
447 )
448 assert_image_ndarray_equals(img[-1:-4:-1, :, :, :], nda[:, :, :, -1:-4:-1])
449
450def test_5d():
451 """testing __getitem__ for 5D image"""

Callers

nothing calls this directly

Calls 2

ImageMethod · 0.80

Tested by

no test coverage detected