Skip to content

Commit

Permalink
Improve usage of SSSOM-py (#666)
Browse files Browse the repository at this point in the history
* Improve SSSOM-py usage

Relies on mapping-commons/sssom-py#440

* Update pyproject.toml

* Update sssom_utils.py

* Update sssom_utils.py

* Make code independent

* Fix typo

* Update lexical_indexer.py

* Update test_sssom_matcher.py

* Update test_sssom_matcher.py

* Update pyproject.toml

* Update lock

* Update lexical_indexer.py

* Update test_sssom_matcher.py

* Update pyproject.toml

* Lock

* Lock again

* Update reqs
  • Loading branch information
cthoyt authored Dec 6, 2023
1 parent 9862ec0 commit 3322bfd
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 207 deletions.
316 changes: 155 additions & 161 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SQLAlchemy = ">=1.4.32"
linkml-runtime = ">=1.5.3"
linkml-renderer = ">=0.3.0"
networkx = ">=2.7.1"
sssom = "<0.4.0"
sssom = "^0.4.0"
ratelimit = ">=2.2.1"
appdirs = ">=1.4.4"
semsql = ">=0.3.1"
Expand Down
4 changes: 1 addition & 3 deletions src/oaklib/interfaces/basic_ontology_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ def set_metamodel_mappings(self, mappings: Union[str, Path, List[Mapping]]) -> N
:param mappings: Mappings for predicates such as rdfs:subClassOf
:return:
"""
if isinstance(mappings, Path):
mappings = str
if isinstance(mappings, str):
if isinstance(mappings, (str, Path)):
msdf = parse_sssom_table(mappings)
msd = to_mapping_set_document(msdf)
mappings = msd.mapping_set.mappings
Expand Down
44 changes: 18 additions & 26 deletions src/oaklib/utilities/lexical/lexical_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
"""
import logging
import re
import typing as t
from collections import defaultdict
from pathlib import Path
from typing import Collection, Dict, List, Optional, Tuple, Union

import curies
from curies import Converter
from linkml_runtime.dumpers import json_dumper, yaml_dumper
from linkml_runtime.loaders import json_loader, yaml_loader
from linkml_runtime.utils.metamodelcore import URIorCURIE
from sssom.constants import LICENSE, MAPPING_SET_ID
from sssom.context import get_default_metadata
from sssom.sssom_document import MappingSetDocument
from sssom.typehints import Metadata
from sssom.util import MappingSetDataFrame, to_mapping_set_dataframe
from sssom_schema import Mapping, MappingSet
from sssom.constants import CURIE_MAP
from sssom.context import ensure_converter
from sssom.util import MappingSetDataFrame
from sssom_schema import Mapping

from oaklib.datamodels.lexical_index import (
LexicalGrouping,
Expand Down Expand Up @@ -244,8 +245,8 @@ def lexical_index_to_sssom(
oi: BasicOntologyInterface,
lexical_index: LexicalIndex,
ruleset: MappingRuleCollection = None,
meta: Metadata = None,
prefix_map: dict = None,
meta: Optional[Dict[str, t.Any]] = None,
prefix_map: Union[None, Converter, t.Mapping[str, str]] = None,
subjects: Collection[CURIE] = None,
objects: Collection[CURIE] = None,
symmetric: bool = False,
Expand Down Expand Up @@ -310,24 +311,15 @@ def lexical_index_to_sssom(
# mappings.append(create_mapping(oi, term, r1, r2))
logging.info("Done creating SSSOM mappings")

if meta is None:
meta = get_default_metadata()
if prefix_map:
meta.prefix_map.update(
{k: v for k, v in prefix_map.items() if k not in meta.prefix_map.keys()}
)
mapping_set_id = meta.metadata[MAPPING_SET_ID]
license = meta.metadata[LICENSE]

mset = MappingSet(mapping_set_id=mapping_set_id, mappings=mappings, license=license)
# doc = MappingSetDocument(prefix_map=oi.prefix_map(), mapping_set=mset)
doc = MappingSetDocument(prefix_map=meta.prefix_map, mapping_set=mset)
msdf = to_mapping_set_dataframe(doc)
num_mappings = len(msdf.df.index)
if ensure_strict_prefixes:
msdf.clean_prefix_map()
if len(msdf.df.index) < num_mappings:
raise ValueError("Mappings included prefixes that were not in the prefix map")
converter = curies.chain(
[
Converter.from_prefix_map((meta or {}).pop(CURIE_MAP, {})),
ensure_converter(prefix_map, use_defaults=False),
oi.converter,
]
)
msdf = MappingSetDataFrame.from_mappings(mappings=mappings, metadata=meta, converter=converter)
msdf.clean_prefix_map(strict=ensure_strict_prefixes)
return msdf


Expand Down
20 changes: 6 additions & 14 deletions src/oaklib/utilities/mapping/sssom_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
from typing import Iterable, List, Optional, Tuple

from linkml_runtime.utils.metamodelcore import URIorCURIE
from sssom.sssom_document import MappingSetDocument
from sssom.util import to_mapping_set_dataframe
from sssom.util import MappingSetDataFrame
from sssom.writers import write_table
from sssom_schema import Mapping, MappingSet
from sssom_schema import Mapping

from oaklib.io.streaming_writer import StreamingWriter
from oaklib.types import CURIE
from oaklib.utilities.basic_utils import get_curie_prefix

UNSPECIFIED_MAPPING_SET_ID = "https://w3id.org/sssom/license/unspecified"


def create_sssom_mapping(
subject_id: CURIE, object_id: CURIE, strict=False, **kwargs
Expand Down Expand Up @@ -74,14 +71,9 @@ def emit(self, obj: Mapping):
self.mappings.append(obj)

def finish(self):
mset = MappingSet(
mapping_set_id="temp", mappings=self.mappings, license=UNSPECIFIED_MAPPING_SET_ID
)
if self.ontology_interface:
prefix_map = self.ontology_interface.prefix_map()
else:
prefix_map = {}
doc = MappingSetDocument(prefix_map=prefix_map, mapping_set=mset)
msdf = to_mapping_set_dataframe(doc)
converter = self.ontology_interface.converter if self.ontology_interface else None
# default metadata, including an auto-generated mapping set URI and
# a license, are automatically added with this function
msdf = MappingSetDataFrame.from_mappings(self.mappings, converter=converter)
msdf.clean_prefix_map(strict=False)
write_table(msdf, self.file)
4 changes: 2 additions & 2 deletions tests/test_utilities/test_sssom_matcher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from pathlib import Path

from sssom.io import get_metadata_and_prefix_map
import yaml
from sssom.parsers import to_mapping_set_document
from sssom.writers import write_table
from sssom_schema import Mapping
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_sssom_with_rules_file(self):
lexical_index = create_lexical_index(oi)
add_labels_from_uris(oi)
ruleset = load_mapping_rules(str(RULES))
metadata = get_metadata_and_prefix_map(MATCHER_META_YML)
metadata = yaml.safe_load(MATCHER_META_YML.read_text())
msdf = lexical_index_to_sssom(self.oi, lexical_index, ruleset=ruleset, meta=metadata)
with open(MATCHER_TEST_SSSOM_OUT, "w", encoding="utf-8") as file:
write_table(msdf, file)
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ deps =
gilda
linkml

[testenv:lock]
skip_install = true
commands = poetry lock
deps = poetry
description = Lock dependencies with poetry

[testenv:lint]
deps =
black
Expand Down

0 comments on commit 3322bfd

Please sign in to comment.