Tator Python Client Documentation

Utility Functions

Shortcuts


get_api

tator.get_api(host=‘https://cloud.tator.io’, token=None)

Retrieves a tator.api instance using the given host and token.


get_parser

tator.get_parser(parser=None, description=None)

Returns an argument parser that includes host and token.


download_media

tator.download_media(api, media, out_path, quality=None, media_type=None, codec_or_mime=None)

Download a media file from Tator to an off-line location.

Example:

api = tator.get_api(host, token)
media = api.get_media(media_id)
out_path = f'/tmp/{media.name}'
for progress in tator.util.download_media(api, media, out_path):
    print(f"Download progress: {progress}%")

#download 720p version streaming
for progress in tator.util.download_media(api,
                                          media,
                                          out_path,
                                          720,
                                          'streaming'):
    print(f"Download progress: {progress}%")

Full utility Library

Utility functions for interacting with the Tator platform


MediaUtil

class tator.util.MediaUtil(temp_dir)

Top-level class to construct to handle per-media manipulations

get_animation(frames, fps, roi=None, render_format=‘mp4’, force_scale=None)

Return an animation of frames at a given FPS.

get_clip(frame_ranges)

Given a list of frame ranges generate a temporary mp4

get_tile_image(frames, rois=None, tile_size=None, render_format=‘jpg’, force_scale=None)

Generate a tile jpeg of the given frame/rois

load_from_media_object(media_obj, quality=None)

Initialize the MediaUtil with a tator media object (loaded with presigned URLs)

load_from_url(video_url, segment_url, height, width, fps)

Setup the MediaUtil instance with primitive types


chunked_create

tator.util.chunked_create(func: Callable, project: int, chunk_size: int = 500, **kwargs)

Breaks a create_*_list operation into chunks. Example:

created_ids = [
    new_id
    for response in tator.util.chunked_create(
        api.create_localization_list, chunk_size=100, body=my_long_list
    )
    for new_id in response.id
]

chunked_file_list

tator.util.chunked_file_list(paths, chunk_size=100)

Breaks file list into chunks for upload via media archive.

Example:

api = tator.get_api(host, token)
batch_num = 0
for batch in tator.util.chunked_file_list(paths):
    print(f"Uploading file {batch_num*100} / {len(paths)}")
    for progress, response in tator.util.upload_media_archive(api, project_id, batch):
        print(f"Upload progress: {progress}%")
    print(response.message)
    batch_num += 1

clone_leaf_list

tator.util.clone_leaf_list(src_api, query_params, dest_project, parent_mapping, leaf_type_mapping, dest_api=None)

Clone leaf list.

This can be used to clone leaves from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
query_params = {'project': 1, 'depth': 1}
dest_project = 1
created_ids = []
parent_mapping = {1: 10} # Mapping of parent leaf IDs
leaf_type_mapping = {1: 10} # Source leaf type ID -> Dest leaf type ID
generator = clone_leaf_list(src_api, query_params, dest_project, parent_mapping,
                            leaf_type_mapping, dest_api)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} leafs...")
    created_ids.append(response.id)
print(f"Finished creating {num_created} leafs!")

Example for same host:

api = tator.get_api(host, token)
query_params = {'project': 1, 'depth': 1}
dest_project = 1
parent_mapping = {1: 10}
leaf_type_mapping = {1: 10} # Source leaf type ID -> Dest leaf type ID
created_ids = []
generator = clone_leaf_list(api, query_params, dest_project, parent_mapping)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} leafs...")
    created_ids += response.id
print(f"Finished creating {num_created} leafs!")

clone_leaf_type

tator.util.clone_leaf_type(src_api, src_type_id, dest_project, dest_api=None)

Clone leaf type.

This can be used to clone leaf types from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_leaf_type(src_api, src_type_id, dest_project, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_leaf_type(src_api, src_type_id, dest_project)
print(response.message)

clone_localization_list

tator.util.clone_localization_list(src_api, query_params, dest_project, version_mapping, media_mapping, localization_type_mapping, dest_api=None)

Clone localization list.

This can be used to clone localizations from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
query_params = {'project': 1, 'media_id': [1]}
version_mapping = {1: 10} # Source version ID -> Dest version ID
media_mapping = {1: 10} # Source media ID -> Dest media ID
localization_type_mapping = {1: 10} # Source localization type ID -> Dest type ID
dest_project = 1
created_ids = []
generator = clone_localization_list(src_api, query_params, dest_project, version_mapping,
                                    media_mapping, localization_type_mapping, dest_api)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} localizations...")
    created_ids.append(response.id)
