From 1cf97b9722b89cab851ba5dfdcfc972f547d74fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Fr=C3=B6se?= Date: Thu, 6 Jul 2023 12:11:37 +0200 Subject: [PATCH] copy pngs --- docs/conf.py | 2 +- examples/examples/README.txt | 6 ++ .../algorithms/convert_images_to_2d.py | 2 - examples/examples/algorithms/dilate_image.py | 2 - .../examples/algorithms/nd_interpolation.py | 4 +- .../examples/core/InstrumentDescription.py | 7 +- examples/examples/core/Tools.py | 11 +-- examples/examples/core/containers.py | 46 +++++----- examples/examples/core/table_writer_reader.py | 75 ++++++++-------- .../examples/visualization/array_display.py | 26 ++---- .../examples/visualization/camera_display.py | 14 ++- .../tutorials/calibrated_data_exploration.py | 11 +-- examples/tutorials/coordinates_example.py | 26 ++---- examples/tutorials/ctapipe_handson.py | 85 ++++++++++--------- examples/tutorials/ctapipe_overview.py | 82 ++++++------------ examples/tutorials/raw_data_exploration.py | 6 +- examples/tutorials/theta_square.py | 3 +- 17 files changed, 170 insertions(+), 238 deletions(-) create mode 100644 examples/examples/README.txt diff --git a/docs/conf.py b/docs/conf.py index 854b46773d6..14e9f567bc1 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -154,7 +154,7 @@ def setup(app): "tutorials", ], # path to where to save gallery generated output "nested_sections": True, - "copyfile_regex": r"index.rst", + "copyfile_regex": r"index.rst|.*\.png", } diff --git a/examples/examples/README.txt b/examples/examples/README.txt new file mode 100644 index 00000000000..d3c2423283f --- /dev/null +++ b/examples/examples/README.txt @@ -0,0 +1,6 @@ +.. _examples_gallery: + +Examples gallery +================ + +The examples gallery provides an overview of different ctapipe modules and how to use them. diff --git a/examples/examples/algorithms/convert_images_to_2d.py b/examples/examples/algorithms/convert_images_to_2d.py index 4817580d1b1..040baeab210 100644 --- a/examples/examples/algorithms/convert_images_to_2d.py +++ b/examples/examples/algorithms/convert_images_to_2d.py @@ -9,8 +9,6 @@ from ctapipe.image.toymodel import Gaussian from ctapipe.instrument import SubarrayDescription -from ctapipe.io import EventSource -from ctapipe.utils import get_dataset_path from ctapipe.visualization import CameraDisplay # get the subarray from an example file diff --git a/examples/examples/algorithms/dilate_image.py b/examples/examples/algorithms/dilate_image.py index 9b6de53e237..b2d827d27cb 100644 --- a/examples/examples/algorithms/dilate_image.py +++ b/examples/examples/algorithms/dilate_image.py @@ -49,8 +49,6 @@ # Now dialte the mask a few times: # -from ctapipe.image.cleaning import dilate - def show_dilate(mask, times=1): m = mask.copy() diff --git a/examples/examples/algorithms/nd_interpolation.py b/examples/examples/algorithms/nd_interpolation.py index 19b0b06e386..d830f7a2d11 100644 --- a/examples/examples/algorithms/nd_interpolation.py +++ b/examples/examples/algorithms/nd_interpolation.py @@ -119,7 +119,7 @@ ) plt.title("Raw table, uninterpolated {0}".format(energy_table.hist.T.shape)) cb = plt.colorbar() -cb.set_label("$\log_{10}(E/\mathrm{TeV})$") +cb.set_label(r"$\log_{10}(E/\mathrm{TeV})$") # the interpolated table plt.subplot(1, 2, 2) @@ -130,7 +130,7 @@ plt.ylabel("Impact Dist(m)") plt.title("Interpolated to a ({0}, {0}) grid".format(N)) cb = plt.colorbar() -cb.set_label("$\log_{10}(E/\mathrm{TeV})$") +cb.set_label(r"$\log_{10}(E/\mathrm{TeV})$") plt.tight_layout() plt.show() diff --git a/examples/examples/core/InstrumentDescription.py b/examples/examples/core/InstrumentDescription.py index ac4a5ccc1bd..fc3857a9855 100644 --- a/examples/examples/core/InstrumentDescription.py +++ b/examples/examples/core/InstrumentDescription.py @@ -8,10 +8,11 @@ """ -import numpy as np +from astropy.coordinates import SkyCoord from ctapipe.io import EventSource from ctapipe.utils.datasets import get_dataset_path +from ctapipe.visualization import CameraDisplay filename = get_dataset_path("gamma_prod5.simtel.zst") @@ -71,7 +72,6 @@ tel.camera.geometry.pix_x # %matplotlib inline -from ctapipe.visualization import CameraDisplay CameraDisplay(tel.camera.geometry) @@ -106,9 +106,6 @@ subarray.optics_types -from astropy.coordinates import SkyCoord - -from ctapipe.coordinates import GroundFrame center = SkyCoord("10.0 m", "2.0 m", "0.0 m", frame="groundframe") coords = subarray.tel_coords # a flat list of coordinates by tel_index diff --git a/examples/examples/core/Tools.py b/examples/examples/core/Tools.py index 212b860bcff..d97e0e7fe28 100644 --- a/examples/examples/core/Tools.py +++ b/examples/examples/core/Tools.py @@ -4,23 +4,17 @@ """ -import logging from time import sleep -from astropy import units as u - from ctapipe.core import Component, TelescopeComponent, Tool from ctapipe.core.traits import ( - Dict, - Float, FloatTelescopeParameter, Integer, - List, Path, TraitError, - Unicode, observe, ) +from ctapipe.instrument import SubarrayDescription from ctapipe.utils import get_dataset_path GAMMA_FILE = get_dataset_path("gamma_prod5.simtel.zst") @@ -52,8 +46,6 @@ def do_thing(self): class SecondaryMyComponent(MyComponent): """A second component""" - pass - class AdvancedComponent(Component): """An advanced technique""" @@ -100,7 +92,6 @@ class TelescopeWiseComponent(TelescopeComponent): # one: # -from ctapipe.instrument import SubarrayDescription, TelescopeDescription subarray = SubarrayDescription.read(GAMMA_FILE) subarray.info() diff --git a/examples/examples/core/containers.py b/examples/examples/core/containers.py index de499c57daa..360fcdc45c7 100644 --- a/examples/examples/core/containers.py +++ b/examples/examples/core/containers.py @@ -15,11 +15,13 @@ import numpy as np from astropy import units as u +from ctapipe.containers import SimulatedShowerContainer from ctapipe.core import Container, Field, Map ###################################################################### # Let’s define a few example containers with some dummy fields in them: -# +# + class SubContainer(Container): junk = Field(-1, "Some junk") @@ -59,14 +61,14 @@ class EventContainer(Container): ###################################################################### # Basic features # -------------- -# +# ev = EventContainer() ###################################################################### # Check that default values are automatically filled in -# +# print(ev.event_id) print(ev.sub) @@ -79,23 +81,21 @@ class EventContainer(Container): ###################################################################### # print the dict representation -# +# print(ev) ###################################################################### # We also get docstrings “for free” -# - -?EventContainer - -?SubContainer +# +help(EventContainer) +help(SubContainer) ###################################################################### # values can be set as normal for a class: -# +# ev.event_id = 100 ev.event_id @@ -108,7 +108,7 @@ class EventContainer(Container): ###################################################################### # and we can add a few of these to the parent container inside the tel # dict: -# +# ev.tel[10] = TelContainer() ev.tel[5] = TelContainer() @@ -121,12 +121,15 @@ class EventContainer(Container): ###################################################################### # Be careful to use the ``default_factory`` mechanism for mutable fields, # see this **negative** example: -# +# + class DangerousContainer(Container): image = Field( np.zeros(10), - description="Attention!!!! Globally mutable shared state. Use default_factory instead", + description=( + "Attention!!!! Globally mutable shared state. Use default_factory instead" + ), ) @@ -145,7 +148,7 @@ class DangerousContainer(Container): ###################################################################### # Converion to dictionaries # ------------------------- -# +# ev.as_dict() @@ -155,7 +158,7 @@ class DangerousContainer(Container): ###################################################################### # for serialization to a table, we can even flatten the output into a # single set of columns -# +# ev.as_dict(recursive=True, flatten=True) @@ -163,7 +166,7 @@ class DangerousContainer(Container): ###################################################################### # Setting and clearing values # --------------------------- -# +# ev.tel[5].image[:] = 9 print(ev) @@ -175,11 +178,10 @@ class DangerousContainer(Container): ###################################################################### # look at a pre-defined Container # ------------------------------- -# +# -from ctapipe.containers import SimulatedShowerContainer -?SimulatedShowerContainer +help(SimulatedShowerContainer) shower = SimulatedShowerContainer() shower @@ -188,10 +190,10 @@ class DangerousContainer(Container): ###################################################################### # Container prefixes # ------------------ -# +# # To store the same container in the same table in a file or give more # information, containers support setting a custom prefix: -# +# c1 = SubContainer(junk=5, value=3, prefix="foo") c2 = SubContainer(junk=10, value=9001, prefix="bar") @@ -199,4 +201,4 @@ class DangerousContainer(Container): # create a common dict with data from both containers: d = c1.as_dict(add_prefix=True) d.update(c2.as_dict(add_prefix=True)) -d \ No newline at end of file +d diff --git a/examples/examples/core/table_writer_reader.py b/examples/examples/core/table_writer_reader.py index 6c9e9a6ab4c..d1d122477d3 100644 --- a/examples/examples/core/table_writer_reader.py +++ b/examples/examples/core/table_writer_reader.py @@ -4,7 +4,7 @@ The ``TableWriter``/``TableReader`` sub-classes allow you to write a ``ctapipe.core.Container`` class and its meta-data to an output table. -They treat the ``Field``\ s in the ``Container`` as columns in the +They treat the ``Field``s in the ``Container`` as columns in the output, and automatically generate a schema. Here we will go through an example of writing out data and reading it back with *Pandas*, *PyTables*, and a ``ctapipe.io.TableReader``: @@ -19,22 +19,26 @@ ###################################################################### # Caveats to think about: \* vector columns in Containers *can* be # written, but some lilbraries like Pandas can not read those (so you must -# use pytables or astropy to read outputs that have vector columns) \* -# units are stored in the table metadata, but some libraries like Pandas +# use pytables or astropy to read outputs that have vector columns) +# \* units are stored in the table metadata, but some libraries like Pandas # ignore them and all other metadata -# +# ###################################################################### # Create some example Containers # ------------------------------ -# +# + +import os import numpy as np +import pandas as pd +import tables from astropy import units as u from ctapipe.core import Container, Field -from ctapipe.io import HDF5TableWriter +from ctapipe.io import HDF5TableReader, HDF5TableWriter, read_table class VariousTypesContainer(Container): @@ -50,7 +54,8 @@ class VariousTypesContainer(Container): ###################################################################### # let’s also make a dummy stream (generator) that will create a series of # these containers -# +# + def create_stream(n_event): @@ -66,6 +71,7 @@ def create_stream(n_event): yield data + for data in create_stream(2): for key, val in data.items(): @@ -76,13 +82,13 @@ def create_stream(n_event): ###################################################################### # Writing the Data (and good practices) # ------------------------------------- -# +# ###################################################################### # Always use context managers with IO classes, as they will make sure the # underlying resources are properly closed in case of errors: -# +# try: with HDF5TableWriter("container.h5", group_name="data") as h5_table: @@ -97,20 +103,19 @@ def create_stream(n_event): h5_table.h5file.isopen == False -!ls container.h5 - +print(os.listdir()) ###################################################################### # Appending new Containers # ------------------------ -# +# ###################################################################### # To append some new containers we need to set the writing in append mode # by using: ‘mode=a’. But let’s now first look at what happens if we # don’t. -# +# for i in range(2): @@ -124,14 +129,13 @@ def create_stream(n_event): print(h5_table.h5file) -!rm -f container.h5 - +os.remove("container.h5") ###################################################################### # Ok so the writer destroyed the content of the file each time it opens # the file. Now let’s try to append some data group to it! (using # mode=‘a’) -# +# for i in range(2): @@ -149,7 +153,7 @@ def create_stream(n_event): ###################################################################### # So we can append some data groups. As long as the data group_name does # not already exists. Let’s try to overwrite the data group : data_1 -# +# try: with HDF5TableWriter("container.h5", mode="a", group_name="data_1") as h5_table: @@ -161,7 +165,7 @@ def create_stream(n_event): ###################################################################### # Good ! I cannot overwrite my data. -# +# print(bool(h5_table.h5file.isopen)) @@ -169,34 +173,33 @@ def create_stream(n_event): ###################################################################### # Reading the Data # ---------------- -# +# ###################################################################### # Reading the whole table at once: # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# # For this, you have several choices. Since we used the HDF5TableWriter in # this example, we have at least these options avilable: -# +# # - Pandas # - PyTables # - Astropy Table -# +# # For other TableWriter implementations, others may be possible (depending # on format) -# +# ###################################################################### # Reading using ``ctapipe.io.read_table`` # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -# +# # This is the preferred method, it returns an astropy ``Table`` and # supports keeping track of units, metadata and transformations. -# +# -from ctapipe.io import read_table table = read_table("container.h5", "/data_0/table") table[:5] @@ -207,13 +210,12 @@ def create_stream(n_event): ###################################################################### # Reading with Pandas: # ^^^^^^^^^^^^^^^^^^^^ -# +# # Pandas is a convenient way to read the output. **HOWEVER BE WARNED** # that so far Pandas does not support reading the table *meta-data* or # *units* for colums, so that information is lost! -# +# -import pandas as pd data = pd.read_hdf("container.h5", key="/data_0/table") data.head() @@ -222,9 +224,8 @@ def create_stream(n_event): ###################################################################### # Reading with PyTables # ^^^^^^^^^^^^^^^^^^^^^ -# +# -import tables h5 = tables.open_file("container.h5") table = h5.root["data_0"]["table"] @@ -233,7 +234,7 @@ def create_stream(n_event): ###################################################################### # note that here we can still access the metadata -# +# table.attrs @@ -241,19 +242,17 @@ def create_stream(n_event): ###################################################################### # Reading one-row-at-a-time: # ~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# ###################################################################### # Rather than using the full-table methods, if you want to read it # row-by-row (e.g. to maintain compatibility with an existing event loop), # you can use a ``TableReader`` instance. -# +# # The advantage here is that units and other metadata are retained and # re-applied -# - -from ctapipe.io import HDF5TableReader +# def read(mode): @@ -271,6 +270,7 @@ def read(mode): print(data.as_dict()) + read("r") read("r+") @@ -278,4 +278,3 @@ def read(mode): read("a") read("w") - diff --git a/examples/examples/visualization/array_display.py b/examples/examples/visualization/array_display.py index 9679cfd9bc3..e5438ae7f7a 100644 --- a/examples/examples/visualization/array_display.py +++ b/examples/examples/visualization/array_display.py @@ -12,9 +12,12 @@ from astropy import units as u from astropy.coordinates import SkyCoord -from ctapipe.containers import HillasParametersContainer -from ctapipe.coordinates import EastingNorthingFrame, GroundFrame +from ctapipe.calib import CameraCalibrator +from ctapipe.coordinates import EastingNorthingFrame +from ctapipe.image import ImageProcessor from ctapipe.instrument import SubarrayDescription +from ctapipe.io import EventSource +from ctapipe.reco import ShowerProcessor from ctapipe.visualization import ArrayDisplay plt.rcParams["figure.figsize"] = (8, 6) @@ -134,17 +137,6 @@ # following example shows its use: # -import matplotlib.pyplot as plt -from astropy.coordinates import SkyCoord -from IPython import display -from matplotlib.animation import FuncAnimation - -from ctapipe.calib import CameraCalibrator -from ctapipe.image import ImageProcessor -from ctapipe.io import EventSource -from ctapipe.reco import ShowerProcessor -from ctapipe.utils import get_dataset_path -from ctapipe.visualization import ArrayDisplay input_url = "dataset://gamma_LaPalma_baseline_20Zd_180Az_prod3b_test.simtel.gz" @@ -161,13 +153,7 @@ def plot_event(event, subarray, ax): true and reconstructed impact position overlaid """ - array_pointing = SkyCoord( - az=event.pointing.array_azimuth, - alt=event.pointing.array_altitude, - frame="altaz", - ) - - angle_offset = event.pointing.array_azimuth + event.pointing.array_azimuth disp = ArrayDisplay(subarray, axes=ax) hillas_dict = {tid: tel.parameters.hillas for tid, tel in event.dl1.tel.items()} diff --git a/examples/examples/visualization/camera_display.py b/examples/examples/visualization/camera_display.py index 7aac6fe6afb..92f66f6d5f5 100644 --- a/examples/examples/visualization/camera_display.py +++ b/examples/examples/visualization/camera_display.py @@ -6,12 +6,16 @@ import astropy.coordinates as c import astropy.units as u -import matplotlib.pylab as plt +import matplotlib.pyplot as plt import numpy as np +from IPython import display +from matplotlib.animation import FuncAnimation +from matplotlib.colors import PowerNorm from ctapipe.coordinates import CameraFrame, EngineeringCameraFrame, TelescopeFrame from ctapipe.image import hillas_parameters, tailcuts_clean, toymodel from ctapipe.instrument import SubarrayDescription +from ctapipe.io import EventSource from ctapipe.visualization import CameraDisplay ###################################################################### @@ -154,7 +158,6 @@ # also provide a custom normalization, for example a ``PowerNorm``: # -from matplotlib.colors import PowerNorm fig, axes = plt.subplots(2, 2, figsize=(14, 10)) norms = ["lin", "log", "symlog", PowerNorm(0.5)] @@ -255,8 +258,6 @@ # display (much faster than generating a new one each time) # -from IPython import display -from matplotlib.animation import FuncAnimation subarray = SubarrayDescription.read("dataset://gamma_prod5.simtel.zst") geom = subarray.tel[1].camera.geometry @@ -330,11 +331,6 @@ def update(frame): # First we load some real data so we have a nice image to view: # -import matplotlib.pyplot as plt -import numpy as np - -from ctapipe.io import EventSource -from ctapipe.visualization import CameraDisplay DATA = "dataset://gamma_20deg_0deg_run1___cta-prod5-lapalma_desert-2158m-LaPalma-dark_100evts.simtel.zst" diff --git a/examples/tutorials/calibrated_data_exploration.py b/examples/tutorials/calibrated_data_exploration.py index da235ebe648..caf80a298a1 100644 --- a/examples/tutorials/calibrated_data_exploration.py +++ b/examples/tutorials/calibrated_data_exploration.py @@ -9,10 +9,11 @@ from matplotlib import pyplot as plt import ctapipe -from ctapipe.instrument import CameraGeometry -from ctapipe.io import EventSeeker, EventSource +from ctapipe.calib import CameraCalibrator +from ctapipe.image import hillas_parameters, tailcuts_clean +from ctapipe.io import EventSource from ctapipe.utils.datasets import get_dataset_path -from ctapipe.visualization import CameraDisplay +from ctapipe.visualization import ArrayDisplay, CameraDisplay # %matplotlib inline plt.style.use("ggplot") @@ -58,7 +59,6 @@ # near future that will be automatic). # -from ctapipe.calib import CameraCalibrator calib = CameraCalibrator(subarray=source.subarray) calib(event) @@ -91,7 +91,6 @@ # Let’s look at the image # -from ctapipe.visualization import CameraDisplay tel_id = sorted(event.r1.tel.keys())[1] sub = source.subarray @@ -100,7 +99,6 @@ disp = CameraDisplay(geometry, image=image) -from ctapipe.image import hillas_parameters, tailcuts_clean mask = tailcuts_clean( geometry, @@ -191,7 +189,6 @@ # version to be more user-friendly) # -from ctapipe.visualization import ArrayDisplay nectarcam_subarray = sub.select_subarray(cam_ids, name="NectarCam") diff --git a/examples/tutorials/coordinates_example.py b/examples/tutorials/coordinates_example.py index 87cac268fa4..8e8b18795c4 100644 --- a/examples/tutorials/coordinates_example.py +++ b/examples/tutorials/coordinates_example.py @@ -8,20 +8,14 @@ import astropy.units as u import matplotlib.pyplot as plt -import numpy as np -from astropy.coordinates import AltAz, SkyCoord - -from ctapipe.calib import CameraCalibrator -from ctapipe.coordinates import ( - CameraFrame, - GroundFrame, - NominalFrame, - TelescopeFrame, - TiltedGroundFrame, -) +from astropy.coordinates import AltAz, EarthLocation, SkyCoord +from astropy.time import Time + +from ctapipe.coordinates import CameraFrame, NominalFrame, TelescopeFrame +from ctapipe.instrument import SubarrayDescription from ctapipe.io import EventSource from ctapipe.utils import get_dataset_path -from ctapipe.visualization import ArrayDisplay +from ctapipe.visualization import CameraDisplay # %matplotlib inline @@ -75,8 +69,6 @@ # oriented East of North (i.e., N=0°, E=90°). # -from astropy.coordinates import EarthLocation -from astropy.time import Time obstime = Time("2013-11-01T03:00") location = EarthLocation.of_site("Roque de los Muchachos") @@ -149,8 +141,6 @@ # and how they might be visible in the camera. # -from ctapipe.instrument import SubarrayDescription -from ctapipe.visualization import CameraDisplay location = EarthLocation.of_site("Roque de los Muchachos") obstime = Time("2018-11-01T04:00") @@ -330,8 +320,8 @@ ) -ax.set_xlabel(f"fov_lon / deg") -ax.set_ylabel(f"fov_lat / deg") +ax.set_xlabel("fov_lon / deg") +ax.set_ylabel("fov_lat / deg") ax.legend() plt.show() diff --git a/examples/tutorials/ctapipe_handson.py b/examples/tutorials/ctapipe_handson.py index a09e5d55051..157e97461d9 100644 --- a/examples/tutorials/ctapipe_handson.py +++ b/examples/tutorials/ctapipe_handson.py @@ -11,13 +11,20 @@ ###################################################################### # Part 1: load and loop over data # ------------------------------- -# +# + +import glob import numpy as np +import pandas as pd +from ipywidgets import interact from matplotlib import pyplot as plt from ctapipe import utils -from ctapipe.io import EventSource +from ctapipe.calib import CameraCalibrator +from ctapipe.image import hillas_parameters, tailcuts_clean +from ctapipe.io import EventSource, HDF5TableWriter +from ctapipe.visualization import CameraDisplay # %matplotlib inline @@ -47,19 +54,17 @@ ###################################################################### # note that this is (:math:`N_{channels}`, :math:`N_{pixels}`, # :math:`N_{samples}`) -# +# plt.pcolormesh(r0tel.waveform[0]) brightest_pixel = np.argmax(r0tel.waveform[0].sum(axis=1)) print(f"pixel {brightest_pixel} has sum {r0tel.waveform[0,1535].sum()}") -plt.plot(r0tel.waveform[0,brightest_pixel], label="channel 0 (high-gain)") -plt.plot(r0tel.waveform[1,brightest_pixel], label="channel 1 (low-gain)") +plt.plot(r0tel.waveform[0, brightest_pixel], label="channel 0 (high-gain)") +plt.plot(r0tel.waveform[1, brightest_pixel], label="channel 1 (low-gain)") plt.legend() -from ipywidgets import interact - @interact def view_waveform(chan=0, pix_id=brightest_pixel): @@ -68,21 +73,21 @@ def view_waveform(chan=0, pix_id=brightest_pixel): ###################################################################### # try making this compare 2 waveforms -# +# ###################################################################### # Part 2: Explore the instrument description # ------------------------------------------ -# +# # This is all well and good, but we don’t really know what camera or # telescope this is… how do we get instrumental description info? -# +# # Currently this is returned *inside* the event (it will soon change to be # separate in next version or so) -# +# -subarray = source.subarray +subarray = source.subarray subarray @@ -108,38 +113,38 @@ def view_waveform(chan=0, pix_id=brightest_pixel): tel.optics.mirror_area -from ctapipe.visualization import CameraDisplay disp = CameraDisplay(tel.camera.geometry) disp = CameraDisplay(tel.camera.geometry) -disp.image = r0tel.waveform[0,:,10] # display channel 0, sample 0 (try others like 10) +disp.image = r0tel.waveform[ + 0, :, 10 +] # display channel 0, sample 0 (try others like 10) ###################################################################### # \*\* aside: \*\* show demo using a CameraDisplay in interactive mode in # ipython rather than notebook -# +# ###################################################################### # Part 3: Apply some calibration and trace integration # ---------------------------------------------------- -# +# -from ctapipe.calib import CameraCalibrator calib = CameraCalibrator(subarray=subarray) for event in EventSource(path, max_events=5): - calib(event) # fills in r1, dl0, and dl1 + calib(event) # fills in r1, dl0, and dl1 print(event.dl1.tel.keys()) event.dl1.tel[3] dl1tel = event.dl1.tel[3] -dl1tel.image.shape # note this will be gain-selected in next version, so will be just 1D array of 1855 +dl1tel.image.shape # note this will be gain-selected in next version, so will be just 1D array of 1855 dl1tel.peak_time @@ -150,9 +155,8 @@ def view_waveform(chan=0, pix_id=brightest_pixel): ###################################################################### # Now for Hillas Parameters -# +# -from ctapipe.image import hillas_parameters, tailcuts_clean image = dl1tel.image mask = tailcuts_clean(tel.camera.geometry, image, picture_thresh=10, boundary_thresh=5) @@ -161,7 +165,7 @@ def view_waveform(chan=0, pix_id=brightest_pixel): CameraDisplay(tel.camera.geometry, image=mask) cleaned = image.copy() -cleaned[~mask] = 0 +cleaned[~mask] = 0 disp = CameraDisplay(tel.camera.geometry, image=cleaned) disp.cmap = plt.cm.coolwarm @@ -177,25 +181,25 @@ def view_waveform(chan=0, pix_id=brightest_pixel): disp.add_colorbar() plt.xlim(0.5, 1.0) plt.ylim(-1.0, 0.0) -disp.overlay_moments(params, color='white', lw=2) +disp.overlay_moments(params, color="white", lw=2) ###################################################################### # Part 4: Let’s put it all together: # ---------------------------------- -# +# # - loop over events, selecting only telescopes of the same type # (e.g. LST:LSTCam) # - for each event, apply calibration/trace integration # - calculate Hillas parameters # - write out all hillas paremeters to a file that can be loaded with # Pandas -# +# ###################################################################### # first let’s select only those telescopes with LST:LSTCam -# +# subarray.telescope_types @@ -204,28 +208,27 @@ def view_waveform(chan=0, pix_id=brightest_pixel): ###################################################################### # Now let’s write out program -# +# -data = utils.get_dataset_path("gamma_prod5.simtel.zst") -source = EventSource(data) # remove the max_events limit to get more stats +data = utils.get_dataset_path("gamma_prod5.simtel.zst") +source = EventSource(data) # remove the max_events limit to get more stats for event in source: calib(event) - + for tel_id, tel_data in event.dl1.tel.items(): tel = source.subarray.tel[tel_id] mask = tailcuts_clean(tel.camera.geometry, tel_data.image) if np.count_nonzero(mask) > 0: params = hillas_parameters(tel.camera.geometry[mask], tel_data.image[mask]) -from ctapipe.io import HDF5TableWriter -with HDF5TableWriter(filename='hillas.h5', group_name='dl1', overwrite=True) as writer: - - source = EventSource(data, allowed_tels=[1,2,3,4], max_events=10) +with HDF5TableWriter(filename="hillas.h5", group_name="dl1", overwrite=True) as writer: + + source = EventSource(data, allowed_tels=[1, 2, 3, 4], max_events=10) for event in source: calib(event) - + for tel_id, tel_data in event.dl1.tel.items(): tel = source.subarray.tel[tel_id] mask = tailcuts_clean(tel.camera.geometry, tel_data.image) @@ -236,19 +239,17 @@ def view_waveform(chan=0, pix_id=brightest_pixel): ###################################################################### # We can now load in the file we created and plot it # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# +# +glob.glob("*.h5") -!ls *.h5 - -import pandas as pd -hillas = pd.read_hdf("hillas.h5", key='/dl1/hillas') +hillas = pd.read_hdf("hillas.h5", key="/dl1/hillas") hillas -_ = hillas.hist(figsize=(8,8)) +_ = hillas.hist(figsize=(8, 8)) ###################################################################### # If you do this yourself, chose a larger file to loop over more events to # get better statistics -# \ No newline at end of file +# diff --git a/examples/tutorials/ctapipe_overview.py b/examples/tutorials/ctapipe_overview.py index 7266aac8f0a..0e7866d91e0 100644 --- a/examples/tutorials/ctapipe_overview.py +++ b/examples/tutorials/ctapipe_overview.py @@ -39,8 +39,34 @@ #

