Overview
This module offers a way of interacting with Labels projects from python.
The central class in this module is the Project
class. It allow you to either create a new local project or load an existing local project:
import imfusion
from imfusion import labels
new_project = labels.Project('New Project', 'path/to/new/project/folder')
existing_project = labels.Project.load('path/to/existing/project')
remote_project = labels.Project.load('http://example.com', '1', 'username', 'password123')
From there you can add new tag definitions, annotation definitions and data to the project:
project.add_tag('NewTag', labels.TagKind.Bool)
project.add_labelmap_layer('NewLabelmap')
project.add_descriptor(imfusion.io.open('/path/to/image')[0])
The Project
instance is also the central way to access this kind of data:
new_tag = project.tags['NewTag'] # can also be indexed with an integer, i.e. tags[0]
new_labelmap = project.labelmap_layers['NewLabelmap'] # can also be indexed with an interger, i.e. labelmap_layers[0]
new_descriptor = project.descriptors[0]
The DataDescriptor
class represents an entry in the project’s database and can be used to access the the entry’s metadata, tags and annotations.
The interface for accessing tags and annotations is the same as in Project
but also offers the additional value
attribute to get the value of the tag / annotation:
name = descriptor.name
shape = (descriptor.n_images, descriptor.n_channels, descriptor.n_slices, descriptor.height, descriptor.width)
new_tag = descriptor.tags['NewTag']
tag_value = descriptor.tags['NewTag'].value
labelmap = descriptor.labelmap_layers['NewLabelmap'].load()
roi = descriptor.roi
image = descriptor.load_image(crop_to_roi=True)
Note
Keep in mind that all modifications made to a local project are stored in memory and will only be saved to disk if you call Project.save()
.
Modifications to remote projects are applied immediately.
Alternatively, you can also use the Project
as a context manager:
with Project('SomeName', /some/path) as project:
... # will automatically save the project when exiting the context if there was no exception
Warning
Changing annotation data is the only exception to this rule. It is written immediately to disk (see :meth:LabelMapLayer.save_new_data`, :meth:LandmarkLayer.save_new_data`, :meth:BoundingBoxLayer.save_new_data`)