Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
 def __init__(self, descriptor, global_image=None):
     self.global_image = global_image
     self.descriptor = descriptor
     self.open = True
     self.transformer = CoordinateTransformer(
         self.descriptor.ranges.origin_start,
         self.descriptor.ranges.image_size, self.descriptor.axis)
    def test_local_global(self, origin, size, order, flip, g_start, g_size, l_start, l_size):
        ct = CoordinateTransformer(origin, size, Axis(order, flip))

        o_l_start, o_l_size = ct.to_local(g_start, g_size)
        self.assertTrue(np.array_equal(o_l_start, l_start))
        self.assertTrue(np.array_equal(o_l_size, l_size))

        o_g_start, o_g_size = ct.to_global(l_start, l_size)
        self.assertTrue(np.array_equal(o_g_start, g_start))
        self.assertTrue(np.array_equal(o_g_size, g_size))
    def test_flip_explicit(self, dim, flip, expected):
        global_image_raw = np.array([[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]],
                        [[20, 21, 22], [23, 24, 25], [26, 27, 28], [29, 30, 31]]])
        global_image = ImageStorage(global_image_raw)
        ct = CoordinateTransformer(np.zeros_like(dim), global_image.get_size(), Axis(dim, flip))
        local_image = ct.image_to_local(global_image)
        np.testing.assert_array_equal(local_image.get_raw(), np.array(expected))
        np.testing.assert_array_equal(local_image.get_raw(), np.array(expected))

        global_image_2 = ct.image_to_global(local_image)
        np.testing.assert_array_equal(global_image_raw, global_image_2.get_raw())
    def test_image(self, origin, size, order, flip, g_start, g_size, l_start, l_size):
        ct = CoordinateTransformer(origin, size, Axis(order, flip))

        transformed_start, transformed_size = ct.to_local(g_start, size)
        global_image = create_dummy_image_storage(size)
        local_image = ct.image_to_local(global_image)
        np.testing.assert_array_equal(size, global_image.get_size())
        np.testing.assert_array_equal(transformed_size, local_image.get_size())
        test_image = global_image.transpose(order)
        test_image = test_image.flip(flip)
        np.testing.assert_array_equal(test_image.get_raw(), local_image.get_raw())

        global_image_2 = ct.image_to_global(local_image)
        np.testing.assert_array_equal(global_image.get_raw(), global_image_2.get_raw())
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    def test_local_source(self, origin, global_size, dim_order, dim_flip, start, size):
        transformer = CoordinateTransformer(origin, global_size, Axis(dim_order, dim_flip))
        data_source = Mock()
        test_image = create_dummy_image(global_size)
        data_source.read_image.return_value = test_image
        source = LocalSource(data_source, transformer)
        local_image = source.read_image(start, size).image
        global_start, t_global_size = transformer.to_global(start, size)
        np.testing.assert_array_equal(data_source.read_image.call_args[0][0],
                                      start)
        np.testing.assert_array_equal(data_source.read_image.call_args[0][1],
                                      size)

        self.assertEqual(data_source.close.call_count, 0)
        source.close()
        self.assertEqual(data_source.close.call_count, 1)

        global_image = transformer.image_to_global(local_image)
        np.testing.assert_array_equal(local_image, test_image.image)
Exemplo n.º 8
0
class FakeImageFileReader(ImageFileReader, Source):
    """Fake data source"""
    def __init__(self, descriptor, global_image=None):
        self.global_image = global_image
        self.descriptor = descriptor
        self.open = True
        self.transformer = CoordinateTransformer(
            self.descriptor.ranges.origin_start,
            self.descriptor.ranges.image_size, self.descriptor.axis)

    def read_image(self, start, size):
        if self.global_image:
            start_global, size_global = self.transformer.to_global(start, size)
            return self.global_image.get_sub_image(start_global,
                                                   size_global).image
        else:
            return None

    def write_image(self, data_source, rescale_limits):
        self.close(
        )  # Note: we generally expect file classes to close themselves after writing the file

    def close(self):
        self.open = False
Exemplo n.º 9
0
def global_coordinate_transformer(size):
    return CoordinateTransformer(np.zeros_like(size), size, Axis(np.arange(0, len(size)), np.zeros_like(size)))