class documentation

Selects the samples from a collection that contain (or do not contain) at least one label that matches the specified criteria.

Note that, unlike SelectLabels and FilterLabels, this stage will not filter the labels themselves; it only selects the corresponding samples.

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 match specific labels
  • Provide the ids argument to match labels with specific IDs
  • Provide the tags argument to match labels with specific tags
  • Provide the filter argument to match labels based on a boolean fiftyone.core.expressions.ViewExpression that is applied to each individual fiftyone.core.labels.Label element
  • Pass bool=False to negate the operation and instead match samples that do not contain at least one label matching the specified criteria

If multiple criteria are specified, labels must match all of them in order to trigger a sample match.

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 search.

Examples:

import fiftyone as fo
import fiftyone.zoo as foz
from fiftyone import ViewField as F

dataset = foz.load_zoo_dataset("quickstart")

#
# Only show samples whose labels are currently selected in the App
#

session = fo.launch_app(dataset)

# Select some labels in the App...

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

#
# Only include samples that contain 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.MatchLabels(ids=ids)
view = dataset.add_stage(stage)

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

#
# Only include samples that contain 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.MatchLabels(tags="test")
view = dataset.add_stage(stage)

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

#
# Only include samples that contain labels matching a filter
#

filter = F("confidence") > 0.99
stage = fo.MatchLabels(filter=filter, fields="predictions")
view = dataset.add_stage(stage)

print(len(view))
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
filtera fiftyone.core.expressions.ViewExpression or MongoDB aggregation expression that returns a boolean describing whether to select a given label. In the case of list fields like fiftyone.core.labels.Detections, the filter is applied to the list elements, not the root field
fieldsa field or iterable of fields from which to select
boolwhether to match samples that have (None or True) or do not have (False) at least one label that matches the specified criteria
Method __init__ Undocumented
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 bool Whether to match samples that have (None or True) or do not have (False) at least one label that matches the specified criteria.
Property fields A list of fields from which labels are being matched.
Property filter A filter expression that defines the labels to match.
Property ids A list of IDs of labels to match.
Property labels A list of dicts specifying the labels to match.
Property tags A list of tags of labels to match.
Class Method _params Returns a list of JSON dicts describing the stage's supported parameters.
Method _get_mongo_filter Undocumented
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.
Constant _FILTER_PREFIX Undocumented
Instance Variable _bool Undocumented
Instance Variable _fields Undocumented
Instance Variable _filter Undocumented
Instance Variable _ids Undocumented
Instance Variable _labels Undocumented
Instance Variable _labels_map 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_edited_fields Returns a list of names of fields or embedded fields that may have been edited by the stage, if any.
Method get_excluded_fields Returns a list of fields that have been excluded 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 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, filter=None, fields=None, bool=None): (source)

Undocumented

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

Whether to match samples that have (None or True) or do not have (False) at least one label that matches the specified criteria.

A list of fields from which labels are being matched.

A filter expression that defines the labels to match.

A list of IDs of labels to match.

A list of dicts specifying the labels to match.

A list of tags of labels to match.

@classmethod
def _params(cls): (source)

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

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

Undocumented

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
_FILTER_PREFIX: str = (source)

Undocumented

Value
'$$FIELD'

Undocumented

Undocumented

Undocumented

Undocumented

Undocumented

_labels_map = (source)

Undocumented

_pipeline = (source)

Undocumented

_sample_ids = (source)

Undocumented

Undocumented