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

209 run in batches #228

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from 5 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
51 changes: 50 additions & 1 deletion python/nrel/routee/compass/compass_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging

from pathlib import Path
from typing import Any, Dict, List, Optional, Union
from typing import Any, Dict, List, Optional, Union, TextIO, Generator
from nrel.routee.compass.routee_compass_py import (
CompassAppWrapper,
)
Expand Down Expand Up @@ -135,6 +135,55 @@ def run(
return results[0]
return results

def run_in_batches(
self, fp: TextIO, batch_size: int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing a file pointer, let's instead pass a Path or string and then create the file pointer in this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha will update

) -> Generator[List[Result], None, None]:
"""
Reads in upto batch_size of queries from a query file before
calling run

Args:

fp: File pointer to where the queries are stored

batch_size: Max number of queries to read in from the json
file.

Returns:
A list of results (or a single result if a single query was read in)

Example:
>>> from nrel.routee.compass import CompassApp
>>> app = CompassApp.from_config_file("config.toml")
>>> result = app.run_in_batches("query.json", 10)
"""
queries = []
query_str = ""
for line in fp:
if len(queries) >= batch_size:
yield self.run(queries)
queries = []

# attempt to read a line and see if we can eval it
line = line.strip()
query_str += line
if query_str[0] == "[":
query_str = query_str[1:]
if query_str == "":
continue
try:
if query_str[-1] in [",", "]"]:
query = eval(query_str[:-1])
else:
query = eval(query_str)
except SyntaxError:
continue

queries.append(query)
query_str = ""
if queries:
yield self.run(queries)

def graph_edge_origin(self, edge_id: int) -> int:
"""
get the origin vertex id for some edge
Expand Down
Loading