Skip to content

Commit

Permalink
improve and more plots to show what was thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Jun 29, 2024
1 parent 5c34f23 commit 5d33f4e
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 35 deletions.
161 changes: 161 additions & 0 deletions plenoirf/summary/scripts/0020_what_was_thrown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/python
import sys
import numpy as np
import plenoirf as irf
import sparse_numeric_table as snt
import os
import json_utils
import spherical_coordinates
import solid_angle_utils
import binning_utils
import spherical_histogram
import sebastians_matplotlib_addons as seb


paths = irf.summary.paths_from_argv(sys.argv)
res = irf.summary.Resources.from_argv(sys.argv)
os.makedirs(paths["out_dir"], exist_ok=True)
seb.matplotlib.rcParams.update(res.analysis["plot"]["matplotlib"])

energy_bin = json_utils.read(
os.path.join(paths["analysis_dir"], "0005_common_binning", "energy.json")
)["trigger_acceptance"]

passing_trigger = json_utils.tree.read(
os.path.join(paths["analysis_dir"], "0055_passing_trigger")
)

nat_bin = binning_utils.Binning(
bin_edges=np.geomspace(1, 100_000, energy_bin["num_bins"])
)

POINTNIG_ZENITH_BIN = res.ZenithBinning("once")

hh = spherical_histogram.HemisphereHistogram(
num_vertices=12_000,
max_zenith_distance_rad=np.deg2rad(90),
)

cmap = seb.plt.colormaps["inferno"].resampled(256)

for pk in res.PARTICLES:
with res.open_event_table(particle_key=pk) as arc:
table = arc.read_table(
levels_and_columns={
"primary": [
snt.IDX,
"energy_GeV",
"azimuth_rad",
"zenith_rad",
],
"instrument_pointing": "__all__",
"groundgrid": "__all__",
}
)

_gg_res = arc.read_table(
levels_and_columns={
"groundgrid_result": [snt.IDX, "num_bins_above_threshold"],
}
)

table["groundgrid"] = irf.event_table.structure.patch_groundgrid(
groundgrid=table["groundgrid"],
groundgrid_result=_gg_res["groundgrid_result"],
)

table = snt.sort_table_on_common_indices(
table=table,
common_indices=table["primary"][snt.IDX],
)

hh.reset()
hh.assign_azimuth_zenith(
azimuth_rad=table["primary"]["azimuth_rad"],
zenith_rad=table["primary"]["zenith_rad"],
)

pointing_range_color = "yellowgreen"
fig = seb.figure(irf.summary.figure.FIGURE_STYLE)
ax = seb.add_axes(fig=fig, span=[0.0, 0.0, 1, 1], style=seb.AXES_BLANK)
ax_legend = seb.add_axes(
fig=fig, span=[0.8, 0.05, 0.2, 0.9], style=seb.AXES_BLANK
)
ax_legend.set_xlim([0, 1])
ax_legend.set_ylim([0, 1])
ax_colorbar = seb.add_axes(
fig=fig,
span=[0.8, 0.4, 0.02, 0.5],
)

vx, vy, vz = hh.bin_geometry.vertices.T
vaz, vzd = spherical_coordinates.cx_cy_cz_to_az_zd(vx, vy, vz)
faces_counts_per_solid_angle = (
hh.bin_counts / hh.bin_geometry.faces_solid_angles
)
faces_counts_per_solid_angle_p99 = np.percentile(
faces_counts_per_solid_angle, q=99
)
faces_colors = cmap(
faces_counts_per_solid_angle / faces_counts_per_solid_angle_p99
)
seb.hemisphere.ax_add_faces(
ax=ax,
azimuths_rad=vaz,
zeniths_rad=vzd,
faces=hh.bin_geometry.faces,
faces_colors=faces_colors,
)
ax.set_aspect("equal")
ptg_circ_az = np.linspace(0, np.pi * 2, 360)
ptg_circ_zd = res.config["pointing"]["range"][
"max_zenith_distance_rad"
] * np.ones(shape=ptg_circ_az.shape)
seb.hemisphere.ax_add_plot(
ax=ax,
azimuths_rad=ptg_circ_az,
zeniths_rad=ptg_circ_zd,
linewidth=0.66,
color=pointing_range_color,
)
seb.hemisphere.ax_add_grid_stellarium_style(
ax=ax, color="grey", linewidth=0.33
)
seb.hemisphere.ax_add_ticklabel_text(ax=ax, color="grey")
ax_legend.plot(
[0.0, 0.1],
[0.1, 0.1],
color=pointing_range_color,
linewidth=0.66,
)
ax_legend.text(
x=0.15,
y=0.1,
s="pointing\nrange",
fontsize=9,
verticalalignment="center",
)
ax_legend.text(
x=0.0,
y=0.2,
s=f"site: {res.SITE['name']:s}",
fontsize=9,
verticalalignment="center",
)
ax_legend.text(
x=0.0,
y=0.3,
s=f"particle: {pk:s}",
fontsize=9,
verticalalignment="center",
)
_norm = seb.plt_colors.Normalize(
vmin=0.0, vmax=faces_counts_per_solid_angle_p99, clip=False
)
_mappable = seb.plt.cm.ScalarMappable(norm=_norm, cmap=cmap)
seb.plt.colorbar(mappable=_mappable, cax=ax_colorbar)
ax_colorbar.set_ylabel(r"intensity / sr$^{-1}$")
fig.savefig(
os.path.join(paths["out_dir"], f"{pk:s}_directions_thrown.jpg")
)
seb.close(fig)
145 changes: 110 additions & 35 deletions plenoirf/summary/scripts/0080_groundgrid_num_bins_above_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@

