.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/examples/01_getting_started/02_math_operations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_examples_01_getting_started_02_math_operations.py: Math Operations =============== Demonstrate splitting and math operations that preserve the mass balance of components. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import pandas as pd from elphick.geomet import Sample from elphick.geomet.utils.data import sample_data .. GENERATED FROM PYTHON SOURCE LINES 16-20 Load Data --------- We get some demo data in the form of a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python 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 26-28 Create Sample ------------- .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: Python obj_smpl: Sample = Sample(df_data, name='sample') print(obj_smpl) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: sample {'wet_mass': {0: 300.0}, 'mass_dry': {0: 260.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.0}, 'SiO2': {0: 3.5153846153846153}, 'Al2O3': {0: 1.8730769230769235}, 'LOI': {0: 4.0}} .. GENERATED FROM PYTHON SOURCE LINES 34-39 Split the Sample ---------------- Split the Sample and return the complement of the split fraction. Splitting does not modify the absolute grade of the input. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python obj_smpl_split, obj_smpl_comp = obj_smpl.split(fraction=0.1, include_supplementary_data=True) print(obj_smpl_split) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: sample_1 {'wet_mass': {0: 30.0}, 'mass_dry': {0: 26.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.0}, 'SiO2': {0: 3.5153846153846153}, 'Al2O3': {0: 1.873076923076923}, 'LOI': {0: 4.0}} .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python print(obj_smpl_comp) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: sample_2 {'wet_mass': {0: 270.0}, 'mass_dry': {0: 234.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.0}, 'SiO2': {0: 3.515384615384616}, 'Al2O3': {0: 1.873076923076923}, 'LOI': {0: 4.000000000000001}} .. GENERATED FROM PYTHON SOURCE LINES 47-52 Operands -------- The math operands +, -, / are supported for the Sample object. We'll add the split and complement parts. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python obj_smpl_sum: Sample = obj_smpl_split + obj_smpl_comp print(obj_smpl_sum) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: None {'wet_mass': {0: 300.0}, 'mass_dry': {0: 260.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.00000000000001}, 'SiO2': {0: 3.5153846153846153}, 'Al2O3': {0: 1.8730769230769235}, 'LOI': {0: 4.000000000000001}} .. GENERATED FROM PYTHON SOURCE LINES 58-60 Notice the name of the resultant sample object is None. We'll confirm the sum of the splits is materially equivalent to the starting object. .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python pd.testing.assert_frame_equal(obj_smpl.data, obj_smpl_sum.data) .. GENERATED FROM PYTHON SOURCE LINES 65-66 Add finally add and then subtract the split portion to the original object, and check the output. .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python obj_smpl_sum: Sample = obj_smpl + obj_smpl_split obj_smpl_minus: Sample = obj_smpl_sum - obj_smpl_split pd.testing.assert_frame_equal(obj_smpl_minus.data, obj_smpl.data) print(obj_smpl_minus) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: None {'wet_mass': {0: 300.0}, 'mass_dry': {0: 260.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.0}, 'SiO2': {0: 3.5153846153846153}, 'Al2O3': {0: 1.8730769230769235}, 'LOI': {0: 4.0}} .. GENERATED FROM PYTHON SOURCE LINES 74-75 Demonstrate division. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python obj_smpl_div: Sample = obj_smpl_split / obj_smpl print(obj_smpl_div) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: None {'wet_mass': {0: 0.30000000000000004}, 'mass_dry': {0: 0.30000000000000004}, 'H2O': {0: 0.0}, 'Fe': {0: 100.0}, 'SiO2': {0: 100.0}, 'Al2O3': {0: 99.99999999999997}, 'LOI': {0: 99.99999999999997}} .. GENERATED FROM PYTHON SOURCE LINES 81-85 Methods ------- Performing math operations with methods allows the resultant objects to be renamed. .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: Python obj_smpl_sum_renamed: Sample = obj_smpl.add(obj_smpl_split, name='Summed object') print(obj_smpl_sum_renamed) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: Summed object {'wet_mass': {0: 330.0}, 'mass_dry': {0: 286.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.00000000000001}, 'SiO2': {0: 3.5153846153846153}, 'Al2O3': {0: 1.8730769230769235}, 'LOI': {0: 4.000000000000001}} .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python obj_smpl_sub_renamed: Sample = obj_smpl.sub(obj_smpl_split, name='Subtracted object') print(obj_smpl_sub_renamed) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample: Subtracted object {'wet_mass': {0: 270.0}, 'mass_dry': {0: 234.0}, 'H2O': {0: 13.333333333333334}, 'Fe': {0: 59.0}, 'SiO2': {0: 3.515384615384616}, 'Al2O3': {0: 1.8730769230769235}, 'LOI': {0: 4.0}} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.827 seconds) .. _sphx_glr_download_auto_examples_examples_01_getting_started_02_math_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_math_operations.ipynb <02_math_operations.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_math_operations.py <02_math_operations.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_