Skip to content

Commit 9ed15a2

Browse files
authored
more explicit data validation and type hints (#214)
* more explicit data validation and type hints * add test data
1 parent 7d73c32 commit 9ed15a2

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests/test_data_temp/Test8A_dataset_2010.zip filter=lfs diff=lfs merge=lfs -text
2+
tests/test_data_temp/Test8B_dataset_2010.zip filter=lfs diff=lfs merge=lfs -text
3+
tests/test_data_temp/elev_lid792_1m.gtiff filter=lfs diff=lfs merge=lfs -text

src/itzi/providers/domain_data.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
GNU General Public License for more details.
1313
"""
1414

15-
from typing import Tuple, Mapping
16-
1715
import numpy as np
1816

1917

@@ -30,22 +28,22 @@ def __init__(
3028
cols: int,
3129
crs_wkt: str,
3230
):
33-
self.north = north
34-
self.south = south
35-
self.east = east
36-
self.west = west
37-
self.rows = rows
38-
self.cols = cols
39-
self.crs_wkt = crs_wkt
31+
self.north = float(north)
32+
self.south = float(south)
33+
self.east = float(east)
34+
self.west = float(west)
35+
self.rows = int(rows)
36+
self.cols = int(cols)
37+
self.crs_wkt = str(crs_wkt)
4038

4139
if self.north < self.south:
4240
raise ValueError(f"north must be superior to south. {self.north=}, {self.south=}")
4341
if self.east < self.west:
4442
raise ValueError(f"east must be superior to west. {self.east=}, {self.west=}")
4543

46-
self.nsres = (self.north - self.south) / self.rows
47-
self.ewres = (self.east - self.west) / self.cols
48-
self.cell_area = self.ewres * self.nsres
44+
self.nsres = float((self.north - self.south) / self.rows)
45+
self.ewres = float((self.east - self.west) / self.cols)
46+
self.cell_area = float(self.ewres * self.nsres)
4947
self.cell_shape = (self.ewres, self.nsres)
5048
self.shape = (self.rows, self.cols)
5149
self.cells = self.rows * self.cols
@@ -54,38 +52,42 @@ def is_in_domain(self, *, x: float, y: float) -> bool:
5452
"""For a given coordinate pair(x, y),
5553
return True is inside the domain, False otherwise.
5654
"""
57-
bool_x = self.west < x < self.east
58-
bool_y = self.south < y < self.north
55+
bool_x: bool = self.west < x < self.east
56+
bool_y: bool = self.south < y < self.north
5957
return bool(bool_x and bool_y)
6058

61-
def coordinates_to_pixel(self, *, x: float, y: float) -> Tuple[float, float] | None:
59+
def coordinates_to_pixel(self, *, x: float, y: float) -> tuple[float, float] | None:
6260
"""For a given coordinate pair(x, y),
6361
return the corresponding row and column indices.
6462
"""
6563
if not self.is_in_domain(x=x, y=y):
6664
return None
6765
else:
68-
norm_row = (y - self.south) / (self.north - self.south)
66+
norm_row = float((y - self.south) / (self.north - self.south))
6967
row = int(np.round((1 - norm_row) * (self.rows - 1)))
7068

71-
norm_col = (x - self.west) / (self.east - self.west)
69+
norm_col = float((x - self.west) / (self.east - self.west))
7270
col = int(np.round(norm_col * (self.cols - 1)))
7371

7472
return (row, col)
7573

76-
def get_coordinates(self) -> Mapping[str, np.ndarray]:
74+
def get_coordinates(self) -> dict[str, np.ndarray]:
7775
"""Return x and y coordinates as numpy arrays representing the center of each cell.
7876
Returns:
7977
A mapping with 'x' and 'y' keys containing 1D numpy arrays.
8078
- 'x': array of shape (cols,) with x-coordinates of cell centers
8179
- 'y': array of shape (rows,) with y-coordinates of cell centers
8280
"""
8381
# X coordinates: from west + half cell width, incrementing by cell width
84-
x_coords = np.linspace(self.west + self.ewres / 2, self.east - self.ewres / 2, self.cols)
82+
x_start: float = self.west + self.ewres / 2
83+
x_stop: float = self.east - self.ewres / 2
84+
x_coords: np.ndarray = np.linspace(x_start, x_stop, num=self.cols)
8585

8686
# Y coordinates: from north - half cell height, decrementing by cell height
8787
# (raster coordinates typically go from north to south)
88-
y_coords = np.linspace(self.north - self.nsres / 2, self.south + self.nsres / 2, self.rows)
88+
y_start: float = self.north - self.nsres / 2
89+
y_stop: float = self.south + self.nsres / 2
90+
y_coords: np.ndarray = np.linspace(y_start, y_stop, num=self.rows)
8991

9092
return {"x": x_coords, "y": y_coords}
9193

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:7f3555b354f174d361034d2dd5986ca0de24d95b76ab0c95384c4ed5377c1380
3+
size 2342624
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:83f521d681ed59335459b2bf8c60067ac434675322fdf8bc5ba52e2afaaadb09
3+
size 2490890
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:f81e3fa70a447c9fc6e124af2ac1433abd99fbfc22388c5218d8a25d73ff3586
3+
size 2103366

0 commit comments

Comments
 (0)