Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEVER MERGE Semra testing #563

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
filter_redundant_rows,
invert_mappings,
merge_msdf,
pandas_set_no_silent_downcasting,
reconcile_prefix_and_data,
remove_unmatched,
sort_df_rows_columns,
Expand Down Expand Up @@ -126,6 +127,9 @@
def main(verbose: int, quiet: bool):
"""Run the SSSOM CLI."""
logger = _logging.getLogger()

pandas_set_no_silent_downcasting()

if verbose >= 2:
logger.setLevel(level=_logging.DEBUG)
elif verbose == 1:
Expand Down
4 changes: 1 addition & 3 deletions src/sssom/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,7 @@ def from_sssom_dataframe(
# Need to revisit this solution.
# This is to address: A value is trying to be set on a copy of a slice from a DataFrame
if CONFIDENCE in df.columns:
df2 = df.copy()
df2[CONFIDENCE].replace(r"^\s*$", np.nan, regex=True, inplace=True)
df = df2
df.replace({CONFIDENCE: r"^\s*$"}, np.nan, regex=True, inplace=True)

mapping_set = _get_mapping_set_from_df(df=df, meta=meta)
doc = MappingSetDocument(mapping_set=mapping_set, converter=converter)
Expand Down
16 changes: 10 additions & 6 deletions src/sssom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,9 @@ def from_mapping_set_document(cls, doc: MappingSetDocument) -> "MappingSetDataFr
df = pd.DataFrame(get_dict_from_mapping(mapping) for mapping in doc.mapping_set.mappings)
meta = _extract_global_metadata(doc)

if pandas_version >= (2, 0, 0):
# For pandas >= 2.0.0, use the 'copy' parameter
df = df.infer_objects(copy=False)
else:
# For pandas < 2.0.0, call 'infer_objects()' without any parameters
df = df.infer_objects()
# remove columns where all values are blank.
df.replace("", np.nan, inplace=True)
df = df.infer_objects()
df.dropna(axis=1, how="all", inplace=True) # remove columns with all row = 'None'-s.

slots = _get_sssom_schema_object().dict["slots"]
Expand Down Expand Up @@ -1493,3 +1488,12 @@ def safe_compress(uri: str, converter: Converter) -> str:
:return: A CURIE
"""
return converter.compress_or_standardize(uri, strict=True)


def pandas_set_no_silent_downcasting(no_silent_downcasting=True):
"""Set pandas future.no_silent_downcasting option. Context https://github.com/pandas-dev/pandas/issues/57734."""
try:
pd.set_option("future.no_silent_downcasting", no_silent_downcasting)
except KeyError:
# Option does not exist in this version of pandas
pass
46 changes: 46 additions & 0 deletions tests/test_semra_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Test for merging MappingSetDataFrames."""

import unittest

from sssom_schema import Mapping

from sssom.context import get_converter
from sssom.parsers import parse_sssom_table
from sssom.util import MappingSetDataFrame
from sssom.writers import write_table


class TestSemraCompatibility(unittest.TestCase):
"""A test case for making sure the model works as intended."""

def test_basic_inference(self):
"""Test if instantiating Mapping() fails when required elements are missing."""
mdict_missing = dict(
subject_id="ID:123"
) # This is missing object_id, predicate_id, mapping_justification

import io

import pandas as pd
from semra.api import infer_chains, infer_reversible
from semra.io import from_sssom_df, get_sssom_df

data = [
["UBERON:1", "skos:exactMatch", "FBbt:9"],
["UBERON:1", "skos:exactMatch", "WBbt:6"],
]

df = pd.DataFrame(data=data, columns=["subject_id", "predicate_id", "object_id"])

mappings = from_sssom_df(df, mapping_set_name="test")
mappings = infer_reversible(mappings, progress=False)
mappings = infer_chains(mappings, progress=False)

df = get_sssom_df(mappings)
print(df)
msdf = MappingSetDataFrame(df=df, converter=get_converter())
print(msdf.df)
msdf.standardize_references()
msdf.clean_prefix_map()
with open("testout.sssom.tsv", "w", encoding="utf-8") as file:
write_table(msdf=msdf, file=file)
Loading