Plot Pixel Data: Time Slice

This example demonstrates how to read the HDF5 output of Luna using h5py and pandas and shows you how to convert the ToA time units to whatever you like.

Luna Command

./tpx3dump process -i /Users/Ciaran/atlassian-bitbucket-pipelines-runner/temp/e71169e4-520a-5b30-a5ab-ee8a44eb5fac/build/docs/source/_static/example_data.tpx3 -o /Users/Ciaran/atlassian-bitbucket-pipelines-runner/temp/e71169e4-520a-5b30-a5ab-ee8a44eb5fac/build/docs/source/_static/example_data.h5 --eps-t 130ns --eps-s 1 --layout single

Python Script

Plot a 2D histogram with cluster data
 1import os, sys
 2import h5py  # ensure you have `pip install h5py`
 3import pandas as pd  # ensure you have `pip install pandas`
 4from typing import *
 5import matplotlib.pyplot as plt
 6
 7import seaborn as sns
 8sns.set_context(context="talk")
 9
10# add some paths to PYTHONPATH
11for directory in ["..", "."]:
12    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), directory)))
13
14# on our system "EXAMPLE_DATA_HDF5" refers to the absolute path
15# to a hdf5 file generated by luna. Replace with your own!
16from env_vars_for_docs_examples import EXAMPLE_DATA_HDF5, PLOTS_DIRECTORY
17
18# re-use functions from previous example
19from ex2_read_data_time_units import load_clusters, TimeUnit
20
21
22def plot_2d_histogram_cluster_data(cluster_data: pd.DataFrame, variable: str, fname: str):
23    """
24    Plots a 2D histogram of cluster data.
25
26    Args:
27        cluster_data (pd.DataFrame): DataFrame containing cluster data.
28        variable (str): The name of the variable to bin and plot, typically 'ctot' or 'size' for clusters.
29        fname (str): The name of the file to save the figure.
30    """
31
32
33    fig, ax = plt.subplots(figsize=(10, 8))
34
35    # Extracting x_centroid, y_centroid, and the variable values
36    x = cluster_data['cx']
37    y = cluster_data['cy']
38    var = cluster_data[variable]
39
40    # Creating a 2D histogram
41    h = ax.hist2d(x, y, weights=var, bins=[256, 256], range=[[0, 256], [0, 256]], cmap='BuPu')
42
43    # Adding a colorbar
44    cbar = plt.colorbar(h[3], ax=ax)
45    cbar.set_label(f'Intensity of {variable}')
46
47    plt.title(f'2D Histogram of {variable} values by Cluster Coordinates')
48    plt.xlabel('Cluster X Coordinate')
49    plt.ylabel('Cluster Y Coordinate')
50
51    # Save the figure
52    plt.savefig(fname, bbox_inches='tight', dpi=300)
53
54
55if __name__ == "__main__":
56
57    variable = "ctot"
58    fname = os.path.join(PLOTS_DIRECTORY, f"ex5_plot_cluster_2d_histogram_{variable}.png")
59
60    cluster_data: pd.DataFrame = load_clusters(EXAMPLE_DATA_HDF5, toa_unit=TimeUnit.Microseconds)
61
62    plot_2d_histogram_cluster_data(cluster_data=cluster_data, variable=variable, fname=fname)

Script Output

Example Output

2D histogram of clusters