Пример #1
0
    def test_to_from_condensed(self, condensed, dim, flip):
        axis = Axis.from_condensed_format(condensed)
        self.assertSequenceEqual(axis.dim_order, dim)
        self.assertSequenceEqual(axis.dim_flip, flip)
        self.assertSequenceEqual(axis.to_condensed_format(), condensed)

        try:
            Axis.from_condensed_format([0, 1, 2])
            self.fail("Zero-indexed array for condensed dim ordering should "
                      "assert")
        except ValueError as e:
            pass
    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())
Пример #4
0
 def __init__(self, size, file_format, dim_order_condensed, data_type, msb,
              voxel_size):
     self.data_type = data_type
     self.file_format = file_format
     self.size = size
     self.num_dims = len(size)
     self.msb = msb
     dim_order = dim_order_condensed if dim_order_condensed \
         else np.arange(1, self.num_dims + 1).tolist()
     self.axis = Axis.from_condensed_format(dim_order)
     self.voxel_size = voxel_size
Пример #5
0
 def __init__(self, filename, file_format, data_type, template, ranges,
              dim_order_condensed, suffix, index, msb, compression):
     self.suffix = suffix
     self.index = index
     self.filename = filename
     self.file_format = file_format
     self.data_type = data_type
     self.template = template
     self.ranges = SubImageRanges(ranges)
     self.axis = Axis.from_condensed_format(dim_order_condensed)
     self.msb = msb
     self.compression = compression
    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())
Пример #7
0
def get_dim_order(header):
    """Return the condensed dimension order and flip string for this header"""
    if header["TransformMatrix"]:
        transform = header["TransformMatrix"]
        new_dimension_order, flip_orientation = \
            mhd_comsines_to_permutation(
                transform[0:3], transform[3:6], transform[6:9])
    elif header["AnatomicalOrientation"]:
        new_dimension_order, flip_orientation = \
            anatomical_to_permutation(
                header["AnatomicalOrientation"])
    else:
        new_dimension_order = [0, 1, 2]
        flip_orientation = [False, False, False]

    return Axis(new_dimension_order, flip_orientation).to_condensed_format()
Пример #8
0
    def test_mhd_cosines_to_permutation(self, cosines):
        for flip_i in [-1, 1]:
            for flip_j in [-1, 1]:
                for flip_k in [-1, 1]:
                    c1 = [x * flip_i for x in cosines[0:3]]
                    c2 = [x * flip_j for x in cosines[3:6]]
                    c3 = [x * flip_k for x in cosines[6:9]]
                    perm_computed, flip_computed = mhd_cosines_to_permutation(
                        c1, c2, c3)
                    cosines_computed = permutation_to_cosine(
                        perm_computed, flip_computed)
                    self.assertEqual(cosines_computed, c1 + c2 + c3)

                    cosines_computed_2 = condensed_to_cosine(
                        Axis(perm_computed,
                             flip_computed).to_condensed_format())
                    self.assertEqual(cosines_computed_2, c1 + c2 + c3)
Пример #9
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)
Пример #10
0
def global_coordinate_transformer(size):
    return CoordinateTransformer(np.zeros_like(size), size, Axis(np.arange(0, len(size)), np.zeros_like(size)))
Пример #11
0
def condensed_to_cosine(condensed_format):
    """Get mhd direction cosine for this condensed format axis"""

    axis = Axis.from_condensed_format(condensed_format)
    return permutation_to_cosine(axis.dim_order, axis.dim_flip)
Пример #12
0
def generate_input_descriptors(input_file, start_index):
    """Create descriptors for one or more input files that do not have a
    descriptor file"""

    format_factory = FormatFactory()
    input_file_base, extension = os.path.splitext(input_file)
    descriptors = []
    current_ranges = None
    combined_header = None
    full_image_size = None

    if start_index is None:
        # If no start index is specified, load a single header file
        file_index = 0
        format_str = ""
    else:
        # Load a series of files starting with the specified prefix
        file_index = start_index
        format_str = _get_format_string(extension, input_file_base,
                                        start_index)

    suffix = format_str.format(start_index)
    header_filename = input_file_base + suffix + extension

    if not os.path.isfile(header_filename):
        raise ValueError('No file series found starting with ' +
                         header_filename)

    # Loop through all the input files
    while True:
        file_descriptor, current_header = parse_header(header_filename,
                                                       format_factory)
        data_type = file_descriptor.data_type
        dim_order = file_descriptor.dim_order
        file_format = file_descriptor.file_format
        current_image_size = file_descriptor.image_size
        voxel_size = file_descriptor.voxel_size
        msb = file_descriptor.msb
        compression = file_descriptor.compression

        # Reorder image size and voxel size dimensions
        axis = Axis.from_condensed_format(dim_order)
        current_image_size = \
            np.take(current_image_size, axis.reverse_dim_order).tolist()
        voxel_size = np.take(voxel_size, axis.reverse_dim_order).tolist()

        if not current_ranges:
            full_image_size = copy.deepcopy(current_image_size)
            combined_header = copy.deepcopy(current_header)
            current_ranges = [[0, current_image_size[0] - 1, 0, 0],
                              [0, current_image_size[1] - 1, 0, 0],
                              [0, current_image_size[2] - 1, 0, 0]]
        else:
            # For multiple input files, concatenate volumes
            if current_image_size[0] != full_image_size[0]:
                raise ValueError(
                    'When loading without a descriptor file, the first '
                    'dimension of each file must '
                    'match')
            if current_image_size[1] != full_image_size[1]:
                raise ValueError(
                    'When loading without a descriptor file, the second '
                    'dimension of each file must '
                    'match')
            full_image_size[2] = full_image_size[2] + current_image_size[2]
            current_ranges[2][0] = current_ranges[2][1] + 1
            current_ranges[2][1] = current_ranges[2][1] + \
                current_image_size[2]

        # Create a descriptor for this subimage
        ranges_to_write = copy.deepcopy(current_ranges)
        descriptors.append(
            SubImageDescriptor(index=file_index,
                               suffix=suffix,
                               filename=header_filename,
                               ranges=ranges_to_write,
                               template=combined_header,
                               data_type=data_type,
                               dim_order_condensed=dim_order,
                               file_format=file_format,
                               msb=msb,
                               compression=compression,
                               voxel_size=voxel_size))

        if start_index is None:
            # Single file already loaded, so terminate the while True loop
            break
        else:
            # Search for next file, and if not found terminate the loop
            file_index += 1
            suffix = format_str.format(file_index)
            header_filename = input_file_base + suffix + extension
            if not os.path.isfile(header_filename):
                break

    full_image_size = np.array(full_image_size).tolist()

    global_descriptor = _aggregate_global_descriptor(descriptors)

    # Update the combined image size
    combined_header["DimSize"] = full_image_size

    # Update voxel size
    combined_header["ElementSize"] = voxel_size

    return combined_header, descriptors, global_descriptor