Skip to content

Commit

Permalink
chunk_size as optional arg
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-fred committed Sep 13, 2023
1 parent 2ecd69b commit 839d1cf
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/snkit/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,30 @@ def _split_edges_at_nodes(
return split_edges


def split_edges_at_nodes(network, tolerance=1e-9):
def split_edges_at_nodes(network: Network, tolerance: float = 1e-9, chunk_size: int | None = None):
"""
Split network edges where they intersect node geometries. Will operate in
parallel if SNKIT_PROCESSES is in the environment and a positive integer.
Split network edges where they intersect node geometries.
N.B. Can operate in parallel if SNKIT_PROCESSES is in the environment and a
positive integer.
Args:
network: Network object to split edges for.
tolerance: Proximity within which nodes are said to intersect an edge.
chunk_size: When splitting in parallel, set the number of edges per
unit of work.
Returns:
Network with edges split at nodes (within proximity tolerance).
"""
split_edges = []

n = len(network.edges)
if PARALLEL_PROCESS_COUNT and (n > 1_000):
chunk_size = max([1, int(n / PARALLEL_PROCESS_COUNT)])
if PARALLEL_PROCESS_COUNT > 1:
if chunk_size is None:
chunk_size = max([1, int(n / PARALLEL_PROCESS_COUNT)])
args = [
(network.edges.iloc[i : i + chunk_size, :], network.nodes, tolerance)
(network.edges.iloc[i: i + chunk_size, :], network.nodes, tolerance)
for i in range(0, n, chunk_size)
]
with multiprocessing.Pool(PARALLEL_PROCESS_COUNT) as pool:
Expand Down

0 comments on commit 839d1cf

Please sign in to comment.