Skip to content

Commit

Permalink
Minor bugfixes, styling, test import cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
morganjwilliams committed May 28, 2024
1 parent e7bc351 commit 2060f50
Show file tree
Hide file tree
Showing 44 changed files with 201 additions and 119 deletions.
1 change: 1 addition & 0 deletions pyrolite/geochem/alteration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Functions for calcuating indexes of chemical alteration.
"""

import pandas as pd

from ..util.log import Handle
Expand Down
15 changes: 9 additions & 6 deletions pyrolite/geochem/ind.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
------
* Incompatibility indexes for spider plot ordering.
"""

import re

import numpy as np
Expand Down Expand Up @@ -254,9 +255,11 @@ def simple_oxides(cation, output="string"):

# for 3.6+, could use f'{cation}{1}O{c//2}', f'{cation}{2}O{c}'
oxides = [
str(cation) + str(1) + "O" + str(c // 2)
if not c % 2
else str(cation) + str(2) + "O" + str(c)
(
str(cation) + str(1) + "O" + str(c // 2)
if not c % 2
else str(cation) + str(2) + "O" + str(c)
)
for c in ions
]
oxides = [pt.formula(ox) for ox in oxides]
Expand Down Expand Up @@ -291,7 +294,7 @@ def get_cations(component: str, exclude=[], total_suffix="T"):

exclude += ["O"]
atms = pt.formula(component).atoms
cations = [el for el in atms.keys() if not el.__str__() in exclude]
cations = [el for el in atms.keys() if el.__str__() not in exclude]
return cations


Expand Down Expand Up @@ -398,7 +401,7 @@ def get_ionic_radii(
variant=[],
source="shannon",
pauling=True,
**kwargs
**kwargs,
):
"""
Function to obtain ionic radii for a given ion and coordination [#ref_1]_
Expand Down Expand Up @@ -464,7 +467,7 @@ def get_ionic_radii(
variant=variant,
source=source,
pauling=pauling,
**kwargs
**kwargs,
)
for e in element
]
Expand Down
1 change: 1 addition & 0 deletions pyrolite/geochem/norm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Reference compostitions and compositional normalisation.
"""

import json
from pathlib import Path

Expand Down
32 changes: 20 additions & 12 deletions pyrolite/geochem/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Functions for converting, transforming and parameterizing geochemical data.
"""

from collections import Counter

import numpy as np
import pandas as pd
import periodictable as pt
Expand All @@ -12,13 +14,7 @@
from ..util.meta import update_docstring_references
from ..util.text import remove_suffix, titlecase
from ..util.types import iscollection
from .ind import (
_common_elements,
_common_oxides,
get_cations,
get_ionic_radii,
simple_oxides,
)
from .ind import _common_elements, _common_oxides, get_cations, simple_oxides
from .norm import Composition, get_reference_composition

logger = Handle(__name__)
Expand Down Expand Up @@ -102,7 +98,7 @@ def devolatilise(
:class:`pandas.DataFrame`
Transformed dataframe.
"""
keep = [i for i in df.columns if not i in exclude]
keep = [i for i in df.columns if i not in exclude]
if renorm:
return renormalise(df.loc[:, keep])
else:
Expand Down Expand Up @@ -141,7 +137,7 @@ def oxide_conversion(oxin, oxout, molecular=False):
# Assertion of simple oxide
assert (len(in_els) == len(out_els)) & (len(in_els) == 1)
assert in_els == out_els # Need to be dealing with the same element!
except:
except AssertionError:
raise ValueError("Incompatible compounds: {} --> {}".format(in_els, out_els))
# Moles of product vs. moles of reactant
cation_coefficient = list(inatoms.values())[0] / list(outatoms.values())[0]
Expand Down Expand Up @@ -342,7 +338,7 @@ def aggregate_element(

for t in targetnames:
if t not in _df:
_df[t] = 0. # avoid missing column errors
_df[t] = 0.0 # avoid missing column errors

coeff = np.array(coeff)
if coeff.ndim == 2:
Expand Down Expand Up @@ -603,7 +599,7 @@ def lambda_lnREE(
norm_df.loc[(norm_df <= 0.0).any(axis=1), :] = np.nan # remove zero or below
norm_df.loc[:, ree] = np.log(norm_df.loc[:, ree])

if not (sigmas is None):
if sigmas is not None:
if isinstance(sigmas, pd.Series): # convert this to an array
sigmas = sigmas[ree].values

Expand Down Expand Up @@ -707,7 +703,7 @@ def convert_chemistry(
# and speciated components
####################################################################################
output_compositional = [
i for i in to if i not in coupled_sets + noncomp + new_ratios
i for i in to if i not in (coupled_sets + noncomp + new_ratios)
]
# check that these are all unique components
assert len(set(output_compositional)) == len(
Expand Down Expand Up @@ -753,6 +749,16 @@ def convert_chemistry(
**kwargs,
)

# TODO: warning for duplication should also be crossed over into speciated components above..
_duplicated_cations = [
str(k)
for k, v in Counter( # get the first cation in each component, and count duplicates
[get_cations(t, total_suffix=total_suffix)[0] for t in output_compositional]
).items()
if v > 1
]
if _duplicated_cations:
logger.warning("Cations duplicated in compositional components: {}. The output retains this duplication!".format(','.join(_duplicated_cations)))
# Aggregate the singular compositional items, then get new columns
for item in output_compositional:
df = aggregate_element(df, to=item, logdata=logdata, molecular=molecular)
Expand Down Expand Up @@ -788,3 +794,5 @@ def convert_chemistry(
else:
logger.debug("Recalculation Done. Data not renormalised.")
return df.loc[:, output_columns]
logger.debug("Recalculation Done. Data not renormalised.")
return df.loc[:, output_columns]
1 change: 1 addition & 0 deletions pyrolite/mineral/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
doi: {hazen1979}
"""

import numpy as np

from ..util.log import Handle
Expand Down
1 change: 1 addition & 0 deletions pyrolite/mineral/mindb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Accessing and modifying the database across multiple with multiple threads/processes
*could* result in database corruption (e.g. through repeated truncation etc).
"""

import functools
from pathlib import Path

Expand Down
3 changes: 1 addition & 2 deletions pyrolite/mineral/normative.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ..comp.codata import close, renormalise
from ..geochem.transform import convert_chemistry, to_molecular
from ..util.classification import TAS
from ..util.log import Handle
from ..util.pd import to_frame
from ..util.units import scale
Expand Down Expand Up @@ -201,8 +202,6 @@ def endmember_decompose(
# CIPW Norm and Related functions
################################################################################

from ..util.classification import TAS

# fuctions which map <TAS field, [SiO2, Na2O + K2O]> to Fe2O3/FeO ratios.
_MiddlemostTASRatios = dict(
F=lambda t, x: 0.4 if x[1] > 10 else 0.3,
Expand Down
2 changes: 1 addition & 1 deletion pyrolite/mineral/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
affinities={"Si{4+}": 0, "Al{3+}": 1, "Fe{3+}": 2},
*args,
mode="cation",
**kwargs
**kwargs,
):
super().__init__(name, coordination, *args, affinities=affinities, **kwargs)

Expand Down
7 changes: 4 additions & 3 deletions pyrolite/util/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ def signify_digit(n, unc=None, leeway=0, low_filter=True):
if round_to <= 0:
fmt = int
else:
fmt = lambda x: x
def fmt(x):
return x
sig_n = round(n, round_to)
if low_filter and sig_n == 0.0:
return np.nan
Expand Down Expand Up @@ -604,12 +605,12 @@ def solve_ratios(*eqs, evaluate=True):
Solve a ternary system (top-left-right) given two constraints on
two ratios, which together describe intersecting lines/a point.
"""
t, l, r = sympy.symbols("t l r")
t, L, r = sympy.symbols("t l r")

def to_sympy(t): # rearrange to have =0 equvalent expressions
return sympy.sympify("-".join(t.split("=")))

result = sympy.solve(
[to_sympy(e) for e in eqs] + [to_sympy("t + l + r = 1")], (t, l, r)
[to_sympy(e) for e in eqs] + [to_sympy("t + l + r = 1")], (t, L, r)
)
return list(result.values())
2 changes: 1 addition & 1 deletion pyrolite/util/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def _metric_name(metric):
msg = "Gaussian Process boostrapping not yet implemented."
raise NotImplementedError(msg)
else:
msg = "Bootstrap method {} not recognised.".format(boostrap_method)
msg = "Bootstrap method {} not recognised.".format(bootstrap_method)
raise NotImplementedError(msg)

# whether to independently estimate metric values for individual categories?
Expand Down
15 changes: 12 additions & 3 deletions test/comp/comp_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import logging
import unittest

import numpy as np

from pyrolite.comp.aggregate import *
import pandas as pd

from pyrolite.comp.aggregate import (
compositional_mean,
cross_ratios,
get_full_column,
nan_weighted_compositional_mean,
nan_weighted_mean,
np_cross_ratios,
standardise_aggregate,
weights_from_array,
)
from pyrolite.util.synthetic import normal_frame


Expand Down
19 changes: 17 additions & 2 deletions test/comp/comp_codata.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import unittest

import numpy as np

from pyrolite.comp.codata import *
import pandas as pd

from pyrolite.comp.codata import (
ALR,
CLR,
ILR,
boxcox,
close,
get_ILR_labels,
inverse_ALR,
inverse_boxcox,
inverse_CLR,
inverse_ILR,
inverse_sphere,
renormalise,
sphere,
)
from pyrolite.util.synthetic import normal_frame


Expand Down
11 changes: 2 additions & 9 deletions test/comp/comp_impute.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import unittest

import numpy as np
import pandas as pd

from pyrolite.comp.aggregate import np_cross_ratios
from pyrolite.comp.impute import EMCOMP, _little_sweep, _multisweep, _reg_sweep
from pyrolite.comp.impute import EMCOMP, _little_sweep, _multisweep
from pyrolite.util.math import augmented_covariance_matrix
from pyrolite.util.synthetic import (
normal_frame,
normal_series,
random_composition,
random_cov_matrix,
)
from pyrolite.util.synthetic import random_composition, random_cov_matrix


class TestRegSweep(unittest.TestCase):
Expand Down
14 changes: 12 additions & 2 deletions test/geochem/geochem_alteration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import unittest

import numpy as np
import pandas as pd

from pyrolite.geochem.alteration import *
from pyrolite.geochem.alteration import (
CCPI,
CIA,
CIW,
PIA,
SAR,
WIP,
IshikawaAltIndex,
SiTiIndex,
)


class TestAlterationIndicies(unittest.TestCase):
Expand Down Expand Up @@ -56,4 +66,4 @@ def test_CCPI_default(self):


if __name__ == "__main__":
unittest.main()
unittest.main()
14 changes: 13 additions & 1 deletion test/geochem/geochem_ind.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import unittest

from pyrolite.geochem.ind import *
import periodictable as pt

from pyrolite.geochem.ind import (
REE,
REY,
by_incompatibility,
by_number,
common_elements,
common_oxides,
get_cations,
get_ionic_radii,
simple_oxides,
)


class TestGetCations(unittest.TestCase):
Expand Down
1 change: 0 additions & 1 deletion test/geochem/geochem_isotope.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

class TestDeadtimeCorrection(unittest.TestCase):
def test_default(self):
pass
deadtime_correction(10000, 20)
5 changes: 0 additions & 5 deletions test/geochem/geochem_norm.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import unittest

import numpy as np

import pyrolite
from pyrolite.geochem.norm import (
Composition,
all_reference_compositions,
Expand All @@ -11,8 +8,6 @@
update_database,
)
from pyrolite.util.general import remove_tempdir, temp_path
from pyrolite.util.meta import pyrolite_datafolder
from pyrolite.util.synthetic import normal_frame


class TestComposition(unittest.TestCase):
Expand Down
7 changes: 6 additions & 1 deletion test/geochem/geochem_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import pandas as pd

from pyrolite.geochem.ind import REE
from pyrolite.geochem.parse import *
from pyrolite.geochem.parse import (
check_multiple_cation_inclusion,
ischem,
repr_isotope_ratio,
tochem,
)


class TestIsChem(unittest.TestCase):
Expand Down
4 changes: 1 addition & 3 deletions test/geochem/geochem_pyrochem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import pyrolite.geochem
from pyrolite.comp.codata import renormalise
from pyrolite.geochem.norm import get_reference_composition
from pyrolite.util.synthetic import normal_frame, normal_series

# [print("# " + i) for i in dir(df.pyrochem) if "__" not in i and not i.startswith("_")]
from pyrolite.util.synthetic import normal_frame


class TestPyrochem(unittest.TestCase):
Expand Down
Loading

0 comments on commit 2060f50

Please sign in to comment.