Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworking xarray interop #4

Merged
merged 10 commits into from
Jan 18, 2022
Merged

Reworking xarray interop #4

merged 10 commits into from
Jan 18, 2022

Conversation

Kirill888
Copy link
Member

@Kirill888 Kirill888 commented Jan 18, 2022

TODO:

add .odc. extension to Dataset as well.

Issue was raised on datacube repo that `.to_epsg` method on pyproj CRS
without epsg code can be slow. The solution is to use `.to_wkt` instead of
`.to_epsg` as a primary key for looking up CRS state from the cache.

But there were other issues, with previous approach, like throwing away an
already constructed pyproj.CRS when it is given on the input.

_make_crs was refactored to accept either string or pyrpoj.CRS object, with
custom key being `.to_wkt()` when given pyproj.CRS.

Also adding key normalization for `epsg:<int>` style CRS strings.
Also convert GeoBox.from_geopolygon to staticmethod
This adds `.odc.` accessor to `xarray.DataArray`, from
which crs,transform,geobox can be accessed.
@Kirill888 Kirill888 requested a review from gadomski January 18, 2022 10:23
@codecov
Copy link

codecov bot commented Jan 18, 2022

Codecov Report

Merging #4 (b558991) into develop (a97c7f8) will increase coverage by 7.27%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop       #4      +/-   ##
===========================================
+ Coverage    89.72%   97.00%   +7.27%     
===========================================
  Files           12       12              
  Lines         1480     1568      +88     
===========================================
+ Hits          1328     1521     +193     
+ Misses         152       47     -105     
Impacted Files Coverage Δ
odc/geo/_geom.py 100.00% <ø> (ø)
odc/geo/_crs.py 100.00% <100.00%> (ø)
odc/geo/_xr_interop.py 100.00% <100.00%> (+70.34%) ⬆️
odc/geo/geobox.py 100.00% <100.00%> (ø)
odc/geo/testutils.py 100.00% <100.00%> (ø)
odc/geo/gridspec.py 100.00% <0.00%> (+4.16%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a97c7f8...b558991. Read the comment docs.

return crs_spec.to_wkt()


@cachetools.cached(_crs_cache, key=_make_crs_key)

Choose a reason for hiding this comment

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

Would it make sense to add a lock in case of multi-threaded access? Globals w/o a lock feel weird to me but mabye that's just me.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure how cachetools deals with concurrency actually. Dictionaries are "thread-safe" in Python, and so are imports, so wrapping cache around a function should be thread-safe as it happens at import time, and then I assume cachetools does reasonable thing afterwards, which at worst will create 2 copies of the same CRS, but will only cache one.

Copy link
Member Author

Choose a reason for hiding this comment

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

There is an option to supply lock to @cached decorator, also should probably replace plain Dict with some cache class instead.
more details here: https://cachetools.readthedocs.io/en/stable/#cachetools.cached

given that this is not part of the API, I think we can delay this change until later. Also need to check pyproj constraints for multi-threaded access.

Assumption is that there is one common projection across all bands
@Kirill888 Kirill888 marked this pull request as ready for review January 18, 2022 22:58
@Kirill888 Kirill888 merged commit 202cd8a into develop Jan 18, 2022
@Kirill888 Kirill888 deleted the xr-ops branch January 18, 2022 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants