Skip to content

Commit

Permalink
Use sertit.types.is_iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Jul 29, 2024
1 parent 9299196 commit 0e6e16c
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 37 deletions.
8 changes: 4 additions & 4 deletions eoreader/bands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 = []
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions eoreader/bands/band_names.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/custom_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/hls_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/landsat_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/planet_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/s2_e84_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/s2_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/s2_theia_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/s3_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/optical/s3_slstr_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions eoreader/products/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand Down
4 changes: 2 additions & 2 deletions eoreader/products/sar/sar_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions eoreader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0e6e16c

Please sign in to comment.