.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/xarray_examples/basic_usage_xr.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_xarray_examples_basic_usage_xr.py: Basic usage XR ============== A simple example demonstrating how to use mass-composition. .. GENERATED FROM PYTHON SOURCE LINES 7-16 .. code-block:: default import xarray as xr import pandas as pd from elphick.mass_composition import MassComposition from elphick.mass_composition.datasets.sample_data import sample_data # noinspection PyUnresolvedReferences import elphick.mass_composition.mc_xarray # keep this "unused" import - it helps .. GENERATED FROM PYTHON SOURCE LINES 17-21 Create a mass-composition (mc) enabled Xarray Dataset ----------------------------------------------------- We get some demo data in the form of a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: default df_data: pd.DataFrame = sample_data() print(df_data.head()) .. rst-class:: sphx-glr-script-out .. code-block:: none wet_mass mass_dry FE SIO2 al2o3 LOI group index 0 100.0 90.0 57.0 5.2 3.0 5.0 grp_1 1 90.0 80.0 59.0 3.1 1.7 4.0 grp_1 2 110.0 90.0 61.0 2.2 0.9 3.0 grp_2 .. GENERATED FROM PYTHON SOURCE LINES 27-28 Construct a MassComposition object first to create a compliant xarray object with the concrete property .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: default xr_ds: xr.Dataset = MassComposition(data=df_data, name='test').to_xarray() print(xr_ds.mc.data()) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 100.0 90.0 110.0 mass_dry (index) float64 24B 90.0 80.0 90.0 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: test mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} .. GENERATED FROM PYTHON SOURCE LINES 34-35 Validate the round trip by converting composition to mass and back to composition .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: default xr_ds_mass = xr_ds.mc.composition_to_mass() print(xr_ds_mass.mc.data()) xr_ds_chem = xr_ds_mass.mc.mass_to_composition() print(xr_ds_chem.mc.data()) pd.testing.assert_frame_equal(xr_ds.to_dataframe(), xr_ds_chem.to_dataframe()) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 100.0 90.0 110.0 mass_dry (index) float64 24B 90.0 80.0 90.0 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 51.3 47.2 54.9 SiO2 (index) float64 24B 4.68 2.48 1.98 Al2O3 (index) float64 24B 2.7 1.36 0.81 LOI (index) float64 24B 4.5 3.2 2.7 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: test mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 100.0 90.0 110.0 mass_dry (index) float64 24B 90.0 80.0 90.0 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: test mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} .. GENERATED FROM PYTHON SOURCE LINES 45-46 Demonstrate splitting an object .. GENERATED FROM PYTHON SOURCE LINES 47-51 .. code-block:: default xr_1, xr_2 = xr_ds.mc.split(fraction=0.25) print(xr_1.mc.data()) print(xr_2.mc.data()) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 25.0 22.5 27.5 mass_dry (index) float64 24B 22.5 20.0 22.5 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: (0.25 * test) mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 75.0 67.5 82.5 mass_dry (index) float64 24B 67.5 60.0 67.5 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: (0.75 * test) mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} .. GENERATED FROM PYTHON SOURCE LINES 52-56 Demonstrate the mc aggregate function ------------------------------------- i.e. weight average of the dataset, a.k.a. head grade .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: default xr_ds_wtd: xr.Dataset = xr_ds.mc.aggregate() print(xr_ds_wtd.mc.data()) # xr_ds_wtd.mc.to_dataframe(original_column_names=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 56B Dimensions: (index: 1) Dimensions without coordinates: index Data variables: mass_wet (index) float64 8B 300.0 mass_dry (index) float64 8B 260.0 H2O (index) float64 8B 13.33 Fe (index) float64 8B 59.0 SiO2 (index) float64 8B 3.515 Al2O3 (index) float64 8B 1.873 LOI (index) float64 8B 4.0 Attributes: mc_name: Aggregate of test mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: [] mc_interval_edges: {} .. GENERATED FROM PYTHON SOURCE LINES 64-65 Convert to a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 66-70 .. code-block:: default print(xr_ds.mc.aggregate(as_dataframe=True, original_column_names=False)) print(xr_ds.mc.aggregate(as_dataframe=True, original_column_names=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI name test 300.0 260.0 13.333333 59.0 3.515385 1.873077 4.0 wet_mass mass_dry H2O FE SIO2 al2o3 LOI name test 300.0 260.0 13.333333 59.0 3.515385 1.873077 4.0 .. GENERATED FROM PYTHON SOURCE LINES 71-72 Aggregate by a group variable .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: default print(xr_ds.mc.aggregate(group_var='group', as_dataframe=True)) .. rst-class:: sphx-glr-script-out .. code-block:: none mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group grp_1 190.0 170.0 10.526316 57.941176 4.211765 2.388235 4.529412 grp_2 110.0 90.0 18.181818 61.000000 2.200000 0.900000 3.000000 .. GENERATED FROM PYTHON SOURCE LINES 77-78 Math operations - we'll go full circle again, so we can check. .. GENERATED FROM PYTHON SOURCE LINES 79-92 .. code-block:: default xr_ds_added: xr.Dataset = xr_1.mc.add(xr_2) print(xr_ds_added.mc.data()) pd.testing.assert_frame_equal(xr_ds.to_dataframe(), xr_ds_added.to_dataframe()) xr_ds_add_sub: xr.Dataset = xr_ds.mc.add(xr_1).mc.sub(xr_1) print(xr_ds_added.mc.data()) pd.testing.assert_frame_equal(xr_ds.to_dataframe(), xr_ds_add_sub.to_dataframe()) print('done') .. rst-class:: sphx-glr-script-out .. code-block:: none Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 100.0 90.0 110.0 mass_dry (index) float64 24B 90.0 80.0 90.0 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: ((0.25 * test) + (0.75 * test)) mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} Size: 216B Dimensions: (index: 3) Coordinates: * index (index) int64 24B 0 1 2 Data variables: mass_wet (index) float64 24B 100.0 90.0 110.0 mass_dry (index) float64 24B 90.0 80.0 90.0 H2O (index) float64 24B 10.0 11.11 18.18 Fe (index) float64 24B 57.0 59.0 61.0 SiO2 (index) float64 24B 5.2 3.1 2.2 Al2O3 (index) float64 24B 3.0 1.7 0.9 LOI (index) float64 24B 5.0 4.0 3.0 group (index) object 24B 'grp_1' 'grp_1' 'grp_2' Attributes: mc_name: ((0.25 * test) + (0.75 * test)) mc_vars_mass: ['mass_wet', 'mass_dry'] mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI'] mc_vars_attrs: ['group'] mc_interval_edges: {} done .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.186 seconds) .. _sphx_glr_download_auto_examples_xarray_examples_basic_usage_xr.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: basic_usage_xr.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: basic_usage_xr.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_