Skip to content

dsa_upload

Upload annotation to DSA

Upload json annotation file as a new annotation to the image in the DSA collection.

Parameters:

Name Type Description Default
dsa_endpoint_url string

DSA API endpoint e.g. http://localhost:8080/api/v1

'???'
annotation_file_urlpath string

URL/path to a DSA annotation json file

''
annotation_file_list_urlpath string

URL/path to a DSA annotation json file

''
collection_name string

name of the collection in DSA

'???'
image_filename string

name of the image file in DSA e.g. 123.svs. If not specified, infer from annotiaton_file_urpath

''
username string

DSA username (defaults to environment variable DSA_USERNAME)

'${oc.env:DSA_USERNAME}'
password string

DSA password (defaults to environment variable DSA_PASSWORD)

'${oc.env:DSA_PASSWORD}'
force bool

upload even if annotation with same name exists for the slide

False
insecure bool

insecure ssl

False
storage_options dict

options to pass to reading functions

{}
local_config string

local config yaml url/path

''

Returns:

Name Type Description
dict

metadata

Source code in src/luna/pathology/cli/dsa_upload.py
@timed
@save_metadata
def cli(
    dsa_endpoint_url: str = "???",
    annotation_file_urlpath: str = "",
    annotation_file_list_urlpath: str = "",
    collection_name: str = "???",
    image_filename: str = "",
    username: str = "${oc.env:DSA_USERNAME}",
    password: str = "${oc.env:DSA_PASSWORD}",
    force: bool = False,
    insecure: bool = False,
    storage_options: dict = {},
    local_config: str = "",
):
    """Upload annotation to DSA

    Upload json annotation file as a new annotation to the image in the DSA collection.

    Args:
        dsa_endpoint_url (string): DSA API endpoint e.g. http://localhost:8080/api/v1
        annotation_file_urlpath (string): URL/path to a DSA annotation json file
        annotation_file_list_urlpath (string): URL/path to a DSA annotation json file
        collection_name (string): name of the collection in DSA
        image_filename (string): name of the image file in DSA e.g. 123.svs. If not specified, infer from annotiaton_file_urpath
        username (string): DSA username (defaults to environment variable DSA_USERNAME)
        password (string): DSA password (defaults to environment variable DSA_PASSWORD)
        force (bool): upload even if annotation with same name exists for the slide
        insecure (bool): insecure ssl
        storage_options (dict): options to pass to reading functions
        local_config (string): local config yaml url/path

    Returns:
        dict: metadata
    """
    config = get_config(vars())

    if (
        not config["annotation_file_urlpath"]
        and not config["annotation_file_list_urlpath"]
    ):
        raise fire.core.FireError(
            "Specify either annotation_file_urlpath or annotation_file_list_urlpath"
        )

    annotation_file_urlpaths = []
    if config["annotation_file_urlpath"]:
        annotation_file_urlpaths.append(config["annotation_file_urlpath"])
    if config["annotation_file_list_urlpath"]:
        with open(config["annotation_file_list_urlpath"], "r") as of:
            data = of.read()
            annotation_file_urlpaths += data.split("\n")

    uuids = []
    for idx, annotation_file_urlpath in enumerate(annotation_file_urlpaths):
        logger.info(
            f"Uploading {annotation_file_urlpath}: {idx+1}/{len(annotation_file_urlpaths)}"
        )
        image_filename = config["image_filename"]
        if not image_filename:
            image_filename = Path(annotation_file_urlpath).with_suffix(".svs").name
            image_filename = re.sub(".*_", "", image_filename)
            if not image_filename:
                raise ValueError(
                    f"Unable to infer image_filename from {annotation_file_urlpath}"
                )
            logger.info(f"Image filename inferred as {image_filename}")
        dsa_uuid = _upload_annotation_to_dsa(
            config["dsa_endpoint_url"],
            annotation_file_urlpath,
            config["collection_name"],
            image_filename,
            config["username"],
            config["password"],
            config["force"],
            config["insecure"],
            config["storage_options"],
        )
        logger.info(f"Uploaded item to {dsa_uuid}")
        if dsa_uuid:
            uuids.append(dsa_uuid)

    return {"item_uuids": uuids}