class documentation

Sets a field or embedded field on each sample in a collection by evaluating the given expression.

This method can process embedded list fields. To do so, simply append [] to any list component(s) of the field path.

Note

There are two cases where FiftyOne will automatically unwind array fields without requiring you to explicitly specify this via the [] syntax:

Top-level lists: when you specify a field path that refers to a top-level list field of a dataset; i.e., list_field is automatically coerced to list_field[], if necessary.

List fields: When you specify a field path that refers to the list field of a |Label| class, such as the Detections.detections attribute; i.e., ground_truth.detections.label is automatically coerced to ground_truth.detections[].label, if necessary.

See the examples below for demonstrations of this behavior.

The provided expr is interpreted relative to the document on which the embedded field is being set. For example, if you are setting a nested field field="embedded.document.field", then the expression expr you provide will be applied to the embedded.document document. Note that you can override this behavior by defining an expression that is bound to the root document by prepending "$" to any field name(s) in the expression.

See the examples below for more information.

Note

Note that you cannot set a non-existing top-level field using this stage, since doing so would violate the dataset's schema. You can, however, first declare a new field via fiftyone.core.dataset.Dataset.add_sample_field and then populate it in a view via this stage.

Examples:

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

dataset = foz.load_zoo_dataset("quickstart")

#
# Replace all values of uniqueness that are less than 0.5 with `None`
#

stage = fo.SetField(
    "uniqueness",
    (F("uniqueness") >= 0.5).if_else(F("uniqueness"), None)
)
view = dataset.add_stage(stage)
print(view.bounds("uniqueness"))

#
# Lower bound all object confidences in the `predictions` field by 0.5
#

stage = fo.SetField(
    "predictions.detections.confidence", F("confidence").max(0.5)
)
view = dataset.add_stage(stage)
print(view.bounds("predictions.detections.confidence"))

#
# Add a `num_predictions` property to the `predictions` field that
# contains the number of objects in the field
#

stage = fo.SetField(
    "predictions.num_predictions",
    F("$predictions.detections").length(),
)
view = dataset.add_stage(stage)
print(view.bounds("predictions.num_predictions"))

#
# Set an `is_animal` field on each object in the `predictions` field
# that indicates whether the object is an animal
#

ANIMALS = [
    "bear", "bird", "cat", "cow", "dog", "elephant", "giraffe",
    "horse", "sheep", "zebra"
]

stage = fo.SetField(
    "predictions.detections.is_animal", F("label").is_in(ANIMALS)
)
view = dataset.add_stage(stage)
print(view.count_values("predictions.detections.is_animal"))
Parameters
fieldthe field or embedded.field.name to set
expra fiftyone.core.expressions.ViewExpression or MongoDB aggregation expression that defines the field value to set
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 to_mongo Returns the MongoDB aggregation pipeline for the stage.
Method validate Validates that the stage can be applied to the given collection.
Property expr The expression to apply.
Property field The field to set.
Class Method _params Returns a list of JSON dicts describing the stage's supported parameters.
Method _get_mongo_expr Undocumented
Method _kwargs Returns a list of [name, value] lists describing the parameters of this stage instance.
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 _allow_missing Undocumented
Instance Variable _expr Undocumented
Instance Variable _expr_dict Undocumented
Instance Variable _field Undocumented
Instance Variable _pipeline 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_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, field, expr, _allow_missing=False): (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 to_mongo(self, sample_collection): (source)

Returns the MongoDB aggregation pipeline for the stage.

Only usable if has_view is False.

Parameters
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

The expression to apply.

The field to set.

@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_expr(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 _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
_allow_missing = (source)

Undocumented

Undocumented

_expr_dict = (source)

Undocumented

Undocumented

_pipeline = (source)

Undocumented