Skip to content

Commit

Permalink
changed @context backto what it was
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd committed Dec 14, 2023
1 parent 2eac05b commit 4c480a5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/sssom/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@
OTHER = "other"
COMMENT = "comment"

JSON_CONTEXT_KEY = "@context"

CURIE_MAP = "curie_map"
SUBJECT_SOURCE_ID = "subject_source_id"
OBJECT_SOURCE_ID = "object_source_id"
Expand Down
4 changes: 2 additions & 2 deletions src/sssom/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from curies import Converter
from rdflib.namespace import is_ncname

from .constants import EXTENDED_PREFIX_MAP, JSON_CONTEXT_KEY
from .constants import EXTENDED_PREFIX_MAP

__all__ = [
"SSSOM_BUILT_IN_PREFIXES",
Expand Down Expand Up @@ -53,7 +53,7 @@ def _get_built_in_prefix_map() -> Converter:
context = _load_sssom_context()
prefix_map = {
prefix: uri_prefix
for prefix, uri_prefix in context[JSON_CONTEXT_KEY].items()
for prefix, uri_prefix in context["@context"].items()
if prefix in SSSOM_BUILT_IN_PREFIXES and isinstance(uri_prefix, str)
}
return Converter.from_prefix_map(prefix_map)
Expand Down
15 changes: 3 additions & 12 deletions src/sssom/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
CONFIDENCE,
CURIE_MAP,
DEFAULT_MAPPING_PROPERTIES,
JSON_CONTEXT_KEY,
LICENSE,
MAPPING_JUSTIFICATION,
MAPPING_JUSTIFICATION_UNSPECIFIED,
Expand Down Expand Up @@ -257,7 +256,6 @@ def parse_sssom_json(

with open(file_path) as json_file:
jsondoc = json.load(json_file)
context_from_jsondoc = jsondoc.get(JSON_CONTEXT_KEY)

# Initialize meta if it's None
if meta is None:
Expand All @@ -267,16 +265,9 @@ def parse_sssom_json(
meta_keys_to_update = [MAPPING_SET_ID, LICENSE]
meta.update({key: jsondoc[key] for key in meta_keys_to_update if key in jsondoc})

# Update CURIE_MAP if context_from_jsondoc exists
if context_from_jsondoc:
prefixes_from_json = {
key: value
for key, value in context_from_jsondoc.items()
if isinstance(value, str) and not key.startswith("@")
}
meta[CURIE_MAP] = prefixes_from_json

converter, meta = _get_prefix_map_and_metadata(prefix_map=prefix_map, meta=meta)
converter_from_jsonld = Converter.from_jsonld(file_path)
converter_via_metadata, meta = _get_prefix_map_and_metadata(prefix_map=prefix_map, meta=meta)
converter = curies.chain([converter_from_jsonld, converter_via_metadata])

msdf = from_sssom_json(jsondoc=jsondoc, prefix_map=converter, meta=meta)
return msdf
Expand Down
6 changes: 3 additions & 3 deletions src/sssom/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from sssom.validators import check_all_prefixes_in_curie_map

from .constants import CURIE_MAP, JSON_CONTEXT_KEY, SCHEMA_YAML, SSSOM_URI_PREFIX
from .constants import CURIE_MAP, SCHEMA_YAML, SSSOM_URI_PREFIX
from .context import _load_sssom_context
from .parsers import to_mapping_set_document
from .util import (
Expand Down Expand Up @@ -439,12 +439,12 @@ def _update_sssom_context_with_prefixmap(converter: Converter):
"""Prepare a JSON-LD context and dump to a string."""
context = _load_sssom_context()
for k, v in converter.bimap.items():
if k in context[JSON_CONTEXT_KEY] and context[JSON_CONTEXT_KEY][k] != v:
if k in context["@context"] and context["@context"][k] != v:
logging.info(
f"{k} namespace is already in the context, ({context['@context'][k]}, "
f"but with a different value than {v}. Overwriting!"
)
context[JSON_CONTEXT_KEY][k] = v
context["@context"][k] = v
return context


Expand Down
15 changes: 7 additions & 8 deletions tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sssom import MappingSetDataFrame
from sssom.constants import (
CREATOR_ID,
JSON_CONTEXT_KEY,
OBJECT_ID,
OBJECT_LABEL,
PREDICATE_ID,
Expand Down Expand Up @@ -109,9 +108,9 @@ def test_write_sssom_json_context(self):
msdf = MappingSetDataFrame(df)
msdf.clean_prefix_map()
json_object = to_json(msdf)
self.assertIn(JSON_CONTEXT_KEY, json_object)
self.assertIn("DOID", json_object[JSON_CONTEXT_KEY])
self.assertIn("mapping_set_id", json_object[JSON_CONTEXT_KEY])
self.assertIn("@context", json_object)
self.assertIn("DOID", json_object["@context"])
self.assertIn("mapping_set_id", json_object["@context"])

def test_update_sssom_context_with_prefixmap(self):
"""Test when writing to JSON, the context is correctly written as well."""
Expand All @@ -124,10 +123,10 @@ def test_update_sssom_context_with_prefixmap(self):
]
converter = Converter.from_extended_prefix_map(records)
context = _update_sssom_context_with_prefixmap(converter)
self.assertIn(JSON_CONTEXT_KEY, context)
self.assertIn("SCTID", context[JSON_CONTEXT_KEY])
self.assertNotIn("snomed", context[JSON_CONTEXT_KEY])
self.assertIn("mapping_set_id", context[JSON_CONTEXT_KEY])
self.assertIn("@context", context)
self.assertIn("SCTID", context["@context"])
self.assertNotIn("snomed", context["@context"])
self.assertIn("mapping_set_id", context["@context"])

def test_write_sssom_fhir(self):
"""Test writing as FHIR ConceptMap JSON."""
Expand Down

0 comments on commit 4c480a5

Please sign in to comment.