Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 1 addition & 97 deletions fiftyone/core/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numpy as np

import eta.core.utils as etau
import pymongo

from fiftyone.core.odm.document import MongoEngineBaseDocument
import fiftyone.core.utils as fou
Expand Down Expand Up @@ -2662,95 +2663,6 @@ def reverse(self):
"""
return ViewExpression({"$reverseArray": self})

def sort(self, key=None, numeric=False, reverse=False):
"""Sorts this expression, which must resolve to an array.

If no ``key`` is provided, this array must contain elements whose
BSON representation can be sorted by JavaScript's ``.sort()`` method.

If a ``key`` is provided, the array must contain documents, which are
sorted by ``key``, which must be a field or embedded field.

Examples::

#
# Sort the tags of each sample in a dataset
#

import fiftyone as fo
from fiftyone import ViewField as F

dataset = fo.Dataset()
dataset.add_samples(
[
fo.Sample(filepath="im1.jpg", tags=["z", "f", "p", "a"]),
fo.Sample(filepath="im2.jpg", tags=["y", "q", "h", "d"]),
fo.Sample(filepath="im3.jpg", tags=["w", "c", "v", "l"]),
]
)

# Sort the `tags` of each sample
view = dataset.set_field("tags", F("tags").sort())

print(view.first().tags)

#
# Sort the predictions in each sample of a dataset by `confidence`
#

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

dataset = foz.load_zoo_dataset("quickstart")

view = dataset.set_field(
"predictions.detections",
F("detections").sort(key="confidence", numeric=True, reverse=True)
)

sample = view.first()
print(sample.predictions.detections[0].confidence)
print(sample.predictions.detections[-1].confidence)

Args:
key (None): an optional field or ``embedded.field.name`` to sort by
numeric (False): whether the array contains numeric values. By
default, the values will be sorted alphabetically by their
string representations
reverse (False): whether to sort in descending order

Returns:
a :class:`ViewExpression`
"""
if key is not None:
if numeric:
comp = "(a, b) => a.{key} - b.{key}"
else:
comp = "(a, b) => ('' + a.{key}).localeCompare(b.{key})"

comp = comp.format(key=key)
elif numeric:
comp = "(a, b) => a - b"
else:
comp = ""

if reverse:
rev = ".reverse()"
else:
rev = ""

sort_fcn = """
function(array) {{
array.sort({comp}){rev};
return array;
}}
""".format(
comp=comp, rev=rev
)

return self._function(sort_fcn)

def filter(self, expr):
"""Applies the given filter to the elements of this expression, which
must resolve to an array.
Expand Down Expand Up @@ -4575,14 +4487,6 @@ def zip(*args, use_longest=False, defaults=None):

return ViewExpression({"$zip": zip_expr})

# Experimental expressions ###############################################

def _function(self, function):
function = " ".join(function.split())
return ViewExpression(
{"$function": {"body": function, "args": [self], "lang": "js"}}
)


class ViewField(ViewExpression):
"""A :class:`ViewExpression` that refers to a field or embedded field of a
Expand Down
45 changes: 32 additions & 13 deletions fiftyone/server/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,19 +947,38 @@ def _add_label_tags(path, field, view):


def _count_list_items(path, view):
function = (
"function(items) {"
"let counts = {};"
"items && items.forEach((i) => {"
"counts[i] = 1 + (counts[i] || 0);"
"});"
"return counts;"
"}"
)

return view.set_field(
path, F(path)._function(function), _allow_missing=True
)
expr = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer to @benjaminpkane to evaluate the suitability of this new implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's not working in mongo6. Solution is getting really ugly ...

Copy link
Contributor

@benjaminpkane benjaminpkane Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think changing this to existence instead of counts could work. These counts are for the tag:count grid bubble values but showing the distinct tags is sufficient imo if we are technically blocked (also a performance improvement for the grid, less computation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a working version now I think. Check it out and lmk.
Of course if you want to change the grid behavior that would work too but that's up to you + your squad

"$reduce": {
"input": F(path), # The field to be processed
"initialValue": {},
"in": {
"$mergeObjects": [
"$$value",
{
"$arrayToObject": [
[
{
"k": "$$this",
"v": {
"$add": [
{
"$ifNull": [
{"$getField": "$$this"},
0,
]
},
1,
]
},
}
]
]
},
]
},
}
}
return view.set_field(path, expr, _allow_missing=True)


def _match_label_tags(view: foc.SampleCollection, label_tags):
Expand Down
Loading