Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
.DS_Store

# C extensions
*.so
Expand Down
40 changes: 29 additions & 11 deletions pyrolite/comp/codata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
import numpy as np
import pandas as pd
import scipy.special
Expand Down Expand Up @@ -34,15 +35,21 @@ def close(X: np.ndarray, sumf=np.sum):

Notes
------
* Does not check for non-positive entries.
Checks for non-positive entries and replaces zeros with NaN values.
"""

if np.any(X <= 0):
warnings.warn("Non-positive entries found. Closure operation assumes all positive entries.", UserWarning)

if X.ndim == 2:
C = np.array(sumf(X, axis=1))[:, np.newaxis]
C = np.array(sumf(X, axis=1), dtype=float)[:, np.newaxis]
else:
C = np.array(sumf(X, axis=0))
C = np.array(sumf(X), dtype=float)

# Replace zero sums with NaN to prevent division by zero
C[np.isclose(C, 0)] = np.nan

# Return the array closed to sum to 1
return np.divide(X, C)


Expand All @@ -66,16 +73,27 @@ def renormalise(df: pd.DataFrame, components: list = [], scale=100.0):
:class:`pandas.DataFrame`
Renormalized dataframe.
"""

dfc = df.copy(deep=True)
if components:
cmpnts = [c for c in components if c in dfc.columns]
dfc.loc[:, cmpnts] = scale * dfc.loc[:, cmpnts].divide(
dfc.loc[:, cmpnts].sum(axis=1).replace(0, np.nan), axis=0
)
return dfc
else:
dfc = dfc.divide(dfc.sum(axis=1).replace(0, 100.0), axis=0) * scale
return dfc
if not all(col in dfc.columns for col in components):
raise ValueError("Not all specified components exist in the DataFrame.")
dfc = dfc[components]

# Replace negative values with NaN
dfc[dfc < 0] = np.nan

if (dfc <= 0).any().any():
warnings.warn("Non-positive entries found in specified components. "
"Negative values have been replaced with NaN. "
"Renormalisation assumes all positive entries.", UserWarning)

# Renormalise all columns if no components are specified
sum_rows = dfc.sum(axis=1)
sum_rows.replace(0, np.nan, inplace=True) # Handle division by zero by replacing zeros with NaN
dfc = dfc.divide(sum_rows, axis=0) * scale

return dfc


def ALR(X: np.ndarray, ind: int = -1, null_col=False):
Expand Down