class documentation

Creates a view that groups the samples in a collection by a specified field or expression.

Examples:

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

dataset = foz.load_zoo_dataset("cifar10", split="test")

#
# Take 1000 samples at random and group them by ground truth label
#

stage = fo.GroupBy("ground_truth.label")
view = dataset.take(1000).add_stage(stage)

for group in view.iter_groups():
    print("%s: %d" % (group[0].ground_truth.label, len(group)))

#
# Variation of above operation that arranges the groups in decreasing
# order of size and immediately flattens them
#

from itertools import groupby

stage = fo.GroupBy(
    "ground_truth.label",
    flat=True,
    sort_expr=F().length(),
    reverse=True,
)
view = dataset.take(1000).add_stage(stage)

rle = lambda v: [(k, len(list(g))) for k, g in groupby(v)]
for label, count in rle(view.values("ground_truth.label")):
    print("%s: %d" % (label, count))
Parameters
field_or_exprthe field or embedded.field.name to group by, or a list of field names defining a compound group key, or a fiftyone.core.expressions.ViewExpression or MongoDB aggregation expression that defines the value to group by
order_byan optional field by which to order the samples in each group
reversewhether to return the results in descending order. Applies to both order_by and sort_expr
flatwhether to return a grouped collection (False) or a flattened collection (True)
match_expran optional fiftyone.core.expressions.ViewExpression or MongoDB aggregation expression that defines which groups to include in the output view. If provided, this expression will be evaluated on the list of samples in each group. Only applicable when flat=True
sort_expran optional fiftyone.core.expressions.ViewExpression or MongoDB aggregation expression that defines how to sort the groups in the output view. If provided, this expression will be evaluated on the list of samples in each group. Only applicable when flat=True
create_indexwhether to create an index, if necessary, to optimize the grouping. Only applicable when grouping by field(s), not expressions
Method __init__ Undocumented
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 to_mongo Returns the MongoDB aggregation pipeline for the stage.
Method validate Validates that the stage can be applied to the given collection.
Property create_index Whether to create an index, if necessary, to optimize the grouping.
Property field_or_expr The field or expression to group by.
Property flat Whether to generate a flattened collection.
Property match_expr An expression to apply to select groups in the output view.
Property order_by The field by which to order the samples in each group.
Property outputs_dynamic_groups Whether this stage outputs or flattens dynamic groups.
Property reverse Whether to sort the groups in descending order.
Property sort_expr An expression defining how the sort the groups in the output view.
Class Method _params Returns a list of JSON dicts describing the stage's supported parameters.
Method _get_group_expr Undocumented
Method _get_mongo_field_or_expr Undocumented
Method _get_mongo_match_expr Undocumented
Method _get_mongo_sort_expr Undocumented
Method _kwargs Returns a list of [name, value] lists describing the parameters of this stage instance.
Method _make_flat_pipeline Undocumented
Method _make_grouped_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 _create_index Undocumented
Instance Variable _field_or_expr Undocumented
Instance Variable _flat Undocumented
Instance Variable _match_expr Undocumented
Instance Variable _order_by Undocumented
Instance Variable _reverse Undocumented
Instance Variable _sort_expr Undocumented
Instance Variable _sort_stage 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_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.
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_or_expr, order_by=None, reverse=False, flat=False, match_expr=None, sort_expr=None, create_index=True): (source)

Undocumented

def get_group_expr(self, sample_collection): (source)

Returns the dynamic group expression for the given stage, if any.

Only usable if outputs_dynamic_groups is True.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
Returns

a tuple of

  • the group expression, or None if the stage does not generate dynamic groups
  • whether the group expression is an ObjectId field, or None
def get_media_type(self, sample_collection): (source)

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.

Parameters
sample_collectionthe fiftyone.core.collections.SampleCollection to which the stage is being applied
Returns
the media type, or None if the stage does not change the type
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
@property
create_index = (source)

Whether to create an index, if necessary, to optimize the grouping.

@property
field_or_expr = (source)

The field or expression to group by.

Whether to generate a flattened collection.

An expression to apply to select groups in the output view.

The field by which to order the samples in each group.

@property
outputs_dynamic_groups = (source)

Whether this stage outputs or flattens dynamic groups.

The possible return values are:

  • True: this stage dynamically groups the input collection
  • False: this stage flattens dynamic groups
  • None: this stage does not change group status

Whether to sort the groups in descending order.

An expression defining how the sort the groups in the output view.

@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_group_expr(self, sample_collection): (source)

Undocumented

def _get_mongo_field_or_expr(self): (source)

Undocumented

def _get_mongo_match_expr(self): (source)

Undocumented

def _get_mongo_sort_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 _make_flat_pipeline(self, sample_collection): (source)

Undocumented

def _make_grouped_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
_create_index = (source)

Undocumented

_field_or_expr = (source)

Undocumented

Undocumented

_match_expr = (source)

Undocumented

_order_by = (source)

Undocumented

_reverse = (source)

Undocumented

_sort_expr = (source)

Undocumented

_sort_stage = (source)

Undocumented