darwin.exporter.formats package

Subpackages

Submodules

darwin.exporter.formats.coco module

darwin.exporter.formats.coco.export(annotation_files: Iterator[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into the coco format inside of the given output_dir.

Parameters:
  • annotation_files (Iterator[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new coco file will be.

darwin.exporter.formats.cvat module

darwin.exporter.formats.cvat.export(annotation_files: Iterator[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFile``s into the cvat format inside of the given ``output_dir.

Parameters:
  • annotation_files (Iterator[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new cvat file will be.

darwin.exporter.formats.darwin module

darwin.exporter.formats.darwin.build_image_annotation(annotation_file: AnnotationFile, team_name: str) Dict[str, Any][source]

Builds and returns a dictionary with the annotations present in the given file in Darwin v2 format.

Parameters:

annotation_file (AnnotationFile) – File with the image annotations to extract. For schema, see: https://darwin-public.s3.eu-west-1.amazonaws.com/darwin_json/2.0/schema.json

Returns:

A dictionary with the annotations in Darwin v2 format.

Return type:

Dict[str, Any]

darwin.exporter.formats.dataloop module

darwin.exporter.formats.dataloop.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into the dataloop format inside of the given output_dir.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new coco file will be.

darwin.exporter.formats.instance_mask module

darwin.exporter.formats.instance_mask.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into instance masks format inside of the given output_dir. Deletes everything within output_dir/masks before writting to it.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new instance mask files will be.

darwin.exporter.formats.mask module

darwin.exporter.formats.mask.get_palette(mode: Literal['index', 'grey', 'rgb'], categories: List[str]) Dict[str, int][source]

Returns a palette for the given mode and categories.

Parameters:
  • mode (dt.MaskTypes.Mode) – The mode to use for the palette.

  • categories (List[str]) – A list of categories to be rendered.

Returns:

A dict of categories and their corresponding palette value.

Return type:

dt.MaskTypes.Palette

darwin.exporter.formats.mask.get_rgb_colours(categories: List[str]) Tuple[List[int], Dict[str, List[int]]][source]

Returns a list of RGB colours and a dict of categories and their corresponding RGB palette value.

Parameters:

categories (dt.MaskTypes.CategoryList) – A list of categories to be rendered.

Returns:

  • dt.MaskTypes.RgbColors – A list of RGB colours for each category.

  • dt.MaskTypes.RgbPalette – A dict of categories and their corresponding RGB palette value.

darwin.exporter.formats.mask.get_render_mode(annotations: List[Annotation | VideoAnnotation]) Literal['raster', 'polygon'][source]

Returns the type of render mode for the given annotations.

Parameters:

annotations (List[dt.AnnotationLike]) – A list of annotations to be rendered.

Returns:

A string reading either β€œraster” or β€œpolygon”.

Return type:

TypeOfRenderType

darwin.exporter.formats.mask.rle_decode(rle: List[int], label_colours: Dict[int, int]) List[int][source]

Decodes a run-length encoded list of integers and substitutes labels by colours.

Parameters:

rle (List[int]) – A run-length encoded list of integers.

Returns:

The decoded list of integers.

Return type:

List[int]

darwin.exporter.formats.mask.get_or_generate_colour(cat_name: str, colours: Dict[str, int]) int[source]

Returns the colour for the given category name, or generates a new one if it doesn’t exist.

Parameters:
  • cat_name (str) – The name of the category.

  • colours (dt.MaskTypes.ColoursDict) – A dictionary of category names and their corresponding colours.

Return type:

int - the integer for the colour name. These will later be reassigned to a wider spread across the colour spectrum.

darwin.exporter.formats.mask.render_polygons(mask: ndarray[Any, dtype[_ScalarType_co]], colours: Dict[str, int], categories: List[str], annotations: List[Annotation | VideoAnnotation], annotation_file: AnnotationFile, height: int, width: int) Tuple[List[Exception], ndarray[Any, dtype[_ScalarType_co]], List[str], Dict[str, int]][source]

Renders the polygons in the given annotations onto the given mask.

Parameters:
  • mask (NDArray) – The mask to render the polygons onto.

  • colours (dt.MaskTypes.ColoursDict) – A dictionary of category names and their corresponding colours.

  • categories (dt.MaskTypes.CategoryList) – A list of category names.

  • annotations (List[dt.AnnotationLike]) – A list of annotations to be rendered.

  • annotation_file (dt.AnnotationFile) – The annotation file that the annotations belong to.

  • height (int) – The height of the image.

  • width (int) – The width of the image.

Return type:

Tuple[List[Exception], Image, dt.MaskTypes.CategoryList, dt.MaskTypes.ColoursDict]

darwin.exporter.formats.mask.render_raster(mask: ndarray[Any, dtype[_ScalarType_co]], colours: Dict[str, int], categories: List[str], annotations: List[Annotation | VideoAnnotation], annotation_file: AnnotationFile, height: int, width: int) Tuple[List[Exception], ndarray[Any, dtype[_ScalarType_co]], List[str], Dict[str, int]][source]

Renders the raster layers in the given annotations onto the given mask.

Parameters:
  • mask (NDArray) – The mask to render the polygons onto. Not used. Only returned if no errors occur.

  • colours (dt.MaskTypes.ColoursDict) – The colours list. Only returned if no errors occur.

  • annotations (List[dt.AnnotationLike]) – A list of annotations to be rendered.

  • annotation_file (dt.AnnotationFile) – Not used. Present for interface consistency.

  • height (int) – The height of the image.

  • width (int) – The width of the image.

Return type:

Tuple[List[Exception], Image, dt.MaskTypes.CategoryList, dt.MaskTypes.ColoursDict]

darwin.exporter.formats.mask.export(annotation_files: Iterable[AnnotationFile], output_dir: Path, mode: Literal['index', 'grey', 'rgb']) None[source]
darwin.exporter.formats.mask.annotations_exceed_window(annotations: List[Annotation], height: int, width: int) bool[source]

Check if any annotations exceed the image window

Parameters:
  • annotations (List[dt.Annotation]) – List of annotations

  • height (int) – height of image

  • width (int) – width of image

Returns:

True if any annotation exceeds window, false otherwise

Return type:

bool

darwin.exporter.formats.mask.get_extents(annotations: List[Annotation], height: int = 0, width: int = 0) Tuple[int, int, int, int][source]

Create a bounding box around all annotations in discrete pixel space

Parameters:
  • annotations (List[dt.Annotation]) – List of annotations

  • height (int) – Height to start with

  • width (int) – Width to start with

Returns:

x_min, x_max, y_min, y_max

Return type:

Tuple[int, int, int, int]

darwin.exporter.formats.mask.offset_polygon(polygon: List, offset_x: int, offset_y: int) List[source]

Offsets a polygon by a given amount

Parameters:
  • polygon (List) – List of coordinates

  • offset_x (int) – x offset value

  • offset_y (int) – y offset value

Returns:

polygon with offset applied

Return type:

List

darwin.exporter.formats.mask.offset_polygon_paths(polygons: List, offset_x: int, offset_y: int) List[source]
darwin.exporter.formats.mask.offset_simple_polygon(polygon: List, offset_x: int, offset_y: int) List[source]

darwin.exporter.formats.nifti module

class darwin.exporter.formats.nifti.Plane(value)[source]

Bases: Enum

An enumeration.

XY = 0
XZ = 1
YZ = 2
class darwin.exporter.formats.nifti.Volume(pixel_array: numpy.ndarray, affine: numpy.ndarray | None, original_affine: numpy.ndarray | None, dims: List, pixdims: List, class_name: str, series_instance_uid: str, from_raster_layer: bool, primary_plane: str)[source]

Bases: object

pixel_array: ndarray
affine: ndarray | None
original_affine: ndarray | None
dims: List
pixdims: List
class_name: str
series_instance_uid: str
from_raster_layer: bool
primary_plane: str
darwin.exporter.formats.nifti.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into nifti format inside of the given output_dir. Deletes everything within output_dir/masks before writting to it.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new instance mask files will be.

Return type:

sends output volumes, image_id and output_dir to the write_output_volume_to_disk function

darwin.exporter.formats.nifti.build_output_volumes(slot: Slot, from_raster_layer: bool = False, class_names_to_export: List[str] | None = None, mask_present: bool | None = False, primary_plane: str = 'AXIAL') Dict[source]

This is a function to create the output volumes based on the whole annotation file

Parameters:
  • annotation_file (dt.AnnotationFile) – The AnnotationFiles to be exported.

  • from_raster_layer (bool) – Whether the output volumes are being built from raster layers or not

  • class_names_to_export (List[str]) – The list of class names to export

  • mask_present (bool) – If mask annotations are present in the annotation

  • primary_plane (str) – The primary plane of the annotation

Returns:

output_volumes – The output volume built per class

Return type:

Dict

darwin.exporter.formats.nifti.check_for_error_and_return_imageid(annotation_file: AnnotationFile, output_dir: Path) str | bool[source]

Given the annotation_file file and the output directory, checks for a range of errors and returns messages accordingly.

Parameters:
  • annotation_file (dt.AnnotationFile) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new instance mask files will be.

Returns:

Returns the image_id if no errors are found, otherwise returns False

Return type:

Union[str, bool]

darwin.exporter.formats.nifti.update_pixel_array(volume: Dict, annotation_class_name: str, im_mask: ndarray, primary_plane: str, frame_idx: int) Dict[source]

Updates the pixel array of the given volume with the given mask.

Parameters:
  • volume (Dict) – Volume with pixel array to be updated

  • annotation_class_name (str) – Name of the annotation class

  • im_mask (np.ndarray) – Mask to be added to the pixel array

  • primary_plane (str) – Plane of the mask

  • frame_idx (int) – Frame index of the mask

Returns:

Updated volume

Return type:

Dict

darwin.exporter.formats.nifti.populate_output_volumes_from_polygons(annotations: List[Annotation | VideoAnnotation], slot_map: Dict, output_volumes: Dict, legacy: bool = False)[source]

Populates the output volumes with the given polygon annotations. The annotations are converted into masks and added to the corresponding volume based on the series instance UID.

Parameters:
  • annotations (List[Union[dt.Annotation, dt.VideoAnnotation]]) – List of polygon annotations used to populate the volume with

  • slot_map (Dict) – Dictionary of the different slots within the annotation file

  • output_volumes (Dict) – Volumes created from the build_output_volumes file

  • legacy (bool, default=False) – If True, the exporter will use the legacy calculation. If False, the exporter will use the new calculation by dividing with pixdims.

darwin.exporter.formats.nifti.populate_output_volumes_from_raster_layer(annotation: Annotation | VideoAnnotation, mask_id_to_classname: Dict, slot_map: Dict, output_volumes: Dict, primary_plane: str) Dict[source]

Populates the output volumes provided with the raster layer annotations

Parameters:
  • annotation (Union[dt.Annotation, dt.VideoAnnotation]) – The Union of these two files used to populate the volume with

  • mask_id_to_classname (Dict) – Map from mask id to class names

  • slot_map (Dict) – Dictionary of the different slots within the annotation file

  • output_volumes (Dict) – volumes created from the build_output_volumes file

  • primary_plane (str) – The primary plane of the volume containing the annotation

Returns:

volume – Returns dict of volumes with class names as keys and volumes as values

Return type:

dict

darwin.exporter.formats.nifti.write_output_volume_to_disk(output_volumes: Dict, image_id: str, output_dir: str | Path, item_name: str, legacy: bool = False, slot_name: str = '0', filename: str | None = None) None[source]

Writes the given output volumes to disk.

Parameters:
  • output_volumes (Dict) – Output volumes to be written to disk

  • image_id (str) – The specific image id

  • output_dir (Union[str, Path]) – The output directory to write the volumes to

  • legacy (bool, default=False) – If True, the exporter will use the legacy calculation. If False, the exporter will use the new calculation by dividing with pixdims.

  • item_name (str) – Name of the dataset item.

  • slot_name (str) – Name of the dataset item slot the volume belongs to.

  • filename (str) – Name of the file occupying the dataset item slot.

Return type:

None

darwin.exporter.formats.nifti.shift_polygon_coords(polygon: List[Dict[str, float]], pixdim: List[float], primary_plane: str, legacy: bool = False) List[Dict[str, float]][source]

Shifts input polygon coordinates based on the primary plane and the pixdim of the volume the polygon belongs to.

If the volume is a legacy volume, we perform isotropic scaling

Parameters:
  • polygon (List[Dict[str, float]]) – The polygon to be shifted

  • pixdim (List[float]) – The (x, y, z) pixel dimensons of the image

  • primary_plane (str) – The primary plane of the volume that the polygon belongs to

  • legacy (bool) – Whether this polygon is being exported from a volume that requires legacy NifTI scaling

Returns:

The shifted polygon

Return type:

List[Dict[str, Number]]

darwin.exporter.formats.nifti.get_view_idx(frame_idx: int, groups: List) int[source]

Returns the view index for the given frame index and groups.

Parameters:
  • frame_idx (int) – Frame index

  • groups (List) – List of groups

Returns:

View index

Return type:

int

darwin.exporter.formats.nifti.process_metadata(metadata: Dict) Tuple[source]

Processes the metadata and returns the volume dimensions, pixel dimensions, affine and original affine.

Parameters:

metadata (Dict) – Metadata to be processed

Returns:

Tuple containing volume dimensions, pixel dimensions, affine and original affine

Return type:

Tuple

darwin.exporter.formats.nifti.process_affine(affine: str | List | ndarray) ndarray | None[source]

Converts affine to numpy array if it is not already.

Parameters:

affine (Union[str, List, np.ndarray]) – affine object to be converted

Returns:

affine as numpy array

Return type:

Optional[np.ndarray]

darwin.exporter.formats.nifti.create_error_message_json(error_message: str, output_dir: str | Path, image_id: str) bool[source]

Creates a json file with the given error message.

Parameters:
  • error_message (str) – Error message to be written to the file

  • output_dir (Union[str, Path]) – Output directory

  • image_id (str) – Associated image id

Returns:

Always returns False

Return type:

bool

darwin.exporter.formats.nifti.decode_rle(rle_data: List[int], width: int, height: int) ndarray[source]

Decodes run-length encoding (RLE) data into a mask array.

Parameters:
  • rle_data (List[int]) – List of RLE data

  • width (int) – Width of the data

  • height (int) – Height of the data

Returns:

RLE data

Return type:

np.ndarray

darwin.exporter.formats.numpy_encoder module

class darwin.exporter.formats.numpy_encoder.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Holds auxiliary functions to bridge numpy functionality with Python primitive types which are JSON friendly.

default(obj: Any) Any[source]

Converts the given numpy object into a Python’s primitive type.

Parameters:

obj (Any) – The object to convert.

Returns:

The converted object.

Return type:

Any

darwin.exporter.formats.pascalvoc module

darwin.exporter.formats.pascalvoc.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into the pascalvoc format inside of the given output_dir.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new pascalvoc files will be.

darwin.exporter.formats.semantic_mask module

darwin.exporter.formats.semantic_mask.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into semantic masks inside of the given output_dir.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new semantic mask files will be.

darwin.exporter.formats.semantic_mask_grey module

darwin.exporter.formats.semantic_mask_grey.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

darwin.exporter.formats.semantic_mask_index module

darwin.exporter.formats.semantic_mask_index.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

darwin.exporter.formats.yolo module

darwin.exporter.formats.yolo.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports the given AnnotationFiles into the YOLO format inside of the given output_dir.

Parameters:
  • annotation_files (Iterable[dt.AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new pascalvoc files will be.

darwin.exporter.formats.yolo_segmented module

class darwin.exporter.formats.yolo_segmented.Point(x, y)

Bases: tuple

x

Alias for field number 0

y

Alias for field number 1

darwin.exporter.formats.yolo_segmented.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None[source]

Exports YoloV8 format as segments

Parameters:
  • annotation_files (Iterable[AnnotationFile]) – The AnnotationFiles to be exported.

  • output_dir (Path) – The folder where the new pascalvoc files will be.

Return type:

None

darwin.exporter.formats.yolo_segmented.normalise(value: float, height_or_width: int) float[source]

Normalises the value to a proportion of the image size

Parameters:
  • value (float) – The value to be normalised.

  • height_or_width (Union[float, int]) – The height or width of the image.

Returns:

The normalised value.

Return type:

float

class darwin.exporter.formats.yolo_segmented.YoloSegmentedAnnotationType(value)[source]

Bases: Enum

The YoloV8 annotation types

UNKNOWN = 1
BOUNDING_BOX = 2
POLYGON = 3

Module contents