Skip to content

Commit

Permalink
Formatting - comp.codata
Browse files Browse the repository at this point in the history
  • Loading branch information
morganjwilliams committed May 17, 2024
1 parent 86a78b0 commit c4da052
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions pyrolite/comp/codata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import warnings

import numpy as np
import pandas as pd
import scipy.special
Expand Down Expand Up @@ -39,7 +40,10 @@ def close(X: np.ndarray, sumf=np.sum):
"""

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

if X.ndim == 2:
C = np.array(sumf(X, axis=1), dtype=float)[:, np.newaxis]
Expand Down Expand Up @@ -81,16 +85,20 @@ def renormalise(df: pd.DataFrame, components: list = [], scale=100.0):
dfc = dfc[components]

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)
warnings.warn(
"Non-positive entries found in specified components. "
"Negative values have been replaced with NaN. "
"Renormalisation assumes all positive entries.",
UserWarning,
)

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

# 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
# Handle division by zero by replacing zeros with NaN
sum_rows.replace(0, np.nan, inplace=True)
dfc = dfc.divide(sum_rows, axis=0) * scale

return dfc
Expand Down Expand Up @@ -347,9 +355,11 @@ def get_ALR_labels(df, mode="simple", ind=-1, **kwargs):

names = [
r"{} / {}".format(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c],
(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c]
),
df.columns[ind],
)
for c in df.columns
Expand Down Expand Up @@ -399,9 +409,11 @@ def get_CLR_labels(df, mode="simple", **kwargs):

names = [
r"{} / γ".format(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c],
(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c]
),
)
for c in df.columns
]
Expand Down Expand Up @@ -462,9 +474,11 @@ def get_ILR_labels(df, mode="latex", **kwargs):
# sub in Phi (the CLR normalisation variable)
names = [
r"{} / γ".format(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c],
(
c
if c not in __sympy_protected_variables__
else __sympy_protected_variables__[c]
),
)
for c in df.columns
]
Expand Down

0 comments on commit c4da052

Please sign in to comment.