Math Operations

Demonstrate splitting and math operations that preserve the mass balance of components.

import pandas as pd

from elphick.geomet import Sample
from elphick.geomet.utils.data import sample_data

Load Data

We get some demo data in the form of a pandas DataFrame

df_data: pd.DataFrame = sample_data()
print(df_data.head())
       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

Create Sample

obj_smpl: Sample = Sample(df_data, name='sample')
print(obj_smpl)
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}}

Split the Sample

Split the Sample and return the complement of the split fraction. Splitting does not modify the absolute grade of the input.

obj_smpl_split, obj_smpl_comp = obj_smpl.split(fraction=0.1, include_supplementary_data=True)
print(obj_smpl_split)
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}}
print(obj_smpl_comp)
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}}

Operands

The math operands +, -, / are supported for the Sample object. We’ll add the split and complement parts.

obj_smpl_sum: Sample = obj_smpl_split + obj_smpl_comp
print(obj_smpl_sum)
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}}

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.

pd.testing.assert_frame_equal(obj_smpl.data, obj_smpl_sum.data)

Add finally add and then subtract the split portion to the original object, and check the output.

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)
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}}

Demonstrate division.

obj_smpl_div: Sample = obj_smpl_split / obj_smpl
print(obj_smpl_div)
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}}

Methods

Performing math operations with methods allows the resultant objects to be renamed.

obj_smpl_sum_renamed: Sample = obj_smpl.add(obj_smpl_split, name='Summed object')
print(obj_smpl_sum_renamed)
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}}
obj_smpl_sub_renamed: Sample = obj_smpl.sub(obj_smpl_split, name='Subtracted object')
print(obj_smpl_sub_renamed)
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}}

Total running time of the script: (0 minutes 0.930 seconds)

Gallery generated by Sphinx-Gallery