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

Don't send so many synonyms #296

Merged
merged 4 commits into from
Sep 28, 2021
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
58 changes: 30 additions & 28 deletions strider/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ async def fetch(
self,
kp_id: str,
request: dict,
input_prefixes: dict = None,
output_prefixes: dict = None,
):
"""Wrap fetch with CURIE mapping(s)."""
request = remove_null_values(request)
Expand Down Expand Up @@ -242,44 +240,48 @@ def map_curie(
data: dict[str, Entity],
prefixes: dict[str, list[str]],
logger: logging.Logger = None,
):
"""Map single CURIE."""
) -> str:
"""Map a single CURIE to the list of preferred equivalent CURIES.

1. Find the most-preferred prefix for which the provided CURIE has synonyms.
2. Return all synonymous CURIEs that have that prefix.
"""
try:
categories, identifiers = data[curie]
except KeyError:
return [curie]
prefixes = {
# Gather the preferred prefixes for each category, deduplicating while retaining order
prefixes = list(dict.fromkeys(
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this just to deduplicate? Maybe switch to set() if that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we need to deduplicate but also retain the order, which is why I've done this rather than using set().

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha. Can we document this in a comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

prefix
for category in categories
for prefix in prefixes.get(category, [])
}
))
if not prefixes:
# no preferred prefixes for these categories
# There are no preferred prefixes for these categories - use the prefixes that Biolink prefers
logger.debug(
"[{}] Cannot not find preferred prefixes for at least one of: {}".format(
getattr(logger, "context", ""),
categories,
)
)
return identifiers
prefixes = identifiers[0].split(":")[0]

# Find CURIEs beginning with any of prefixes
prefix_identifiers = [
curie
for curie in identifiers
if any(
curie.startswith(prefix)
for prefix in prefixes
)
]
if not prefix_identifiers:
# no preferred curie with these prefixes
logger.debug(
"[{}] Cannot find identifier in {} with a preferred prefix in {}".format(
getattr(logger, "context", ""),
identifiers,
prefixes,
),
)
return [curie]
return prefix_identifiers
# Find CURIEs beginning with the most-preferred prefix
for prefix in prefixes:
curies = [
_curie
for _curie in identifiers
if _curie.startswith(prefix)
]
if curies:
Copy link
Contributor

Choose a reason for hiding this comment

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

So this returns curies for the first prefix where curies exist, instead of all curies for all prefixes? If I'm understanding it right, can we document this behavior explicitly in a docstring or comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

return curies

# There is no equivalent CURIE with any of the acceptable prefixes - return the original CURIE
logger.debug(
"[{}] Cannot find identifier in {} with a preferred prefix in {}".format(
getattr(logger, "context", ""),
identifiers,
prefixes,
),
)
return [curie]
2 changes: 0 additions & 2 deletions strider/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,6 @@ async def setup(
details,
self.portal,
kp_id,
self.kp_preferred_prefixes[kp_id],
self.preferred_prefixes,
)
for kp_id, details in kps.items()
}
6 changes: 1 addition & 5 deletions strider/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,18 @@ async def post_json(url, request, logger, log_name):

class KnowledgeProvider():
"""Knowledge provider."""
def __init__(self, details, portal, id, in_prefixes, out_prefixes, *args, **kwargs):
def __init__(self, details, portal, id, *args, **kwargs):
"""Initialize."""
self.details = details
self.portal = portal
# self.portal: KnowledgePortal = portal
self.id = id
self.in_prefixes = in_prefixes
self.out_prefixes = out_prefixes

async def solve_onehop(self, request):
"""Solve one-hop query."""
return await self.portal.fetch(
self.id,
{"message": {"query_graph": request}},
self.in_prefixes,
self.out_prefixes,
)


Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def generate_kps(qty):
)
)

return {str(i): kp for i, kp in enumerate(kp_generator) if i < qty}
return {str(i): kp for i, kp in zip(range(qty), kp_generator)}


def query_graph_from_string(s):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ async def test_fetch():
response = await portal.fetch(
kp_id="ctd",
request={"message": {"query_graph": query_graph}},
input_prefixes=CTD_PREFIXES,
output_prefixes=preferred_prefixes,
)

allowed_response_prefixes = [
Expand Down