Constrain

A simple example that demonstrates the constrain method of mass-composition.

It is possible that a MassComposition object is created from a Machine Learning Model estimation. If either the ML model is over-fitted, or the features supplied to create the estimation are out of range, improbable results can be generated. The constrain method will provide a way to manage these outliers.

import pandas as pd

from elphick.mass_composition import MassComposition
from elphick.mass_composition.datasets.sample_data import sample_data

Create a MassComposition object

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

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

obj_mc: MassComposition = MassComposition(df_data)
obj_mc.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 57.0 5.2 3.0 5.0 grp_1
1 90.0 80.0 11.111111 59.0 3.1 1.7 4.0 grp_1
2 110.0 90.0 18.181818 61.0 2.2 0.9 3.0 grp_2


Constraining by Clip

Constraining by clip simpy clips the mass or composition values. The simplest way to constrain is with a tuple of the limits.

obj_1: MassComposition = obj_mc.constrain(clip_mass=(85, 100))
obj_1.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 57.0 5.2 3.0 5.0 grp_1
1 90.0 85.0 5.555556 59.0 3.1 1.7 4.0 grp_1
2 100.0 90.0 10.000000 61.0 2.2 0.9 3.0 grp_2


Notice that the mass has been constrained for some records and the H2O has been modified accordingly.

More granularity is possible by passing a dict[variable: tuple_of_limits]

obj_2: MassComposition = obj_mc.constrain(clip_mass={'mass_wet': (0, 100)})
obj_2.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 57.0 5.2 3.0 5.0 grp_1
1 90.0 80.0 11.111111 59.0 3.1 1.7 4.0 grp_1
2 100.0 90.0 10.000000 61.0 2.2 0.9 3.0 grp_2


Constraining Relative to Another Object

Sometimes constraining relative to another object is useful. This can be described as “constraining by recovery”. The object is converted to absolute mass (where components are converted to mass units) and divided by the reference (other) object, also converted to mass units. In mineral processing, this is known as recovery.

First we’ll make another object to act as our reference.

obj_other: MassComposition = obj_mc.add(obj_mc, name='feed')

obj_3: MassComposition = obj_mc.constrain(relative_mass=(0.0, 0.1), other=obj_other)
obj_3.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 20.0 18.0 10.000000 57.0 5.2 3.0 5.0 grp_1
1 18.0 16.0 11.111111 59.0 3.1 1.7 4.0 grp_1
2 22.0 18.0 18.181818 61.0 2.2 0.9 3.0 grp_2


Here we constrain Fe to 10% recovery of 2 x the original object…

obj_4: MassComposition = obj_mc.constrain(relative_composition={'Fe': (0.0, 0.1)}, other=obj_other)
obj_4.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 11.4 5.2 3.0 5.0 grp_1
1 90.0 80.0 11.111111 11.8 3.1 1.7 4.0 grp_1
2 110.0 90.0 18.181818 12.2 2.2 0.9 3.0 grp_2


Arguments can be combined to perform multiple constraints in one call.

obj_5: MassComposition = obj_mc.constrain(clip_mass=(85, 100),
                                          relative_composition={'Fe': (0.0, 0.1)}, other=obj_other)
obj_5.data.to_dataframe()
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 11.4 5.2 3.0 5.0 grp_1
1 90.0 80.0 11.111111 11.8 3.1 1.7 4.0 grp_1
2 110.0 90.0 18.181818 12.2 2.2 0.9 3.0 grp_2


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

Gallery generated by Sphinx-Gallery