def setUp(self): self.G = augmented_covariance_matrix(np.array([1.1, 0.9]), random_cov_matrix(2)) self.G3 = augmented_covariance_matrix(np.array([1.1, 0.9, 1.05]), random_cov_matrix(3))
def setUp(self): self.mean = np.random.randn(5) self.cov = random_cov_matrix(5)
.. note:: This example and features in this module are currently incomplete and a work in progress. """ import numpy as np import pandas as pd import matplotlib.pyplot as plt from pyrolite.comp.impute import EMCOMP from pyrolite.util.synthetic import random_composition, random_cov_matrix from pyrolite.plot import pyroplot np.random.seed(41) ######################################################################################## sample_data = random_composition( size=1000, D=4, cov=random_cov_matrix(3, sigmas=[0.1, 0.3, 0.15]), propnan=0.1, missing="MNAR", missingcols=3, ) ######################################################################################## imputed_data, p0, niter = EMCOMP(sample_data, threshold=np.nanpercentile(sample_data, 10, axis=0), tol=0.01) imputed_data = pd.DataFrame(imputed_data, columns=["A", "B", "C", "D"]) ######################################################################################## fig, ax = plt.subplots(1, 3, sharex=True, sharey=True, figsize=(12, 5)) ax[0].set_title("Original Data")
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 normal_frame, random_cov_matrix df = normal_frame( size=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 (here using default simple labelling;
cs = cs.reshape(cs.shape[0], *c1.shape) # generate samples from each samples = np.vstack( [ np.random.multivariate_normal(m.flatten(), cs[ix], size=size // resolution) for ix, m in enumerate(ms) ] ) # combine together. return inverse_ilr(samples) ######################################################################################## # First we create an array of compositions which represent a trend. # m1, m2 = np.array([[0.3, 0.1, 2.1]]), np.array([[0.5, 2.5, 0.05]]) c1, c2 = ( random_cov_matrix(2, sigmas=[0.15, 0.05]), random_cov_matrix(2, sigmas=[0.05, 0.2]), ) trend = pd.DataFrame( random_compositional_trend(m1, m2, c1, c2, resolution=100, size=5000), columns=["A", "B", "C"], ) ######################################################################################## # We can visualise this compositional trend with a density plot. # ax = trend.pyroplot.density(mode="density", bins=100) plt.show() ######################################################################################## # First we can see where the geometric mean would fall: #
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 normal_frame, random_cov_matrix df = normal_frame( size=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.axes 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")
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