-
-
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
Conversation
xarray/backends/common.py
Outdated
|
||
self.opener = opener | ||
self.open_args = args | ||
self.open_kwargs = kwargs |
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.
For simplicity, let's pool these into a single functools.partial
object. Note that you can get out keyword arguments if desired from partial.keywords
.
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.
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 comment
The 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.
xarray/backends/common.py
Outdated
self._isopen = False | ||
|
||
@property | ||
def value(self): |
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.
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 mode
argument if never actually accessed the file?
For now can we stick with constructing value
eagerly?
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.
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 comment
The 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 comment
The 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
xarray/backends/common.py
Outdated
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 comment
The 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 .value
.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in 097e264
@@ -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): |
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
xarray/tests/test_backends.py
Outdated
with create_tmp_geotiff() as (tmp_file, expected): | ||
with xr.open_rasterio(tmp_file) as rioda: | ||
temp = pickle.dumps(rioda) | ||
actual = pickle.loads(temp) |
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.
Something to think about here -- I'm pretty sure this leaks a file descriptor. I'm not quite sure what the best way to deal with this is (maybe it's not worth worrying about yet), but in the past this has caused issues with tests on various platforms (especially Windows).
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.
And indeed, it's causing a test failure on Windows :).
I think it should work to explicitly open actual
in a context manager, e.g.,
with pickle.loads(temp) as actual:
...
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.
Thanks for digging into this! I don't fully understand what's going on here but I wondered if we shouldn't close the file instead of just dereferencing it.
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
del state['_ds'] |
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.
shouldn't we close the file here? state['_ds'].close()
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 might be used after it is pickled.
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.
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 pickle.load()
.
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.
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.
xarray/backends/common.py
Outdated
|
||
def __setstate__(self, state): | ||
self.__dict__.update(state) | ||
self._ds = None |
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.
Here too?
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.
Glad to see work in this direction.
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
del state['_ds'] |
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 might be used after it is pickled.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 55a3abc.
@jhamman should I test this out on my original workflow or wait a bit? |
@rsignell-usgs - I think this should work in its current form. I'm writing some additional tests now and have some cleanup tasks as indicated in the reviews but I do not expect the behavior to change. |
Thinking about this a little more, maybe we should consolidate pickling with autoclosing into a single, composable wrapper class. Usage would look something like: wrapper = FileWrapper(netCDF4.Dataset, path, mode='r', autoclose=False, kwargs=kwargs)
with wrapper:
# will close/open wrapped file
with wrapper.acquire() as ds:
# context manager is dummy, simply returning the open file object
... # do stuff with ds, a netCDF4.Dataset instance
# or could write wrapper.close() instead of using the context manager
wrapper = FileWrapper(netCDF4.Dataset, path, mode='r', autoclose=True, kwargs=kwargs)
with wrapper:
# explicit opening/closing the wrapper is a no-op if autoclose=True
with wrapper.acquire() as ds:
# context manager opens/closes file
# if file is already open, it's a no-op
... # do stuff with ds, a netCDF4.Dataset instance We could then expose Eventually, we might even change the default for |
It's working for me! |
@shoyer - I like the idea of a wrapper object that achieves both of these tasks. I'm incrementally working that way and have just updated the Pickle Wrapper for now. |
@jhamman , still very much interested in this -- could the existing functionality be merged and enhanced later? |
xarray/tests/test_backends.py
Outdated
|
||
lines = ['foo bar spam eggs'] | ||
|
||
with create_tmp_file() as tmp: |
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.
This needs to add allow_cleanup_failure=ON_WINDOWS
for the test to pass on windows
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.
Great, that did fix the windows failure.
All the tests are passing here. Its been a while so perhaps it would be good to get another review from @pydata/xarray. @rsignell-usgs - can you give this another whirl on your use case? |
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.
LGTM, thanks!
@jhamman , although I'm getting distributed workers to compute the mean from a bunch of images, I'm getting a "Failed to Serialize" error in cell [23] of this notebook: You should be able to run this notebook without modification. |
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.
There's one bug to fix here, but otherwise I agree that we can merge this soon and clean it up later.
xarray/backends/common.py
Outdated
return self._ds | ||
|
||
def __getstate__(self): | ||
del self._ds |
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.
You definitely need to delete this from state
rather than from the class, like what we do in DataStorePickleMixin
:
xarray/xarray/backends/common.py
Lines 461 to 468 in bc52f8a
def __getstate__(self): | |
state = self.__dict__.copy() | |
del state['_ds'] | |
del state['_isopen'] | |
if self._mode == 'w': | |
# file has already been created, don't override when restoring | |
state['_mode'] = 'a' | |
return state |
Otherwise, this will break when you try to pickle this more than once, which is the error that shows up in @rsignell-usgs's notebook.
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.
Thanks, I had just noticed this problem and it should be fixed/tested it in 35520c0.
@jhamman woohoo! Cell [20] completes nicely now: |
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.
LGTM
Might this PR warrant a new minor release? |
@rsignell-usgs Sure, though |
Sounds good. Thanks @shoyer! |
v0.10.7 is out |
whats-new.rst
for all changes andapi.rst
for new APIcc @rsignell-usgs