Skip to content

Commit

Permalink
Fix/recursion limit (#208)
Browse files Browse the repository at this point in the history
* dynamically handled recursion limit

* removed redundant qupath files

* rectified chunksize error

* added return type in function which sets recursionlimit

* yield after setting the recursionlimit

* removed trailing whitespace
  • Loading branch information
swaradgat19 authored Dec 6, 2023
1 parent eb59563 commit 2e337ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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)

0 comments on commit 2e337ec

Please sign in to comment.