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
AnnotationFile
s into the coco format inside of the givenoutput_dir
.- Parameters:
annotation_files (Iterator[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s 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
AnnotationFile
s into the dataloop format inside of the givenoutput_dir
.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s into instance masks format inside of the givenoutput_dir
. Deletes everything withinoutput_dir/masks
before writting to it.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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.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
AnnotationFile
s into nifti format inside of the givenoutput_dir
. Deletes everything withinoutput_dir/masks
before writting to it.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s 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
AnnotationFile
s 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. IfFalse
, 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. IfFalse
, 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.
darwin.exporter.formats.pascalvoc moduleο
- darwin.exporter.formats.pascalvoc.export(annotation_files: Iterable[AnnotationFile], output_dir: Path) None [source]ο
Exports the given
AnnotationFile
s into the pascalvoc format inside of the givenoutput_dir
.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s into semantic masks inside of the givenoutput_dir
.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s into the YOLO format inside of the givenoutput_dir
.- Parameters:
annotation_files (Iterable[dt.AnnotationFile]) β The
AnnotationFile
s 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
AnnotationFile
s 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