Getting Started with the API Client
First, import the Api class and create an instance:
from datamint import Api
api = Api() # Uses API key from environment or config
The Api class provides access to different endpoint handlers:
api.resources- For uploading, downloading, and managing resourcesapi.annotations- For creating and managing annotations/segmentationsapi.projects- For creating and managing projectsapi.channels- For organizing resources into channelsapi.users- For user management operationsapi.models- For managing registered modelsapi.annotationsets- For working with annotation set configurationsapi.deploy- For deploying models to the Datamint platformapi.inference- For running and managing inference jobs
Most day-to-day workflows can stay object-based. Endpoint handlers return
entity objects such as Resource,
Project, and
Annotation, and those entities expose
convenience methods for you to use.
Working with Resources
Upload resource files
Use api.resources.upload_resource() to upload any resource type, such as DICOMs, videos, and image files:
# Upload a single file
api.resources.upload_resource("/path/to/dicom.dcm")
# Upload multiple files at once
api.resources.upload_resources([
"/path/to/dicom.dcm",
"/path/to/video.mp4",
])
List and filter resources
You can see the list of all uploaded resources by calling api.resources.get_list():
# Get resources with different filters
inbox_resources = api.resources.get_list(status="inbox")
dicom_resources = api.resources.get_list(mimetype="application/dicom")
ct_resources = api.resources.get_list(channel="CT scans")
for resource in ct_resources:
print(resource.filename, resource.status)
Upload with options
You can customize the upload with various parameters:
# Upload with channel organization
api.resources.upload_resource(
"/path/to/dicom.dcm",
channel="CT scans",
)
# Upload with anonymization and labels
api.resources.upload_resource(
"/path/to/dicom.dcm",
anonymize=True,
tags=["baseline", "ct"],
)
# Upload and publish directly to a project
project = api.projects.get_by_name("Liver Review")
api.resources.upload_resource(
"/path/to/dicom.dcm",
publish_to=project,
)
Download resources
To download a resource, use api.resources.download_resource_file():
# Get a resource
resources = api.resources.get_list(status="inbox", mimetype="application/dicom")
resource = resources[0]
# Download as bytes through the entity helper
bytes_obj = resource.fetch_file_data(auto_convert=False)
# Auto-convert to the appropriate object (for example pydicom.Dataset)
dicom_obj = resource.fetch_file_data(auto_convert=True)
# Save directly to file
resource.fetch_file_data(save_path="path/to/dicomfile.dcm")
With auto_convert=True, the function uses the resource mimetype to automatically convert to the appropriate object type (pydicom.Dataset for DICOM, etc.).
Publishing resources
To publish a resource, use api.resources.publish_resources():
resources = api.resources.get_list(status="inbox")
resource = resources[0] # assuming there is at least one resource in the inbox
# Change status from 'inbox' to 'published'
api.resources.publish_resources(resource)
# Add the published resource to a project
project = api.projects.get_by_name("Liver Review")
api.projects.add_resources(resource, project)
If you want the resource to land directly in a project, prefer
upload_resource(..., publish_to=project) during upload.
Working with Annotations
Inspect annotations from a resource
Every Resource can fetch its own annotations:
resource = api.resources.get_list(project_name="Liver Review")[0]
annotations = resource.fetch_annotations()
for annotation in annotations:
print(annotation.name, annotation.annotation_type)
Upload segmentations
To upload a segmentation, use api.annotations.upload_segmentations():
resource = api.resources.get_list(filename="dicom.dcm")[0]
# Upload segmentation
api.annotations.upload_segmentations(
resource,
"path/to/segmentation.png",
name="SegmentationName",
)
Multi-class segmentations
If your segmentation has multiple classes, you can pass a dictionary mapping pixel values to class names:
class_names = {
# Background (0) is automatic, don't specify it
1: "tumor",
2: "vessel",
}
api.annotations.upload_segmentations(
resource,
"path/to/segmentation.png",
name=class_names,
)
Volume segmentations
Use api.annotations.upload_volume_segmentation() for NIfTI masks and other 3D segmentations:
volume_resource = api.resources.get_list(filename="volume.nii.gz")[0]
api.annotations.upload_volume_segmentation(
volume_resource,
"path/to/segmentation.nii.gz",
{1: "liver", 2: "tumor"},
)
Inspect annotation entities
Annotation entities can fetch their own files and lazily resolve the source resource:
resource = api.resources.get_list(project_name="Liver Review")[0]
annotation = resource.fetch_annotations(annotation_type="segmentation")[0]
mask = annotation.fetch_file_data(use_cache=True)
source_resource = annotation.resource
print(annotation.name, source_resource.filename)
Working with Projects
Create and manage projects
# Create a new project
project = api.projects.create(
name="My Project",
description="Project description",
)
# Add existing resources to it
resources = api.resources.get_list(channel="CT scans")
api.projects.add_resources(resources, project)
# Work with project resources through the entity
for resource in project.fetch_resources():
print(resource.filename)
Project helper methods
The Project entity provides shortcuts for common project workflows:
project = api.projects.get_by_name("My Project")
# Cache all resource files locally for faster follow-up access
project.cache_resources()
resource = project.fetch_resources()[0]
project.set_work_status(resource, "annotated")
specs = project.get_annotations_specs()
print([spec.identifier for spec in specs])
Working with Channels
Organize resources with channels
# List all channels
channels = api.channels.get_list()
# Create a new channel
api.channels.create(name="CT Scans", description="CT scan images")
See also the tutorial notebooks: upload_data.ipynb
Working with Models & Deployment
Register and deploy models via api.models and api.deploy:
# List all registered models
models = api.models.get_list()
# Deploy a registered model
deploy_job = api.deploy.deploy(model_name="my-model", version="1.0")
print(deploy_job.status)
Running inference
Use api.inference to trigger inference jobs against deployed models:
resource = api.resources.get_list(project_name="Liver Review")[0]
job = api.inference.run(model_name="my-model", resource=resource)
print(job.status)
See the tutorial notebooks: deploy_model_demo.ipynb and external_model_deployment_tutorial.ipynb