{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to Generate a Last Interaction Velocity (LIV) Plot\n", "The Last Interaction Velocity Plot tracks and display the velocities at which different elements (or species) last interacted with packets in the simulation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, create and run a simulation for which you want to generate this plot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis import run_tardis\n", "from tardis.io.atom_data import download_atom_data\n", "\n", "# We download the atomic data needed to run the simulation\n", "download_atom_data('kurucz_cd23_chianti_H_He')\n", "\n", "sim = run_tardis(\"tardis_example.yml\", virtual_packet_logging=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "Note\n", "\n", "The virtual packet logging capability must be active in order to produce the Last Interaction Velocity Plot for virtual packets population. Thus, make sure to set `virtual_packet_logging: True` in your configuration file if you want to generate the Last Interaction Velocity Plot with virtual packets. It should be added under the `virtual` property of the `spectrum` property, as described in the [configuration schema](https://tardis-sn.github.io/tardis/io/configuration/components/spectrum.html).\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, import the plotting interface for Last Interaction Velocity Plot, i.e. the `LIVPlotter` class." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis.visualization.tools.liv_plot import LIVPlotter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And create a plotter object to process the data of simulation object `sim` for generating the Last Interaction Velocity plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter = LIVPlotter.from_simulation(sim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Static Plot (in matplotlib)\n", "You can now call the `generate_plot_mpl()` method on your plotter object to create a highly informative and visually appealing Last Interaction Velocity plot using matplotlib." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Virtual packets mode\n", "By default, a Last Interaction Velocity plot is produced for the virtual packet population of the simulation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Real packets mode\n", "You can produce a Last Interaction Velocity plot for the real packet population of the simulation by setting `packets_mode=\"real\"` which is `\"virtual\"` by default." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(packets_mode=\"real\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific wavelength range\n", "You can also restrict the wavelength range of escaped packets that you want to plot by specifying `packet_wvl_range`. It should be a quantity in Angstroms, containing two values - lower lambda and upper lambda i.e. `[lower_lambda, upper_lambda] * u.AA`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from astropy import units as u" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(packet_wvl_range=[3000, 9000] * u.AA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting only the top contributing elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `nelements` option allows you to plot the top contributing elements to the spectrum. Only the top elements are shown in the plot. Please note this works only for elements and not for ions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(nelements=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Choosing what elements/ions to plot\n", "\n", "You can pass a `species_list` for the species you want plotted in the Last Interaction Velocity Plot. Valid options include elements (e.g., Si), ions (specified in Roman numeral format, e.g., Si II), a range of ions (e.g., Si I-III), or any combination of these." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(species_list = [\"Si I-III\", \"O\", \"Ca\", \"S\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using both the `nelements` and the `species_list` options, `species_list` takes precedence. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(species_list = [\"Si I-III\", \"Ca\", \"S\"], nelements=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific number of bins\n", "You can regroup the bins with broader widths within the same velocity range using `num_bins`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(species_list = [\"Si I-III\", \"O\", \"Ca\", \"S\"], num_bins=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting on the Log Scale\n", "You can plot on the log scale on x-axis using `xlog_scale=True` and on y-axis using `ylog_scale=True` by default both are set to `False` which plots on a linear scale." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(species_list = [\"Si I-III\", \"O\", \"Ca\", \"S\"], xlog_scale=True, ylog_scale=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific velocity range\n", "You can restrict the range of bins to plot in the Last Interaction Velocity Plot by specifying a valid `velocity_range`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_mpl(species_list = [\"Si I-III\", \"O\", \"Ca\", \"S\"], velocity_range=(12500, 15050))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Additional plotting options" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To list all available options (or parameters) with their description\n", "help(plotter.generate_plot_mpl)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `generate_plot_mpl` method also has options specific to the matplotlib API, thereby providing you with more control over how your last interaction velocity looks. Possible cases where you may use them are:\n", "\n", "- `ax`: To plot on an Axis of a plot you're already working with, e.g. for subplots.\n", "\n", "- `figsize`: To resize the plot as per your requirements.\n", "\n", "- `cmapname`: To use a colormap of your preference, instead of \"jet\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive Plot (in plotly)\n", "If you're using the Last Interaction Velocity plot for exploration, consider creating an interactive version with `generate_plot_ply()`. This allows you to zoom, pan, inspect data values by hovering, resize the scale, and more conveniently.\n", "\n", "\n", "\n", "**This method takes the same arguments as `generate_plot_mpl` except for a few specific to the Plotly library.** You can produce all the plots shown above in Plotly by passing the same arguments." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Virtual packets mode\n", "By default, a Last Interaction Velocity plot is produced for the virtual packet population of the simulation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Real packets mode\n", "You can produce a Last Interaction Velocity plot for the real packet population of the simulation by setting `packets_mode=\"real\"` which is `\"virtual\"` by default." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(packets_mode=\"real\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific wavelength range\n", "You can also restrict the wavelength range of escaped packets that you want to plot by specifying `packet_wvl_range`. It should be a quantity in Angstroms, containing two values - lower lambda and upper lambda i.e. `[lower_lambda, upper_lambda] * u.AA`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from astropy import units as u" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(packet_wvl_range=[3000, 9000] * u.AA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting only the top contributing elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `nelements` option allows you to plot the top contributing elements to the spectrum. Only the top elements are shown in the plot. Please note this works only for elements and not for ions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(nelements=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Choosing what elements/ions to plot\n", "\n", "You can pass a `species_list` for the species you want plotted in the Last Interaction Velocity Plot. Valid options include elements (e.g., Si), ions (specified in Roman numeral format, e.g., Si II), a range of ions (e.g., Si I-III), or any combination of these." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(species_list = [\"Si I-III\", \"Ca\", \"S\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using both the `nelements` and the `species_list` options, `species_list` takes precedence. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(species_list = [\"Si I-III\", \"Ca\", \"S\"], nelements=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific number of bins\n", "You can regroup the bins with broader widths within the same velocity range using `num_bins`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(species_list = [\"Si I-III\", \"Ca\", \"S\"], num_bins=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting on the Log Scale\n", "You can plot on the log scale on x-axis using `xlog_scale=True` and on y-axis using `ylog_scale=True` by default both are set to `False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(species_list = [\"Si I-III\", \"Ca\", \"S\"], xlog_scale=True, ylog_scale=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting a specific velocity range\n", "You can restrict the range of bins to plot in the Last Interaction Velocity Plot by specifying a valid `velocity_range`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plotter.generate_plot_ply(species_list = [\"Si I-III\", \"Ca\", \"S\"], velocity_range=(12500, 15050))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Additional plotting options" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To list all available options (or parameters) with their description\n", "help(plotter.generate_plot_ply)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "The `generate_plot_ply` method also has options specific to the plotly API, thereby providing you with more control over how your last interaction velocity plot looks. Possible cases where you may use them are:\n", "\n", " - `fig`: To plot the last interaction velocity plot on a figure you are already using e.g. for subplots.\n", "\n", " - `graph_height`: To specify the height of the graph as needed.\n", " \n", " - `cmapname`: To use a colormap of your preference instead of \"jet\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using simulation saved as HDF\n", "Other than producing the Last Interaction Velocity Plot for simulation objects in runtime, you can also produce it for saved TARDIS simulations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# hdf_plotter = LIVPlotter.from_hdf(\"demo.h5\") ## Files is too large - just as an example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This `hdf_plotter` object is similar to the `plotter` object we used above, **so you can use each plotting method demonstrated above with this too.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Static plot with virtual packets mode\n", "# hdf_plotter.generate_plot_mpl()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Static plot with real packets mode\n", "#hdf_plotter.generate_plot_mpl(packets_mode=\"real\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Interactive plot with virtual packets mode and specific list of species\n", "# hdf_plotter.generate_plot_ply(species_list=[\"Si I-III\", \"Ca\", \"O\", \"S\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Interactive plot with virtual packets mode and regrouped bins\n", "# hdf_plotter.generate_plot_ply(num_bins=10)" ] } ], "metadata": { "kernelspec": { "display_name": "tardis", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }