datajoint-python icon indicating copy to clipboard operation
datajoint-python copied to clipboard

FEAT: `dj.Top` orders the preview with `order_by`

Open CBroz1 opened this issue 5 months ago • 0 comments
trafficstars

Feature Request

Problem

I'm using dj.Top to monitor the top results of a given computation. Intuitively, running MyTable & dj.Top(order_by='field DESC', limit=n) should show the top n values of this field, in this order. Instead, I need to scan the result ordered by primary key, or fetch it.

Requirements

The script below outlines expected vs actual outcome.

Demo
import datajoint as dj

schema = dj.schema("cbroz_temp")


@schema
class TopDemo(dj.Lookup):
    definition = """
    id : int
    ---
    my_var: varchar(64)
    my_float: float
    """

    contents = [(1, "C", 2.0), (2, "D", 4.0), (3, "A", 1.0), (4, "B", 3.0)]


if __name__ == "__main__":
    print("TopDemo contents:\n", TopDemo())
    print(
        "Order by my_float ascending:\n",
        "Expected IDs 3, 4, 1. Got IDs: 1, 3, 4\n",
        TopDemo & dj.Top(order_by="my_float ASC", limit=3),
    )
    print(
        "Order by my_var descending:\n",
        "Expected IDs 2, 1, 4. Got IDs: 1, 2, 4\n",
        TopDemo & dj.Top(order_by="my_var DESC", limit=3),
    )

Justification

This would cut down on redundant use of order_by

Alternative Considerations

Currently, I pass the same order_by arg to a fetch with format='frame'

(MyTable & dj.Top(order_by="field DESC", limit=3)).fetch(order_by"field DESC", format=Frame)

Related Errors

n/a

Please include steps to reproduce provided errors as follows:

  • OS: Linux
  • Python Version: 3.9.6
  • MySQL Version: 8 latest
  • MySQL Deployment Strategy: local docker
  • DataJoint Version: 0.14.3
  • Minimum number of steps to reliably reproduce the issue: see above
  • Complete error stack as a result of evaluating the above steps: n/a

Screenshots

n/a

Additional Research and Context

preview is already running the alternative step of fetching as a frame

https://github.com/datajoint/datajoint-python/blob/5f37f8345348c76606e35b951700d43a6b39e47b/datajoint/preview.py#L6-L13

If query expressions were assigned an order_by attr by Top, QueryExpression.preview could pass this as a kwarg to preview.

CBroz1 avatar Jun 11 '25 17:06 CBroz1