energy_bin = json_utils.read(
os.path.join(paths["analysis_dir"], "0005_common_binning", "energy.json")
)["trigger_acceptance_onregion"]
)["trigger_acceptance"]

passing_trigger = json_utils.tree.read(
os.path.join(paths["analysis_dir"], "0055_passing_trigger")
)

nat_bin = binning_utils.Binning(bin_edges=np.geomspace(1, 100_000, 31))
nat_bin = binning_utils.Binning(
bin_edges=np.geomspace(1, 100_000, energy_bin["num_bins"])
)

POINTNIG_ZENITH_BIN = res.ZenithBinning("once")


bbb = {}
huh = {}
for pk in res.PARTICLES:
bbb[pk] = {"avg": [], "au": []}
huh[pk] = {}

with res.open_event_table(particle_key=pk) as arc:
table = arc.read_table(
Expand Down Expand Up @@ -64,8 +69,18 @@
common_indices=table["primary"][snt.IDX],
)

min_number_samples = 10
huh[pk]["bin_edges"] = np.geomspace(
1e0,
1e6,
int(np.sqrt(table["groundgrid"].shape[0])),
)

huh[pk]["bin_counts"] = np.histogram(
table["groundgrid"]["num_bins_above_threshold"],
bins=huh[pk]["bin_edges"],
)[0]

min_number_samples = 10
for zdbin in range(POINTNIG_ZENITH_BIN.num):
zd_start = POINTNIG_ZENITH_BIN.edges[zdbin]
zd_stop = POINTNIG_ZENITH_BIN.edges[zdbin + 1]
Expand All @@ -75,8 +90,8 @@
table["instrument_pointing"]["zenith_rad"] < zd_stop,
)

