.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/visualizing_contributions.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_visualizing_contributions.py: Visualizing Contributions ========================= This example illustrates the use of the various tools for visualizing contributions to a TSUNAMI-IP integral index (such as :math:`E`, or even contributions to :math:`(\Delta k/k)^2`). This example shows the visualization of :math:`(\Delta k/k)^2` contributions, but the same tools can be used for visualizing contributions to :math:`E`. .. GENERATED FROM PYTHON SOURCE LINES 11-19 Isotope-Wise Contributions -------------------------- First, we demonstrate the tools for visualizing `isotope-wise` contributions to the nuclear data induced variance, :math:`(\Delta k/k)^2`. First we need to get the contributions (further details are provided in :ref:`sphx_glr_auto_examples_uncertainty_contributions.py`). Note that visualizing the contributions to the nuclear data induced `uncertainty` with these tools will produce visually misleading plots, as the uncertainty contributions don't add to the total nuclear data induced uncertainty, so their representation in a pie chart is dubious, and in certain cases causes the reaction-wise plots to have incorrect nuclide-wise totals. .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: Python from tsunami_ip_utils.integral_indices import get_uncertainty_contributions from paths import EXAMPLES application_filenames = [ f"{EXAMPLES}/data/example_sdfs/MCT/MIX-COMP-THERM-001-001.sdf" ] uncertainty_contributions_nuclide, uncertainty_contributions_nuclide_reaction = \ get_uncertainty_contributions(application_filenames, variance=True) .. GENERATED FROM PYTHON SOURCE LINES 28-32 The isotope-wise contributions can be plotted using the :func:`tsunami_ip_utils.viz.contribution_plot` function. This function takes a contribution list and creates a plot of the specified type. First, we must extract a list of contributions for a specific application from the :func:`tsunami_ip_utils.integral_indices.get_uncertainty_contributions` output. .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python contributions = uncertainty_contributions_nuclide['application'][0] .. GENERATED FROM PYTHON SOURCE LINES 35-38 Bar Chart ^^^^^^^^^ The contributions can then be plotted as a matplotlib bar chart. .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python from tsunami_ip_utils.viz.viz import contribution_plot import matplotlib.pyplot as plt fig, axs = contribution_plot(contributions, plot_type='bar',integral_index_name='$\\%(\\Delta k/k)^2$') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_visualizing_contributions_001.png :alt: Contributions to $\%(\Delta k/k)^2$ :srcset: /auto_examples/images/sphx_glr_visualizing_contributions_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-50 Pie Chart ^^^^^^^^^ The contributions can also be plotted as a matplotlib pie chart. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. code-block:: Python fig, axs = contribution_plot(contributions, plot_type='pie',integral_index_name='$\\%(\\Delta k/k)^2$') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_visualizing_contributions_002.png :alt: Contributions to $\%(\Delta k/k)^2$ :srcset: /auto_examples/images/sphx_glr_visualizing_contributions_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 56-59 Interactive Pie Chart ^^^^^^^^^^^^^^^^^^^^^ The contributions can also be plotted as an interactive plotly pie chart. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python fig = contribution_plot(contributions, plot_type='interactive_pie', integral_index_name='%(Δk/k)^2', interactive_legend=False) fig.show() .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 64-68 Interactive Pie Chart With Interactive Legend ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The contributions can also be plotted as an interactive plotly pie chart with an interactive legend. This feature has yet to be formally implemented in Plotly Express (although an issue is open `here `_ ). .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python fig = contribution_plot(contributions, plot_type='interactive_pie', integral_index_name='%(Δk/k)^2', interactive_legend=True) fig.show() .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 73-75 Note that inner wedges in the pie chart (which may be difficult to select or see manually) can be selected by clicking on the corresponding legend entry. This will highlight the corresponding wedge in the pie chart. .. GENERATED FROM PYTHON SOURCE LINES 77-80 Reaction-Wise Contributions --------------------------- The isotope-reaction-wise contributions can be plotted similarly. .. GENERATED FROM PYTHON SOURCE LINES 82-84 Bar Chart ^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 84-91 .. code-block:: Python contributions = uncertainty_contributions_nuclide_reaction['application'][0] fig, axs = contribution_plot(contributions, plot_type='bar',integral_index_name='$\\%(\\Delta k/k)^2$') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_visualizing_contributions_003.png :alt: Contributions to $\%(\Delta k/k)^2$ (including redundant/irrelvant reactions) :srcset: /auto_examples/images/sphx_glr_visualizing_contributions_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 92-97 Note that, in this case, since the reaction wise contributions aren't strictly positive (the isotope-wise contributions aren't either for dissimilar nuclides, but this example only contains like nuclide covariances), the bar chart displays negative contributions `below` the x-axis and positive contributions `above` the x-axis. Then, to visualize how the positive and negative contributions cancel out, a hatched "effective" contribution box is displayed, which corresponds to the isotope wise contribution. .. GENERATED FROM PYTHON SOURCE LINES 99-101 Pie Chart ^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: Python fig, axs = contribution_plot(contributions, plot_type='pie',integral_index_name='$\\%(\\Delta k/k)^2$') plt.tight_layout() plt.show() .. image-sg:: /auto_examples/images/sphx_glr_visualizing_contributions_004.png :alt: Contributions to $\%(\Delta k/k)^2$ (including redundant/irrelvant reactions) :srcset: /auto_examples/images/sphx_glr_visualizing_contributions_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-111 In the pie chart above, you can see that the isotope-wise contributions are on the inner ring, and the reaction-wise contributions are on the outer ring. The `negative` (reaction-wise) contributions are hatched, and to demonstrate how the positive and negative contributions cancel out, there is white space in the inner ring corresponding to the contribution that was "lost" due to the cancellations. .. GENERATED FROM PYTHON SOURCE LINES 113-115 Interactive Pie Chart ^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 115-119 .. code-block:: Python fig = contribution_plot(contributions, plot_type='interactive_pie', integral_index_name='%(Δk/k)^2', interactive_legend=False) fig.show() .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 120-126 The interactive pie chart (a Plotly Express `sunburst chart `_) takes a different approach than the static (matplotlib) pie chart in the previous section. The contributions are delineated first by nuclide, then within each nuclide, by wether the contribution is positive or negative (these wedges are weighted by the absolute value of the sum of the contributions of the respective sign) and finally by reaction. As with all Plotly Express sunburst charts, clicking on a given wedge will zoom in on that wedge, and clicking on the center will zoom out - easily allowing the user to explore contributions of interest. .. GENERATED FROM PYTHON SOURCE LINES 128-130 Interactive Pie Chart With Interactive Legend ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 130-134 .. code-block:: Python fig = contribution_plot(contributions, plot_type='interactive_pie', integral_index_name='%(Δk/k)^2', interactive_legend=True) fig.show() .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 135-136 The interactive legend in this case serves more of a purpose, because the legend entries are clickable, and expand the reaction-wise contributions for a specific nuclide. This can be helpful if small contributions are of interest. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.083 seconds) .. _sphx_glr_download_auto_examples_visualizing_contributions.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: visualizing_contributions.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: visualizing_contributions.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: visualizing_contributions.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_