Skip to content

Commit

Permalink
Ensure 'data_type' attribute is a string to make it valid for the dat…
Browse files Browse the repository at this point in the history
…aset.

Fixes #49.

PiperOrigin-RevId: 571994074
  • Loading branch information
Xee authors committed Oct 9, 2023
1 parent 744491f commit 348093c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
30 changes: 28 additions & 2 deletions xee/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ class EarthEngineStore(common.AbstractDataStore):

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

ATTRS_VALID_TYPES = (
str,
int,
float,
complex,
np.ndarray,
np.number,
list,
tuple
)

@classmethod
def open(
cls,
Expand Down Expand Up @@ -164,7 +175,7 @@ def __init__(
coordinates=f'{self.primary_dim_name} {x_dim_name} {y_dim_name}',
crs=self.crs_arg,
)

self._props = self._make_attrs_valid(self._props)
# Scale in the projection's units. Typically, either meters or degrees.
# If we use the default CRS i.e. EPSG:3857, the units is in meters.
default_scale = self.SCALE_UNITS.get(self.scale_units, 1)
Expand Down Expand Up @@ -324,13 +335,28 @@ def _band_attrs(self, band_name: str) -> types.BandInfo:
def _bands(self) -> list[str]:
return [b['id'] for b in self._img_info['bands']]

def _make_attrs_valid(
self, attrs: dict[str, Any]
) -> dict[
str,
Union[
str, int, float, complex, np.ndarray, np.number, list[Any], tuple[Any]
],
]:
return {
key: (str(value)
if not isinstance(value, self.ATTRS_VALID_TYPES)
else value)
for key, value in attrs.items()
}

def open_store_variable(self, name: str) -> xarray.Variable:
arr = EarthEngineBackendArray(name, self)
data = indexing.LazilyIndexedArray(arr)

x_dim_name, y_dim_name = self.dimension_names
dimensions = [self.primary_dim_name, x_dim_name, y_dim_name]
attrs = self._band_attrs(name)
attrs = self._make_attrs_valid(self._band_attrs(name))
encoding = {
'source': attrs['id'],
'scale_factor': arr.scale,
Expand Down
18 changes: 17 additions & 1 deletion xee/ext_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
# ==============================================================================
r"""Integration tests for the Google Earth Engine backend for Xarray."""

import pathlib

from absl.testing import absltest
Expand Down Expand Up @@ -358,6 +357,23 @@ def test_data_sanity_check(self):
self.assertNotEqual(temperature_2m.min(), 0.0)
self.assertNotEqual(temperature_2m.max(), 0.0)

def test_validate_band_attrs(self):
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, # in degrees
n_images=3,
)
valid_types = (str, int, float, complex, np.ndarray, np.number, list, tuple)

# Check attrs on the dataset itself
for _, value in ds.attrs.items():
self.assertIsInstance(value, valid_types)

# Check attrs on each variable within the dataset
for variable in ds.variables.values():
for _, value in variable.attrs.items():
self.assertIsInstance(value, valid_types)

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

0 comments on commit 348093c

Please sign in to comment.