update type hints

This commit is contained in:
Jan Petykiewicz 2024-07-18 00:30:18 -07:00
parent 0a81c97af1
commit 6e32eda1c7
2 changed files with 31 additions and 24 deletions

View File

@ -1,13 +1,14 @@
from typing import Tuple, Optional from numpy.typing import ArrayLike, NDArray
import numpy # type: ignore import numpy
from numpy import logical_and, diff, floor, ceil, ones, zeros, hstack, full_like, newaxis from numpy import logical_and, diff, floor, ceil, ones, zeros, hstack, full_like, newaxis
from scipy import sparse # type: ignore from scipy import sparse
def raster(vertices: numpy.ndarray, def raster(
grid_x: numpy.ndarray, vertices: ArrayLike,
grid_y: numpy.ndarray grid_x: ArrayLike,
) -> numpy.ndarray: grid_y: ArrayLike,
) -> NDArray[numpy.float64]:
""" """
Draws a polygon onto a 2D grid of pixels, setting pixel values equal to the fraction of the Draws a polygon onto a 2D grid of pixels, setting pixel values equal to the fraction of the
pixel area covered by the polygon. This implementation is written for accuracy and works with pixel area covered by the polygon. This implementation is written for accuracy and works with
@ -49,10 +50,10 @@ def raster(vertices: numpy.ndarray,
def find_intersections( def find_intersections(
vertices: numpy.ndarray, vertices: NDArray[numpy.float_],
grid_x: numpy.ndarray, grid_x: NDArray[numpy.float_],
grid_y: numpy.ndarray grid_y: NDArray[numpy.float_],
) -> Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]: ) -> tuple[NDArray[numpy.float64], NDArray[numpy.float64], NDArray[numpy.float64]]:
""" """
Find intersections between a polygon and grid lines Find intersections between a polygon and grid lines
""" """
@ -126,10 +127,10 @@ def find_intersections(
def create_vertices( def create_vertices(
vertices: numpy.ndarray, vertices: NDArray[numpy.float_],
grid_x: numpy.ndarray, grid_x: NDArray[numpy.float_],
grid_y: numpy.ndarray, grid_y: NDArray[numpy.float_],
new_vertex_data: Optional[Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]] = None new_vertex_data: tuple[NDArray[numpy.float64], NDArray[numpy.float64], NDArray[numpy.float64]] | None = None
) -> sparse.coo_matrix: ) -> sparse.coo_matrix:
""" """
Create additional vertices where a polygon crosses gridlines Create additional vertices where a polygon crosses gridlines
@ -176,12 +177,12 @@ def create_vertices(
def clip_vertices_to_window( def clip_vertices_to_window(
vertices: numpy.ndarray, vertices: NDArray[numpy.float64],
min_x: float = -numpy.inf, min_x: float = -numpy.inf,
max_x: float = numpy.inf, max_x: float = numpy.inf,
min_y: float = -numpy.inf, min_y: float = -numpy.inf,
max_y: float = numpy.inf max_y: float = numpy.inf
) -> numpy.ndarray: ) -> NDArray[numpy.float64]:
""" """
""" """
# Remove points outside the window (these will only be original points) # Remove points outside the window (these will only be original points)
@ -200,9 +201,9 @@ def clip_vertices_to_window(
def get_raster_parts( def get_raster_parts(
vertices: numpy.ndarray, vertices: ArrayLike,
grid_x: numpy.ndarray, grid_x: ArrayLike,
grid_y: numpy.ndarray grid_y: ArrayLike,
) -> sparse.coo_matrix: ) -> sparse.coo_matrix:
""" """
This function performs the same task as `raster(...)`, but instead of returning a dense array This function performs the same task as `raster(...)`, but instead of returning a dense array
@ -229,14 +230,20 @@ def get_raster_parts(
Returns: Returns:
Complex sparse COO matrix containing area and cover information Complex sparse COO matrix containing area and cover information
""" """
vertices = numpy.array(vertices)
grid_x = numpy.array(grid_x)
grid_y = numpy.array(grid_y)
if grid_x.size < 2 or grid_y.size < 2: if grid_x.size < 2 or grid_y.size < 2:
raise Exception('Grid must contain at least one full pixel') raise Exception('Grid must contain at least one full pixel')
num_xy_px = numpy.array([grid_x.size, grid_y.size]) - 1 num_xy_px = numpy.array([grid_x.size, grid_y.size]) - 1
vertices = clip_vertices_to_window(vertices, vertices = clip_vertices_to_window(
vertices,
grid_x[0], grid_x[-1], grid_x[0], grid_x[-1],
grid_y[0], grid_y[-1]) grid_y[0], grid_y[-1],
)
# If the shape fell completely outside our area, just return a blank grid # If the shape fell completely outside our area, just return a blank grid
if vertices.size == 0: if vertices.size == 0:

View File

@ -30,7 +30,7 @@ classifiers = [
requires-python = ">=3.8" requires-python = ">=3.8"
dynamic = ["version"] dynamic = ["version"]
dependencies = [ dependencies = [
"numpy~=1.21", "numpy~=1.26",
"scipy", "scipy",
] ]