class documentation

Selects only the specified labels from a collection.

The returned view will omit samples, sample fields, and individual labels that do not match the specified selection criteria.

You can perform a selection via one or more of the following methods:

  • Provide the labels argument, which should contain a list of dicts in the format returned by fiftyone.core.session.Session.selected_labels, to select specific labels
  • Provide the ids argument to select labels with specific IDs
  • Provide the tags argument to select labels with specific tags

If multiple criteria are specified, labels must match all of them in order to be selected.

By default, the selection is applied to all fiftyone.core.labels.Label fields, but you can provide the fields argument to explicitly define the field(s) in which to select.

Examples:

import fiftyone as fo
import fiftyone.zoo as foz

dataset = foz.load_zoo_dataset("quickstart")

#
# Only include the labels currently selected in the App
#

session = fo.launch_app(dataset)

# Select some labels in the App...

stage = fo.SelectLabels(labels=session.selected_labels)
view = dataset.add_stage(stage)

#
# Only include labels with the specified IDs
#

# Grab some label IDs
ids = [
    dataset.first().ground_truth.detections[0].id,
    dataset.last().predictions.detections[0].id,
]

stage = fo.SelectLabels(ids=ids)
view = dataset.add_stage(stage)

print(view.count("ground_truth.detections"))
print(view.count("predictions.detections"))

#
# Only include labels with the specified tags
#

# Grab some label IDs
ids = [
    dataset.first().ground_truth.detections[0].id,
    dataset.last().predictions.detections[0].id,
]

# Give the labels a "test" tag
dataset = dataset.clone()  # create copy since we're modifying data
dataset.select_labels(ids=ids).tag_labels("test")

print(dataset.count_values("ground_truth.detections.tags"))
print(dataset.count_values("predictions.detections.tags"))

# Retrieve the labels via their tag
stage = fo.SelectLabels(tags="test")
view = dataset.add_stage(stage)

print(view.count("ground_truth.detections"))
print(view.count("predictions.detections"))
Parameters
labelsa list of dicts specifying the labels to select in the format returned by fiftyone.core.session.Session.selected_labels
idsan ID or iterable of IDs of the labels to select
tagsa tag or iterable of tags of labels to select
fieldsa field or iterable of fields from which to select
omit_emptywhether to omit samples that have no labels after filtering
Method __init__ Undocumented
Method get_edited_fields Returns a list of names of fields or embedded fields that may have been edited by the stage, if any.
Method get_filtered_fields Returns a list of names of fields or embedded fields that contain arrays have been filtered by the stage, if any.
Method to_mongo Returns the MongoDB aggregation pipeline for the stage.
Method validate Validates that the stage can be applied to the given collection.
Property fields A list of fields from which labels are being selected.
Property ids A list of IDs of labels to select.
Property labels A list of dicts specifying the labels to select.
Property omit_empty Whether to omit samples that have no labels after filtering.
Property tags A list of tags of labels to select.
Class Method _params Returns a list of JSON dicts describing the stage's supported parameters.
Method _kwargs Returns a list of [name, value] lists describing the parameters of this stage instance.
Method _make_labels_pipeline Undocumented
Method _make_pipeline Undocumented
Method _needs_frames Whether the stage requires frame labels of video samples to be attached.
Method _needs_group_slices Whether the stage requires group slice(s) to be attached.
Instance Variable _fields Undocumented
Instance Variable _ids Undocumented
Instance Variable _labels Undocumented
Instance Variable _labels_map Undocumented
Instance Variable _omit_empty Undocumented
Instance Variable _pipeline Undocumented
Instance Variable _sample_ids Undocumented
Instance Variable _tags Undocumented

Inherited from ViewStage:

Method __eq__ Undocumented
Method __repr__ Undocumented
Method __str__ Undocumented
Method get_excluded_fields Returns a list of fields that have been excluded by the stage, if any.
Method get_group_expr Returns the dynamic group expression for the given stage, if any.
Method get_media_type Returns the media type outputted by this stage when applied to the given collection, if and only if it is different from the input type.
Method get_selected_fields Returns a list of fields that have been selected by the stage, if any.
Method load_view Loads the fiftyone.core.view.DatasetView containing the output of the stage.
Property has_view Whether this stage's output view should be loaded via load_view rather than appending stages to an aggregation pipeline via to_mongo.
Property outputs_dynamic_groups Whether this stage outputs or flattens dynamic groups.
Class Method _from_dict Creates a ViewStage instance from a serialized JSON dict representation of it.
Method _serialize Returns a JSON dict representation of the ViewStage.
Instance Variable _uuid Undocumented
def __init__(self, labels=None, ids=None, tags=None, fields=None, omit_empty=True): (source)

Undocumented

def get_edited_fields(self, sample_collection, frames=False): (source)

Returns a list of names of fields or embedded fields that may have been edited by the stage, if any.

The "frames." prefix should be omitted when frames is True.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
frames:Falsewhether to return sample-level (False) or frame-level (True) fields
Returns
a list of fields, or None if no fields have been edited
def get_filtered_fields(self, sample_collection, frames=False): (source)

Returns a list of names of fields or embedded fields that contain arrays have been filtered by the stage, if any.

For example, if a stage filters a fiftyone.core.labels.Detections field called "predictions", it should include "predictions.detections" in the returned list.

The "frames." prefix should be omitted when frames is True.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
frames:Falsewhether to return sample-level (False) or frame-level (True) fields
Returns
a list of fields, or None if no fields have been filtered
def to_mongo(self, _): (source)

Returns the MongoDB aggregation pipeline for the stage.

Only usable if has_view is False.

Parameters
_Undocumented
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
Returns
a MongoDB aggregation pipeline (list of dicts)
def validate(self, sample_collection): (source)

Validates that the stage can be applied to the given collection.

Parameters
sample_collectiona fiftyone.core.collections.SampleCollection
Raises
ViewStageErrorif the stage cannot be applied to the collection

A list of fields from which labels are being selected.

A list of IDs of labels to select.

A list of dicts specifying the labels to select.

Whether to omit samples that have no labels after filtering.

A list of tags of labels to select.

@classmethod
def _params(cls): (source)

Returns a list of JSON dicts describing the stage's supported parameters.

Returns
a list of JSON dicts
def _kwargs(self): (source)

Returns a list of [name, value] lists describing the parameters of this stage instance.

Returns
a list of [name, value] lists
def _make_labels_pipeline(self, sample_collection): (source)

Undocumented

def _make_pipeline(self, sample_collection): (source)

Undocumented

def _needs_frames(self, sample_collection): (source)

Whether the stage requires frame labels of video samples to be attached.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
Returns
True/False
def _needs_group_slices(self, sample_collection): (source)

Whether the stage requires group slice(s) to be attached.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
Returns
None, or a list of group slices

Undocumented

Undocumented

Undocumented

_labels_map = (source)

Undocumented

_omit_empty = (source)

Undocumented

_pipeline = (source)

Undocumented

_sample_ids = (source)

Undocumented

Undocumented