-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Feature/pickle rasterio #2131
Feature/pickle rasterio #2131
Changes from 6 commits
4319aac
fb8b3d7
2b7176b
c8de3a5
45f0aeb
99c9661
55a3abc
8b286c0
6669035
097e264
822a080
40c15a6
78ffcb4
35520c0
8d7c768
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -503,3 +503,43 @@ def assert_open(self): | |
if not self._isopen: | ||
raise AssertionError('internal failure: file must be open ' | ||
'if `autoclose=True` is used.') | ||
|
||
|
||
class PickleByReconstructionWrapper(object): | ||
|
||
def __init__(self, opener, *args, **kwargs): | ||
|
||
self.opener = opener | ||
self.open_args = args | ||
self.open_kwargs = kwargs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For simplicity, let's pool these into a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we just have the backends pass in a partial function then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't anticipate adding any argument, then I think it's just as easy to make the partial function here. I suppose there's some potential overlap in needs with auto-closing a file, so I'm OK either way. |
||
|
||
self._ds = None | ||
self._isopen = False | ||
|
||
@property | ||
def value(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little concerned about the complexity of making file opening lazy. State makes things harder to reason about. For example, what happens with the For now can we stick with constructing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually not a big fan of dropping the lazy opening. For one, there will be a non-negligible performance penalty. Perhaps a better argument is that we've been using this logic successfully in xarray for a while now. That said, its not clear to me what the potential problems with this approach so I'm open to convincing otherwise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we always open files immediately to pull out metadata? (e.g., variables and dimensions) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are right. I've removed most of this in 6669035 |
||
if self._ds is not None and self._isopen: | ||
return self._ds | ||
self._ds = self.opener(*self.open_args, **self.open_kwargs) | ||
self._isopen = True | ||
return self._ds | ||
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
del state['_ds'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we close the file here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be used after it is pickled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah OK, maybe I misunderstood. I thought the purpose of this wrapper was to dereference (and close) the file before 'dump()' and re-open it after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The basic idea is that most open file handles can't be pickled so we need to provide a mechanism to remove the existing handle and generate a new one in the dump/load steps in the pickling. In most cases, we do want to keep the original file open. |
||
del state['_isopen'] | ||
if self.open_kwargs.get('mode', None) == 'w': | ||
# file has already been created, don't override when restoring | ||
state['open_kwargs']['mode'] = 'a' | ||
return state | ||
|
||
def __setstate__(self, state): | ||
self.__dict__.update(state) | ||
self._ds = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too? |
||
self._isopen = False | ||
|
||
def __getitem__(self, key): | ||
return self.value[key] | ||
|
||
def __getattr__(self, name): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This magic is convenient but I think it could possibly be a source of bugs. I would stick to requiring pulling out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was thinking someone would call me on this :). It was the path of least resistance for getting this working but I think a lot of care would be required not to trample on attributes. Do you think I should drop both getitem/getattr? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in 097e264 |
||
return getattr(self.value, name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2649,6 +2649,14 @@ def test_chunks(self): | |
ex = expected.sel(band=1).mean(dim='x') | ||
assert_allclose(ac, ex) | ||
|
||
def test_pickle_rio(self): | ||
# regression test for https://github.com/pydata/xarray/issues/2121 | ||
with create_tmp_geotiff() as (tmp_file, expected): | ||
with xr.open_rasterio(tmp_file) as rioda: | ||
temp = pickle.dumps(rioda) | ||
with pickle.loads(temp) as actual: | ||
assert_equal(actual, rioda) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to also add an integration test reading rasterio data with dask.distributed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 55a3abc. |
||
|
||
def test_ENVI_tags(self): | ||
rasterio = pytest.importorskip('rasterio', minversion='1.0a') | ||
from rasterio.transform import from_origin | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add a unit test verifies that this works properly independently of any concrete datastore.
Maybe something simple with
open()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8b286c0