tardis.visualization.plot_util module

Utility functions to be used in plotting.

tardis.visualization.plot_util.axis_label_in_latex(label_text, unit, only_text=True)[source]

Get axis label for plotly plots that can show units in latex.

Parameters:
label_textstr

Text to show on label, may be expressed in latex

unitastropy.units

Unit of the label which needs to be expressed in latex

only_textbool

If label_text is expressed purely in text (i.e. without using latex) or not. Default value is True

Returns:
str

Latex string for label renderable by plotly

tardis.visualization.plot_util.extract_and_process_packet_data(simulation, packets_mode)[source]

Extract and process packet data from the simulation object.

This includes converting the packet data into a DataFrame and appending processed line interaction information.

Parameters:
simulationtardis.simulation.BaseSimulation

Full TARDIS simulation object containing transport state, plasma, and atomic data.

packets_modestr

Type of packets to extract: - ‘virtual’: Use virtual packet tracker. - ‘real’: Use emitted real packets.

Returns:
dict

Dictionary containing raw packet data, the full DataFrame packets_df, and a filtered packets_df_line_interaction with line interaction info.

tardis.visualization.plot_util.extract_and_process_packet_data_hdf(hdf, packets_mode)[source]

Extract and process packet data from an HDF file.

This function retrieves packet data from an HDF file and processes it based on the specified packet mode (either “virtual” or “real”). The extracted data is organized into a dictionary and further processed to include line interaction information.

Parameters:
hdfh5py.File or dict-like

The HDF file object containing the simulation data.

packets_modestr

The mode of packets to process. Can be “virtual” for virtual packets or any other value for real packets.

Returns:
dict

A dictionary containing the processed packet data.

Raises:
KeyError

If required keys are missing in the HDF file.

ValueError

If an invalid packets_mode is provided.

tardis.visualization.plot_util.extract_spectrum_data_hdf(hdf, packets_mode)[source]

Extract spectrum data from HDF5.

Parameters:
hdfh5py.File

Open HDF5 file containing simulation output.

packets_modestr

Packet mode to extract spectrum data for (e.g., ‘real’, ‘virtual’).

Returns:
dict

Dictionary containing: - “spectrum_delta_frequency” : Quantity

Frequency bin width in Hz.

  • “spectrum_frequency_bins”Quantity

    Frequency bin edges in Hz.

  • “spectrum_luminosity_density_lambda”Quantity

    Luminosity density in erg / s / Å.

  • “spectrum_wavelength”Quantity

    Wavelength values in Å.

tardis.visualization.plot_util.get_hex_color_strings(length, name='jet')[source]

Generate a list of hex color strings from a discrete colormap.

Parameters:
lengthint

Number of discrete colors to extract from the colormap.

namestr, optional

Name of the Matplotlib colormap to use (default is ‘jet’).

Returns:
list of str

List of hex color strings ([‘#ff0000’, ‘#00ff00’, ‘#0000ff’])

tardis.visualization.plot_util.get_mid_point_idx(arr)[source]

Get index of the middle point of a sorted array (ascending or descending).

The values in array may not be evenly distributed so it picks the middle point not by index but by their values.

Parameters:
arrnp.array
Returns:
int
tardis.visualization.plot_util.get_spectrum_data(packets_mode, sim)[source]

Get spectrum data from simulation based on mode.

Parameters:
packets_modestr

Packet mode to extract spectrum data for (e.g., ‘real’, ‘virtual’).

simSimulation

Simulation object containing the spectrum solver and data.

Returns:
dict

Dictionary containing: - “spectrum_delta_frequency” : Quantity

Frequency bin width in Hz.

  • “spectrum_frequency_bins”Quantity

    Frequency bin edges in Hz.

  • “spectrum_luminosity_density_lambda”Quantity

    Luminosity density in erg / s / Å.

  • “spectrum_wavelength”Quantity

    Wavelength values in Å.

tardis.visualization.plot_util.make_colorbar_labels(species, species_list=None, species_mapped=None)[source]

Generate labels for the colorbar based on species.

Parameters:
specieslist of int

List of species identifiers (Z * 100 + ion) or atomic numbers.

species_listlist, optional

Optional list of species to filter against.

species_mappeddict, optional

Mapping from species key (Z * 100 + ion) to lists of species IDs.

Returns:
list of str

List of formatted species labels

tardis.visualization.plot_util.parse_species_list_util(species_list)[source]

Parse user-requested species list and create list of species IDs to be used.

The function interprets element or ion names and ion ranges, converting them into (Z, ion) tuples, where Z is the atomic number and ion is the zero-based ionization stage.

Parameters:
species_listlist of str

List of species that the user wants to show in distinct colors. Species can be given as: - An ion (e.g. ‘Fe II’) - An element (e.g. ‘Ca’) - A range of ions (e.g. ‘Si I - V’) - A combination of the above (e.g. [‘Si II’, ‘Fe I - III’, ‘Ca’])

Returns:
species_mapped_resultdict

Dictionary mapping (Z, ion) to lists of (Z, ion) tuples.

species_list_resultlist of tuple

Flattened list of all (Z, ion) tuples to be used.

keep_colour_resultlist of int

Atomic numbers of elements that should be grouped by color.

full_species_listlist of str

Expanded list of user-requested species in string format.

Examples

‘Fe II’ -> [(26, 1)] ‘Ca’ -> [(20, 0), (20, 1), …, (20, 19)] ‘Si I - V’ -> [(14, 0), (14, 1), (14, 2), (14, 3), (14, 4)]

tardis.visualization.plot_util.process_line_interactions(packet_data, lines_df)[source]

Add line interaction metadata to the packet DataFrame.

Filters packets that experienced a line interaction and computes: - Atomic number of the last interaction (out). - Species ID (Z * 100 + ion number).

Parameters:
packet_datadict

Dictionary containing a ‘packets_df’ DataFrame.

lines_dfpandas.DataFrame

DataFrame with ‘atomic_number’ and ‘ion_number’ indexed by line ID.

tardis.visualization.plot_util.to_rgb255_string(color_tuple)[source]

Convert a matplotlib RGBA tuple to a generic RGB 255 string.

Parameters:
color_tupletuple

Matplotlib RGBA tuple of float values in closed interval [0, 1]

Returns:
str

RGB string of format rgb(r,g,b) where r,g,b are integers between 0 and 255 (both inclusive)