Exemplo n.º 1
0
    def _build_manifest(self, image: Image, segment_slugs: dict) -> dict:

        manifest = {
            "image": {
                "precision_bytes":
                image.get_image_data().dtype.itemsize,
                "size":
                list(image.get_image_data().shape
                     ),  # the image volume byte sequence does not contain this
                "voxel_size":
                list(image.get_voxel_size())
                if image.get_voxel_size() else None,
                "voxel_spacing":
                list(image.get_voxel_spacing())
                if image.get_voxel_spacing() else None,
            },
            "meta_data":
            image.get_meta_data(),
            "slices": [
                self._build_image_slice_manifest(image_slice)
                for image_slice in image.get_slices()
            ],
            "segments": [
                self._build_image_segment_manifest(
                    image_segment,
                    segment_slugs[image_segment.get_identifier()])
                for image_segment in image.get_segments()
            ],
        }

        # make sure the result will actually be readable
        self._validate_manifest(manifest)

        return manifest
Exemplo n.º 2
0
    def _generate_segment_slugs(self, image: Image) -> dict:

        result = {}

        for num, segment in enumerate(image.get_segments()):

            while True:

                slug = generate_random_string()

                if slug not in result:
                    break

            result[segment.get_identifier()] = slug

        return result
Exemplo n.º 3
0
    def write(self,
              image: Image,
              path: str,
              *,
              override_if_existing: bool = False) -> None:

        self._logger.debug("Writing image to gmh file under: {}".format(path))

        if os.path.exists(path) and not override_if_existing:
            raise ValueError(f"Path does already exist: {path}")

        temporary_file_path = f"{path}.tmp"
        assert not os.path.exists(temporary_file_path)

        with tarfile.open(temporary_file_path, "w") as tar_file_handle:

            def add_file(name: str, content) -> None:

                if isinstance(content, str):
                    content = content.encode()

                member = tarfile.TarInfo(name)
                member.size = len(content)

                tar_file_handle.addfile(member, io.BytesIO(content))

            segment_slugs = self._generate_segment_slugs(image)

            add_file("manifest.json",
                     self._build_manifest_document(image, segment_slugs))
            add_file("image_data.npy", image.get_image_data().tobytes())

            for image_segment in image.get_segments():

                if image_segment.is_empty():
                    continue

                segment_slug = segment_slugs[image_segment.get_identifier()]

                add_file(
                    IMAGE_SEGMENT_MASK_MEMBER_NAME_FORMAT.format(segment_slug),
                    image_segment.get_mask_in_bounding_box().tobytes())

        # swap written file into target path
        if os.path.exists(path):
            os.remove(path)
        os.rename(temporary_file_path, path)