print(f"Finished creating {num_created} localizations!")

Example for same host:

api = tator.get_api(host, token)
query_params = {'media_id': [1]}
dest_project = 1
version_mapping = {1: 10}
media_mapping = {1: 10}
localization_type_mapping = {1: 10} # Source localization type ID -> Dest type ID
created_ids = []
generator = clone_localization_list(src_api, query_params, dest_project, version_mapping,
                                    media_mapping, localization_type_mapping)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} localizations...")
    created_ids += response.id
print(f"Finished creating {num_created} localizations!")

clone_localization_type

tator.util.clone_localization_type(src_api, src_type_id, dest_project, media_type_mapping, dest_api=None)

Clone localization type.

This can be used to clone localization types from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_type_id = 1
dest_project = 1
media_type_mapping = {1: 10}
response = tator.util.clone_localization_type(src_api, src_type_id, dest_project,
                                              media_type_mapping, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_localization_type(src_api, src_type_id, dest_project,
                                              media_type_mapping)
print(response.message)

clone_media_list

tator.util.clone_media_list(src_api, query_params, dest_project, media_mapping={}, dest_type=-1, dest_section=”, dest_api=None, ignore_transfer=False)

Clone media list.

This can be used to clone media from one project to another or from one host to another. In the case of the same host, the media files are not copied. If the destination host is different (dest_api is a tator.TatorApi object), the transcoded files for each media object is downloaded and then uploaded to the other host, and a new tator.Media object is created.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
query_params = {'project': 1, 'media_id': [1]}
media_mapping = {} # Only required if query contains multi media types
dest_project = 1
dest_type = -1
dest_section = 'My cloned media'
created_ids = []
generator = clone_media_list(src_api, query_params, dest_project, media_mapping,
                             dest_type, dest_section, dest_api)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} files...")
    created_ids.append(response.id[0])
print(f"Finished creating {num_created} files!")

Example for same host:

api = tator.get_api(host, token)
query_params = {'media_id': [1]}
dest_project = 1
created_ids = []
generator = clone_media_list(src_api, query_params, dest_project)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} files...")
    created_ids += response.id[0] # This response is from the CloneMedia endpoint.
print(f"Finished creating {num_created} files!")

clone_media_type

tator.util.clone_media_type(src_api, src_type_id, dest_project, dest_api=None)

Clone media type.