# 1D
# --
# 1D VS energy
# ------------
hh_exposure = np.histogram(
table["primary"]["energy_GeV"][mask],
bins=energy_bin["edges"],
Expand All @@ -98,8 +113,8 @@
bbb[pk]["avg"].append(avg_num_bins)
bbb[pk]["au"].append(avg_num_bins_au)

# 2D
# --
# 2D VS energy
# ------------
cm = confusion_matrix.init(
ax0_key="primary/energy_GeV",
ax0_values=table["primary"]["energy_GeV"][mask],
Expand All @@ -120,19 +135,16 @@
cm["ax0_bin_edges"],
cm["ax1_bin_edges"],
np.transpose(cm["counts_normalized_on_ax0"]),
cmap="Greys",
cmap=res.PARTICLE_COLORMAPS[pk],
norm=seb.plt_colors.PowerNorm(gamma=0.5),
)
seb.plt.colorbar(_pcm_confusion, cax=ax_cb, extend="max")
circ_str = r"$^\circ{}$"
zenith_range_str = (
f"zenith range: {np.rad2deg(zd_start):0.1f}"
+ circ_str
+ " to "
+ f"{np.rad2deg(zd_stop):0.1f}"
+ circ_str

zenith_range_str = irf.summary.make_angle_range_str(
start_rad=POINTNIG_ZENITH_BIN.edges[zdbin],
stop_rad=POINTNIG_ZENITH_BIN.edges[zdbin + 1],
)
ax_c.set_title(zenith_range_str)
ax_c.set_title("zenith: " + zenith_range_str)
ax_c.set_ylabel("num. bins above threshod / 1")
ax_c.loglog()
res.ax_add_site_marker(ax_c, x=0.2, y=0.1)
Expand All @@ -142,8 +154,8 @@
y=0.05,
s="normalized for each column",
fontsize=8,
horizontalalignment="center",
# verticalalignment="center",
# horizontalalignment="center",
verticalalignment="center",
transform=ax_c.transAxes,
)

Expand All @@ -169,30 +181,93 @@
seb.close(fig)


particle_colors = res.analysis["plot"]["particle_colors"]

fig = seb.figure(style=seb.FIGURE_1_1)
ax = seb.add_axes(fig=fig, span=[0.175, 0.15, 0.75, 0.8])

fig = seb.figure(style=irf.summary.figure.FIGURE_STYLE)
ax = seb.add_axes(fig=fig, span=[0.175, 0.15, 0.6, 0.8])
ax_legend = seb.add_axes(
fig=fig, span=[0.8, 0.15, 0.1, 0.8], style=seb.AXES_BLANK
)
linestyles = ["-", "--", ":"]
for pk in res.PARTICLES:
for zdbin in range(POINTNIG_ZENITH_BIN.num):
seb.ax_add_histogram(
ax=ax,
bin_edges=energy_bin["edges"],
bincounts=bbb[pk]["avg"][zdbin],
ax.plot(
energy_bin["centers"],
bbb[pk]["avg"][zdbin],
linestyle=linestyles[zdbin],
linecolor=particle_colors[pk],
linealpha=1.0,
bincounts_upper=None,
bincounts_lower=None,
face_color=particle_colors[pk],
face_alpha=0.3,
color=res.PARTICLE_COLORS[pk],
)
res.ax_add_site_marker(ax)
ax.set_ylim([1e3, 1e4])
_yy = 1
for pk in res.PARTICLES:
_yy -= 0.1
ax_legend.plot(
[0, 0.3],
[_yy, _yy],
linestyle="-",
color=res.PARTICLE_COLORS[pk],
)
ax_legend.text(
x=0.4,
y=_yy,
s=pk,
fontsize=8,
verticalalignment="center",
)

_yy -= 0.1
ax_legend.text(
x=0.2,
y=_yy,
s="zenith ranges",
fontsize=8,
verticalalignment="center",
)

for zdbin in range(POINTNIG_ZENITH_BIN.num):
_yy -= 0.1
ax_legend.plot(
[0, 0.3],
[_yy, _yy],
linestyle=linestyles[zdbin],
color="grey",
)
ax_legend.text(
x=0.4,
y=_yy,
s=irf.summary.make_angle_range_str(
start_rad=POINTNIG_ZENITH_BIN.edges[zdbin],
stop_rad=POINTNIG_ZENITH_BIN.edges[zdbin + 1],
),
fontsize=8,
verticalalignment="center",
)
ax_legend.set_xlim([0, 1])

res.ax_add_site_marker(ax, x=0.8, y=0.1)
ax.set_ylim([1e0, 1e5])
ax.loglog()
ax.set_xlabel("energy / GeV")
ax.set_ylabel("num. bins above threshold / 1")
fig.savefig(os.path.join(paths["out_dir"], "num_bins_above_threshold.jpg"))
seb.close(fig)


fig = seb.figure(style=seb.FIGURE_1_1)
ax = seb.add_axes(fig=fig, span=[0.175, 0.15, 0.75, 0.8])
for pk in res.PARTICLES:
seb.ax_add_histogram(
ax=ax,
bin_edges=huh[pk]["bin_edges"],
bincounts=huh[pk]["bin_counts"] / np.sum(huh[pk]["bin_counts"]),
linestyle="-",
linecolor=res.PARTICLE_COLORS[pk],
linealpha=1.0,
bincounts_upper=None,
bincounts_lower=None,
face_color=res.PARTICLE_COLORS[pk],
face_alpha=0.3,
)
res.ax_add_site_marker(ax)
ax.loglog()
ax.set_xlabel("num. bins above threshold / 1")
ax.set_ylabel("relative intensity / 1")
fig.savefig(os.path.join(paths["out_dir"], "what_was_throwm.jpg"))
seb.close(fig)

0 comments on commit 5d33f4e

Please sign in to comment.