def test_targetio_calibrator():
    from ctapipe_io_targetio import TargetIOEventSource
    from ctapipe.calib import CameraR1Calibrator

    url_r0 = get_dataset_path("targetmodule_r0.tio")
    url_r1 = get_dataset_path("targetmodule_r1.tio")
    pedpath = get_dataset_path("targetmodule_ped.tcal")

    source_r0 = TargetIOEventSource(input_url=url_r0)
    source_r1 = TargetIOEventSource(input_url=url_r1)

    r1c = CameraR1Calibrator.from_eventsource(eventsource=source_r0)

    event_r0 = source_r0._get_event_by_index(0)
    event_r1 = source_r1._get_event_by_index(0)

    r1c.calibrate(event_r0)
    assert_array_equal(event_r0.r0.tel[0].waveform,
                       event_r0.r1.tel[0].waveform)

    r1c = CameraR1Calibrator.from_eventsource(eventsource=source_r0,
                                              pedestal_path=pedpath)
    r1c.calibrate(event_r0)
    assert_array_almost_equal(
        event_r0.r1.tel[0].waveform,
        event_r1.r1.tel[0].waveform,
        1,
    )
Example #2
0
def test_datasets_in_custom_path(tmpdir_factory):
    """
    check that a dataset in a user-defined CTAPIPE_SVC_PATH is located
    """

    tmpdir1 = tmpdir_factory.mktemp('datasets1')
    tmpdir2 = tmpdir_factory.mktemp('datasets2')
    os.environ['CTAPIPE_SVC_PATH'] = ":".join([str(tmpdir1), str(tmpdir2)])

    # create a dummy dataset to search for:

    dataset_name = "test_dataset_1.txt"
    dataset_path = str(tmpdir1.join(dataset_name))

    with open(dataset_path, "w") as fp:
        fp.write("test test test")

    # try to find dummy dataset
    path = datasets.get_dataset_path(dataset_name)
    assert path == dataset_path

    with pytest.raises(FileNotFoundError):
        datasets.get_dataset_path("does_not_exist")

    # try using find_all_matching_datasets:

    ds = datasets.find_all_matching_datasets("test.*",
                                             searchpath=os.environ[
                                                 'CTAPIPE_SVC_PATH'])
    assert dataset_name in ds
Example #3
0
def test_find_datasets():
    # find all datasets matching pattern
    r = datasets.find_all_matching_datasets(r"(.*)\.camgeom\.fits\.gz")
    assert len(r) > 3

    # get the full filename for a resrouces
    assert datasets.get_dataset_path(r[0].name).exists()

    # try using a pattern
    r = datasets.find_all_matching_datasets(r"(.*)\.camgeom\.fits\.gz", regexp_group=1)
    assert not str(r[0]).endswith("gz")
Example #4
0
def test_path_url():
    class C(Component):
        thepath = Path()

    c = C()
    # test relative
    c.thepath = "file://foo.hdf5"
    assert c.thepath == (pathlib.Path() / "foo.hdf5").absolute()

    # test absolute
    c.thepath = "file:///foo.hdf5"
    assert c.thepath == pathlib.Path("/foo.hdf5")

    # test http downloading
    c.thepath = DEFAULT_URL + "optics.ecsv.txt"
    assert c.thepath.name == "optics.ecsv.txt"

    # test dataset://
    c.thepath = "dataset://optics.ecsv.txt"
    assert c.thepath == get_dataset_path("optics.ecsv.txt")
Example #5
0
def test_optics_from_dump_instrument():
    # test with file written by dump-instrument

    svc_path_before = os.getenv("CTAPIPE_SVC_PATH")
    cwd = os.getcwd()

    with tempfile.TemporaryDirectory() as tmp_dir:
        os.chdir(tmp_dir)
        os.environ["CTAPIPE_SVC_PATH"] = tmp_dir

        infile = get_dataset_path("gamma_test_large.simtel.gz")
        run_tool(DumpInstrumentTool(), [f"--input={infile}", "--format=ecsv"])

        lst = OpticsDescription.from_name("LST_LST_LSTCam",
                                          "MonteCarloArray.optics")
        assert lst.num_mirrors == 1
        assert lst.equivalent_focal_length.to_value(u.m) == 28
        assert lst.num_mirror_tiles == 198

    os.chdir(cwd)
    if svc_path_before is None:
        del os.environ["CTAPIPE_SVC_PATH"]
    else:
        os.environ["CTAPIPE_SVC_PATH"] = svc_path_before
