-
Notifications
You must be signed in to change notification settings - Fork 17
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
Open Data using File-Like Object #189
Comments
Thanks for raising and the clear description. Could you please also provide the full traceback? Thanks! Which xarray version are you on? |
I am on the current xarray version 2024.7.0. Can you reproduce it? TracebackTraceback (most recent call last):
File "/scratch/p/Paul.Ockenfuss/Test/test_polardata/xradar_bug.py", line 6, in <module>
ds = xr.open_dataset(f, group="sweep_0", engine="odim")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/backends/api.py", line 588, in open_dataset
backend_ds = backend.open_dataset(
^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xradar/io/backends/odim.py", line 825, in open_dataset
ds = ds.sortby(dim0)
^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/dataset.py", line 8182, in sortby
aligned_vars = align(self, *arrays, join="left")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/alignment.py", line 882, in align
aligner.align()
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/alignment.py", line 582, in align
self.reindex_all()
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/alignment.py", line 557, in reindex_all
self.results = tuple(
^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/alignment.py", line 558, in <genexpr>
self._reindex_one(obj, matching_indexes)
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/alignment.py", line 546, in _reindex_one
return obj._reindex_callback(
^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/dataset.py", line 3517, in _reindex_callback
reindexed = self.copy(deep=aligner.copy)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/dataset.py", line 1366, in copy
return self._copy(deep=deep, data=data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/dataset.py", line 1402, in _copy
variables[k] = v._copy(deep=deep, data=data.get(k), memo=memo)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/scratch-local/Paul.Ockenfuss/virtual_environments/radar/lib/python3.11/site-packages/xarray/core/variable.py", line 925, in _copy
ndata = copy.deepcopy(ndata, memo)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in <listcomp>
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in <listcomp>
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in <listcomp>
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 231, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 271, in _reconstruct
state = deepcopy(state, memo)
^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in <listcomp>
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 146, in deepcopy
y = copier(x, memo)
^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in _deepcopy_tuple
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 211, in <listcomp>
y = [deepcopy(a, memo) for a in x]
^^^^^^^^^^^^^^^^^
File "/software/opt/el_9/x86_64/python/3.11-2023.09/lib/python3.11/copy.py", line 161, in deepcopy
rv = reductor(4)
^^^^^^^^^^^
TypeError: cannot pickle '_io.BufferedReader' object |
Yes, and I think I've already hit this, if you look at the tests: Lines 222 to 232 in bf03ab8
The issue is triggered by import numpy as np
import netCDF4
import xarray as xr
import fsspec
import io
# create file with netCDF4 to have all bits and pieces
filename = "test.h5"
with netCDF4.Dataset(filename,'w') as f:
f.createDimension('x', 3)
f.createDimension('y', 6)
var = f.createVariable('var', 'int8', ('x', 'y'))
xcoord = f.createVariable('x', 'int8', ('x'))
ycoord = f.createVariable('y', 'int8', ('y'))
xcoord[:] = [2, 1, 0]
ycoord[:] = [0, 1, 2, 3, 4, 5]
var[:] = np.arange(18).reshape((3, 6)) This works, maybe because with xr.backends.H5NetCDFStore.open(filename) as store:
store_entrypoint = xr.backends.StoreBackendEntrypoint()
ds = store_entrypoint.open_dataset(store)
display(ds["var"].values)
ds = ds.sortby('x')
display(ds["var"].values)
This works too, with with open(filename, 'rb') as f:
print(type(f))
store = xr.backends.H5NetCDFStore.open(f)
store_entrypoint = xr.backends.StoreBackendEntrypoint()
ds = store_entrypoint.open_dataset(store)
display(ds["var"].values)
ds = ds.reindex({"x": np.array([0, 1, 2])})
display(ds["var"].values) <class '_io.BufferedReader'>
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17]], dtype=int8)
array([[12, 13, 14, 15, 16, 17],
[ 6, 7, 8, 9, 10, 11],
[ 0, 1, 2, 3, 4, 5]], dtype=int8) This breaks with sortby. with open(filename, 'rb') as f:
print(type(f))
store = xr.backends.H5NetCDFStore.open(f)
store_entrypoint = xr.backends.StoreBackendEntrypoint()
ds = store_entrypoint.open_dataset(store)
display(ds["var"].values)
ds = ds.sortby("x")
display(ds["var"].values) Output with Traceback
There are quite a bunch of GH issues out there, which are discussing that problem. It seems, that io.BufferedReader can't be pickled. XRef:
What I'm wondering is why the reindex above works and the sortby fails? @benbovy, can you shed some light here? |
Thanks for the investigation @kmuehlbauer! Interesting that it works with reindex, but not sortby. |
There is a deepcopy in the sortby code flow somewhere, but not in the reindex. Let's see if we get some answers from @benbovy (sorry for tagging twice). |
Sorry @kmuehlbauer not sure what is causing the different behavior here, although I haven't inspected further the reindex vs. sortby code. |
Thanks @benbovy, I'll open an issue on the xarray tracker for further investigation. |
Is it possible to use a file-like object instead of filenames when using xradar? According to the xarray open_dataset documentation, as well as the xradar Odim Backend Documentation, file objects are supported, but with the 'odim' engine, I get an error:
Thank you for any help and the xradar development in general!
Paul
The text was updated successfully, but these errors were encountered: