def setUp(self): self.cols = ["MgO", "SiO2", "CaO"] # can run into interesting singular matrix errors with bivariate random data self.tridf = test_df(cols=self.cols, index_length=100) self.bidf = self.tridf.loc[:, self.cols[:2]] self.multidf = test_df(cols=REE(), index_length=100)
def setUp(self): self.X = test_df(index_length=20).apply(close, axis=1) iris = sklearn.datasets.load_iris() self.data, self.target = iris["data"], iris["target"] svc = SVC_pipeline(probability=True) self.gs = svc.fit(self.data, self.target) self.clf = self.gs.best_estimator_
def test_LambdaTransformer(self): """Test the LambdaTransformer transfomer.""" df = test_df(cols=REE()).apply(close, axis=1) tmr = LambdaTransformer() for input in [df]: with self.subTest(input=input): out = tmr.transform(input)
def setUp(self): self.df = test_df() self.df.loc[:, "Title"] = [ "Title {}".format(x) for x in self.df.index.values ] self.ser = test_ser() self.ser.loc["Title"] = "Test_title"
def setUp(self): self.cols = ["SiO2", "CaO", "MgO", "FeO", "TiO2"] self.df = test_df(cols=self.cols) nans = 10 self.df.iloc[ np.random.randint(1, 10, size=nans), np.random.randint(1, len(self.cols), size=nans), ] = np.nan
def setUp(self): self.X = test_df(index_length=20).apply(close, axis=1) self.gs = test_classifier() self.y = np.ones(self.X.index.size) self.y[4:] += 1 self.X_train, self.X_test, self.y_train, self.y_test = train_test_split( self.X, self.y, test_size=0.3, stratify=self.y) self.gs.fit(self.X_train, self.y_train) self.clf = self.gs.best_estimator_
def setUp(self): cols = [ "MgO", "SiO2", "CaO", "FeO", "Ti", "Hf", "Zr", "H2O", ] + pyrolite.geochem.REE() self.df = test_df(index_length=4, cols=cols) self.df = renormalise(self.df)
including the Total Alkali-Silica (TAS) classification. """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from pyrolite.util.classification import Geochemistry from pyrolite.util.synthetic import test_df, random_cov_matrix # sphinx_gallery_thumbnail_number = 2 ######################################################################################## # We'll first generate some synthetic data to play with: # mean = np.array([49, 11, 15, 4, 0.5, 4, 1.5]) df = (test_df( cols=["SiO2", "CaO", "MgO", "FeO", "TiO2", "Na2O", "K2O"], mean=mean, index_length=100, seed=82, ) * mean.sum()) df.head(3) ######################################################################################## # We can visualise how this chemistry corresponds to the TAS diagram: # from pyrolite.util.classification import Geochemistry df["TotalAlkali"] = df["Na2O"] + df["K2O"] cm = Geochemistry.TAS() fig, ax = plt.subplots(1) ax.scatter(df["SiO2"], df["TotalAlkali"], c="k", alpha=0.2)
the compositional space. The commonly used log-transformations include the Additive Log-Ratio (:func:`~pyrolite.comp.pyrocomp.ALR`), Centred Log-Ratio (:func:`~pyrolite.comp.pyrocomp.CLR`), and Isometric Log-Ratio (:func:`~pyrolite.comp.pyrocomp.ILR`) [#ref_1]_ [#ref_2]_. This example will show you how to access and use some of these functions in pyrolite. """ ######################################################################################## # First let's create some example data: # from pyrolite.util.synthetic import test_df, random_cov_matrix df = test_df( index_length=100, cov=random_cov_matrix(sigmas=[0.1, 0.05, 0.3, 0.6], dim=4, seed=32), seed=32, ) df.describe() ######################################################################################## # Let's have a look at some of the log-transforms, which can be accessed directly from # your dataframes (via :class:`pyrolite.comp.pyrocomp`), after you've imported # :mod:`pyrolite.comp`. Note that the transformations will return *new* dataframes, # rather than modify their inputs. For example: # import pyrolite.comp lr_df = df.pyrocomp.CLR() # using a centred log-ratio transformation ######################################################################################## # The transformations are implemented such that the column names generally make it # evident which transformations have been applied:
def setUp(self): self.df = test_df(cols=["Si", "Mg", "MgO", "CaO", "Li", "B"])
def setUp(self): self.df = test_df().apply(close, axis=1)
def setUp(self): self.df0 = test_df() self.others = [test_df()] * 4
including the Total Alkali-Silica (TAS) classification. """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from pyrolite.util.classification import TAS from pyrolite.util.synthetic import test_df, random_cov_matrix # sphinx_gallery_thumbnail_number = 2 ######################################################################################## # We'll first generate some synthetic data to play with: # df = ( test_df( cols=["SiO2", "Na2O", "K2O", "Al2O3"], mean=[0.5, 0.04, 0.05, 0.4], index_length=100, seed=49, ) * 100 ) df.head(3) ######################################################################################## # We can visualise how this chemistry corresponds to the TAS diagram: # import pyrolite.plot df["Na2O + K2O"] = df["Na2O"] + df["K2O"] cm = TAS() fig, ax = plt.subplots(1)
def setUp(self): self.cols = ["SiO2", "CaO", "MgO", "FeO", "TiO2"] self.df = test_df(cols=self.cols)
def setUp(self): self.cols = ["SiO2", "CaO", "MgO", "FeO", "TiO2"] self.d = len(self.cols) self.n = 10 self.df = test_df(cols=self.cols, index_length=self.n)
def setUp(self): self.cols = ["SiO2", "CaO", "MgO", "FeO", "TiO2"] self.df = test_df(cols=self.cols) self.df = self.df.apply(lambda x: x / np.sum(x), axis="columns")
""" Break down a list of phases into oxide components. """ atoms = set() # get atoms which are not O for p in phases: atoms |= set(get_cations(p)) print(atoms) oxides = [simple_oxides(a)[1] for a in atoms] return oxides from pyrolite.util.synthetic import test_df, random_cov_matrix X = test_df( cols=["cpx", "olivine", "plag"], cov=random_cov_matrix(dim=2, sigmas=np.array([0.5, 0.7])), ) component_matrix(components=X.columns, phases=["CaAl2Si2O8", "Mg2Si2O4", "CaMgSi2O6"]) mixed = X @ components.pyrochem.to_weight().pyrocomp.renormalise(scale=1) mixed def unmix_compositions(X, components, bdl=10 * -5): x = X.copy() c = components.copy() c = c.pyrochem.to_weight().pyrocomp.renormalise(scale=1) # c[c == 0] = bdl; clr
""" Geochemical Indexes and Selectors ================================== """ import pyrolite.geochem import pandas as pd pd.set_option("precision", 3) # smaller outputs ######################################################################################## from pyrolite.util.synthetic import test_df df = test_df( cols=["CaO", "MgO", "SiO2", "FeO", "Mn", "Ti", "La", "Lu", "Mg/Fe"]) ######################################################################################## df.head(2).pyrochem.oxides ######################################################################################## df.head(2).pyrochem.elements ######################################################################################## df.head(2).pyrochem.REE ######################################################################################## df.head(2).pyrochem.compositional ########################################################################################
def setUp(self): self.data = test_df(cols=["SiO2", "CaO", "MgO"], index_length=20).values
def setUp(self): self.df = test_df().apply(close, axis=1) self.group = (self.df["MgO"] > 0.21).apply(np.int)
represents the best of both worlds. A version inspired by similar existing visualisations is implemented with :func:`~pyrolite.plot.pyroplot.heatscatter`. """ import pandas as pd import numpy as np import matplotlib.pyplot as plt from pyrolite.plot import pyroplot np.random.seed(12) ######################################################################################## # First we'll create some example data from pyrolite.util.synthetic import test_df, random_cov_matrix df = test_df( index_length=1000, cov=random_cov_matrix(sigmas=np.random.rand(4) * 2, dim=4, seed=12), seed=12, ) ######################################################################################## # We can compare a minimal :func:`~pyrolite.plot.pyroplot.heatscatter` plot to other # visualisations for the same data: # from pyrolite.util.plot import share_axes fig, ax = plt.subplots(3, 4, figsize=(12, 9)) ax = ax.flat share_axes(ax[:4], which="xy") share_axes(ax[4:8], which="xy") share_axes(ax[8:], which="xy")
def setUp(self): self.df = test_df().applymap(str)
def setUp(self): self.cols = ["MgO", "SiO2", "CaO"] # can run into interesting singular matrix errors with bivariate random data self.tridf = test_df(cols=self.cols, index_length=100)
def setUp(self): self.ser = test_ser() self.df = test_df()
def setUp(self): self.df = test_df().pyrocomp.renormalise()
One of pyrolite's strengths is converting mixed elemental and oxide data to a new form. The simplest way to perform this is by using the :func:`~pyrolite.geochem.transform.convert_chemistry` function. Note that by default pyrolite assumes that data are in the same units. """ import pyrolite.geochem import pandas as pd pd.set_option("precision", 3) # smaller outputs ######################################################################################## # Here we create some synthetic data to work with, which has some variables in Wt% and # some in ppm. Notably some elements are present in more than one column (Ca, Na): # from pyrolite.util.synthetic import test_df df = test_df(cols=["MgO", "SiO2", "FeO", "CaO", "Na2O", "Te", "K", "Na"]) * 100 df.pyrochem.elements *= 100 # elements in ppm ######################################################################################## df.head(2) ######################################################################################## # As the units are heterogeneous, we'll need to convert the data frame to a single set of # units (here we use Wt%): # df.pyrochem.elements = df.pyrochem.elements.pyrochem.scale('ppm', 'wt%') # ppm to wt% ######################################################################################## # We can transform this chemical data to a new set of compositional variables. # Here we i) convert CaO to Ca, ii) aggregate Na2O and Na to Na and iii) calculate # mass ratios for Na/Te and MgO/SiO2. # Note that you can also use this function to calculate mass ratios: #
""" Unit Scaling ============= """ import pyrolite.geochem import pandas as pd pd.set_option("precision", 3) # smaller outputs ######################################################################################## from pyrolite.util.synthetic import test_df df = test_df( cols=['CaO', 'MgO', 'SiO2', 'FeO', 'Ni', 'Ti', 'La', 'Lu', 'Mg/Fe']) ######################################################################################## cols = ["Ni", "NiO", "La", "La2O3"] df.head(2).pyrochem.convert_chemistry(to=cols)[cols] # these are in ppm!
def setUp(self): self.df = test_df()
.. note:: This tutorial is a work in progress and will be gradually updated. In this tutorial we will illustrate some straightfoward formatting for your plots which will allow for greater customisation as needed. As :mod:`pyrolite` heavily uses and exposes the API of :mod:`matplotlib` for the visualisation components (and also :mod:`mpltern` for ternary diagrams), you should also check out their documentation pages for more in-depth guides, examples and API documentation. """ ####################################################################################### # First let's pull in a simple dataset to use throughout these examples: # from pyrolite.util.synthetic import test_df # sphinx_gallery_thumbnail_number = 7 df = test_df(cols=["SiO2", "CaO", "MgO", "Al2O3", "TiO2", "27Al", "d11B"]) ####################################################################################### # Basic Figure and Axes Settings # ------------------------------ # # :mod:`matplotlib` makes it relatively straightfoward to customise most settings for # your figures and axes. These settings can be defined at creation (e.g. in a call to # :func:`~matplotlib.pyplot.subplots`), or they can be defined after you've created an # axis (with the methods :code:`ax.set_<parameter>()`). For example: # import matplotlib.pyplot as plt fig, ax = plt.subplots(1) ax.set_xlabel("My X Axis Label") ax.set_title("My Axis Title", fontsize=12)