Repository navigation
Sequence-based Results containers with replayable query params (proposal for #1298) - #1508
Conversation
- add Results, GranuleResults, CollectionResults (Sequence-based; items loaded once from a list or an iterator, then kept) - search_data()/search_datasets() record their kwargs and CMR hit count so a search can be repeated with search_data(**results.query_params) - download()/open() convert results to a list before calling Store, so Store behaviour is unchanged - widen to_geopandas() annotations to accept Sequence - update integration assertions that expected a plain list
Conflicts resolved in favour of the Sequence-based Results container: - earthaccess/results.py: replace the query-holding Results classes with Results/GranuleResults/CollectionResults; keep the _umm_record() hardening of DataCollection from this branch - earthaccess/search.py: drop the get()/get_all() overrides (get() returns a list again; search_data()/search_datasets() build the results); keep the DataCollectionsQuery docstring rename - earthaccess/api.py: take the containers' search/download/open changes - tests/unit/test_geo_interface.py: replace the Results(query=...) test with one using GranuleResults Co-Authored-By: Claude <noreply@anthropic.com>
|
I will automatically update this comment whenever this PR is modified
|
| these results. A deep copy is stored, so later changes to the | ||
| caller's objects do not change it. |
There was a problem hiding this comment.
So if the user now reruns results = earthaccess.search_data(params) with different inputs, results.query_params won't get updated? My initial thought it that's a counterintuitive behavior from the user perspective and query_params should be updated to the latest submitted parameters, so they reflect the actual results associated with that object (and not some arbitrary previous results). Otherwise it sort of defeats the purpose of having the query parameters attached to the results.
There was a problem hiding this comment.
Rerunning will still update it, because each call returns a new GranuleResults object.
This deep copy covers a different case though: a case where a list or dict is passed to the search and then those lists or dicts are changed afterwards. If that happens, results.query_params still shows what a search actually used. I'll reword the docstring to say so and we can add a new unit test for it, one that runs two searches and checks the params.
| self._items: list[T] | ||
| self._source: Iterator[T] | None |
There was a problem hiding this comment.
here (and throughout) - I'm concerned about AI's prolific use of type "T" instead of being more specific. In the few cases I've encountered in other work, a specific type is easy to plug in. Here I think that would be list[DataGranule] | list[DataCollection] or similar.
There was a problem hiding this comment.
Yeah, that's a good point about "T". How about Results[ResultItem: (DataGranule, DataCollection)], going to give that a try.
| f"params={self.query.params!r}, " | ||
| f"options={self.query.options!r})" | ||
| f"{type(self).__name__}({self._loaded_label()}, hits={self._hits!r}, " | ||
| f"query_params={self._query_params!r})" |
There was a problem hiding this comment.
For the __repr__ and other display items: I'd be keen to see some of the content/how it's displayed preserved. That was something that humans put a lot of effort into for determining readability, clarity, usability, and user-friendliness.
There was a problem hiding this comment.
Yeah, good point. I will change that back to the original layout.
And apparently, from a discussion with Opus 5.5, since the values are now stored when the search runs instead of read from the query object, we can have the display of results no longer call CMR.
| collections = ea.search_datasets( | ||
| keyword="above ground biomass", | ||
| ) | ||
| gdf = ea.DataCollectionsQuery.to_geopandas(collections) |
There was a problem hiding this comment.
flagging to test (I previously found errors in these examples) - I can't recall if we have pytest enabled on docstrings.
|
|
||
| granule_gdf = DataGranulesQuery.to_geopandas(granules) | ||
| collection_gdf = DataCollectionsQuery.to_geopandas(collections) | ||
| gdf = DataGranulesQuery.to_geopandas(GranuleResults(iter(granules))) |
There was a problem hiding this comment.
gdf = DataGranulesQuery.to_geopandas(GranuleResults(iter(granules)))
That's kinda ugly. I'd like to just be able to pass in granules or collections.
Also, this removes the collection_gdf test, but we still have both granule- and collection- to_geopandas functions.
There was a problem hiding this comment.
Okay, I will work on changing this back.
JessicaS11
left a comment
There was a problem hiding this comment.
See comments throughout (with the additional note that pending some of the other changes we should review the tests again). This neatly addresses the lazy generator and in-memory approaches though.
|
Unrelated: Don't we have an automatic pre-commit fix action e.g. @pre-commit fix? |
We do, but the check didn't pass even after I ran the fix action in the |
|
In general I think this goes in the right direction, we are still not lazy loading results which is fine and saves us a breaking change (although, more are coming). One feature we may consider for this PR is to have a wrapper on |
- constrain Results type parameter to DataGranule/DataCollection (ResultItem) - restore the original repr/str/HTML layout, using stored hits and a snapshot of the CMR query options instead of live CMR calls - clarify that each search returns results with its own query_params - restore Iterable to_geopandas signatures and fix the stale ea.DataGranules name in the granule example - restore the granule and collection to_geopandas results test
- delegate to the existing DataGranulesQuery/DataCollectionsQuery to_geopandas() static methods (earthaccess-dev#1461), so both paths return the same GeoDataFrame - add a test that to_gdf() matches to_geopandas() for both result types
|
@JessicaS11, @betolink, |
My Description
This changes the
Resultsclass so it can hold either a list or an iterator, and so that we have at least:download()andopen()to work as now, materializing all of the results.This PR does not add lazy loading (searches would still get every result before returning, as they do today).
AI-generated Description
A proposal for #1298 (which addresses #847). It answers the open question there about how
Resultsshould be iterable, and it fixes the CI failures on the current branch.What it does
Resultsis acollections.abc.Sequence.GranuleResultsandCollectionResultsare its typed subclasses, exported fromearthaccess. The type parameter is limited to the two result types,Results[ResultItem: (DataGranule, DataCollection)], so type checkers know thatsearch_data(...)[0]is aDataGranule.search_data()andsearch_datasets()store their own arguments ({"count": count, **kwargs}, deep-copied), soearthaccess.search_data(**results.query_params)repeats a search. Each search returns a new object with its ownquery_params. The CMR hit count the search already fetches is kept asresults.hits.download()andopen()work as before: both convert aGranuleResultsto a plain list with.all()before callingStore.store.pyis unchanged.search_data()andsearch_datasets()fetch all results (up tocount) and pass them in as a list, the same requests as today. Indexing, slicing,len(),bool()and repeated iteration work as on a list.Resultsalso accepts an iterator, takes each item from it only when first needed, and keeps it, so the results can be iterated again without searching again. Nothing in this PR passes an iterator; this is the hook for a lazy search later (the TODO inresults.py).__repr__,__str__and_repr_html_keep the layout from this branch (the same rows, labels and wording). The values are stored when the search runs, so displaying results makes no CMR request. "Query parameters" shows the arguments passed to the search, and "Query options" shows the CMR query options.DataGranulesQuery.to_geopandas()andDataCollectionsQuery.to_geopandas()accept any iterable, as on this branch, including results objects.GranuleResults.to_gdf()andCollectionResults.to_gdf()call those sameto_geopandas()methods from Add to_geopandas() to DataGranules and DataCollections #1461, soearthaccess.search_data(...).to_gdf()gives the same GeoDataFrame.Changes relative to
results-objectResultsclasses that hold the query object.get()/get_all()overrides on the query classes.get()returns a list again, and the results objects are built inapi.py._umm_record()hardening ofDataCollection, theDataCollectionsQuerydocstring rename, and theto_geopandassignatures from this branch.DataGranulesQuery.to_geopandas()docstring example, which used the old nameea.DataGranules, and addscount=100to it.list.CI failures on
results-objectthis addressesTypeError: type 'Results' is not subscriptableat import on Python 3.12, 3.13 and in the min-deps job.Resultsnot generic,get()overrides conflicting with python-cmr'slist[Any]stub, and__getitem__returning a union.DispatchError: _get: 0 methods foundfromdownload()/open()on Python 3.14.Decisions for reviewers
countis stored inquery_params, so a replay returns the same number of results. It could be left out if a replay should mean "same filters, all matches".count=100can show a much larger number.DataGranulesQuery(...).get()) get a list and no provenance.isinstance(results, list)is now false. That's the breaking change.__eq__, or__add__(combining searches).all(),query_params,GranuleResultsandCollectionResultsmatchSearchResultson thepluginsbranch, so moving to its streaming model later wouldn't rename the API. Note thatlen()means something different there: the number of items loaded so far.Testing
mypy: no issues.query_paramssnapshots, separatequery_paramsper search, display output without HTTP requests, pickling, replay,download/openpassingStorea list, andto_gdf()matchingto_geopandas().pre-commitpasses on all changed files.demo_old.ipynbhas been removed from this branch.to_geopandas()andto_gdf()docstring examples were run against CMR (171 collections, 100 granules).store.py:522for an unrelated reason, onmaintoo.)AI usage disclosure
Using AI tools is welcome. Per our AI Policy,
we just ask that you disclose any use of generative AI tools in this contribution.
Claude Code (Claude Opus 5.5) drafted the implementation, the tests, the merge-conflict resolution and the "AI-Generated Description" above at my direction, and ran the checks listed under Testing.
"Ready for review" checklist
Merge checklist
closes #1)CHANGELOG.mdupdatedREADME.mdupdated📚 Documentation preview 📚: https://earthaccess--1508.org.readthedocs.build/en/1508/