class SetField(ViewStage): (source)
Constructor: SetField(field, expr, _allow_missing)
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 | |
field | the field or embedded.field.name to set |
expr | a fiftyone.core.expressions.ViewExpression or
MongoDB aggregation expression
that defines the field value to set |
Method | __init__ |
Undocumented |
Method | get |
Returns a list of names of fields or embedded fields that may have been edited by the stage, if any. |
Method | to |
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 |
Undocumented |
Method | _kwargs |
Returns a list of [name, value] lists describing the parameters of this stage instance. |
Method | _needs |
Whether the stage requires frame labels of video samples to be attached. |
Method | _needs |
Whether the stage requires group slice(s) to be attached. |
Instance Variable | _allow |
Undocumented |
Instance Variable | _expr |
Undocumented |
Instance Variable | _expr |
Undocumented |
Instance Variable | _field |
Undocumented |
Instance Variable | _pipeline |
Undocumented |
Inherited from ViewStage
:
Method | __eq__ |
Undocumented |
Method | __repr__ |
Undocumented |
Method | __str__ |
Undocumented |
Method | get |
Returns a list of fields that have been excluded by the stage, if any. |
Method | get |
Returns a list of names of fields or embedded fields that contain arrays have been filtered by the stage, if any. |
Method | get |
Returns the dynamic group expression for the given stage, if any. |
Method | get |
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 |
Returns a list of fields that have been selected by the stage, if any. |
Method | load |
Loads the fiftyone.core.view.DatasetView containing the output of the stage. |
Property | has |
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 |
Whether this stage outputs or flattens dynamic groups. |
Class Method | _from |
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 |
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 | the
fiftyone.core.collections.SampleCollection to which
the stage is being applied |
frames:False | whether to return sample-level (False) or frame-level (True) fields |
Returns | |
a list of fields, or None if no fields have been edited |
fiftyone.core.stages.ViewStage.to_mongo
Returns the MongoDB aggregation pipeline for the stage.
Only usable if has_view
is False.
Parameters | |
sample | the
fiftyone.core.collections.SampleCollection to which
the stage is being applied |
Returns | |
a MongoDB aggregation pipeline (list of dicts) |
fiftyone.core.stages.ViewStage.validate
Validates that the stage can be applied to the given collection.
Parameters | |
sample | a
fiftyone.core.collections.SampleCollection |
Raises | |
ViewStageError | if the stage cannot be applied to the collection |
fiftyone.core.stages.ViewStage._params
Returns a list of JSON dicts describing the stage's supported parameters.
Returns | |
a list of JSON dicts |
fiftyone.core.stages.ViewStage._kwargs
Returns a list of [name, value] lists describing the parameters of this stage instance.
Returns | |
a list of [name, value] lists |
Whether the stage requires frame labels of video samples to be attached.
Parameters | |
sample | the
fiftyone.core.collections.SampleCollection to which
the stage is being applied |
Returns | |
True/False |
Whether the stage requires group slice(s) to be attached.
Parameters | |
sample | the
fiftyone.core.collections.SampleCollection to which
the stage is being applied |
Returns | |
None, or a list of group slices |