Example #6
0
def find_nice_event():
    filename = datasets.get_dataset_path("gamma_test_large.simtel.gz")
    source = event_source(filename)
    calib = CameraCalibrator()
    
    for i, event in tqdm(enumerate(source)):
        # from IPython import embed; embed()
        # if i in [0, 1, 2, 3, 4, 39]:  # skip ugly events
        #     print(i, event.mc.energy)
        #     continue 
        subarray = event.inst.subarray
        calib(event)
        for tel_id in event.dl0.tels_with_data:
            # Camera Geometry required for hillas parametrization
            camgeom = subarray.tel[tel_id].camera
            # note the [0] is for channel 0 which is high-gain channel
            image = event.dl1.tel[tel_id].image
            # Cleaning  of the image
            cleaned_image = image.copy()
            # create a clean mask of pixels above the threshold
            cleanmask = tailcuts_clean(
                camgeom, image, picture_thresh=10, boundary_thresh=5, min_number_picture_neighbors=3
            )
            # set all rejected pixels to zero
            cleaned_image[~cleanmask] = 0

            # Calculate hillas parameters
            try:
                d = hillas_parameters(camgeom, cleaned_image)
            except HillasParameterizationError:
                pass  # skip failed parameterization (normally no signal)
            # from IPython import embed; embed()
            tel_name = event.inst.subarray.tel[tel_id].name
            if tel_name == 'LST' and d.r < 1 * u.m and d.intensity > 400:
                print(i, d.intensity, event.mc.energy)
                return tel_id, d, event, cleanmask
Example #7
0
import numpy as np
import sys

import matplotlib.pyplot as plt
from ctapipe.visualization import CameraDisplay
import datetime

# unoptimized cleaning levels, copied from
# https://github.com/tudo-astroparticlephysics/cta_preprocessing
cleaning_level = {
    'LSTCam': (3.5, 7.5, 2),  # ?? (3, 6) for Abelardo...
    'FlashCam': (4, 8, 2),  # there is some scaling missing?
    'ASTRICam': (5, 7, 2),
}

input_url = get_dataset_path('gamma_test_large.simtel.gz')
event_source = event_source(input_url, max_events=2)

calibrator = CameraCalibrator()

for event in event_source:
    calibrator(event)

    for telescope_id, dl1 in event.dl1.tel.items():

        image = dl1.image
        peakpos = dl1.pulse_time
        camera = event.inst.subarray.tels[telescope_id].camera

        # display dl1 images
        fig, axs = plt.subplots(1, 1, figsize=(6, 3))
Example #8
0
from astropy import units as u

from ctapipe.calib import CameraCalibrator
from ctapipe.coordinates import TiltedGroundFrame, MissingFrameAttributeWarning
from ctapipe.image import hillas_parameters, tailcuts_clean, HillasParameterizationError
from ctapipe.image import timing_parameters
from ctapipe.io import EventSource
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay
import warnings

if __name__ == "__main__":
    warnings.filterwarnings("ignore", category=MissingFrameAttributeWarning)

    # importing data from avaiable datasets in ctapipe
    filename = datasets.get_dataset_path("gamma_test_large.simtel.gz")

    if len(sys.argv) > 1:
        filename = sys.argv[1]

    # reading the Monte Carlo file for LST
    source = EventSource(filename)

    # pointing direction of the telescopes
    point_azimuth = {}
    point_altitude = {}

    subarray = source.subarray
    calib = CameraCalibrator(subarray=subarray)
    off_angles = []
    first_event = True
Example #9
0
import matplotlib.pylab as plt
import numpy as np
from astropy import units as u

from ctapipe.io import event_source
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay

if __name__ == '__main__':

    plt.figure(figsize=(9.5, 8.5))

    # load up a single event, so we can get the subarray info:
    source = event_source(
        datasets.get_dataset_path("gamma_test_large.simtel.gz"),
        max_events=1,
    )

    event = next(iter(source))

    # display the array
    subarray = event.inst.subarray
    ad = ArrayDisplay(subarray, tel_scale=3.0)

    print("Now setting vectors")
    plt.pause(1.0)
    plt.tight_layout()

    for phi in np.linspace(0, 360, 30) * u.deg:
        r = np.cos(phi / 2)
from ctapipe.utils.datasets import get_dataset_path

import matplotlib.pyplot as plt
from ctapipe.visualization import ArrayDisplay


# unoptimized cleaning levels, copied from
# https://github.com/tudo-astroparticlephysics/cta_preprocessing
cleaning_level = {
    'LSTCam': (3.5, 7.5, 2),  # ?? (3, 6) for Abelardo...
    'FlashCam': (4, 8, 2),  # there is some scaling missing?
    'ASTRICam': (5, 7, 2),
}


input_url = get_dataset_path('gamma_test_large.simtel.gz')
event_source = EventSourceFactory.produce(input_url=input_url)

calibrator = CameraCalibrator(
    eventsource=event_source,
)

reco = HillasReconstructor()

