From 0e6e16c417fe0b3d8ddd7c05da948c8a4d0ebf97 Mon Sep 17 00:00:00 2001 From: BRAUN REMI Date: Mon, 29 Jul 2024 11:54:05 +0200 Subject: [PATCH] Use sertit.types.is_iterable --- eoreader/bands/__init__.py | 8 ++++---- eoreader/bands/band_names.py | 6 +++--- eoreader/products/custom_product.py | 4 ++-- eoreader/products/optical/hls_product.py | 4 ++-- eoreader/products/optical/landsat_product.py | 4 ++-- eoreader/products/optical/planet_product.py | 4 ++-- eoreader/products/optical/s2_e84_product.py | 4 ++-- eoreader/products/optical/s2_product.py | 4 ++-- eoreader/products/optical/s2_theia_product.py | 4 ++-- eoreader/products/optical/s3_product.py | 4 ++-- eoreader/products/optical/s3_slstr_product.py | 4 ++-- eoreader/products/product.py | 12 ++++++------ eoreader/products/sar/sar_product.py | 4 ++-- eoreader/reader.py | 4 ++-- requirements.txt | 2 +- setup.py | 2 +- 16 files changed, 37 insertions(+), 37 deletions(-) diff --git a/eoreader/bands/__init__.py b/eoreader/bands/__init__.py index f04b8a87..14b01fc0 100644 --- a/eoreader/bands/__init__.py +++ b/eoreader/bands/__init__.py @@ -460,14 +460,14 @@ def convert_to_band(tc) -> BandNames: if as_list: band_list = [] - if not isinstance(to_convert, list): + if not types.is_iterable(to_convert): to_convert = [to_convert] for tc in to_convert: tc_band = convert_to_band(tc=tc) band_list.append(tc_band) return band_list else: - if isinstance(to_convert, list): + if types.is_iterable(to_convert): raise _ite(f"Set as_list=True(default) for list arguments") return convert_to_band(to_convert) @@ -492,7 +492,7 @@ def to_str( list: str bands """ if as_list: - if not isinstance(to_convert, list): + if not types.is_iterable(to_convert): to_convert = [to_convert] bands_str = [] @@ -508,7 +508,7 @@ def to_str( bands_str.append(band_str) return bands_str else: - if isinstance(to_convert, list): + if types.is_iterable(to_convert): raise _ite(f"Set as_list=True(default) for list arguments") try: band_str = tc.name diff --git a/eoreader/bands/band_names.py b/eoreader/bands/band_names.py index 7c06be19..9dbc0fd4 100644 --- a/eoreader/bands/band_names.py +++ b/eoreader/bands/band_names.py @@ -1,12 +1,12 @@ from typing import Union -from sertit.misc import ListEnum +from sertit import misc, types from eoreader.exceptions import InvalidTypeError from eoreader.stac import StacCommonNames -class BandNames(ListEnum): +class BandNames(misc.ListEnum): """Super class for band names, **do not use it**.""" @classmethod @@ -25,7 +25,7 @@ def from_list(cls, name_list: Union[list, str]) -> list: Returns: list: List of enums """ - if not isinstance(name_list, list): + if not types.is_iterable(name_list): name_list = [name_list] try: band_names = [cls(name) for name in name_list] diff --git a/eoreader/products/custom_product.py b/eoreader/products/custom_product.py index deda4781..99d3d946 100644 --- a/eoreader/products/custom_product.py +++ b/eoreader/products/custom_product.py @@ -28,7 +28,7 @@ from lxml.builder import E from rasterio import crs from rasterio.enums import Resampling -from sertit import logs, misc, path, rasters +from sertit import logs, misc, path, rasters, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -423,7 +423,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/products/optical/hls_product.py b/eoreader/products/optical/hls_product.py index 9dabab6f..5bba779d 100644 --- a/eoreader/products/optical/hls_product.py +++ b/eoreader/products/optical/hls_product.py @@ -31,7 +31,7 @@ import xarray as xr from lxml import etree from rasterio.enums import Resampling -from sertit import path, rasters, rasters_rio, xml +from sertit import path, rasters, rasters_rio, types, xml from sertit.misc import ListEnum from sertit.types import AnyPathType @@ -873,7 +873,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/products/optical/landsat_product.py b/eoreader/products/optical/landsat_product.py index d1733ac7..18416bb2 100644 --- a/eoreader/products/optical/landsat_product.py +++ b/eoreader/products/optical/landsat_product.py @@ -29,7 +29,7 @@ from lxml import etree from lxml.builder import E from rasterio.enums import Resampling -from sertit import AnyPath, path, rasters, rasters_rio +from sertit import AnyPath, path, rasters, rasters_rio, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -1430,7 +1430,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/products/optical/planet_product.py b/eoreader/products/optical/planet_product.py index 371a5d71..f2b73693 100644 --- a/eoreader/products/optical/planet_product.py +++ b/eoreader/products/optical/planet_product.py @@ -31,7 +31,7 @@ import xarray as xr from lxml import etree from rasterio.enums import Resampling -from sertit import path, rasters, strings +from sertit import path, rasters, strings, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -1004,7 +1004,7 @@ def _get_path( Union[list, str]: Paths(s) """ - if invalid_lookahead is not None and not isinstance(invalid_lookahead, list): + if invalid_lookahead is not None and not types.is_iterable(invalid_lookahead): invalid_lookahead = [invalid_lookahead] ok_paths = [] diff --git a/eoreader/products/optical/s2_e84_product.py b/eoreader/products/optical/s2_e84_product.py index aeaba10b..d9fc8e4f 100644 --- a/eoreader/products/optical/s2_e84_product.py +++ b/eoreader/products/optical/s2_e84_product.py @@ -26,7 +26,7 @@ import xarray as xr from lxml import etree from rasterio.enums import Resampling -from sertit import AnyPath, files, path, rasters_rio +from sertit import AnyPath, files, path, rasters_rio, types from sertit.files import CustomDecoder from sertit.types import AnyPathStrType, AnyPathType @@ -310,7 +310,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/products/optical/s2_product.py b/eoreader/products/optical/s2_product.py index 0998ef56..06aebd73 100644 --- a/eoreader/products/optical/s2_product.py +++ b/eoreader/products/optical/s2_product.py @@ -36,7 +36,7 @@ from rasterio import errors, features, transform from rasterio.crs import CRS from rasterio.enums import Resampling -from sertit import AnyPath, files, geometry, path, rasters, vectors +from sertit import AnyPath, files, geometry, path, rasters, types, vectors from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType from shapely.geometry import box @@ -532,7 +532,7 @@ def _get_res_band_folder(self, band_list: list, pixel_size: float = None) -> dic dict: Dictionary containing the folder path for each queried band """ if pixel_size is not None: - if isinstance(pixel_size, (list, tuple)): + if types.is_iterable(pixel_size): pixel_size = pixel_size[0] # Open the band directory names diff --git a/eoreader/products/optical/s2_theia_product.py b/eoreader/products/optical/s2_theia_product.py index 53a4bbe3..c09d6066 100644 --- a/eoreader/products/optical/s2_theia_product.py +++ b/eoreader/products/optical/s2_theia_product.py @@ -29,7 +29,7 @@ import xarray as xr from lxml import etree from rasterio.enums import Resampling -from sertit import geometry, path, rasters +from sertit import geometry, path, rasters, types from sertit.types import AnyPathStrType, AnyPathType from eoreader import DATETIME_FMT, EOREADER_NAME, cache, utils @@ -820,7 +820,7 @@ def _create_mask( xr.DataArray: Mask masked array """ - if not isinstance(bit_ids, list): + if not types.is_iterable(bit_ids): bit_ids = [bit_ids] conds = rasters.read_bit_array(bit_array.astype(np.uint8), bit_ids) cond = reduce(lambda x, y: x | y, conds) # Use every condition (bitwise or) diff --git a/eoreader/products/optical/s3_product.py b/eoreader/products/optical/s3_product.py index 35d8adf0..264bd3aa 100644 --- a/eoreader/products/optical/s3_product.py +++ b/eoreader/products/optical/s3_product.py @@ -41,7 +41,7 @@ from rasterio import crs as riocrs from rasterio.enums import Resampling from rasterio.errors import NotGeoreferencedWarning -from sertit import path, vectors, xml +from sertit import path, types, vectors, xml from sertit.misc import ListEnum from sertit.rasters import MAX_CORES from sertit.types import AnyPathStrType, AnyPathType @@ -555,7 +555,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/products/optical/s3_slstr_product.py b/eoreader/products/optical/s3_slstr_product.py index 52b753ca..90504542 100644 --- a/eoreader/products/optical/s3_slstr_product.py +++ b/eoreader/products/optical/s3_slstr_product.py @@ -31,7 +31,7 @@ import xarray as xr from rasterio import features from rasterio.enums import Resampling -from sertit import path, rasters +from sertit import path, rasters, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -1098,7 +1098,7 @@ def _create_mask( xr.DataArray: Mask masked array """ - if not isinstance(bit_ids, list): + if not types.is_iterable(bit_ids): bit_ids = [bit_ids] conds = rasters.read_bit_array(bit_array, bit_ids) cond = reduce(lambda x, y: x | y, conds) # Use every condition (bitwise or) diff --git a/eoreader/products/product.py b/eoreader/products/product.py index 58a7d8ea..02ec8b9a 100644 --- a/eoreader/products/product.py +++ b/eoreader/products/product.py @@ -43,7 +43,7 @@ from rasterio.crs import CRS from rasterio.enums import Resampling from rasterio.vrt import WarpedVRT -from sertit import AnyPath, files, logs, misc, path, rasters, strings, xml +from sertit import AnyPath, files, logs, misc, path, rasters, strings, types, xml from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -873,7 +873,7 @@ def load( ) pixel_size = kwargs.pop("resolution") - if (isinstance(bands, list) and ("GREEN1" in bands or GREEN1 in bands)) or ( + if (types.is_iterable(bands) and ("GREEN1" in bands or GREEN1 in bands)) or ( "GREEN1" == bands or GREEN1 == bands ): logs.deprecation_warning( @@ -1239,7 +1239,7 @@ def has_bands(self, bands: Union[list, BandNames, str]) -> bool: Returns: bool: True if the products has the specified band """ - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] return all([self.has_band(band) for band in set(bands)]) @@ -1715,7 +1715,7 @@ def _update_attrs( # Are we sure of that ? xarr.attrs = {} - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] long_name = to_str(bands) xr_name = "_".join(long_name) @@ -1887,7 +1887,7 @@ def _res_to_str(res): return f"{abs(res):.2f}m".replace(".", "-") if pixel_size: - if isinstance(pixel_size, (tuple, list)): + if types.is_iterable(pixel_size): res_x = _res_to_str(pixel_size[0]) res_y = _res_to_str(pixel_size[1]) if res_x == res_y: @@ -2032,7 +2032,7 @@ def to_band(self, raw_bands: Union[list, BandNames, str, int]) -> list: Returns: list: Mapped bands """ - if not isinstance(raw_bands, list): + if not types.is_iterable(raw_bands): raw_bands = [raw_bands] bands = [] diff --git a/eoreader/products/sar/sar_product.py b/eoreader/products/sar/sar_product.py index 81a04eb5..0cc1a0cd 100644 --- a/eoreader/products/sar/sar_product.py +++ b/eoreader/products/sar/sar_product.py @@ -30,7 +30,7 @@ import xarray as xr from rasterio import crs from rasterio.enums import Resampling -from sertit import AnyPath, misc, path, rasters, snap, strings +from sertit import AnyPath, misc, path, rasters, snap, strings, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType, AnyPathType @@ -633,7 +633,7 @@ def _load_bands( return {} # Get band paths - if not isinstance(bands, list): + if not types.is_iterable(bands): bands = [bands] if pixel_size is None and size is not None: diff --git a/eoreader/reader.py b/eoreader/reader.py index 03b4a457..c9a33d9c 100644 --- a/eoreader/reader.py +++ b/eoreader/reader.py @@ -26,7 +26,7 @@ from zipfile import BadZipFile import validators -from sertit import AnyPath, path, strings +from sertit import AnyPath, path, strings, types from sertit.misc import ListEnum from sertit.types import AnyPathStrType @@ -440,7 +440,7 @@ def _compile_(regex_str: str): return re.compile(f"{prefix}{regex_str}{suffix}") # Case folder is not enough to identify the products (i.e. COSMO Skymed) - if isinstance(regex, list): + if types.is_iterable(regex): comp = [_compile_(regex) for regex in regex] else: comp = [_compile_(regex)] diff --git a/requirements.txt b/requirements.txt index 8c14a839..d723070a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,7 +37,7 @@ geopandas>=0.11.0 spyndex>=0.3.0 # SERTIT libs -sertit[full]>=1.38.0 +sertit[full]>=1.39.0 # Optimizations dask[complete]>=2021.10.0 diff --git a/setup.py b/setup.py index 61ca0772..8fc713d3 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ "xarray>=0.18.0", "rioxarray>=0.10.0", "geopandas>=0.11.0", - "sertit[full]>=1.30.0", + "sertit[full]>=1.39.0", "spyndex>=0.3.0", "pyresample", "zarr",