def test_read_image(self, dim_order, ranges, start, size): descriptor = SubImageDescriptor.from_dict({ "filename": 'TestFileName', "suffix": "SUFFIX", "index": 0, "data_type": "XXXX", "template": [], "dim_order": dim_order, "ranges": ranges, "file_format": "mhd", "msb": "False", "compression": []}) read_file = Mock() global_image_size = len(dim_order)*[50] image = create_dummy_image(global_image_size) image_wrapper = image sub_image = image_wrapper.get_sub_image(start, size) read_file.read_image.return_value = sub_image.image file_factory = Mock() file_factory.create_read_file.return_value = read_file si = SubImage(descriptor, file_factory) # CoordinateTransforer is tested elsewhere. Here we do not make sure # the coordinates have been transformed, using another # CoordinateTransformer to check, but we are not verifying the # transformations are correct, since the CoordinateTransformer test # should do this transformer = CoordinateTransformer( descriptor.ranges.origin_start, descriptor.ranges.image_size, descriptor.axis) expected_start, expected_size = transformer.to_local(start, size) test_image = si.read_image(start, size) np.testing.assert_array_equal(test_image.image, sub_image.image) np.testing.assert_array_equal(read_file.read_image.call_args[0][0], expected_start) np.testing.assert_array_equal(read_file.read_image.call_args[0][1], expected_size)
def test_get_limits(self, dim_order, ranges): descriptor = SubImageDescriptor.from_dict({ "filename": 'TestFileName', "suffix": "SUFFIX", "index": 0, "data_type": "XXXX", "template": [], "dim_order": dim_order, "ranges": ranges, "file_format": "mhd", "msb": "False", "compression": [], "voxel_size": [1, 1, 1] }) read_file = Mock() global_image_size = len(dim_order) * [50] image = create_dummy_image(global_image_size) image_wrapper = image read_file.read_image.return_value = image_wrapper.image file_factory = Mock() file_factory.create_read_file.return_value = read_file si = SubImage(descriptor, file_factory) minv, maxv = si.get_limits() actual_min = np.min(image_wrapper.image.get_raw()) actual_max = np.max(image_wrapper.image.get_raw()) self.assertEqual(minv, actual_min) self.assertEqual(maxv, actual_max)
def test_write_image(self, dim_order, ranges, start, size): descriptor = SubImageDescriptor.from_dict({ "filename": 'TestFileName', "suffix": "SUFFIX", "index": 0, "data_type": "XXXX", "template": [], "dim_order": dim_order, "ranges": ranges, "file_format": "mhd", "msb": "False", "compression": [], "voxel_size": [1, 1, 1] }) file_factory = Mock() out_file = Mock() file_factory.create_write_file.return_value = out_file # This test verifies that the read source supplied to the output file # correctly converts from the file coordinate system to the global # coordinate system source = Mock() dummy_image = create_dummy_image(len(size) * [20]) source.read_image.return_value = dummy_image si = SubImage(descriptor, file_factory) si.write_image(source, None) # CoordinateTransforer is tested elsewhere. transformer = CoordinateTransformer(descriptor.ranges.origin_start, descriptor.ranges.image_size, descriptor.axis) local_start, local_size = transformer.to_local(start, size) # Fetch the local data source provided to the file write method local_data_source = out_file.write_image.call_args[0][0] # Read from the local data source to trigger a read in the global source input_image = local_data_source.read_image(local_start, local_size).image input_image = local_data_source.read_image(local_start, local_size).image # Get the arguments test_start = source.read_image.call_args[0][0] test_size = source.read_image.call_args[0][1] np.testing.assert_array_equal(test_start, local_start) np.testing.assert_array_equal(test_size, local_size)
def _make_descriptor(self, index, ranges): return SubImageDescriptor.from_dict({ "filename": 'TestFileName', "ranges": ranges, "suffix": "SUFFIX", "dim_order": [1, 2, 3], "data_type": "XXXX", "index": index, "template": [], "file_format": "mhd", "msb": "True", "compression": [], "voxel_size": [1, 1, 1] })
def test_close(self): descriptor = SubImageDescriptor.from_dict({ "filename": 'TestFileName', "ranges": [[0, 10, 0, 0], [0, 10, 0, 0], [0, 10, 0, 0]], "suffix": "SUFFIX", "index": 0, "dim_order": [1, 2, 3], "data_type": "XXXX", "template": [], "file_format": "mhd", "msb": "False", "compression": [], "voxel_size": [1, 1, 1] }) # Check that reading creates only one read file and it is left open file_factory = FakeFileFactory(create_dummy_image([11, 11, 11])) self.assertEqual(len(file_factory.read_files), 0) self.assertEqual(len(file_factory.write_files), 0) si = SubImage(descriptor, file_factory) si.read_image([1, 1, 1], [1, 1, 1]) self.assertEqual(len(file_factory.read_files), 1) self.assertTrue(file_factory.read_files[0].open) self.assertEqual(len(file_factory.write_files), 0) self.assertTrue(file_factory.read_files[0].open) si.read_image([1, 1, 1], [2, 2, 2]) self.assertEqual(len(file_factory.read_files), 1) self.assertEqual(len(file_factory.write_files), 0) # Check that close() closes the read image self.assertTrue(file_factory.read_files[0].open) si.close() self.assertEqual(len(file_factory.read_files), 1) self.assertEqual(len(file_factory.write_files), 0) self.assertFalse(file_factory.read_files[0].open) # Check that file is closed after writing source = FakeImageFileReader(descriptor) self.assertEqual(len(file_factory.write_files), 0) si.write_image(source, None) self.assertEqual(len(file_factory.write_files), 1) self.assertFalse(file_factory.write_files[0].open)
def test_bind_by_roi(self, ranges, start, size, valid, valid_start, valid_size): descriptor = SubImageDescriptor.from_dict({ "filename": 'TestFileName', "suffix": "SUFFIX", "index": 0, "data_type": "XXXX", "template": [], "dim_order": np.arange(1, len(start) + 1), "ranges": ranges, "file_format": "mhd", "msb": "False", "compression": []}) file_factory = FakeFileFactory() si = SubImage(descriptor, file_factory) start_test, size_test = si.bind_by_roi(start_global=start, size_global=size) if valid: # For values within bounds, check they are correct np.testing.assert_array_equal(size_test, valid_size) np.testing.assert_array_equal(start_test, valid_start) else: # For values out of bounds, the actual values are irrelevant but at least one of the size dimensions must be <= 0 self.assertTrue(np.any(np.less(size_test, np.zeros_like(size_test))))
def test_from_dict(self): d_orig = self.make_dict() sid = SubImageDescriptor.from_dict(d_orig) d2 = SubImageDescriptor.to_dict(sid) self.assertDictEqual(d_orig, d2)