Skip to content

Commit

Permalink
Add update-justifcation flag to invert method (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
matentzn committed Mar 22, 2024
1 parent f1df5a6 commit 5db5296
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,19 @@ def remove(input: str, output: TextIO, remove_map: str):
is_flag=True,
help="If True (default), add inverted mappings to the input mapping set, else, just return inverted mappings as a separate mapping set.",
)
@click.option(
"--update-justification/--no-update-justification",
default=True,
is_flag=True,
help="If True (default), the justification is updated to 'sempav:MappingInversion', else it is left as it is.",
)
@click.option("--inverse-map", help="Path to file that contains the inverse predicate dictionary.")
def invert(
input: str,
output: TextIO,
subject_prefix: Optional[str],
merge_inverted: bool,
update_justification: bool,
inverse_map: TextIO,
):
"""
Expand All @@ -746,6 +753,8 @@ def invert(
:param subject_prefix: Prefix of all subject_ids.
:param merge_inverted: If True (default), add inverted dataframe to input else,
just return inverted data.
:param update_justification: If True (default), the justification is updated to "sempav:MappingInversion",
else it is left as it is.
:param inverse_map: YAML file providing the inverse mapping for predicates.
:param output: SSSOM TSV file with columns sorted.
"""
Expand All @@ -754,12 +763,15 @@ def invert(
with open(inverse_map, "r") as im: # type: ignore
inverse_dictionary = yaml.safe_load(im)
inverse_predicate_map = inverse_dictionary["inverse_predicate_map"]
else:
inverse_predicate_map = None

msdf.df = invert_mappings(
msdf.df,
subject_prefix,
merge_inverted,
inverse_predicate_map,
df=msdf.df,
subject_prefix=subject_prefix,
merge_inverted=merge_inverted,
update_justification=update_justification,
predicate_invert_dictionary=inverse_predicate_map,
)
write_table(msdf, output)

Expand Down
6 changes: 5 additions & 1 deletion src/sssom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ def invert_mappings(
df: pd.DataFrame,
subject_prefix: Optional[str] = None,
merge_inverted: bool = True,
update_justification: bool = True,
predicate_invert_dictionary: dict = None,
) -> pd.DataFrame:
"""Switching subject and objects based on their prefixes and adjusting predicates accordingly.
Expand All @@ -1400,6 +1401,8 @@ def invert_mappings(
:param subject_prefix: Prefix of subjects desired.
:param merge_inverted: If True (default), add inverted dataframe to input else,
just return inverted data.
:param update_justification: If True (default), the justification is updated to "sempav:MappingInversion",
else it is left as it is.
:param predicate_invert_dictionary: YAML file providing the inverse mapping for predicates.
:return: Pandas dataframe with all subject IDs having the same prefix.
"""
Expand Down Expand Up @@ -1458,7 +1461,8 @@ def invert_mappings(
)
inverted_df = inverted_df[df.columns]
inverted_df[PREDICATE_ID] = inverted_df[PREDICATE_ID].map(predicate_invert_map)
inverted_df[MAPPING_JUSTIFICATION] = SEMAPV.MappingInversion.value
if update_justification:
inverted_df[MAPPING_JUSTIFICATION] = SEMAPV.MappingInversion.value
if not prefixed_subjects_df.empty:
return_df = pd.concat([prefixed_subjects_df, inverted_df]).drop_duplicates()
else:
Expand Down

0 comments on commit 5db5296

Please sign in to comment.