Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/recursion limit #208

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions wsinfer/patchlib/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@
from shapely import Point
from shapely import Polygon
from shapely import STRtree
from contextlib import contextmanager
import sys
from typing import Iterator

logger = logging.getLogger(__name__)


@contextmanager
def temporary_recursion_limit(limit: int) -> Iterator[None]:
old_limit = sys.getrecursionlimit()
try:
sys.setrecursionlimit(limit)
yield
finally:
sys.setrecursionlimit(old_limit)


def get_multipolygon_from_binary_arr(
arr: npt.NDArray[np.int_], scale: tuple[float, float] | None = None
) -> tuple[MultiPolygon, Sequence[npt.NDArray[np.int_]], npt.NDArray[np.int_]]:
Expand Down Expand Up @@ -96,8 +109,10 @@ def merge_polygons(polygon: MultiPolygon, idx: int, add: bool) -> MultiPolygon:

return polygon

# Call the function with an initial empty polygon and start from contour 0
polygon = merge_polygons(MultiPolygon(), 0, True)
temp_limit = max(sys.getrecursionlimit(), len(contours))
with temporary_recursion_limit(temp_limit):
# Call the function with an initial empty polygon and start from contour 0
polygon = merge_polygons(MultiPolygon(), 0, True)

# Add back the axis in hierarchy because we squeezed it before.
return polygon, contours_unscaled, hierarchy[np.newaxis]
Expand Down
2 changes: 1 addition & 1 deletion wsinfer/write_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ def write_geojsons(csvs: list[Path], results_dir: Path, num_workers: int) -> Non
output.mkdir(parents=True, exist_ok=True)

func = partial(make_geojson, results_dir=results_dir)
process_map(func, csvs, max_workers=num_workers)
process_map(func, csvs, max_workers=num_workers, chunksize=1)