Skip to content

Commit

Permalink
feat(CLR): add closure_target parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
msorvoja committed Dec 17, 2024
1 parent 33f4d78 commit 3bcfedd
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions eis_toolkit/transformations/coda/clr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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))
Expand Down

0 comments on commit 3bcfedd

Please sign in to comment.