-
Notifications
You must be signed in to change notification settings - Fork 12
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
API? #6
Comments
One additional thing I've realized is needed since writing hgrecco/pint#849 (comment) is a helper for coordinate unit conversion. Not sure what the best name for this would be though. In MetPy, the tentative name is See Unidata/MetPy#1325 / https://github.com/Unidata/MetPy/compare/v0.12.0...jthielen:0-12-patch-xarray-0-15-1?expand=1 |
we could also provide work-arounds for operations like |
So a Also what about |
Not quite, it would be separate. I'm thinking of something to change the units on a coordinate of def coord_to(self, coord, units):
new_coord_var = self.da[coord].copy(
data=Quantity(self.da[coord].values, self.da[coord].attrs.get('units')).m_as(units)
)
new_coord_var.attrs['units'] = str(units)
return self.da.assign_coords(coords={coord: new_coord_var}) |
how about also accepting a Obviously, we need something really similar for |
So long as we don't lose the easy ability to just convert data units (e.g., |
So as a to-do list, that would mean one positional arg for the data and multiple keyword args for the coords in
|
Agreed for points 1 ( |
something else that is not a part of the API but in scope for this package (I think?): maybe we could monkeypatch the |
I definitely think that's in scope for this package, at least until some more general solution for reprs of all wrapped arrays is implemented upstream. Monkey-patching actually seems like a reasonable temporary solution here I think? The worst that can happen is that some other method which uses the repr fails to display the units right |
from #11:
|
if we can get the reprs to work (pydata/xarray#2773, hgrecco/pint#1133), improve the documentation and maybe also expose the |
1133: Change HTML repr away from LaTeX to a Sparse/Dask-like repr r=hgrecco a=jthielen While I've never been a big fan of Pint's LaTeX repr in JupyterLab/Jupyter Notebook due to working with large arrays that would crash the renderer, I've recently been working a lot with various wrapping combinations of xarray, Pint, Dask, and Sparse, in which Pint's LaTeX repr performs particularly poorly. Even if Pint's repr were elided (#765), it would still poorly nest with the other HTML reprs. This motivated me to implement an alternative HTML repr for Pint using a simple HTML table similar to what both Dask and Sparse use. As shown in [this demonstration notebook](https://nbviewer.jupyter.org/urls/dl.dropbox.com/s/jlgqm6s92kzvagi/pint_html_repr_demo.ipynb), this allows much better nesting of HTML reprs. A quick example image is below: ![](https://www.meteor.iastate.edu/~jthielen/Screenshot%20from%202020-07-14%2017-07-51.png) This doesn't quite take care of [xarray's unit repr discussions](pydata/xarray#2773) (since this only shows up when expanding the data in a dataset), but I still think it is a worthwhile improvement for the expanded view. @keewis do you have any thoughts on this particular interaction? Marking as a WIP/draft for now to see if this is a welcome format for the HTML repr first before I dig too much into optimizing the implementation and writing tests for it. - [x] Closes #654 (even though it was already closed in favor of #765, I think the are related but separate issues) - [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors - [x] The change is fully covered by automated unit tests - [x] Documented in docs/ as appropriate - [x] Added an entry to the CHANGES file Tagging @nbren12, @hgrecco, @jondoesntgit, @natezb, @ipcoder, @keewis, and @TomNicholas for input based on past issues (#654, #765, and xarray-contrib/pint-xarray#6). Co-authored-by: Jon Thielen <[email protected]>
Sounds great! Right now though it looks like the only placeholders are |
I was thinking of Edit: let's continue this in #25 |
from #61: try to |
@jthielen proposed a rough accessor API in pint/#849, to which I've added a couple of things:
DataArray:
da.pint.to(...)
: return dataarray with converted units (clean-up and implement the conversion methods #11)da.pint.to_base_units()
: return dataarray with base unitsda.pint.units
: units of quantity (as a Unit)da.pint.magnitude
: magnitude of quantityda.pint.quantify(unit_registry=None, unit=None)
: create DataArray wrapping a Quantity based on string unit attribute of DataArray or specified unitda.pint.dequantify()
: replace data with the Quantity's magnitude, and add back string unit attribute from Quantity's unitda.pint.sel()
: wrap da.sel to handle indexing with Quantities (by casting to magnitude in the coordinate's units similar to how MetPy does it, since true unit-aware indexing is not available yet in xarray)da.pint.loc
: wrap da.loc likewiseda.pint.to_system(system)
: convert all variables to be expressed in the (base?) units of a different system. Might require upstream additions to pint.Dataset:
ds.pint.to(...)
: return Dataset with converted units (clean-up and implement the conversion methods #11)ds.pint.to_base_units()
: return dataset with base unitsds.pint.quantify(unit_registry=None)
: convert all data variables to quantitiesds.pint.dequantify()
: convert all data variables from quantities to magnitudes with units as an attributeds.pint.sel()
: wrap ds.sel to handle indexing with Quantitiesds.pint.loc
: wrap ds.loc likewiseds.pint.to_system(system)
: convert all variables to be expressed in the (base?) units of a different system.Anything else?
At some point when the integration is more solidified (but before official release) we should change the accessor from
pint
tounits
, to get a interface more like what's described here.This would be:
a) More intuitive
b) Units-library agnostic
c) A good fit for potentially using an entrypoint to choose which units library you want to use. There's already an entrypoint for plotting backends in xarray, and plans to add one for storage backends too.
The text was updated successfully, but these errors were encountered: