
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/05_heatmap.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_05_heatmap.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_05_heatmap.py:


Heatmap
=======

A heatmap allows you to visualize the distribution of a specific attribute across a 3D block model from
a 2D perspective.

.. GENERATED FROM PYTHON SOURCE LINES 9-23

.. code-block:: Python


    import tempfile
    from typing import cast

    from pathlib import Path

    import numpy as np
    import plotly.express as px
    import plotly.graph_objects as go
    import plotly.io as pio

    from parq_blockmodel import ParquetBlockModel
    from parq_blockmodel.utils.geometry_utils import rotate_points








.. GENERATED FROM PYTHON SOURCE LINES 24-32

Create a Toy Parquet Block Model
--------------------------------
We leverage the create_toy_blockmodel class method to create a model with
a synthetic Fe distribution, which makes threshold heatmaps more informative.

Use a finer grid for a smoother heatmap while preserving the same overall
model extent. ``axis_azimuth`` is measured from +x (east), so 30° from north
corresponds to 60° from east.

.. GENERATED FROM PYTHON SOURCE LINES 32-63

.. code-block:: Python


    temp_dir = Path(tempfile.gettempdir()) / "block_model_example"
    temp_dir.mkdir(parents=True, exist_ok=True)

    axis_azimuth = 60.0  # 30° east of north
    deposit_center = tuple(
        rotate_points(
            np.array([[10.0, 7.5, 5.0]]),
            azimuth=axis_azimuth,
            dip=0.0,
            plunge=0.0,
        )[0]
    )

    pbm: ParquetBlockModel = ParquetBlockModel.create_toy_blockmodel(
        filename=temp_dir / "toy_block_model.parquet",
        shape=(40, 30, 20),
        block_size=(0.5, 0.5, 0.5),
        corner=(0.0, 0.0, 0.0),
        axis_azimuth=axis_azimuth,
        axis_dip=0.0,
        axis_plunge=0.0,
        grade_name="fe",
        grade_min=50.0,
        grade_max=65.0,
        deposit_center=deposit_center,
        deposit_radii=(8.0, 5.0, 3.0),
        noise_std=0.0,
    )
    pbm





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ParquetBlockModel(name=toy_block_model, path=/tmp/block_model_example/toy_block_model.pbm)



.. GENERATED FROM PYTHON SOURCE LINES 64-67

Create Heatmap
--------------
We'll create a plan-view heatmap showing blocks above a high-Fe threshold.

.. GENERATED FROM PYTHON SOURCE LINES 67-73

.. code-block:: Python


    fig: go.Figure = pbm.plot_heatmap(attribute='fe',
                                      threshold=57.0,
                                      axis='z')
    pio.show(fig)  # type: ignore[arg-type]




.. raw:: html
    :file: images/sphx_glr_05_heatmap_001.html





.. GENERATED FROM PYTHON SOURCE LINES 74-75

View the underlying heatmap matrix.

.. GENERATED FROM PYTHON SOURCE LINES 75-85

.. code-block:: Python

    heatmap_matrix = cast(
        np.ndarray,
        pbm.create_heatmap_from_threshold(attribute='fe',
                                          threshold=57.0,
                                          axis='z',
                                          return_array=True),
    )

    heatmap_matrix





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    pyvista_ndarray([[0, 0, 0, ..., 0, 0, 0],
                     [0, 0, 0, ..., 0, 0, 0],
                     [0, 0, 0, ..., 0, 0, 0],
                     ...,
                     [0, 0, 0, ..., 0, 0, 0],
                     [0, 0, 0, ..., 0, 0, 0],
                     [0, 0, 0, ..., 0, 0, 0]])



.. GENERATED FROM PYTHON SOURCE LINES 86-87

Plot the returned matrix directly with Plotly as well.

.. GENERATED FROM PYTHON SOURCE LINES 87-95

.. code-block:: Python

    matrix_fig: go.Figure = px.imshow(
        heatmap_matrix,
        origin="lower",
        color_continuous_scale="Viridis",
        labels={"x": "i", "y": "j", "color": "count above Fe threshold"},
        title="Heatmap matrix derived from thresholded Fe",
    )
    pio.show(matrix_fig)  # type: ignore[arg-type]



.. raw:: html
    :file: images/sphx_glr_05_heatmap_002.html






.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_auto_examples_05_heatmap.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 05_heatmap.ipynb <05_heatmap.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 05_heatmap.py <05_heatmap.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 05_heatmap.zip <05_heatmap.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
