Skip to content

Commit

Permalink
added closure checks also when geochemical data that is in ppm or ppb
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlait authored and nmaarnio committed Dec 17, 2024
1 parent 3db18bd commit b8ae3de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions eis_toolkit/utilities/checks/compositional.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def check_in_simplex_sample_space(df: pd.DataFrame, tolerance: Number = 0.0001)
row_sums = df.sum(axis=1)
closed_to_one = (row_sums - 1).abs() < tolerance
closed_to_hundred = (row_sums - 100).abs() < tolerance
closed_to_million = (row_sums - 1e6).abs() < tolerance
closed_to_billion = (row_sums - 1e9).abs() < tolerance

if not closed_to_one.all() and not closed_to_hundred.all():
raise InvalidCompositionException(f"Input data is not closed to 1 or 100 within tolerance of {tolerance}.")
is_valid = closed_to_one.all() or closed_to_hundred.all() or closed_to_million.all() or closed_to_billion.all()

if not is_valid:
raise InvalidCompositionException(
f"Input data is not closed to 1, 100 (%), 10^6 (ppm) or 10^9 (ppb) within tolerance of {tolerance}."
)

return None
18 changes: 18 additions & 0 deletions tests/utilities/compositional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ def test_compositional_data_invalid():
def test_check_for_simplex_sample_space():
"""Test whether or not a dataframe belongs to a simplex sample space is correctly identified."""
unit_simplex_df = pd.DataFrame([[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.2, 0.3]])
closed_to_hundred_df = unit_simplex_df * 100
closed_to_million_df = unit_simplex_df * 1e6
closed_to_billion_df = unit_simplex_df * 1e9
non_simplex_positive_df = pd.DataFrame([1, 2, 3, 4], [5, 6, 7, 8])
non_positive_df = pd.DataFrame([-1, 2, 3, 4], [1, 2, 3, 4])

Expand All @@ -91,3 +94,18 @@ def test_check_for_simplex_sample_space():
check_in_simplex_sample_space(unit_simplex_df)
except Exception as ex:
assert False, f"{type(ex)}: {ex}"

try:
check_in_simplex_sample_space(closed_to_hundred_df)
except Exception as ex:
assert False, f"{type(ex)}: {ex}"

try:
check_in_simplex_sample_space(closed_to_million_df)
except Exception as ex:
assert False, f"{type(ex)}: {ex}"

try:
check_in_simplex_sample_space(closed_to_billion_df)
except Exception as ex:
assert False, f"{type(ex)}: {ex}"

0 comments on commit b8ae3de

Please sign in to comment.