This can be used to clone media types from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_media_type(src_api, src_type_id, dest_project, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_media_type(src_api, src_type_id, dest_project)
print(response.message)

clone_membership

tator.util.clone_membership(src_api, src_membership_id, dest_project, user_mapping=None, dest_api=None)

Clone membership.

This can be used to clone memberships from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_membership_id = 1
dest_project = 1
user_mapping = {1: 10} # Mapping between user ID on source host and destination host.
response = tator.util.clone_membership(src_api, src_membership_id, dest_project,
                                       user_mapping, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_membership_id = 1
dest_project = 1
response = tator.util.clone_membership(src_api, src_membership_id, dest_project)
print(response.message)

clone_section

tator.util.clone_section(src_api, src_section_id, dest_project, dest_api=None)

Clone section.

This can be used to clone sections from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_section_id = 1
dest_project = 1
response = tator.util.clone_section(src_api, src_section_id, dest_project, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_section_id = 1
dest_project = 1
response = tator.util.clone_section(src_api, src_section_id, dest_project)
print(response.message)

clone_state_list

tator.util.clone_state_list(src_api, query_params, dest_project, version_mapping, media_mapping, localization_mapping, state_type_mapping, dest_api=None)

Clone state list.

This can be used to clone states from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
query_params = {'project': 1, 'media_id': [1]}
version_mapping = {1: 10} # Source version ID -> Dest version ID
media_mapping = {1: 10} # Source media ID -> dest media ID
localization_mapping = {} # For tracks, source localization ID -> dest localization ID
state_type_mapping = {1: 10} # Source state type ID -> Dest state type ID
dest_project = 1
dest_version = -1
created_ids = []
generator = clone_state_list(src_api, query_params, dest_project, version_mapping,
                             media_mapping, localization_mapping, state_type_mapping,
                             dest_api)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} states...")
    created_ids.append(response.id)
print(f"Finished creating {num_created} states!")

Example for same host:

api = tator.get_api(host, token)
query_params = {'media_id': [1]}
dest_project = 1
version_mapping = {1: 10} # Source version ID -> Dest version ID
media_mapping = {1: 10}
localization_mapping = {}
state_type_mapping = {1: 10} # Source state type ID -> Dest state type ID
created_ids = []
generator = clone_state_list(src_api, query_params, dest_project, version_mapping,
                             media_mapping, localization_mapping, state_type_mapping)
for num_created, num_total, response, id_map in generator:
    print(f"Created {num_created} of {num_total} states...")
    created_ids += response.id
print(f"Finished creating {num_created} states!")

clone_state_type

tator.util.clone_state_type(src_api, src_type_id, dest_project, media_type_mapping, dest_api=None)

Clone state type.

This can be used to clone state types from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_type_id = 1
dest_project = 1
media_type_mapping = {1: 10}
response = tator.util.clone_state_type(src_api, src_type_id, dest_project,
                                              media_type_mapping, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_type_id = 1
dest_project = 1
response = tator.util.clone_state_type(src_api, src_type_id, dest_project,
                                              media_type_mapping)
print(response.message)

clone_version

tator.util.clone_version(src_api, src_version_id, dest_project, version_mapping={}, dest_api=None)

Clone version.

This can be used to clone versions from one project to another or from one host to another.

Example for different host:

src_api = tator.get_api(host, token)
dest_api = tator.get_api(other_host, other_token)
src_version_id = 1
dest_project = 1
version_mapping = {} # Necessary if source version has base versions.
response = tator.util.clone_version(src_api, src_version_id, dest_project,
                                    version_mapping, dest_api)
print(response.message)

Example for same host:

api = tator.get_api(host, token)
src_version_id = 1
dest_project = 1
response = tator.util.clone_version(src_api, src_version_id, dest_project)
print(response.message)

download_attachment

tator.util.download_attachment(api, media, out_path, index=0)

Download an attachment from Tator to an off-line location.

Example:

api = tator.get_api(host, token)
media = api.get_media(media_id)
out_path = f'/tmp/{media.name}'
for progress in tator.util.download_attachment(api, media, out_path):
    print(f"Download progress: {progress}%")

#download second attachment
for progress in tator.util.download_attachment(api, media, out_path, index=1):
    print(f"Download progress: {progress}%")

download_media

tator.util.download_media(api, media, out_path, quality=None, media_type=None, codec_or_mime=None)

Download a media file from Tator to an off-line location.

Example:

api = tator.get_api(host, token)
media = api.get_media(media_id)
out_path = f'/tmp/{media.name}'
for progress in tator.util.download_media(api, media, out_path):
    print(f"Download progress: {progress}%")

#download 720p version streaming
for progress in tator.util.download_media(api,
                                          media,
                                          out_path,
                                          720,
                                          'streaming'):
    print(f"Download progress: {progress}%")

download_temporary_file

tator.util.download_temporary_file(api, temporary_file, out_path)

Download a temporary file from Tator to an off-line location.

Example:

api = tator.get_api(host, token)
temporary_file = api.get_temporary_file(temporary_file_id)
out_path = f'/tmp/{temporary_file.name}'
for progress in tator.util.download_temporary_file(api, temporary_file,
                                              out_path):
    print(f"Download progress: {progress}%")

find_single_change

tator.util.find_single_change(api: tator.TatorApi, project_id: int, entity_id: int, field_name: str, old_value: Any | None = None, new_value: Any | None = None, find_last_change: bool = False)

Finds the ChangeLog containing the desired field/value combination.

Example:

api = tator.get_api(host, token)
change_log = tator.util.find_change(api, media_id, "_deleted", new_value=True)
print(change_log)

At least one of old_value and new_value must be specified; if both are specified, then the matching changelog must match both, not just one.

The field_name must be prefixed with an underscore if it is not a user-defined attribute, note how “_deleted” corresponds to the Media.deleted field.


full_state_graphic

tator.util.full_state_graphic(api, state_id, force_scale=None)

Retrieves all detection images for a state.

Example:

state_graphic = full_state_graphic(api, state_id)

get_api

tator.util.get_api(host=‘https://cloud.tator.io’, token=None)

Retrieves a tator.api instance using the given host and token.


get_images

tator.util.get_images(file_path, media_or_state, num_images=None, width=None, height=None)

Loads image saved by tator.api.get_frame()

Can also be used with tator.api.get_state_graphic().


get_paginator

tator.util.get_paginator(api, func_name: str, batch_size: int | None = 1000)

Returns a Paginator object that returns pages of results from the given func_name. Example:

import tator
api = tator.get_api(host, token)
media_paginator = tator.util.get_paginator(api, "get_media_list")
page_iterator = media_paginator.paginate(project=project_id)
all_media = []
for page in page_iterator:
    for media in page:
        all_media.append(media)

import_media

tator.util.import_media(api, type_id, url, md5=None, section=None, fname=None, upload_gid=None, upload_uid=None, chunk_size=2097152, attributes=None, media_id=None, size=None, _request_timeout=10)

Imports a hosted media file.

Example:

api = tator.get_api(host, token)
response = tator.util.import_media(api, type_id, url)
print(response.message)

make_concat

tator.util.make_concat(api, name, media_ids, section, offsets=None)

Uploads a single media file.


make_live_stream

tator.util.make_live_stream(api, type_id, layout, name, section, feedInfo)

Make a live stream tator media object.


make_multi_stream

tator.util.make_multi_stream(api, type_id, layout, name, media_ids, section, quality=None, frame_offset=None)

Creates a multiview from the given arguments


md5sum

tator.util.md5sum(fname, size=None)

Computes md5sum-based fingerprint of a file contents. The return

is the md5sum of the file contents (up to 100Mb) hashed along with
the file size.

register_algorithm

tator.util.register_algorithm(host: str, token: str, project: int, manifest: str, algorithm_name: str, files_per_job: int, categories: list = [], description: str = ”, cluster_id: int | None = None) layout: ../../../../layouts/DocLayout.astro

Registers an algorithm argo workflow using the provided parameters

This will upload the given manifest file, save it to tator, and the new server side URL will be used when registering the workflow. This may change the filename of the manifest file.

Args:

host: Host URL
token: User token used for connecting to the host
project: Unique identifier of project associated with algorithm
manifest: Local path to argo workflow manifest file to upload, save,

> and register the algorithm with

algorithm_name: Unique name of the workflow
files_per_job: Number of media to process per workflow created
description: Optional description of workflow

layout: ../../../../layouts/DocLayout.astro cluster_id: Optional unique integer identifying the job cluster


register_applet

tator.util.register_applet(host: str, token: str, project: int, html_file: str, applet_name: str, categories: list = [], description: str = ”) layout: ../../../../layouts/DocLayout.astro

Registers a applet using the provided parameters

This will upload the given html file, save it to tator, and the new server side URL will be used when registering the applet. This may change the filename of the uploaded file


supplement_video_to_media

tator.util.supplement_video_to_media(api, media_id, role, configuration, filepath: str, force=False)

This routine will transcode filepath based on configuration to media_object See tator.api.delete_video_file() for information on deleting resolutions from a media object


to_dataframe

tator.util.to_dataframe(objects)

Converts list of objects to pandas.DataFrame.


update_applet

tator.util.update_applet(host: str, token: str, applet_id: int, html_file: str | None = None, applet_name: str | None = None, categories: list | None = None, description: str | None = None) layout: ../../../../layouts/DocLayout.astro

Updates an existing applet using the provided parameters


upload_attachment

tator.util.upload_attachment(api, media, path, name=None, chunk_size=104857600)

Upload a file as an attachment to a media object. Example:

api = tator.get_api(host, token)
for progress, response in tator.util.upload_attachment(api, media_id, path):
    print(f"Upload progress: {progress}%")
print(response.message)

upload_generic_file

tator.util.upload_generic_file(api, file_type, path, description, name=None, attributes=None, timeout=None)

Upload a file to the File storage location.

Example:

api = tator.get_api(host, token)
for progress, response in tator.util.upload_generic_file(api, project_id, path):
    print(f"Upload progress: {progress}%")
print(response)

upload_media

tator.util.upload_media(api, type_id, path, md5=None, section=None, fname=None, upload_gid=None, upload_uid=None, chunk_size=10485760, attributes=None, email_spec=None, media_id=None, timeout=30, max_workers=10)

Uploads a single media file.

Example:

api = tator.get_api(host, token)
for progress, response in tator.util.upload_media(api, type_id, path):
    print(f"Upload progress: {progress}%")
print(response.message)

upload_temporary_file

tator.util.upload_temporary_file(api, project, path, lookup=None, hours=24, name=None, chunk_size=104857600)

Upload a file to the temporary file storage location.

Example:

api = tator.get_api(host, token)
for progress, response in tator.util.upload_temporary_file(api, project_id, path):
    print(f"Upload progress: {progress}%")
print(response.message)