Skip to content

Commit

Permalink
Merge pull request voxel51#1586 from voxel51/histogram-values-fix
Browse files Browse the repository at this point in the history
Fixing frame aggregations
  • Loading branch information
brimoor authored Feb 6, 2022
2 parents 059861d + 61363f9 commit 21daa65
Showing 2 changed files with 56 additions and 10 deletions.
25 changes: 15 additions & 10 deletions fiftyone/core/aggregations.py
Original file line number Diff line number Diff line change
@@ -1704,6 +1704,7 @@ def _parse_field_and_expr(
if field_name is None:
field_name, expr = _extract_prefix_from_expr(expr)

root = "." not in field_name
found_expr = expr is not None

field_type = _get_field_type(
@@ -1754,9 +1755,12 @@ def _parse_field_and_expr(

if keep_top_level:
if is_frame_field:
path = "frames." + path
unwind_list_fields = ["frames." + f for f in unwind_list_fields]
other_list_fields = ["frames." + f for f in other_list_fields]
if not root:
prefix = "frames."
path = prefix + path
unwind_list_fields = [prefix + f for f in unwind_list_fields]
other_list_fields = [prefix + f for f in other_list_fields]

other_list_fields.insert(0, "frames")
elif unwind_list_fields:
first_field = unwind_list_fields.pop(0)
@@ -1765,13 +1769,14 @@ def _parse_field_and_expr(
pipeline.append({"$project": {path: True}})
elif auto_unwind:
if is_frame_field:
pipeline.extend(
[
{"$unwind": "$frames"},
{"$project": {"frames." + path: True}},
{"$replaceRoot": {"newRoot": "$frames"}},
]
)
pipeline.append({"$unwind": "$frames"})
if not root:
pipeline.extend(
[
{"$project": {"frames." + path: True}},
{"$replaceRoot": {"newRoot": "$frames"}},
]
)
else:
pipeline.append({"$project": {path: True}})
elif unwind_list_fields:
41 changes: 41 additions & 0 deletions tests/unittests/aggregation_tests.py
Original file line number Diff line number Diff line change
@@ -765,6 +765,47 @@ def test_batching(self):
results = dataset.values(fields)
self.assertEqual(len(fields), len(results))

@drop_datasets
def test_video_frames(self):
sample = fo.Sample(filepath="video.mp4")
sample.frames[1] = fo.Frame(
detections=fo.Detections(
detections=[
fo.Detection(label="cat"),
fo.Detection(label="dog"),
]
)
)
sample.frames[2] = fo.Frame()
sample.frames[3] = fo.Frame(
detections=fo.Detections(
detections=[
fo.Detection(label="rabbit"),
fo.Detection(label="squirrel"),
fo.Detection(label="fox"),
]
)
)

dataset = fo.Dataset()
dataset.add_sample(sample)

num_objs = F("frames").map(F("detections.detections").length())

values = dataset.values(num_objs)
self.assertListEqual(values, [[2, 0, 3]])

counts = dataset.count_values(num_objs)
self.assertDictEqual(counts, {2: 1, 3: 1, 0: 1})

max_objs = F("frames").map(F("detections.detections").length()).max()

values = dataset.values(max_objs)
self.assertListEqual(values, [3])

counts = dataset.count_values(max_objs)
self.assertDictEqual(counts, {3: 1})


if __name__ == "__main__":
fo.config.show_progress_bars = False

0 comments on commit 21daa65

Please sign in to comment.