def test_applies_rotation(self): deformation_field = deformation_utils.create_2d_deformation_field( raw_image_center_pos_pix=tf.constant([1.0, 1.0]), raw_image_element_size_um=tf.constant([1.0, 1.0]), net_input_spatial_shape=[3, 3], net_input_element_size_um=tf.constant([1.0, 1.0]), control_grid_spacings_pix=[2.0, 2.0], deformations_magnitudes_um=tf.constant([0.0, 0.0]), rotation_angle=tf.constant(np.pi / 4.), scale_factors=tf.constant([1.0, 1.0]), mirror_factors=tf.constant([1, 1]), shearing_coefs=tf.constant([0.0, 0.0]), cropping_offset_pix=tf.constant([0.0, 0.0])) expected_output = np.array([[[-0.4142135624, 1.], [0.2928932188, 1.7071067812], [1., 2.4142135624]], [[0.2928932188, 0.2928932188], [1., 1.], [1.7071067812, 1.7071067812]], [[1., -0.4142135624], [1.7071067812, 0.2928932188], [2.4142135624, 1]]]) with self.session() as sess: np.testing.assert_allclose(expected_output, sess.run(deformation_field), atol=_ARRAY_COMPARE_TOLERANCE)
def test_oddEvenErrorHandling(self): with tf.Session(): deform = deformation_utils.create_2d_deformation_field( np.array([101, 101]) / 2, raw_image_element_size_um=tf.constant([1., 1.]), net_input_spatial_shape=[50, 101], net_input_element_size_um=tf.constant([2., 1.]), control_grid_spacings_pix=[10, 10], deformations_magnitudes_um=tf.constant((0., 0.)), rotation_angle=tf.constant(0.), scale_factors=tf.constant((1., 1.)), mirror_factors=tf.constant((1., 1.)), shearing_coefs=tf.constant((0., 0., 0., 0.)), cropping_offset_pix=tf.constant((0., 0.))) with self.assertRaisesWithPredicateMatch( tf.errors.InvalidArgumentError, "factor must be odd as input and output size is even"): deform.eval()
def test_applies_cropping_offset(self): deformation_field = deformation_utils.create_2d_deformation_field( raw_image_center_pos_pix=tf.constant([1.0, 1.0]), raw_image_element_size_um=tf.constant([1.0, 1.0]), net_input_spatial_shape=[3, 3], net_input_element_size_um=tf.constant([1.0, 1.0]), control_grid_spacings_pix=[2.0, 2.0], deformations_magnitudes_um=tf.constant([0.0, 0.0]), rotation_angle=tf.constant(0.0), scale_factors=tf.constant([1.0, 1.0]), mirror_factors=tf.constant([1, 1]), shearing_coefs=tf.constant([0.0, 0.0]), cropping_offset_pix=tf.constant([2.0, 3.0])) expected_output = np.array([[[2, 3], [2, 4], [2, 5]], [[3, 3], [3, 4], [3, 5]], [[4, 3], [4, 4], [4, 5]]]) with self.session() as sess: np.testing.assert_allclose(expected_output, sess.run(deformation_field), atol=_ARRAY_COMPARE_TOLERANCE)
def test_applies_multiple_transforms_together(self): deformation_field = deformation_utils.create_2d_deformation_field( raw_image_center_pos_pix=tf.constant([1.0, 1.0]), raw_image_element_size_um=tf.constant([1.0, 1.0]), net_input_spatial_shape=[3, 3], net_input_element_size_um=tf.constant([1.0, 1.0]), control_grid_spacings_pix=[2.0, 2.0], deformations_magnitudes_um=tf.constant([0.0, 0.0]), rotation_angle=tf.constant(np.pi / 2.), scale_factors=tf.constant([1.0, 2.0]), mirror_factors=tf.constant([1, -1]), shearing_coefs=tf.constant([0.1, 0.0]), cropping_offset_pix=tf.constant([3.0, 5.0])) expected_output = np.array([[[3., 3.9], [4., 4.], [5., 4.1]], [[3., 5.9], [4., 6.], [5., 6.1]], [[3., 7.9], [4., 8.], [5., 8.1]]]) with self.session() as sess: np.testing.assert_allclose(expected_output, sess.run(deformation_field), atol=_ARRAY_COMPARE_TOLERANCE)