# +import tempfile +from copy import deepcopy + +import astropy.units as u import matplotlib.pyplot as plt import numpy as np +from astropy.coordinates import AltAz +from astropy.coordinates.angle_utilities import angular_separation +from matplotlib.colors import ListedColormap +from scipy.sparse.csgraph import connected_components +from traitlets.config import Config + +from ctapipe.calib import CameraCalibrator +from ctapipe.image import ( + ImageProcessor, + camera_to_shower_coordinates, + concentration_parameters, + hillas_parameters, + leakage_parameters, + number_of_islands, + timing_parameters, + toymodel, +) +from ctapipe.image.cleaning import tailcuts_clean +from ctapipe.io import DataWriter, EventSource, TableLoader +from ctapipe.reco import ShowerProcessor +from ctapipe.utils.datasets import get_dataset_path +from ctapipe.visualization import ArrayDisplay, CameraDisplay # %matplotlib inline @@ -49,22 +75,6 @@ plt.rcParams["figure.figsize"] -###################################################################### -# .. raw:: html -# -#

-# -# Table of Contents -# -# .. raw:: html -# -#

-# -# .. container:: -# :name: toc -# - - ###################################################################### # General Information # ------------------- @@ -159,8 +169,6 @@ # ~~~~~~~~~~~~~~~~~~~~~~~ # -from ctapipe.io import EventSource -from ctapipe.utils.datasets import get_dataset_path input_url = get_dataset_path("gamma_prod5.simtel.zst") @@ -198,7 +206,6 @@ # images). # -from ctapipe.calib import CameraCalibrator calibrator = CameraCalibrator(subarray=source.subarray) @@ -223,7 +230,6 @@ dl1.image -from ctapipe.visualization import CameraDisplay display = CameraDisplay(geometry) @@ -238,7 +244,6 @@ # ~~~~~~~~~~~~~~ # -from ctapipe.image.cleaning import tailcuts_clean # unoptimized cleaning levels cleaning_level = { @@ -281,14 +286,6 @@ # ~~~~~~~~~~~~~~~~ # -from ctapipe.image import ( - camera_to_shower_coordinates, - concentration_parameters, - hillas_parameters, - leakage_parameters, - number_of_islands, - timing_parameters, -) hillas = hillas_parameters(geometry[clean], dl1.image[clean]) @@ -316,8 +313,8 @@ plt.plot(long[clean], dl1.peak_time[clean], "o") plt.plot(long[clean], timing.slope * long[clean] + timing.intercept) -l = leakage_parameters(geometry, dl1.image, clean) -print(l) +leakage = leakage_parameters(geometry, dl1.image, clean) +print(leakage) disp = CameraDisplay(geometry) disp.image = dl1.image @@ -347,19 +344,6 @@ # format is available as ``ctapipe-process`` # -import tempfile -from copy import deepcopy - -import astropy.units as u -from astropy.coordinates import AltAz, SkyCoord -from traitlets.config import Config - -from ctapipe.calib import CameraCalibrator -from ctapipe.containers import ImageParametersContainer -from ctapipe.image import ImageProcessor -from ctapipe.io import DataWriter, EventSource -from ctapipe.reco import ShowerProcessor -from ctapipe.utils.datasets import get_dataset_path image_processor_config = Config( { @@ -424,10 +408,6 @@ writer(event) -import pandas as pd -from astropy.coordinates.angle_utilities import angular_separation - -from ctapipe.io import TableLoader loader = TableLoader(f.name, load_dl2=True, load_simulated=True) @@ -450,7 +430,6 @@ # ------------ # -from ctapipe.visualization import ArrayDisplay angle_offset = plotting_event.pointing.array_azimuth @@ -539,8 +518,6 @@ # Find all groups of pixels, that survived the cleaning # -from ctapipe.image import toymodel -from ctapipe.instrument import SubarrayDescription geometry = loader.subarray.tel[1].camera.geometry @@ -623,7 +600,6 @@ def num_islands_python(camera, clean): n_islands, island_ids = num_islands_python(geometry, clean) -from matplotlib.colors import ListedColormap cmap = plt.get_cmap("Paired") cmap = ListedColormap(cmap.colors[:n_islands]) @@ -637,8 +613,6 @@ def num_islands_python(camera, clean): # %timeit num_islands_python(geometry, clean) -from scipy.sparse.csgraph import connected_components - def num_islands_scipy(geometry, clean): neighbors = geometry.neighbor_matrix_sparse diff --git a/examples/tutorials/raw_data_exploration.py b/examples/tutorials/raw_data_exploration.py index b12c35ff70c..3e0ad08f3de 100644 --- a/examples/tutorials/raw_data_exploration.py +++ b/examples/tutorials/raw_data_exploration.py @@ -15,10 +15,10 @@ # Setup: # -from astropy import units as u +import numpy as np from matplotlib import pyplot as plt +from scipy import signal -from ctapipe.instrument import CameraGeometry from ctapipe.io import EventSource from ctapipe.utils import get_dataset_path from ctapipe.visualization import CameraDisplay @@ -278,8 +278,6 @@ # https://docs.scipy.org/doc/scipy/reference/signal.html # -import numpy as np -from scipy import signal pix_ids = np.arange(len(data)) has_signal = sums > 300 diff --git a/examples/tutorials/theta_square.py b/examples/tutorials/theta_square.py index 1e5dc881653..b7f9694383e 100644 --- a/examples/tutorials/theta_square.py +++ b/examples/tutorials/theta_square.py @@ -1,4 +1,4 @@ -""" +r""" Make a theta-square plot ======================== @@ -12,7 +12,6 @@ import matplotlib.pyplot as plt import numpy as np from astropy import units as u -from astropy.coordinates import AltAz, SkyCoord from astropy.coordinates.angle_utilities import angular_separation from tqdm.auto import tqdm