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

feat: Allow users to configure the internal thread pool (#11) #54

Closed
wants to merge 7 commits into from
15 changes: 12 additions & 3 deletions xee/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class EarthEngineStore(common.AbstractDataStore):

DEFAULT_MASK_VALUE = np.iinfo(np.int32).max

def __init__(self, executor_kwargs=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already is a constructor.

if executor_kwargs is None:
self.executor_kwargs = {}
else:
self.executor_kwargs = executor_kwargs

@classmethod
def open(
cls,
Expand Down Expand Up @@ -634,7 +640,7 @@ def reduce_bands(x, acc):
return target_image

def _raw_indexing_method(
self, key: tuple[Union[int, slice], ...]
self, key: tuple[Union[int, slice], ...]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove extra space (to minimize the diff).

) -> np.typing.ArrayLike:
key, squeeze_axes = self._key_to_slices(key)

Expand Down Expand Up @@ -682,8 +688,9 @@ def _raw_indexing_method(
for _ in range(shape[0])
]

# TODO(#11): Allow users to configure this via kwargs.
with concurrent.futures.ThreadPoolExecutor() as pool:

# Pass executor_kwargs to ThreadPoolExecutor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is a bit too verbose for my taste.

with concurrent.futures.ThreadPoolExecutor(**self.executor_kwargs) as pool:
for (i, j, k), arr in pool.map(
self._make_tile, self._tile_indexes(key[0], bbox)
):
Expand Down Expand Up @@ -772,6 +779,7 @@ def open_dataset(
primary_dim_name: Optional[str] = None,
primary_dim_property: Optional[str] = None,
ee_mask_value: Optional[float] = None,
executor_kwargs: Optional[dict] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to adding it to the arguments, please update the docstring.

) -> xarray.Dataset:
"""Open an Earth Engine ImageCollection as an Xarray Dataset.

Expand Down Expand Up @@ -859,6 +867,7 @@ def open_dataset(
primary_dim_name=primary_dim_name,
primary_dim_property=primary_dim_property,
mask_value=ee_mask_value,
executor_kwargs=executor_kwargs,
)

store_entrypoint = backends_store.StoreBackendEntrypoint()
Expand Down
12 changes: 12 additions & 0 deletions xee/ext_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ def test_data_sanity_check(self):
self.assertNotEqual(temperature_2m.min(), 0.0)
self.assertNotEqual(temperature_2m.max(), 0.0)

def test_open_dataset_with_executor_kwargs(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the test!

executor_kwargs = {'max_workers': 2}
ds = self.entry.open_dataset(
'ee://LANDSAT/LC08/C01/T1',
drop_variables=tuple(f'B{i}' for i in range(3, 12)),
scale=25.0,
n_images=3,
executor_kwargs=executor_kwargs,
)

self.assertEqual(ds.thread_pool.max_workers, executor_kwargs['max_workers'])


if __name__ == '__main__':
absltest.main()