From 3bcfedddd26aa0b4a0f89cfe846744965f40745c Mon Sep 17 00:00:00 2001 From: msorvoja Date: Tue, 17 Dec 2024 10:07:47 +0200 Subject: [PATCH] feat(CLR): add closure_target parameter --- eis_toolkit/transformations/coda/clr.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/eis_toolkit/transformations/coda/clr.py b/eis_toolkit/transformations/coda/clr.py index d5cf7761..7a6c4728 100644 --- a/eis_toolkit/transformations/coda/clr.py +++ b/eis_toolkit/transformations/coda/clr.py @@ -9,7 +9,7 @@ from eis_toolkit.exceptions import InvalidColumnException, NumericValueSignException from eis_toolkit.utilities.aitchison_geometry import _closure from eis_toolkit.utilities.checks.compositional import check_in_simplex_sample_space -from eis_toolkit.utilities.miscellaneous import rename_columns, rename_columns_by_pattern +from eis_toolkit.utilities.miscellaneous import perform_closure, rename_columns, rename_columns_by_pattern @beartype @@ -26,13 +26,18 @@ def _clr_transform(df: pd.DataFrame) -> pd.DataFrame: @beartype -def clr_transform(df: pd.DataFrame, columns: Optional[Sequence[str]] = None) -> pd.DataFrame: +def clr_transform( + df: pd.DataFrame, + columns: Optional[Sequence[str]] = None, + closure_target: Optional[int] = None, +) -> pd.DataFrame: """ Perform a centered logratio transformation on the data. Args: df: A dataframe of compositional data. columns: The names of the columns to be transformed. + closure_target: Target row sum for closure. If None, no closure is performed. Returns: A new dataframe containing the CLR transformed data. @@ -52,6 +57,9 @@ def clr_transform(df: pd.DataFrame, columns: Optional[Sequence[str]] = None) -> else: columns_to_transform = df.columns.to_list() + if closure_target is not None: + df = perform_closure(df, columns_to_transform, closure_target) + check_in_simplex_sample_space(df) return rename_columns_by_pattern(_clr_transform(df))