-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Upgrading (non-bijective) prefix maps #99
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,10 @@ | |
"DuplicateValueError", | ||
"DuplicatePrefixes", | ||
"DuplicateURIPrefixes", | ||
# Utilities | ||
"chain", | ||
"upgrade_prefix_map", | ||
# Loaders | ||
"load_extended_prefix_map", | ||
"load_prefix_map", | ||
"load_jsonld_context", | ||
|
@@ -2191,3 +2194,85 @@ def _get_shacl_line(prefix: str, uri_prefix: str, pattern: Optional[str] = None) | |
pattern = pattern.replace("\\", "\\\\") | ||
line += f'; sh:pattern "{pattern}"' | ||
return line + " ]" | ||
|
||
|
||
def upgrade_prefix_map(prefix_map: Mapping[str, str]) -> List[Record]: | ||
"""Convert a (potentially problematic) prefix map (i.e., not bijective) into a list of records. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love how thorough your docstrings are. |
||
|
||
A prefix map is bijective if it has no duplicate CURIE prefixes (i.e., keys in a dictionary) and | ||
no duplicate URI prefixes (i.e., values in a dictionary). Because of the way that dictionaries work in Python, | ||
we are always guaranteed that there are no duplicate keys. | ||
|
||
However, it is both possible and frequent to have duplicate values. This happens because many semantic spaces | ||
have multiple synonymous CURIE prefixes. For example, the `OBO in OWL <https://bioregistry.io/oboinowl>`_ | ||
vocabulary has two common, interchangable prefixes: ``oio`` and ``oboInOwl`` (and the case variant ``oboinowl``). | ||
Therefore, a prefix map might contain the following parts that make it non-bijective: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"oio": "http://www.geneontology.org/formats/oboInOwl#", | ||
"oboInOwl": "http://www.geneontology.org/formats/oboInOwl#" | ||
} | ||
|
||
This is bad because this prefix map can't be used to determinstically compress a URI. For example, should | ||
``http://www.geneontology.org/formats/oboInOwl#hasDbXref`` be compressed to ``oio:hasDbXref`` or | ||
``oboInOwl:hasDbXref``? Neither is necessarily incorrect, but the issue here is that there is not an explicit | ||
choice by the data modeler, meaning that data compressed into CURIEs with this non-bijective map might not be | ||
readily integrable with other datasets. | ||
|
||
The best solution to this situation is not more code, but rather for the data modeler to address the issue | ||
upstream in the following steps: | ||
|
||
1. Choose the which of prefix synonyms is going to be the primary prefix. If you're not sure, the | ||
`Bioregistry <https://bioregistry.io/>`_ is a comprehensive registry of prefixes and their syonyms | ||
applicable in the semantic web and the natural sciences. It gives a good suggestion of what the best | ||
prefix is. In the OBO in OWL case, it suggests ``oboInOwl``. | ||
2. Update all related data artifacts to only use that preferred prefix | ||
3. Either 1) remove the other synonyms (in this example, ``oio``) from the prefix map *or* 2) transition to | ||
using :ref:`epms`, a more modern data structure for supporting URI and CURIE interconversion. | ||
|
||
The first part of step 3 in this solution highlights one of the key shortcomings of prefix maps themselves - | ||
they can't keep track of synonyms, which are often useful in data integration, especially when a single | ||
prefix map is defined on the level of a project or community. The extended prefix map is a simple data structure | ||
proposed to address this. | ||
|
||
* * * | ||
|
||
This function is for people who are not in the position to make the sustainable fix, and want to automate | ||
the assignment of which is the preferred prefix. It uses a deterministic algorithm to choose from two or more | ||
CURIE prefixes that have the same URI prefix and generate an extended prefix map in which they have bene collapsed | ||
into a single record. More specitically, the algorithm is based on a case-sensitive lexical sort of the prefixes. | ||
The first in the sort order becomes the primary prefix and the others become synonyms in the resulting record. | ||
|
||
:param prefix_map: A mapping whose keys represent CURIE prefixes and values represent URI prefixes | ||
:return: A list of :class:`curies.Record` objects that together constitute an extended prefix map | ||
|
||
>>> from curies import Converter, upgrade_prefix_map | ||
>>> pm = {"a": "https://example.com/a/", "b": "https://example.com/a/"} | ||
>>> records = upgrade_prefix_map(pm) | ||
>>> converter = Converter(records) | ||
>>> converter.expand("a:1") | ||
'https://example.com/a/1' | ||
>>> converter.expand("b:1") | ||
'https://example.com/a/1' | ||
>>> converter.compress("https://example.com/a/1") | ||
'a:1' | ||
|
||
.. note:: | ||
|
||
Thanks to `Joe Flack <https://github.com/joeflack4>`_ for proposing this algorithm | ||
`in this discussion <https://github.com/mapping-commons/sssom-py/pull/485#discussion_r1451812733>`_. | ||
|
||
""" | ||
uri_prefix_to_curie_synonyms = defaultdict(list) | ||
for curie_prefix, uri_prefix in prefix_map.items(): | ||
uri_prefix_to_curie_synonyms[uri_prefix].append(curie_prefix) | ||
priority_prefix_map = { | ||
uri_prefix: sorted(curie_prefixes) | ||
for uri_prefix, curie_prefixes in uri_prefix_to_curie_synonyms.items() | ||
} | ||
return [ | ||
Record(prefix=prefix, prefix_synonyms=prefix_synonyms, uri_prefix=uri_prefix) | ||
for uri_prefix, (prefix, *prefix_synonyms) in sorted(priority_prefix_map.items()) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I'm sure your test passes as well, but I ran it locally on my own and worked for me as well.