def test_pad_nd_image_and_seg_2D(self): input_shape = (5, 5, 30, 30) data = np.random.random(input_shape) seg = np.random.random(data.shape) new_shape = (15, 15, 50, 50) new_shape2 = (4, 2, 10, 10) d, s = pad_nd_image_and_seg(data, seg, new_shape=new_shape) d2, s2 = pad_nd_image_and_seg(data, seg, new_shape=new_shape2) self.assertTrue(all(i == j for i, j in zip(new_shape, d.shape)), "data has unexpected shape") self.assertTrue(all(i == j for i, j in zip(new_shape, s.shape)), "seg has unexpected shape") np.testing.assert_array_equal(d[5:10, 5:10, 10:40, 10:40], data, err_msg="data wrongly padded") np.testing.assert_array_equal(s[5:10, 5:10, 10:40, 10:40], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(d.flatten()), np.sum(data.flatten()), msg="Padding of data is not zero") self.assertAlmostEqual(np.sum(s.flatten()), np.sum(seg.flatten()), msg="Padding of data is not zero") self.assertTrue(all(i == j for i, j in zip(input_shape, d2.shape)), "data has unexpected shape") self.assertTrue(all(i == j for i, j in zip(input_shape, s2.shape)), "seg has unexpected shape") np.testing.assert_array_equal(d2, data, err_msg="data wrongly padded for smaller output shape than input shape") np.testing.assert_array_equal(s2, seg, err_msg="seg wrongly padded for smaller output shape than input shape")
def __call__(self, **data_dict): data = data_dict.get(self.data_key) seg = data_dict.get(self.label_key) assert len(self.new_size) + 2 == len(data.shape), "new size must be a tuple/list/np.ndarray with shape " \ "(x, y(, z))" data, seg = pad_nd_image_and_seg(data, seg, self.new_size, None, np_pad_kwargs_data=self.np_pad_kwargs_data, np_pad_kwargs_seg=self.np_pad_kwargs_seg, pad_mode_data=self.pad_mode_data, pad_mode_seg=self.pad_mode_seg) data_dict[self.data_key] = data if seg is not None: data_dict[self.label_key] = seg return data_dict
def test_pad_nd_image_and_seg_3D(self): print('Test test_pad_nd_image_and_seg_3D. [START]') input_shape = (5, 5, 30, 30, 30) data = np.random.random(input_shape) seg = np.random.random(data.shape) new_shape = (15, 15, 50, 50, 50) new_shape2 = (4, 2, 10, 10, 10) new_shape3 = (4, 2, 50, 50, 50) new_shape4 = (5, 5, 51, 51, 49) new_shape5 = (50, 50) new_shape6 = (2, 5, 5, 50, 50, 50) new_shape7 = (5, 5, 30, 30, 50) expected_shape3 = (5, 5, 50, 50, 50) expected_shape5 = (5, 5, 30, 50, 50) number_of_padded_ones7 = np.prod(new_shape7) - np.prod(input_shape) data_padded, seg_padded = pad_nd_image_and_seg(data, seg, new_shape=new_shape) data_padded2, seg_padded2 = pad_nd_image_and_seg(data, seg, new_shape=new_shape2) data_padded3, seg_padded3 = pad_nd_image_and_seg(data, seg, new_shape=new_shape3) data_padded4, seg_padded4 = pad_nd_image_and_seg(data, seg, new_shape=new_shape4) data_padded5, seg_padded5 = pad_nd_image_and_seg(data, seg, new_shape=new_shape5) data_padded7, seg_padded7 = pad_nd_image_and_seg( data, seg, new_shape=new_shape7, np_pad_kwargs_data={'constant_values': 1}, np_pad_kwargs_seg={'constant_values': 1}) print('Zero padding to bigger output shape in all dimensions. [START]') self.assertTrue( all(i == j for i, j in zip(new_shape, data_padded.shape)), "padded data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(new_shape, seg_padded.shape)), "padded seg has unexpected shape") np.testing.assert_array_equal(data_padded[5:10, 5:10, 10:40, 10:40, 10:40], data, err_msg="data wrongly padded") np.testing.assert_array_equal(seg_padded[5:10, 5:10, 10:40, 10:40, 10:40], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(data_padded.flatten()), np.sum(data.flatten()), msg="Padding of data is not zero") self.assertAlmostEqual(np.sum(seg_padded.flatten()), np.sum(seg.flatten()), msg="Padding of data is not zero") print('Zero padding to bigger output shape in all dimensions. [DONE]') print( 'Zero padding to smaller output shape in all dimensions. [START]') self.assertTrue( all(i == j for i, j in zip(input_shape, data_padded2.shape)), "padded data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(input_shape, seg_padded2.shape)), "padded seg has unexpected shape") np.testing.assert_array_equal( data_padded2, data, err_msg= "data wrongly padded for smaller output shape than input shape") np.testing.assert_array_equal( seg_padded2, seg, err_msg= "seg wrongly padded for smaller output shape than input shape") print('Zero padding to smaller output shape in all dimensions. [DONE]') print( 'Zero padding to smaller output shape in first two dimensions and bigger output shape in last two dimensions. [START]' ) self.assertTrue( all(i == j for i, j in zip(expected_shape3, data_padded3.shape)), "padded data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(expected_shape3, seg_padded3.shape)), "padded seg has unexpected shape") np.testing.assert_array_equal(data_padded3[:, :, 10:40, 10:40, 10:40], data, err_msg="data wrongly padded") np.testing.assert_array_equal(seg_padded3[:, :, 10:40, 10:40, 10:40], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(data_padded3.flatten()), np.sum(data.flatten()), msg="Padding of data is not zero") self.assertAlmostEqual(np.sum(seg_padded3.flatten()), np.sum(seg.flatten()), msg="Padding of data is not zero") print( 'Zero padding to smaller output shape in first two dimensions and bigger output shape in last two dimensions. [DONE]' ) print('Zero padding to odd padding dimensions. [START]') self.assertTrue( all(i == j for i, j in zip(new_shape4, data_padded4.shape)), "padded data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(new_shape4, seg_padded4.shape)), "padded seg has unexpected shape") np.testing.assert_array_equal(data_padded4[:, :, 10:40, 10:40, 9:39], data, err_msg="data wrongly padded") np.testing.assert_array_equal(seg_padded4[:, :, 10:40, 10:40, 9:39], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(data_padded4.flatten()), np.sum(data.flatten()), msg="Padding of data is not zero") self.assertAlmostEqual(np.sum(seg_padded4.flatten()), np.sum(seg.flatten()), msg="Padding of data is not zero") print('Zero padding to odd padding dimensions. [DONE]') print( 'Zero padding with new_shape.shape smaller than data.shape. [START]' ) self.assertTrue( all(i == j for i, j in zip(expected_shape5, data_padded5.shape)), "data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(expected_shape5, seg_padded5.shape)), "seg has unexpected shape") np.testing.assert_array_equal(data_padded5[:, :, :, 10:40, 10:40], data, err_msg="data wrongly padded") np.testing.assert_array_equal(seg_padded5[:, :, :, 10:40, 10:40], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(data_padded5.flatten()), np.sum(data.flatten()), msg="Padding of data is not zero") self.assertAlmostEqual(np.sum(seg_padded5.flatten()), np.sum(seg.flatten()), msg="Padding of data is not zero") print( 'Zero padding with new_shape.shape smaller than data.shape. [DONE]' ) print( 'Zero padding with new_shape.shape bigger than data.shape. [START]' ) self.assertRaises(IndexError, pad_nd_image_and_seg, data, seg, new_shape=new_shape6) print( 'Zero padding with new_shape.shape bigger than data.shape. [DONE]') print( 'Padding to bigger output shape in all dimensions with constant_value=1 for segmentation padding . [START]' ) self.assertTrue( all(i == j for i, j in zip(new_shape, data_padded.shape)), "padded data has unexpected shape") self.assertTrue( all(i == j for i, j in zip(new_shape, seg_padded.shape)), "padded seg has unexpected shape") np.testing.assert_array_equal(data_padded7[:, :, :, :, 10:40], data, err_msg="data wrongly padded") np.testing.assert_array_equal(seg_padded7[:, :, :, :, 10:40], seg, err_msg="seg wrongly padded") self.assertAlmostEqual(np.sum(data_padded7.flatten()), np.sum(data.flatten()) + number_of_padded_ones7, msg="Padding of data is not one") self.assertAlmostEqual(np.sum(seg_padded7.flatten()), np.sum(seg.flatten()) + number_of_padded_ones7, msg="Padding of data is not one") print( 'Padding to bigger output shape in all dimensions with constant_value=1 for segmentation padding . [DONE]' ) print('Test test_pad_nd_image_and_seg_3D. [DONE]')