Skip to content

Commit a0d215b

Browse files
authored
Fix mapping set inversion (#555)
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.
1 parent 40b6fc5 commit a0d215b

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/sssom/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ def invert_mappings(
14641464
inverted_df = df_to_invert.rename(
14651465
columns=_invert_column_names(list_of_subject_object_columns, columns_invert_map)
14661466
)
1467-
inverted_df = inverted_df[df.columns]
1467+
inverted_df = sort_df_rows_columns(inverted_df, by_rows=False)
14681468
inverted_df[PREDICATE_ID] = inverted_df[PREDICATE_ID].map(predicate_invert_map)
14691469
if update_justification:
14701470
inverted_df[MAPPING_JUSTIFICATION] = SEMAPV.MappingInversion.value

tests/data/asymmetric.tsv

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#curie_map:
2+
# orcid: https://orcid.org/
3+
# x: http://example.org/x/
4+
# y: http://example.org/y/
5+
# z: http://example.org/z/
6+
#mapping_set_id: https://w3id.org/sssom/mapping/tests/data/asymmetric.tsv
7+
#creator_id:
8+
# - orcid:1234
9+
# - orcid:5678
10+
#license: https://creativecommons.org/publicdomain/zero/1.0/
11+
#mapping_date: 2020-05-30
12+
subject_id subject_label predicate_id object_id mapping_justification object_source confidence
13+
x:appendage appendage owl:equivalentClass y:appendage semapv:ManualMappingCuration y:example 0.841
14+
x:appendage appendage owl:equivalentClass z:appendage semapv:LexicalMatching z:example 0.882
15+
x:appendage appendage owl:equivalentClass z:appendage semapv:ManualMappingCuration z:example 0.841
16+
x:bone_element bone element owl:equivalentClass y:bone semapv:LexicalMatching y:example 0.739
17+
x:bone_element bone element owl:equivalentClass y:bone semapv:ManualMappingCuration y:example 0.535

tests/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ def test_invert_nodes_without_prefix(self):
175175
inverted_df = invert_mappings(df=self.msdf2.df, merge_inverted=False)
176176
self.assertEqual(len(inverted_df), len(self.msdf2.df.drop_duplicates()))
177177

178+
def test_invert_asymmetric_nodes(self):
179+
"""Test inverting sets containing imbalanced subject/object columns."""
180+
msdf = parse_sssom_table(f"{data_dir}/asymmetric.tsv")
181+
inverted_df = invert_mappings(msdf.df, merge_inverted=False)
182+
self.assertEqual(len(inverted_df), len(msdf.df))
183+
original_subject_labels = msdf.df["subject_label"].values
184+
inverted_object_labels = inverted_df["object_label"].values
185+
self.assertNotIn(False, original_subject_labels == inverted_object_labels)
186+
178187
def test_inject_metadata_into_df(self):
179188
"""Test injecting metadata into DataFrame is as expected."""
180189
expected_creators = "orcid:0000-0001-5839-2535|orcid:0000-0001-5839-2532"

0 commit comments

Comments
 (0)