Skip to content

Commit

Permalink
Fix mapping set inversion (#555)
Browse files Browse the repository at this point in the history
This is a putative fix to #554.

It implements the fix suggested in [this
comment](#554 (comment)),
and adds a test case to check that inverting a set containing
“imbalanced” columns (e.g., with a `subject_label` column but without a
`object_label` column) does not trigger the #554 bug.
  • Loading branch information
gouttegd authored Oct 24, 2024
1 parent 40b6fc5 commit a0d215b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sssom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,7 @@ def invert_mappings(
inverted_df = df_to_invert.rename(
columns=_invert_column_names(list_of_subject_object_columns, columns_invert_map)
)
inverted_df = inverted_df[df.columns]
inverted_df = sort_df_rows_columns(inverted_df, by_rows=False)
inverted_df[PREDICATE_ID] = inverted_df[PREDICATE_ID].map(predicate_invert_map)
if update_justification:
inverted_df[MAPPING_JUSTIFICATION] = SEMAPV.MappingInversion.value
Expand Down
17 changes: 17 additions & 0 deletions tests/data/asymmetric.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#curie_map:
# orcid: https://orcid.org/
# x: http://example.org/x/
# y: http://example.org/y/
# z: http://example.org/z/
#mapping_set_id: https://w3id.org/sssom/mapping/tests/data/asymmetric.tsv
#creator_id:
# - orcid:1234
# - orcid:5678
#license: https://creativecommons.org/publicdomain/zero/1.0/
#mapping_date: 2020-05-30
subject_id subject_label predicate_id object_id mapping_justification object_source confidence
x:appendage appendage owl:equivalentClass y:appendage semapv:ManualMappingCuration y:example 0.841
x:appendage appendage owl:equivalentClass z:appendage semapv:LexicalMatching z:example 0.882
x:appendage appendage owl:equivalentClass z:appendage semapv:ManualMappingCuration z:example 0.841
x:bone_element bone element owl:equivalentClass y:bone semapv:LexicalMatching y:example 0.739
x:bone_element bone element owl:equivalentClass y:bone semapv:ManualMappingCuration y:example 0.535
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ def test_invert_nodes_without_prefix(self):
inverted_df = invert_mappings(df=self.msdf2.df, merge_inverted=False)
self.assertEqual(len(inverted_df), len(self.msdf2.df.drop_duplicates()))

def test_invert_asymmetric_nodes(self):
"""Test inverting sets containing imbalanced subject/object columns."""
msdf = parse_sssom_table(f"{data_dir}/asymmetric.tsv")
inverted_df = invert_mappings(msdf.df, merge_inverted=False)
self.assertEqual(len(inverted_df), len(msdf.df))
original_subject_labels = msdf.df["subject_label"].values
inverted_object_labels = inverted_df["object_label"].values
self.assertNotIn(False, original_subject_labels == inverted_object_labels)

def test_inject_metadata_into_df(self):
"""Test injecting metadata into DataFrame is as expected."""
expected_creators = "orcid:0000-0001-5839-2535|orcid:0000-0001-5839-2532"
Expand Down

0 comments on commit a0d215b

Please sign in to comment.