-
-
Notifications
You must be signed in to change notification settings - Fork 404
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
Improved xarray support #2319
Comments
Agreed this should be pretty straightforward, this is where you'd implement that.
Agreed, this should be consistent and should be handled in the same place as the previous issue.
Not all conversions are currently supported, generally we only support converting the standard formats, i.e. dictionary based representations, but full ability to convert between all formats would be a good goal to shoot for. |
Actually a bit of code in Dataset.clone could probably detect if the current datatype is in the list of datatypes that the user supplied and if not just drop down to a generic format. That will take care of almost all conversions I think. |
import holoviews as hv
import xarray as xr
import numpy as np
hv.notebook_extension()
%opts Image [colorbar=True]
xs = np.linspace(0, 2.5, 51)
ys = np.linspace(0, 1, 21)
zs = np.random.random((21, 51))
arr = xr.DataArray(zs, coords=[("y", ys), ("x", xs)], name="field")
x_dim = hv.Dimension("x", unit="m")
y_dim = hv.Dimension("y", unit="m")
z_dim = hv.Dimension("field", unit="mT")
dataset = hv.Dataset(arr, kdims=[x_dim, y_dim], vdims=[z_dim])
dataset.to(hv.Image) The unit mT does not show up next to the colorbar. More generally it would be nice to have improved support for changing dimension names, labels and units, not just for xarray related stuff - the |
It seems like your latest changes to xarray overwrite existing units on the dimensions.
Concrete examples would be appreciated, I'm not currently aware of any issues with |
That's a point I did not think about previously (I'm still using 1.9.5, not sure if it's a good idea to use the current master for everyday work). Ideally, specified key and value dimensions should override anything thats present in the xarray, but I haven't checked if that's the case - I'll look into it, perhaps another PR is needed.
xdim = hv.Dimension("x", label="my x dimension", unit="mJ")
ydim = hv.Dimension("y", label="my y dimension", unit="kB")
zdim = hv.Dimension("z", label="my z dimension", unit="fs")
image = hv.Dataset(([0, 1], [1, 2], [[3, 4], [5, 6]]), kdims=[xdim, ydim], vdims=[zdim]).to(hv.Image)
image = image.redim(z='time')
image = image.redim.unit(x='eV', y='TB')
image = image.redim.label(x='Energy', y='Capacity') Actually, I saw that my only issue with redim is that it refuses to change existing labels - everything else works like I would expect it to. So as far as I know, if you come across a dimension label you don't like, the only option is to extract the dimension values and rebuild the element from scratch. I would prefer if redim worked the same way on labels as it does on units and dimension names. |
Tbh I agree, @jlstevens you originally objected to being able to override the label, but imo this is an annoying and artificial restriction. What was there some convincing reasoning? |
The |
And using .redim isn't explicit enough? You can already override the dimension as a whole using redim so I really don't see what is gained by disallowing |
Explicitly declared. |
From my perspective, the |
I actually agree with that @drs251 . Changing the dimension name was always a misfeature imho and I think it was introduced before |
My point was that you can already override both name and label using |
I suppose so. The intended design is already broken. |
It's absolutely not a misfeature as it's crucial for certain operations, e.g. if we ever want to implement a melt operation and it's already used for similar operations in places.
How can you say that when completely replacing a dimension is an essential operation that absolutely must be supported. |
For operations perhaps, but for changing the title on a plot? All it does is make things more difficult to reason about. Do you really think the semantics of a dimension change because you want a different title? |
For changing the title of a plot axis you indeed shouldn't change the whole dimension, especially the name, which is precisely why disallowing changing the label easily is such a pain. Instead of simply using
But we don't have distinct mechanisms to do this inside an operation, so are you suggesting introducing a whole other API? There's many reasons to change the names of dimensions so a user should be able to do it somehow that doesn't require knowledge about the type of data, |
Really, the point is that the title should be set as plot option, not by changing the dimension definition. What the dimension is has nothing to do with the title you want. Does your data fundamentally change in semantics because you want to tweak a title?
This I do agree with at least (as I mentioned above) which is a reason to allow the label to be changed. The entire idea of changing the core identity of a dimension to change a title is the real problem here. |
|
Could you please provide an example? There are extensive unit tests for .to and .groupby which are all passing. If it is failing that is a critical bug and needs to be fixed before a release tomorrow. |
import holoviews as hv
import xarray as xr
import numpy as np
xs = [0.1, 0.2, 0.3]
ys = [0, 1]
zs = np.array([[0, 1], [2, 3], [4, 5]])
da = xr.DataArray(zs, coords=[('x_dim', xs), ('y_dim', ys)], name="data_name", dims=['y_dim', 'x_dim'])
da.attrs['long_name'] = "data long name"
da.attrs['units'] = "array_unit"
da.x_dim.attrs['units'] = "x_unit"
da.y_dim.attrs['long_name'] = "y axis long name"
dataset = hv.Dataset(da)
image = dataset.to(hv.Image) I get
This does not happen in version 1.9.5 |
This looks like the critical param bug that is in the process of being fixed. This will involve a new param release (hopefully today!) and the fix should simply be to install this new version (which will also be required by 1.10). |
Thanks for getting back to us so quickly @drs251, that is indeed related to param. I'm actually very glad to hear that this doesn't affect 1.9.5 because I had thought this was broken for everyone. |
I think all the issues here were addressed, if I'm mistaken please reopen a more specific issue. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I noticed some points concerning xarray support in holoviews which could be improved:
Both xarray and holoviews support measurements units, but it appears that they are lost during in- and exporting. I think it would be pretty straightforward to add this feature in the holoviews importing and exporting code, I could look into it.
When importing a netcdf file via xarray and passing it to
hv.Dataset
, the key dimensions are sometimes ordered differently, depending whether the file is imported as axr.DataArray
orxr.Dataset
. This is not a gigantic problem, especially since one can manually specify the key dimensions' order, although it is still pretty annoying when dealing with images...EDIT: (actually, this seems to have been fixed already sometime between 1.9.3 and the current master)
I've never had any luck converting the data storage backend from 'xarray' to anything else, writing
img.clone(datatype=['image'])
, as shown in the gridded datasets guide: http://holoviews.org/user_guide/Gridded_Datasets.html Perhaps it would be nice to show this more explicitly in the documentation, or fix it if it is a bug.EDIT:
I've noticed that dimensions without coordinates are not accepted by
XArrayInterface
- although omitting coordinates seems to be the recommended way in xarray to deal with dimensions without meaningful coordinates (such as pixels in an image). In this case, it should be possible to just automatically assign simple range coordinates to these dimensions when making a holoviews Dataset.The treatment of non-dimension coordinates could be more concise. Take for example the "rasm" example that comes with xarray, which looks like this
Right now, coordinates are converted into kdims, and dimensions without coordinates are ignored. IMO, it would be better to let the user select if they want (time, y, c) or (time, yc, xc) coordinates when making the holoviews Dataset, where the default should be the proper dimensions.
The text was updated successfully, but these errors were encountered: