monailabel.datastore.local module

class monailabel.datastore.local.DataModel(**data)[source]

Bases: pydantic.main.BaseModel

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

ext: str
info: Dict[str, Any]
model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'ext': FieldInfo(annotation=str, required=False, default=''), 'info': FieldInfo(annotation=Dict[str, Any], required=False, default={})}

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

class monailabel.datastore.local.ImageLabelModel(**data)[source]

Bases: pydantic.main.BaseModel

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

image: monailabel.datastore.local.DataModel
labels: Dict[str, monailabel.datastore.local.DataModel]
model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'image': FieldInfo(annotation=DataModel, required=True), 'labels': FieldInfo(annotation=Dict[str, monailabel.datastore.local.DataModel], required=False, default={})}

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

tags()[source]
class monailabel.datastore.local.LocalDatastore(datastore_path, images_dir='.', labels_dir='labels', datastore_config='datastore_v2.json', extensions=('*.nii.gz', '*.nii'), auto_reload=False, read_only=False)[source]

Bases: monailabel.interfaces.datastore.Datastore

Class to represent a datastore local to the MONAI-Label Server

`name

The name of the datastore

Type

str`

`description

The description of the datastore

Type

str`

Creates a LocalDataset object

Parameters:

datastore_path: str

a string to the directory tree of the desired dataset

datastore_config: str

optional file name of the dataset configuration file (by default dataset.json)

__init__(datastore_path, images_dir='.', labels_dir='labels', datastore_config='datastore_v2.json', extensions=('*.nii.gz', '*.nii'), auto_reload=False, read_only=False)[source]

Creates a LocalDataset object

Parameters:

datastore_path: str

a string to the directory tree of the desired dataset

datastore_config: str

optional file name of the dataset configuration file (by default dataset.json)

add_image(image_id, image_filename, image_info)[source]

Save a image for the given image id and return the newly saved image’s id

Parameters
  • image_id (str) – the image id for the image; If None then base filename will be used

  • image_filename (str) – the path to the image file

  • image_info (Dict[str, Any]) – additional info for the image

Return type

str

Returns

the image id for the saved image filename

datalist(full_path=True)[source]

Return a dictionary of image and label pairs corresponding to the ‘image’ and ‘label’ keys respectively

Return type

List[Dict[str, Any]]

Returns

the {‘label’: image, ‘label’: label} pairs for training

description()[source]

Gets the description field for the dataset

Return description

str

Return type

str

get_dataset_archive(limit_cases)[source]

Retrieve ZIP archive of the full dataset containing images, labels and metadata

Parameters

limit_cases (Optional[int]) – limit the included cases to this number

Return type

str

Returns

path to ZIP archive of the full dataset

get_image(image_id, params=None)[source]

Retrieve image object based on image id

Parameters
  • image_id (str) – the desired image’s id

  • params – any optional params

Return type

Any

Returns

return the “image”

get_image_info(image_id)[source]

Get the image information for the given image id

Parameters

image_id (str) – the desired image id

Return type

Dict[str, Any]

Returns

image info as a list of dictionaries Dict[str, Any]

get_image_uri(image_id)[source]

Retrieve image uri based on image id

Parameters

image_id (str) – the desired image’s id

Return type

str

Returns

return the image uri

get_label(label_id, label_tag, params=None)[source]

Retrieve image object based on label id

Parameters
  • label_id (str) – the desired label’s id

  • label_tag (str) – the matching label’s tag

  • params – any optional params

Return type

Any

Returns

return the “label”

get_label_by_image_id(image_id, tag)[source]

Retrieve label id for the given image id and tag

Parameters
  • image_id (str) – the desired image’s id

  • tag (str) – matching tag name

Return type

str

Returns

label id

get_label_info(label_id, label_tag)[source]

Get the label information for the given label id

Parameters
  • label_id (str) – the desired label id

  • label_tag (str) – the matching label tag

Return type

Dict[str, Any]

Returns

label info as a list of dictionaries Dict[str, Any]

get_label_uri(label_id, label_tag)[source]

Retrieve label uri based on image id

Parameters
  • label_id (str) – the desired label’s id

  • label_tag (str) – the matching label’s tag

Return type

str

Returns

return the label uri

get_labeled_images(label_tag=None, labels=None)[source]

Get all images that have a corresponding label

Return type

List[str]

Returns

list of image ids List[str]

get_labels_by_image_id(image_id)[source]

Retrieve all label ids for the given image id

Parameters

image_id (str) – the desired image’s id

Return type

Dict[str, str]

Returns

label ids mapped to the appropriate LabelTag as Dict[LabelTag, str]

get_unlabeled_images(label_tag=None, labels=None)[source]

Get all images that have no corresponding label

Return type

List[str]

Returns

list of image ids List[str]

json()[source]

Return json representation of datastore

list_images()[source]

Return list of image ids available in the datastore

Return type

List[str]

Returns

list of image ids List[str]

name()[source]

Dataset name (if one is assigned)

Returns

Dataset name as string

Return type

name (str)

refresh()[source]

Refresh the datastore based on the state of the files on disk

remove_image(image_id)[source]

Remove image for the datastore. This will also remove all associated labels.

Parameters

image_id (str) – the image id for the image to be removed from datastore

Return type

None

remove_label(label_id, label_tag)[source]

Remove label from the datastore

Parameters
  • label_id (str) – the label id for the label to be removed from datastore

  • label_tag (str) – the label tag for the label to be removed from datastore

Return type

None

save_label(image_id, label_filename, label_tag, label_info)[source]

Save a label for the given image id and return the newly saved label’s id

Parameters
  • image_id (str) – the image id for the label

  • label_filename (str) – the path to the label file

  • label_tag (str) – the tag for the label

  • label_info (Dict[str, Any]) – additional info for the label

Return type

str

Returns

the label id for the given label filename

set_description(description)[source]

Set a description for the dataset

Parameters

description (str) – str

set_name(name)[source]

Sets the dataset name in a standardized format (lowercase, no spaces).

Parameters:

name (str): Desired dataset name

status()[source]

Return current statistics of datastore

Return type

Dict[str, Any]

update_image_info(image_id, info)[source]

Update (or create a new) info tag for the desired image

Parameters
  • image_id (str) – the id of the image we want to add/update info

  • info (Dict[str, Any]) – a dictionary of custom image information Dict[str, Any]

Return type

None

update_label_info(label_id, label_tag, info)[source]

Update (or create a new) info tag for the desired label

Parameters
  • label_id (str) – the id of the label we want to add/update info

  • label_tag (str) – the matching label tag

  • info (Dict[str, Any]) – a dictionary of custom label information Dict[str, Any]

Return type

None

class monailabel.datastore.local.LocalDatastoreModel(**data)[source]

Bases: pydantic.main.BaseModel

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

base_path: str
description: str
filter_by_tag(tag)[source]
image_path()[source]
images_dir: str
label(id, tag)[source]
label_path(tag)[source]
labels_dir: str
labels_path()[source]
model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'base_path': FieldInfo(annotation=str, required=False, default=''), 'description': FieldInfo(annotation=str, required=True), 'images_dir': FieldInfo(annotation=str, required=False, default=''), 'labels_dir': FieldInfo(annotation=str, required=False, default='labels'), 'name': FieldInfo(annotation=str, required=True), 'objects': FieldInfo(annotation=Dict[str, monailabel.datastore.local.ImageLabelModel], required=False, default={})}

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

name: str
objects: Dict[str, monailabel.datastore.local.ImageLabelModel]
tags()[source]