for event in event_source:
    print('Event', event.count)
    calibrator.calibrate(event)

    # mapping of telescope_id to parameters for stereo reconstruction
    hillas_containers = {}
    pointing_azimuth = {}
Example #11
0
    print("============================================")
    print("n or [enter]    - go to Next event")
    print("d               - Display the event")
    print("p               - Print all event data")
    print("i               - event Info")
    print("s               - save event image")
    print("q               - Quit")
    return input("Choice: ")


if __name__ == '__main__':

    if len(sys.argv) > 1:
        filename = sys.argv.pop(1)
    else:
        filename = get_dataset_path("gamma_test_large.simtel.gz")

    plt.style.use("ggplot")
    plt.show(block=False)

    # loop over events and display menu at each event:
    source = event_source(filename)

    for event in source:

        print(
            "EVENT_ID: ", event.r0.event_id, "TELS: ", event.r0.tels_with_data,
            "MC Energy:", event.mc.energy
        )

        while True:
Example #12
0
import matplotlib.pylab as plt
import numpy as np
from astropy import units as u

from ctapipe.io import event_source
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay

if __name__ == "__main__":

    plt.figure(figsize=(9.5, 8.5))

    # load up a single event, so we can get the subarray info:
    source = event_source(
        datasets.get_dataset_path("gamma_test_large.simtel.gz"), max_events=1,
    )

    event = next(iter(source))

    # display the array
    subarray = source.subarray
    ad = ArrayDisplay(subarray, tel_scale=3.0)

    print("Now setting vectors")
    plt.pause(1.0)
    plt.tight_layout()

    for phi in np.linspace(0, 360, 30) * u.deg:
        r = 200 * np.cos(phi / 2) * u.m
        ad.set_vector_rho_phi(r, phi)
Example #13
0
#!/usr/bin/env python3

import matplotlib.pylab as plt
import numpy as np
from astropy import units as u

from ctapipe.io import event_source
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay

if __name__ == '__main__':

    plt.figure(figsize=(9.5, 8.5))

    # load up a single event, so we can get the subarray info:
    source = event_source(datasets.get_dataset_path("gamma_test.simtel.gz"),
                          max_events=1)
    for event in source:
        pass

    # display the array
    subarray = event.inst.subarray
    ad = ArrayDisplay(subarray, tel_scale=3.0)

    print("Now setting vectors")
    plt.pause(1.0)
    plt.tight_layout()

    for phi in np.linspace(0, 360, 30) * u.deg:
        r = np.cos(phi / 2)
        ad.set_vector_rho_phi(r, phi)
Example #14
0
config_prod3b_CTAS = resource_filename("protopipe", "scripts/tests/test_config_analysis_south.yaml")
config_AdaBoostRegressor = resource_filename("protopipe", "scripts/tests/test_AdaBoostRegressor.yaml")
config_RandomForestRegressor = resource_filename("protopipe", "scripts/tests/test_RandomForestRegressor.yaml")
config_RandomForestClassifier = resource_filename("protopipe", "scripts/tests/test_RandomForestClassifier.yaml")
config_DL3_ED_prod3b = resource_filename("protopipe", "scripts/tests/test_performance_ED_prod3b.yaml")

# TEST FILES

URL_TEST_DATA = "http://cccta-dataserver.in2p3.fr/data/protopipe/testData/"
URL_PROD3B_CTAN = f"{URL_TEST_DATA}/prod3_laPalma_baseline_Az180_Zd20"
URL_PROD3B_CTAS = f"{URL_TEST_DATA}/prod3_Paranal_baseline_Az180_Zd20"

input_data = {

    "PROD3B_CTA_NORTH": {"config": config_prod3b_CTAN,
                         "gamma1": get_dataset_path("gamma1.simtel.gz",
                                                    url=f"{URL_PROD3B_CTAN}"),
                         "gamma2": get_dataset_path("gamma2.simtel.gz",
                                                    url=f"{URL_PROD3B_CTAN}"),
                         "gamma3": get_dataset_path("gamma3.simtel.gz",
                                                    url=f"{URL_PROD3B_CTAN}"),
                         "proton1": get_dataset_path("proton1.simtel.gz",
                                                     url=f"{URL_PROD3B_CTAN}"),
                         "proton2": get_dataset_path("proton2.simtel.gz",
                                                     url=f"{URL_PROD3B_CTAN}"),
                         "electron1": get_dataset_path("electron1.simtel.gz",
                                                       url=f"{URL_PROD3B_CTAN}")
                         },

    "PROD3B_CTA_SOUTH": {"config": config_prod3b_CTAS,
                         "gamma1": get_dataset_path("gamma1.simtel.gz",
                                                    url=f"{URL_PROD3B_CTAS}"),