Skip to content

Commit

Permalink
removed testing of converting the java lists to python list, should b…
Browse files Browse the repository at this point in the history
…e done in gp2 project codes
  • Loading branch information
roopehub committed Aug 23, 2024
1 parent eae40a7 commit 993a264
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 47 deletions.
15 changes: 1 addition & 14 deletions src/r5py/r5/travel_time_matrix_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
"""Calculate travel times between many origins and destinations."""

import copy
import numpy as np

import pandas

from r5py.util.custom_cost_conversions import convert_java_lists_to_python_in_batches

from .base_travel_time_matrix_computer import BaseTravelTimeMatrixComputer
from ..util import start_jvm
Expand Down Expand Up @@ -110,17 +107,7 @@ def _parse_results(self, from_id, results):
# add OSM IDs if found in results
# osmIdsResults are generated when routing with custom_cost_transport_network
if hasattr(results, "osmIdResults") and results.osmIdResults:
# use batching for larger sest to avoid memory issues
# only use batchinkg for large lists
if len(results.osmIdResults) > 1_000:
od_matrix["osm_ids"] = convert_java_lists_to_python_in_batches(
results.osmIdResults
)
else:
osm_ids_python = [
np.array(sublist).tolist() for sublist in results.osmIdResults
]
od_matrix["osm_ids"] = osm_ids_python
od_matrix["osm_ids"] = results.osmIdResults

# R5’s NULL value is MAX_INT32
od_matrix = self._fill_nulls(od_matrix)
Expand Down
33 changes: 0 additions & 33 deletions src/r5py/util/custom_cost_conversions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import os
import math
import concurrent.futures
import numpy as np

import jpype
import jpype.imports

Expand Down Expand Up @@ -146,31 +141,3 @@ def convert_python_custom_costs_to_java_custom_costs(
raise CustomCostConversionError(
"Failed to convert from python to java for custom cost factors. custom_cost_segment_weight_factors must be provided for custom cost transport network"
) from e


def get_dynamic_batch_size(total_paths, min_batch_size=500, max_batch_size=10_000):
if total_paths <= 10_000:
# For small datasets, use a higher batch size
return min(total_paths // 2, max_batch_size)
else:
# For larger datasets, scale batch size logarithmically
batch_size = min_batch_size * math.log10(total_paths / 10_000)
# Ensure the batch size is within the desired range
return int(max(min(batch_size, max_batch_size), min_batch_size))


def convert_sublist_to_python(sublist):
return np.array(sublist).tolist()


def convert_java_lists_to_python_in_batches(java_lists):
# Get the number of available CPU cores
batch_size = get_dynamic_batch_size(len(java_lists))
max_workers = os.cpu_count()
python_lists = []
for i in range(0, len(java_lists), batch_size):
batch = java_lists[i : i + batch_size]
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
# Convert each batch in parallel using threading
python_lists.extend(executor.map(convert_sublist_to_python, batch))
return python_lists

0 comments on commit 993a264

Please sign in to comment.