Skip to content

Commit

Permalink
Add default confidence param to sssom file while creating ptable (#…
Browse files Browse the repository at this point in the history
…388)

* Add default `confidence` of 0.95 to sssom file

* added docstring

* updated docstring

* refactored code

* correction

* fixed

* switched `default_confidence` from flag to float. default confidence multiplied to existing if not null

* reformatted

* typos fixed

* removed commented and redundant code

* removed unnecessary imports

* Improved logic

* typo

* fixed casting

* changes to address None float

* Nonee => np.Nan

* changed type to float range

* added space

* removed space

* added space in comment to test something
  • Loading branch information
hrshdhgd authored Jun 30, 2023
1 parent fa533bc commit 810e69e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/sssom/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- When you run ``python3 -m sssom`` python will execute``__main__.py`` as a script. That means there won't be any
``sssom.__main__`` in ``sys.modules``.
- When you import __main__ it will get executed again (as a module) because
there's no ``sssom.__main__`` in ``sys.modules``.
there's no ``sssom.__main__`` in ``sys.modules`` .
.. seealso:: https://click.palletsprojects.com/en/8.0.x/setuptools/
"""
Expand Down Expand Up @@ -49,7 +49,6 @@
SSSOM_EXPORT_FORMATS,
SSSOM_READ_FORMATS,
MappingSetDataFrame,
collapse,
compare_dataframes,
dataframe_to_ptable,
filter_redundant_rows,
Expand Down Expand Up @@ -253,14 +252,18 @@ def split(input: str, output_directory: str):
@input_argument
@output_option
@click.option("-W", "--inverse-factor", help="Inverse factor.")
def ptable(input, output: TextIO, inverse_factor):
@click.option(
"--default-confidence",
type=click.FloatRange(0, 1),
help="Default confidence to be assigned if absent.",
)
def ptable(input, output: TextIO, inverse_factor: float, default_confidence: float):
"""Convert an SSSOM file to a ptable for kboom/`boomer <https://github.com/INCATools/boomer>`_."""
# TODO should maybe move to boomer (but for now it can live here, so cjm can tweak
msdf = parse_sssom_table(input)
# df = parse(input)
df = collapse(msdf.df)
# , priors=list(priors)
rows = dataframe_to_ptable(df, inverse_factor=inverse_factor)
rows = dataframe_to_ptable(
msdf.df, inverse_factor=inverse_factor, default_confidence=default_confidence
)
for row in rows:
print(*row, sep="\t", file=output)

Expand Down
26 changes: 25 additions & 1 deletion src/sssom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,17 +504,41 @@ def compare_dataframes(df1: pd.DataFrame, df2: pd.DataFrame) -> MappingSetDiff:
return d


def dataframe_to_ptable(df: pd.DataFrame, *, inverse_factor: float = None):
def add_default_confidence(df: pd.DataFrame, confidence: float = np.NAN) -> pd.DataFrame:
"""Add `confidence` column to DataFrame if absent and initializes to 0.95.
If `confidence` column already exists, only fill in the None ones by 0.95.
:param df: DataFrame whose `confidence` column needs to be filled.
:return: DataFrame with a complete `confidence` column.
"""
if CONFIDENCE in df.columns:
df[CONFIDENCE] = df[CONFIDENCE].apply(lambda x: confidence * x if x is not None else x)
df[CONFIDENCE].fillna(float(confidence), inplace=True)
else:
df[CONFIDENCE] = float(confidence)

return df


def dataframe_to_ptable(
df: pd.DataFrame, *, inverse_factor: float = None, default_confidence: float = None
):
"""Export a KBOOM table.
:param df: Pandas DataFrame
:param inverse_factor: Multiplier to (1 - confidence), defaults to 0.5
:param default_confidence: Default confidence to be assigned if absent.
:raises ValueError: Predicate value error
:raises ValueError: Predicate type value error
:return: List of rows
"""
if not inverse_factor:
inverse_factor = 0.5

if default_confidence:
df = add_default_confidence(df, default_confidence)

df = collapse(df)
rows = []
for _, row in df.iterrows():
Expand Down

0 comments on commit 810e69e

Please sign in to comment.