.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/examples/02_interval_sample/05_resampling_interval_data.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_02_interval_sample_05_resampling_interval_data.py: Resampling Interval Data ======================== Interval (or fractional) data is common in metallurgy and mineral processing. Samples are sized using sieves in a laboratory and each resultant fraction is often assayed to determine chemical composition. The typical nomenclature is of the interval edges is size_retained, size passing - any particle within an interval or fraction was retained by the lower sieve size, but passed the sieve size above it. .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python import logging import numpy as np import pandas as pd import plotly from elphick.geomet import IntervalSample from elphick.geomet.datasets.sample_data import size_by_assay from elphick.geomet.utils.size import sizes_all .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python logging.basicConfig(level=logging.WARNING, format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s', datefmt='%Y-%m-%dT%H:%M:%S%z', ) .. GENERATED FROM PYTHON SOURCE LINES 28-33 Create a MassComposition object ------------------------------- We get some demo data in the form of a pandas DataFrame We create this object as 1D based on the pandas index .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python df_data: pd.DataFrame = size_by_assay() df_data .. raw:: html
mass_dry fe sio2 al2o3
size_retained size_passing
0.850 2.000 3.3 64.15 2.04 2.68
0.500 0.850 9.9 64.33 2.05 2.23
0.150 0.500 26.5 64.52 1.84 2.19
0.075 0.150 2.5 62.65 2.88 3.32
0.045 0.075 8.8 62.81 2.12 2.25
0.000 0.045 49.0 55.95 6.39 6.34


.. GENERATED FROM PYTHON SOURCE LINES 39-40 The size index is of the Interval type, maintaining the fractional information. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python size_fractions: IntervalSample = IntervalSample(df_data, name='Sample', moisture_in_scope=False) size_fractions.data .. raw:: html
mass_dry Fe SiO2 Al2O3
size
[0.85, 2.0) 3.3 64.15 2.04 2.68
[0.5, 0.85) 9.9 64.33 2.05 2.23
[0.15, 0.5) 26.5 64.52 1.84 2.19
[0.075, 0.15) 2.5 62.65 2.88 3.32
[0.045, 0.075) 8.8 62.81 2.12 2.25
[0.0, 0.045) 49.0 55.95 6.39 6.34


.. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python size_fractions.aggregate .. raw:: html
mass_dry Fe SiO2 Al2O3
0 100.0 60.09245 4.14753 4.27716


.. GENERATED FROM PYTHON SOURCE LINES 50-53 Plot the original sample intervals ---------------------------------- First we'll plot the intervals of the original sample .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python fig = size_fractions.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=False) plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_05_resampling_interval_data_001.html .. GENERATED FROM PYTHON SOURCE LINES 59-61 Size distributions are often plotted in the cumulative form. Cumulative passing is achieved by setting the direction = ascending. .. GENERATED FROM PYTHON SOURCE LINES 62-67 .. code-block:: Python fig = size_fractions.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=True, direction='ascending') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 68-71 Resample on a defined grid -------------------------- Now we will resample on a defined grid (interval edges) and view the resampled fractions .. GENERATED FROM PYTHON SOURCE LINES 71-82 .. code-block:: Python new_edges = np.unique(np.geomspace(1.0e-03, size_fractions.data.index.right.max(), 50)) new_coords = np.insert(new_edges, 0, 0) upsampled: IntervalSample = size_fractions.resample_1d(interval_edges=new_edges, precision=3, include_original_edges=True) fig = upsampled.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=False) # noinspection PyTypeChecker plotly.io.show(fig) .. raw:: html :file: images/sphx_glr_05_resampling_interval_data_002.html .. GENERATED FROM PYTHON SOURCE LINES 83-86 Close inspection of the plot above reals some sharp dips for some mass intervals. This is caused by those intervals being narrower than the adjacent neighbours, hence they have less absolute mass. This is a visual artefact only, numerically it is correct, as shown by the cumulative plot. .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python fig = upsampled.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=True, direction='ascending') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 91-96 Up-sample by a factor --------------------- We can up-sample each of the original fraction by a factor. Since adjacent fractions are similar, the fractional plot is reasonably smooth. Note however, that fraction widths are still different, caused by the original sieve selection. .. GENERATED FROM PYTHON SOURCE LINES 96-101 .. code-block:: Python upsampled_2: IntervalSample = size_fractions.resample_1d(interval_edges=10, precision=3) fig = upsampled_2.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=False) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 102-106 Up-sample to a sieve series --------------------------- Standard sieve series account for the log nature of a particle size distribution. This results in reasonably equal width intervals on a log scale. We will up-sample to a standard sieve series and plot the results. .. GENERATED FROM PYTHON SOURCE LINES 106-111 .. code-block:: Python new_sizes = [s for s in sizes_all if s >= size_fractions.data.index.left.min() and s <= size_fractions.data.index.right.max()] new_sizes = sorted(new_sizes) .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: Python upsampled_3: IntervalSample = size_fractions.resample_1d(interval_edges=new_sizes, precision=3) fig = upsampled_3.plot_intervals(variables=['mass_dry', 'Fe', 'SiO2', 'Al2O3'], cumulative=False) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 117-118 Validate the head grade against the original sample .. GENERATED FROM PYTHON SOURCE LINES 118-128 .. code-block:: Python pd.testing.assert_frame_equal(size_fractions.aggregate.reset_index(drop=True), upsampled.aggregate.reset_index(drop=True)) pd.testing.assert_frame_equal(size_fractions.aggregate.reset_index(drop=True), upsampled_2.aggregate.reset_index(drop=True)) pd.testing.assert_frame_equal(size_fractions.aggregate.reset_index(drop=True), upsampled_3.aggregate.reset_index(drop=True)) .. GENERATED FROM PYTHON SOURCE LINES 129-130 Complete a round trip by converting the up-sampled objects back to the original intervals and validate. .. GENERATED FROM PYTHON SOURCE LINES 130-141 .. code-block:: Python orig_index = size_fractions.data.index original_edges: np.ndarray = np.sort(np.unique(list(orig_index.left) + list(orig_index.right))) downsampled: IntervalSample = upsampled.resample_1d(interval_edges=original_edges, precision=3) downsampled_2: IntervalSample = upsampled_2.resample_1d(interval_edges=original_edges, precision=3) downsampled_3: IntervalSample = upsampled_3.resample_1d(interval_edges=original_edges, precision=3) pd.testing.assert_frame_equal(size_fractions.data, downsampled.data) pd.testing.assert_frame_equal(size_fractions.data, downsampled_2.data) pd.testing.assert_frame_equal(size_fractions.data, downsampled_3.data) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.104 seconds) .. _sphx_glr_download_auto_examples_examples_02_interval_sample_05_resampling_interval_data.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05_resampling_interval_data.ipynb <05_resampling_interval_data.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05_resampling_interval_data.py <05_resampling_interval_data.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_