Note
Click here to download the full example code
Math Operations
Demonstrate splitting and math operations that preserve the mass balance of components.
import pandas as pd
from elphick.mass_composition.datasets.sample_data import sample_data
from elphick.mass_composition import MassComposition
Create a mass-composition (mc) enabled Xarray Dataset
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
# Construct a MassComposition object and standardise the chemistry variables
obj_mc: MassComposition = MassComposition(df_data)
print(obj_mc)
unnamed
<xarray.Dataset> 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: unnamed
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
Split the original Dataset and return the complement of the split fraction. Splitting does not modify the absolute grade of the input.
obj_mc_split, obj_mc_comp = obj_mc.split(fraction=0.1)
print(obj_mc_split)
(0.1 * unnamed)
<xarray.Dataset> Size: 216B
Dimensions: (index: 3)
Coordinates:
* index (index) int64 24B 0 1 2
Data variables:
mass_wet (index) float64 24B 10.0 9.0 11.0
mass_dry (index) float64 24B 9.0 8.0 9.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.1 * unnamed)
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
print(obj_mc_comp)
(0.9 * unnamed)
<xarray.Dataset> Size: 216B
Dimensions: (index: 3)
Coordinates:
* index (index) int64 24B 0 1 2
Data variables:
mass_wet (index) float64 24B 90.0 81.0 99.0
mass_dry (index) float64 24B 81.0 72.0 81.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.9 * unnamed)
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
Add the split and complement parts using the mc.add method
obj_mc_sum: MassComposition = obj_mc_split + obj_mc_comp
print(obj_mc_sum)
((0.1 * unnamed) + (0.9 * unnamed))
<xarray.Dataset> 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.1 * unnamed) + (0.9 * unnamed))
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
Confirm the sum of the splits is materially equivalent to the starting object.
pd.testing.assert_frame_equal(obj_mc.data.to_dataframe(), obj_mc_sum.data.to_dataframe())
Add finally add and then subtract the split portion to the original object, and check the output.
obj_mc_sum: MassComposition = obj_mc + obj_mc_split
obj_mc_minus: MassComposition = obj_mc_sum - obj_mc_split
pd.testing.assert_frame_equal(obj_mc_minus.data.to_dataframe(), obj_mc.data.to_dataframe())
print(obj_mc_minus)
((unnamed + (0.1 * unnamed)) - (0.1 * unnamed))
<xarray.Dataset> 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: ((unnamed + (0.1 * unnamed)) - (0.1 * unnamed))
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
Demonstrate division.
obj_mc_div: MassComposition = obj_mc_split / obj_mc
print(obj_mc_div)
((0.1 * unnamed) / unnamed)
<xarray.Dataset> Size: 216B
Dimensions: (index: 3)
Coordinates:
* index (index) int64 24B 0 1 2
Data variables:
mass_wet (index) float64 24B 0.1 0.1 0.1
mass_dry (index) float64 24B 0.1 0.1 0.1
H2O (index) float64 24B 0.0 0.0 0.0
Fe (index) float64 24B 0.1 0.1 0.1
SiO2 (index) float64 24B 0.1 0.1 0.1
Al2O3 (index) float64 24B 0.1 0.1 0.1
LOI (index) float64 24B 0.1 0.1 0.1
group (index) object 24B 'grp_1' 'grp_1' 'grp_2'
Attributes:
mc_name: ((0.1 * unnamed) / unnamed)
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
Math operations with rename The alternative syntax, methods rather than operands, allows renaming of the result object
obj_mc_sum_renamed: MassComposition = obj_mc.add(obj_mc_split, name='Summed object')
print(obj_mc_sum_renamed)
Summed object
<xarray.Dataset> Size: 216B
Dimensions: (index: 3)
Coordinates:
* index (index) int64 24B 0 1 2
Data variables:
mass_wet (index) float64 24B 110.0 99.0 121.0
mass_dry (index) float64 24B 99.0 88.0 99.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: Summed object
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
obj_mc_sub_renamed: MassComposition = obj_mc.sub(obj_mc_split, name='Subtracted object')
print(obj_mc_sum_renamed)
print('done')
Summed object
<xarray.Dataset> Size: 216B
Dimensions: (index: 3)
Coordinates:
* index (index) int64 24B 0 1 2
Data variables:
mass_wet (index) float64 24B 110.0 99.0 121.0
mass_dry (index) float64 24B 99.0 88.0 99.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: Summed object
mc_vars_mass: ['mass_wet', 'mass_dry']
mc_vars_chem: ['Fe', 'SiO2', 'Al2O3', 'LOI']
mc_vars_attrs: ['group']
mc_interval_edges: {}
done
Total running time of the script: ( 0 minutes 0.451 seconds)