darwin.importer.formats package

Submodules

darwin.importer.formats.coco module

darwin.importer.formats.coco.parse_path(path: Path) List[AnnotationFile] | None[source]

Parses the given coco file and returns a List[dt.AnnotationFile] with the parsed information.

Parameters:

path (Path) – The Path to the coco file.

Returns:

Returns None if the given file is not in json format, or List[dt.AnnotationFile] otherwise.

Return type:

Optional[List[dt.AnnotationFile]]

darwin.importer.formats.coco.parse_json(path: Path, data: Dict[str, Any]) Iterator[AnnotationFile][source]

Parses the given json structure into an Iterator[dt.AnnotationFile].

Parameters:
  • path (Path) – The Path where file containing the data is.

  • data (Dict[str, Any]) – The json data to process.

Returns:

An iterator of all parsed annotation files.

Return type:

Iterator[dt.AnnotationFile]

darwin.importer.formats.coco.parse_annotation(annotation: Dict[str, Any], category_lookup_table: Dict[str, Any]) List[Annotation][source]

Parses the given json dictionary into a darwin Annotation if possible.

Parameters:
  • annotation (Dict[str, dt.UnknownType]) – The json dictionary to parse.

  • category_lookup_table (Dict[str, dt.UnknownType]) – Dictionary with all the categories from the coco file.

Returns:

A darwin Annotation if the parse was successful, or None otherwise.

Return type:

Optional[dt.Annotation]

darwin.importer.formats.coco.decode_binary_rle(data: str) List[int][source]

Decodes binary rle to integer list rle.

darwin.importer.formats.csv_tags module

darwin.importer.formats.csv_tags.parse_path(path: Path) List[AnnotationFile] | None[source]

Parses the given file and returns a List[dt.AnnotationFile] with the parsed files, or None if the given file’s extension is not .csv.

Parameters:

path (Path) – The Path of the file to parse.

Returns:

A List[dt.AnnotationFile] or None if the function was not able to parse the file.

Return type:

Optional[List[dt.AnnotationFile]]

darwin.importer.formats.csv_tags_video module

darwin.importer.formats.csv_tags_video.parse_path(path: Path) List[AnnotationFile] | None[source]

Parses the given csv video file and returns a List[dt.AnnotationFile], or None if the extension of the given file is not .csv.

Parameters:

path (Path) – The Path to the file to be parsed.

Returns:

A List[dt.AnnotationFile] or None if the file was parseable.

Return type:

Optional[List[dt.AnnotationFile]]

darwin.importer.formats.darwin module

darwin.importer.formats.darwin.parse_path(path: Path) AnnotationFile | None[source]

Parses the given file into a darwin AnnotationFile or returns None if the file does not have a .json extension.

Parameters:

path (Path) – The Path of the file to parse.

Returns:

The AnnotationFile file or None if the file was not parseable.

Return type:

Optional[dt.AnnotationFile]

darwin.importer.formats.dataloop module

darwin.importer.formats.dataloop.parse_path(path: Path) AnnotationFile | None[source]

Parses the given dataloop file and returns the corresponding darwin AnnotationFile, or None if the file’s extension is not .json.

Parameters:

path (Path) – The Path of the file to parse.

Returns:

The corresponding AnnotationFile, or None if the given file was not parseable.

Return type:

Optional[dt.AnnotationFile]

darwin.importer.formats.labelbox module

darwin.importer.formats.labelbox.parse_path(path: Path) List[AnnotationFile] | None[source]

Parses the given LabelBox file and maybe returns the corresponding annotations. The file must have a structure similar to the following:

[
    {
        "Label":{
            "objects":[
                {
                    "title": "SomeTitle",
                    "bbox":{"top":3558, "left":145, "height":623, "width":449}
                },
                { }
            ],
            "classifications": [
                {
                    "value": "a_question",
                    "answer": {"value": "an_answer"}
                }
            ]
        },
        "External ID": "demo-image-7.jpg"
    },
    { }
]

You can check the Labelbox Schemas in labelbox_schemas.py.

Currently we support the following annotations:

We also support conversion from question/answer to Annotation Tags for the following:

  • Radio Buttons

  • Checklists

  • Free Text

Parameters:

path (Path) – The path of the file to parse.

Returns:

The AnnotationFiles with the parsed information from the file or None, if the file is not a json file.

Return type:

Optional[List[darwin.datatypes.AnnotationFile]]

Raises:

ValidationError – If the given JSON file is malformed or if it has an unknown annotation. To see a list of possible annotation formats go to: https://docs.labelbox.com/docs/annotation-types-1

darwin.importer.formats.labelbox_schemas module

darwin.importer.formats.nifti module

darwin.importer.formats.nifti.parse_path(path: Path, legacy_remote_file_slot_affine_maps: Dict[Path, Dict[str, Any]] = {}, pixdims_and_primary_planes: Dict[Path, Dict[str, Tuple[List[float], str]]] = {}) List[AnnotationFile] | None[source]

Parses the given nifti file and returns a List[dt.AnnotationFile] with the parsed information.

Parameters:
  • path (Path) – The Path to the nifti file.

  • legacy_remote_file_slot_affine_maps (Optional[Dict[Path, Dict[str, Any]]]) – A dictionary of remote file full paths to their slot affine maps

  • remote_file_pixdims (Optional[Dict[Path, Dict[str, Tuple[List[float], str]]]]) – A dictionary of remote file full paths to their pixel dimensions and primary plane

Returns:

Returns None if the given file is not in json format, or List[dt.AnnotationFile] otherwise.

Return type:

Optional[List[dt.AnnotationFile]]

darwin.importer.formats.nifti.get_polygon_video_annotations(volume: ndarray, class_name: str, class_idxs: List[int], slot_names: List[str], is_mpr: bool, pixdims_and_primary_planes: Dict[Path, Dict[str, Tuple[List[float], str]]], remote_file_path: Path, legacy: bool = False) List[VideoAnnotation] | None[source]
darwin.importer.formats.nifti.get_mask_video_annotations(volume: ndarray, processed_class_map: Dict, slot_names: List[str], pixdims_and_primary_planes: Dict[Path, Dict[str, Tuple[List[float], str]]], remote_file_path: Path, isotropic: bool = False) List[VideoAnnotation] | None[source]

The function takes a volume and a class map and returns a list of video annotations

We write a single raster layer for the volume but K mask annotations, where K is the number of classes.

darwin.importer.formats.nifti.nifti_to_video_polygon_annotation(volume: ndarray, class_name: str, class_idxs: List[int], slot_names: List[str], pixdims: List[float], view_idx: int, legacy: bool = False) List[VideoAnnotation] | None[source]
darwin.importer.formats.nifti.mask_to_polygon(mask: ndarray, class_name: str, pixdims: List[float], legacy: bool = False) Annotation | None[source]
darwin.importer.formats.nifti.process_class_map(class_map)[source]

This function takes a class_map and returns a dictionary with the class names as keys and all the corresponding class indexes as values.

darwin.importer.formats.nifti.rectify_header_sform_qform(img_nii)[source]

Look at the sform and qform of the nifti object and correct it if any incompatibilities with pixel dimensions

Adapted from https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/io/misc_io.py

Parameters:

img_nii – nifti image object

darwin.importer.formats.nifti.affine_to_spacing(affine: ~numpy.ndarray, r: int = 3, dtype=<class 'float'>, suppress_zeros: bool = True) ndarray[source]

Copied over from monai.data.utils - https://docs.monai.io/en/stable/_modules/monai/data/utils.html

Computing the current spacing from the affine matrix.

Parameters:
  • affine – a d x d affine matrix.

  • r – indexing based on the spatial rank, spacing is computed from affine[:r, :r].

  • dtype – data type of the output.

  • suppress_zeros – whether to surpress the zeros with ones.

Returns:

an r dimensional vector of spacing.

darwin.importer.formats.nifti.correct_nifti_header_if_necessary(img_nii)[source]

Check nifti object header’s format, update the header if needed. In the updated image pixdim matches the affine.

Parameters:

img_nii – nifti image object

darwin.importer.formats.nifti.process_nifti(input_data: Nifti1Image, ornt: List[List[float]] | None = [[0.0, -1.0], [1.0, -1.0], [2.0, -1.0]], remote_file_path: Path = PosixPath('/'), legacy_remote_file_slot_affine_maps: Dict[Path, Dict[str, Any]] = {}) Tuple[ndarray, Tuple[float], str][source]

Converts a NifTI object of any orientation to the passed ornt orientation. The default ornt is LPI.

Files that were uploaded before the MED_2D_VIEWER feature are legacy. Non-legacy files are uploaded and re-oriented to the LPI orientation. Legacy files files were treated differently: - Legacy NifTI files were re-oriented to LPI, but their

affine was stored as RAS, which is the opposite orientation. However, because their pixel data is stored in LPI, we can treat them the same way as non-legacy files.

  • Legacy DICOM files were not always re-oriented to LPI. We therefore use the affine of the dataset item from slot_affine_map to re-orient the NifTI file to be imported

Parameters:
  • input_data – nibabel NifTI object.

  • ornt – (n,2) orientation array. It defines a transformation to LPI ornt[N,1] is a flip of axis N of the array, where 1 means no flip and -1 means flip. ornt[:,0] is the transpose that needs to be done to the implied array, as in arr.transpose(ornt[:,0]).

  • remote_file_path – Path The full path of the remote file

  • legacy_remote_file_slot_affine_maps – Dict[Path, Dict[str, Any]] A dictionary of remote file full paths to their slot affine maps

Returns:

pixel array with orientation ornt.

Return type:

data_array

darwin.importer.formats.nifti.convert_to_dense_rle(raster: ndarray) List[int][source]
darwin.importer.formats.nifti.get_new_axial_size(volume: ndarray, pixdims: List[float], isotropic: bool = False) Tuple[int, int][source]

Get the new size of the Axial plane after resizing to isotropic pixel dimensions.

Parameters:
  • volume – Input volume.

  • pixdims – The pixel dimensions / spacings of the volume.

  • isotropic – bool, default: True If True, the function will attempt to resize the annotations to isotropic pixel dimensions. If False, the function will not attempt to resize the annotations to isotropic pixel dimensions.

Returns:

The new size of the Axial plane.

Return type:

Tuple[int, int]

darwin.importer.formats.nifti_schemas module

darwin.importer.formats.pascal_voc module

darwin.importer.formats.pascal_voc.parse_path(path: Path) AnnotationFile | None[source]

Parses the given pascalvoc file and maybe returns the corresponding annotation. The file must have the following structure:

<filename>SOME_FILE_NAME</filename>
<object>
    <name>CLASS_NAME</name>
    <bndbox>
        <xmax>NUMBER</xmax>
        <xmin>NUMBER</xmin>
        <ymax>NUMBER</ymax>
        <ymin>NUMBER</ymin>
    </bndbox>
</object>
<object>
    ...
</object>
Parameters:

path (Path) – The path of the file to parse.

Returns:

An AnnotationFile with the parsed information from the file or None, if the file is not a XML file.

Return type:

Optional[darwin.datatypes.AnnotationFile]

Raises:

ValueError – If a mandatory child element is missing or is empty. Mandatory child elements are: filename, name, bndbox, xmin, xmax, ymin and ymax.

darwin.importer.formats.superannotate module

darwin.importer.formats.superannotate.parse_path(path: Path) AnnotationFile | None[source]

Parses SuperAnnotate annotations inside the given file and returns the corresponding Darwin JSON annotations. If the given file is not a .json or is a classes.json then None is returned instead.

Each annotation file must have a structure similar to the following:

{
    "instances": [
        {
            "classId": 1,
            "attributes": [],
            "type": "point",
            "x": 1,
            "y": 0
        },
        // { ... }
    ],
    "tags": ["a_tag_here"],
    "metadata": {
        "name": "a_file_name.json"
    }
}

Currently we support the following annotations:

We also support attributes and tags.

Each file must also have in the same folder a classes.json file with information about the classes. This file must have a structure similar to:

[
    {"name": "a_name_here", "id": 1, "attribute_groups": []},
    // { ... }
]

You can check the SuperAnnotate Schemas in superannotate_schemas.py.

Parameters:

path (Path) – The path of the file to parse.

Returns:

The AnnotationFile with the parsed information from each SuperAnnotate annotation inside or None if the given file is not a .json or is classes.json.

Return type:

Optional[darwin.datatypes.AnnotationFile]

Raises:

ValidationError – If any given JSON file is malformed or if it has an unknown annotation. To see a list of possible annotation formats go to: https://doc.superannotate.com/docs/vector-json

darwin.importer.formats.superannotate_schemas module

Module contents