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 aList[dt.AnnotationFile]
with the parsed information.- Parameters:
path (Path) β The
Path
to thecoco
file.- Returns:
Returns
None
if the given file is not injson
format, orList[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 anIterator[dt.AnnotationFile]
.- Parameters:
path (Path) β The
Path
where file containing thedata
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 darwinAnnotation
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, orNone
otherwise.- Return type:
Optional[dt.Annotation]
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 returnsNone
if the file does not have a.json
extension.- Parameters:
path (Path) β The
Path
of the file to parse.- Returns:
The
AnnotationFile
file orNone
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 darwinAnnotationFile
, orNone
if the fileβs extension is not.json
.- Parameters:
path (Path) β The
Path
of the file to parse.- Returns:
The corresponding
AnnotationFile
, orNone
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:
bounding-box
Image
: https://docs.labelbox.com/docs/bounding-box-jsonpolygon
Image
: https://docs.labelbox.com/docs/polygon-jsonpoint
Image
: https://docs.labelbox.com/docs/point-jsonpolyline
Image
: https://docs.labelbox.com/docs/polyline-json
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 aList[dt.AnnotationFile]
with the parsed information.- Parameters:
path (Path) β The
Path
to thenifti
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 injson
format, orList[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.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 aclasses.json
thenNone
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:
point
Vector
: https://doc.superannotate.com/docs/vector-json#pointellipse
Vector
: https://doc.superannotate.com/docs/vector-json#ellipsecuboid
Vector
: https://doc.superannotate.com/docs/vector-json#cuboidbbox
Vector
(not rotated): https://doc.superannotate.com/docs/vector-json#bounding-box-and-rotated-bounding-boxpolygon and polyline
Vector
s: https://doc.superannotate.com/docs/vector-json#polyline-and-polygon
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 isclasses.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