Skip to content

Commit

Permalink
fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jtherrmann committed Jan 11, 2025
1 parent 9083a09 commit 6e90f6a
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ warn_unused_ignores = true
warn_unreachable = true
strict_equality = true
check_untyped_defs = true
explicit_package_bases = true
2 changes: 1 addition & 1 deletion src/asf_tools/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def reproject_to_target(raster_info: dict, target_epsg_code: int, target_resolut
return target_raster_info


def make_composite(out_name: str, rasters: list[str], resolution: float = None):
def make_composite(out_name: str, rasters: list[str], resolution: float | None = None):
"""Creates a local-resolution-weighted composite from Sentinel-1 RTC products
Args:
Expand Down
12 changes: 6 additions & 6 deletions src/asf_tools/hydrosar/flood_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
log = logging.getLogger(__name__)


def get_pw_threshold(water_array: np.array) -> float:
def get_pw_threshold(water_array: np.ndarray) -> float:
hist, bin_edges = np.histogram(water_array, density=True, bins=100)
reverse_cdf = np.cumsum(np.flipud(hist)) * (bin_edges[1] - bin_edges[0])
ths_orig = np.flipud(bin_edges)[np.searchsorted(np.array(reverse_cdf), 0.95)]
return round(ths_orig) + 1


def get_waterbody(input_info: dict, threshold: float | None = None) -> np.array:
def get_waterbody(input_info: dict, threshold: float | None = None) -> np.ndarray:
epsg = get_epsg_code(input_info)

west, south, east, north = get_coordinates(input_info)
Expand Down Expand Up @@ -67,9 +67,9 @@ def get_waterbody(input_info: dict, threshold: float | None = None) -> np.array:


def iterative(
hand: np.array,
extent: np.array,
water_levels: np.array = np.arange(15),
hand: np.ndarray,
extent: np.ndarray,
water_levels: np.ndarray = np.arange(15),
minimization_metric: str = 'ts',
):
def get_confusion_matrix(w):
Expand Down Expand Up @@ -344,7 +344,7 @@ def optional_float(value: str) -> float | None:
def _get_cli(interface: Literal['hyp3', 'main']) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)

available_estimators = ['iterative', 'logstat', 'nmad', 'numpy']
available_estimators: list[str | None] = ['iterative', 'logstat', 'nmad', 'numpy']
estimator_help = 'Flood depth estimation approach.'
if interface == 'hyp3':
parser.add_argument('--bucket')
Expand Down
2 changes: 1 addition & 1 deletion src/asf_tools/hydrosar/hand/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def make_copernicus_hand(


def none_or_int(value: str):
if value.lower == 'none':
if value.lower() == 'none':
return None
return int(value)

Expand Down
2 changes: 1 addition & 1 deletion src/asf_tools/hydrosar/threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def expectation_maximization_threshold(tile: np.ndarray, number_of_classes: int
class_means = class_means + minimum - 1
s = image_copy.shape
posterior = np.zeros((s[0], s[1], number_of_classes))
posterior_lookup = dict()
posterior_lookup: dict = dict()
for i in range(0, s[0]):
for j in range(0, s[1]):
pixel_val = image_copy2[i, j]
Expand Down
12 changes: 6 additions & 6 deletions src/asf_tools/hydrosar/water_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def select_hand_tiles(

tile_indexes = np.arange(tiles.shape[0])

tiles = np.ma.masked_greater_equal(tiles, hand_threshold)
percent_valid_pixels = np.sum(~tiles.mask, axis=(1, 2)) / (tiles.shape[1] * tiles.shape[2])
masked_tiles = np.ma.masked_greater_equal(tiles, hand_threshold)
percent_valid_pixels = np.sum(~masked_tiles.mask, axis=(1, 2)) / (masked_tiles.shape[1] * masked_tiles.shape[2])

return tile_indexes[percent_valid_pixels > hand_fraction]

Expand Down Expand Up @@ -100,10 +100,10 @@ def calculate_slope_magnitude(array: np.ndarray, pixel_size) -> np.ndarray:
def determine_membership_limits(
array: np.ndarray, mask_percentile: float = 90.0, std_range: float = 3.0
) -> tuple[float, float]:
array = np.ma.masked_values(array, 0.0)
array = np.ma.masked_greater(array, np.nanpercentile(array.filled(np.nan), mask_percentile))
lower_limit = np.ma.median(array)
upper_limit = lower_limit + std_range * array.std() + 5.0
masked_array = np.ma.masked_values(array, 0.0)
masked_array = np.ma.masked_greater(masked_array, np.nanpercentile(masked_array.filled(np.nan), mask_percentile))
lower_limit = np.ma.median(masked_array)
upper_limit = lower_limit + std_range * masked_array.std() + 5.0
return lower_limit, upper_limit


Expand Down
8 changes: 4 additions & 4 deletions src/asf_tools/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ def read_as_masked_array(raster: str | Path, band: int = 1) -> np.ma.MaskedArray
"""
log.debug(f'Reading raster values from {raster}')
ds = gdal.Open(str(raster))
band = ds.GetRasterBand(band)
data = np.ma.masked_invalid(band.ReadAsArray())
nodata = band.GetNoDataValue()
raster_band = ds.GetRasterBand(band)
data = np.ma.masked_invalid(raster_band.ReadAsArray())
nodata = raster_band.GetNoDataValue()
if nodata is not None:
return np.ma.masked_values(data, nodata)
del ds # How to close w/ gdal
return data


def read_as_array(raster: str, band: int = 1) -> np.array:
def read_as_array(raster: str, band: int = 1) -> np.ndarray:
"""Reads data from a raster image into memory
Args:
Expand Down
4 changes: 3 additions & 1 deletion src/asf_tools/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def tile_array(
array: np.ndarray | np.ma.MaskedArray,
tile_shape: tuple[int, int] = (200, 200),
pad_value: float = None,
pad_value: float | None = None,
) -> np.ndarray | np.ma.MaskedArray:
"""Tile a 2D numpy array
Expand Down Expand Up @@ -49,6 +49,7 @@ def tile_array(
raise ValueError(f'Cannot evenly tile a {array.shape} array into ({tile_rows},{tile_columns}) tiles')

if rpad or cpad:
assert pad_value is not None
padded_array = np.pad(array, ((0, rpad), (0, cpad)), constant_values=pad_value)
if isinstance(array, np.ma.MaskedArray):
mask = np.pad(array.mask, ((0, rpad), (0, cpad)), constant_values=True)
Expand Down Expand Up @@ -127,6 +128,7 @@ def untile_array(
] = tiled_array[ii * untiled_columns + jj, :, :]

if isinstance(tiled_array, np.ma.MaskedArray):
assert len(untiled.shape) == 2
untiled_mask = untile_array(tiled_array.mask, untiled.shape)
untiled = np.ma.MaskedArray(untiled, mask=untiled_mask)

Expand Down
1 change: 1 addition & 0 deletions src/asf_tools/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get_property_values_for_intersecting_features(geometry: ogr.Geometry, featur
for feature in features:
if feature.GetGeometryRef().Intersects(geometry):
return True
return False


def intersecting_feature_properties(geometry: ogr.Geometry, features: Iterator, feature_property: str) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion tests/hydrosar/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def raster_tiles():
tiles_file = Path(__file__).parent / 'data' / 'em_tiles.npz'
tile_data = np.load(tiles_file)
tiles = np.ma.MaskedArray(tile_data['tiles'], mask=tile_data['mask'])
tiles: np.ma.MaskedArray = np.ma.MaskedArray(tile_data['tiles'], mask=tile_data['mask'])
return np.log10(tiles) + 30


Expand Down
2 changes: 1 addition & 1 deletion tests/test_raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_convert_scale():


def test_convert_scale_masked_arrays():
masked_array = np.ma.MaskedArray([-1, 0, 1, 4, 9], mask=[False, False, False, False, False])
masked_array: np.ma.MaskedArray = np.ma.MaskedArray([-1, 0, 1, 4, 9], mask=[False, False, False, False, False])
c = raster.convert_scale(masked_array, 'power', 'db')
assert np.allclose(c.mask, [True, True, False, False, False])
assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_tile_masked_array():
]
)

ma = np.ma.MaskedArray(a, mask=m)
ma: np.ma.MaskedArray = np.ma.MaskedArray(a, mask=m)
tiled = tile.tile_array(ma, tile_shape=(2, 2))

assert tiled.shape == (4, 2, 2)
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_untile_masked_array():
]
)

ma = np.ma.MaskedArray(a, mask=m)
ma: np.ma.MaskedArray = np.ma.MaskedArray(a, mask=m)
untiled = tile.untile_array(tile.tile_array(ma.copy(), tile_shape=(2, 2)), array_shape=a.shape)

assert np.all(ma == untiled)
Expand Down

0 comments on commit 6e90f6a

Please sign in to comment.