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

Issue with xarray dimensions #319

Closed
juanfcocontreras opened this issue Nov 19, 2020 · 12 comments
Closed

Issue with xarray dimensions #319

juanfcocontreras opened this issue Nov 19, 2020 · 12 comments

Comments

@juanfcocontreras
Copy link

With the following xarray file, I get the following exception when trying to access the xarray dimensions:

ValueError: If using all scalar values, you must pass an index

I think the problem is on line 2718 of the views.py file:

dim = pd.DataFrame({"value": ds.coords[dim].data})

And I think it would be enough to change it for the next one to fix it:

dim = pd.Series({"value": ds.coords[dim].data}).to_frame()

@aschonfeld
Copy link
Collaborator

@juanfcocontreras Thanks for catching this and I plan on adding this to the release this weekend. Here's how I'll do it:

@dtale.route("/xarray-dimension-values/<data_id>/<dim>")
@exception_decorator
def get_xarray_dimension_values(data_id, dim):
    ds = global_state.get_dataset(data_id)
    try:
        dim = pd.DataFrame({"value": ds.coords[dim].data})
    except ValueError:
        dim = pd.Series({"value": ds.coords[dim].data}).to_frame()
    dim_f, _ = build_formatters(dim)
    return jsonify(data=dim_f.format_dicts(dim.itertuples()))

Let me know if you're alright with that and I'll move forward 🙏

@juanfcocontreras
Copy link
Author

juanfcocontreras commented Nov 21, 2020

After that fix, dtale gives another exception in:

Input:

ds = xr.open_dataset("./roi.nc")
show(ds)

Output:

Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/views.py", line 79, in _handle_exceptions
    return func(*args, **kwargs)
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/views.py", line 2723, in get_xarray_dimension_values
    return jsonify(data=dim_f.format_dicts(dim.itertuples()))
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/utils.py", line 373, in format_dicts
    return list(map(self.format_dict, lsts))
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/utils.py", line 369, in format_dict
    for idx, name, f in self.fmts
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/utils.py", line 369, in <dictcomp>
    for idx, name, f in self.fmts
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/utils.py", line 323, in f
    return json_string(x)
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/utils.py", line 196, in json_string
    if pd.isnull(x):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@juanfcocontreras
Copy link
Author

juanfcocontreras commented Nov 21, 2020

It's been a while since I've used dtale for xarray, but I think there have been several changes, probably in pandas that have affected its functionality.

If a single-point dataset is selected, it gives another exception. I don't know if both exceptions are related.

Input:

poi = ds.isel(rlat=0, rlon=0)
show(poi)

Output:

Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/xarray/core/utils.py", line 542, in __len__
    return self.shape[0]
IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/views.py", line 79, in _handle_exceptions
    return func(*args, **kwargs)
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/views.py", line 2709, in get_xarray_coords
    for coord, info in ds.coords.items()
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/dtale/views.py", line 2709, in <listcomp>
    for coord, info in ds.coords.items()
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/xarray/core/dataarray.py", line 555, in __len__
    return len(self.variable)
  File "/usr/local/Caskroom/miniconda/base/envs/conda-forge/lib/python3.7/site-packages/xarray/core/utils.py", line 544, in __len__
    raise TypeError("len() of unsized object")
TypeError: len() of unsized object

@aschonfeld
Copy link
Collaborator

Ok, thanks. Maybe i’ll hold off on this release and dig back into xarray for the next one. Thanks for your help

@aschonfeld
Copy link
Collaborator

@juanfcocontreras good news, I did some digging and the issue was with how I was obtaining the dimensions. I was assuming that everything in ds.coords were dimensions, but what I really needed to be iterating was ds.coords.dims. Now lat & lon won't show up as dimensions, which they shouldn't. I'll have this ready for the release 👍

@juanfcocontreras
Copy link
Author

That's great, thank you very much!

By the way, I don't know if to put it right here or open a new issue, when open_browser=True, it shouldn't appear dtale embedded in Jupyter (or nteract in my case), right? The browser is open, but still appears in the output of the cell.

@aschonfeld
Copy link
Collaborator

Hmm, I never actually tried doing open_browser=True in jupyter before. It worked for me but left this cell behind it:
image
I can see if I can update it to no longer create that extra cell in that case

@aschonfeld
Copy link
Collaborator

Ok I think I have a fix for that too

@juanfcocontreras
Copy link
Author

Thas is awesome!! Thank you so much!!

PD: I've played a little bit with the xarray fix and it seems to work fine!

@aschonfeld
Copy link
Collaborator

Terrific, I'll let you know when I publish 1.23.0

@aschonfeld
Copy link
Collaborator

Ok v1.23.0 should be on conda in the next 15 minutes. Just merged the version update

@juanfcocontreras
Copy link
Author

Both fixes seem to be working great. Thank you very much!

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

No branches or pull requests

2 participants