Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCP upgrades: semi-automatic scripts and update of the current scripts (1 LST -> 4 LSTs); Torino team #154

Merged
merged 77 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
72cdd35
DL0 to DL1 reduction
ranieremenezes Sep 26, 2023
f93a73a
Add files via upload
ranieremenezes Sep 26, 2023
a22c872
Add files via upload
ranieremenezes Sep 26, 2023
d9c69d8
"Merge" with master
Elisa-Visentin Sep 26, 2023
493ad1c
remove duplicate function (io, gadf)
Elisa-Visentin Sep 27, 2023
37027e3
pyflakes
Elisa-Visentin Sep 27, 2023
89cac19
trying to fix tests
Elisa-Visentin Sep 27, 2023
ea94f71
Try to fix tests
Elisa-Visentin Sep 27, 2023
7fb3dc3
tests
Elisa-Visentin Sep 27, 2023
8d8849f
Fix test functions
Elisa-Visentin Sep 27, 2023
14786b9
pyflakes
Elisa-Visentin Sep 27, 2023
c0395e5
readme + bug
Elisa-Visentin Sep 27, 2023
ef7822b
test (gamma combo_types)
Elisa-Visentin Sep 27, 2023
31aa48c
use pre-offset search in automatic script
Elisa-Visentin Sep 27, 2023
19760ac
Fixed tests (now they don't fail, locally)
Elisa-Visentin Sep 27, 2023
f2f634d
Update README.md
ranieremenezes Sep 27, 2023
332500d
Updated README
ranieremenezes Sep 27, 2023
e2c6c31
failed git test
Elisa-Visentin Sep 27, 2023
24cbdf6
Remove commented functions + remove get_stereo()
Elisa-Visentin Sep 28, 2023
93d729a
config.yaml default
Elisa-Visentin Sep 28, 2023
fe69420
LST_version
Elisa-Visentin Sep 28, 2023
517a046
calib module for Calibration functions
Elisa-Visentin Sep 28, 2023
75093a6
Focal length (MCs)
Elisa-Visentin Sep 28, 2023
756782e
Config and scripts paths + setup.py
Elisa-Visentin Sep 28, 2023
cded8f5
partial analysis
Elisa-Visentin Sep 28, 2023
5313599
Bug
Elisa-Visentin Sep 28, 2023
0913458
config file in resources
Elisa-Visentin Sep 28, 2023
c487e0d
readme and resources
Elisa-Visentin Sep 28, 2023
2bdf63b
README + analysis_type + 'with open...'
Elisa-Visentin Oct 2, 2023
10e8849
env name, minor fixes
Elisa-Visentin Oct 2, 2023
85dbbb6
minor fixes
Elisa-Visentin Oct 2, 2023
76fb3fd
f_string
Elisa-Visentin Oct 2, 2023
0911807
f-string
Elisa-Visentin Oct 2, 2023
273690b
Console scripts in bash
Elisa-Visentin Oct 2, 2023
5f96f5b
Calibration docstring
Elisa-Visentin Oct 2, 2023
174e377
Fixed Calibrate_LST (input)
Elisa-Visentin Oct 2, 2023
9923e48
Bug
Elisa-Visentin Oct 2, 2023
972ee8d
one single Calibrate function
Elisa-Visentin Oct 2, 2023
1a486e4
Bug (bash scripts)
Elisa-Visentin Oct 3, 2023
733afad
Writing bash script with writelines()
ranieremenezes Oct 3, 2023
aaaa867
List of functions in alphabetical order
ranieremenezes Oct 3, 2023
06e3f6c
List of functions in alphabetical order
ranieremenezes Oct 3, 2023
5d24322
List of functions in alphabetical order
ranieremenezes Oct 3, 2023
9a56f9e
List of functions in alphabetical order
ranieremenezes Oct 3, 2023
937f025
Standardized inline comments
ranieremenezes Oct 3, 2023
69e7b6f
Moved IT scripts to a different folder
Elisa-Visentin Oct 3, 2023
c5cfff6
Moving tutorial
Elisa-Visentin Oct 3, 2023
da99132
Update README.md
ranieremenezes Oct 3, 2023
cd9b396
Remove IT scripts
Elisa-Visentin Oct 4, 2023
e0756c5
Calibrate (MAGIC & LST)
Elisa-Visentin Oct 4, 2023
ece3300
Minor fixes + calib Docstring
Elisa-Visentin Oct 4, 2023
69ca7c1
calib exception (+ minor fixes)
Elisa-Visentin Oct 4, 2023
ee00aaa
Bug
Elisa-Visentin Oct 4, 2023
8c9a75c
Update ci.yml
Elisa-Visentin Oct 5, 2023
b47ee02
Minor fixes + test tel. combinations
Elisa-Visentin Oct 5, 2023
03d5cbb
removed max_multiplicity in get_stereo
Elisa-Visentin Oct 5, 2023
334ed6d
tests
Elisa-Visentin Oct 6, 2023
bb44aff
minor fixes
Elisa-Visentin Oct 6, 2023
db0f298
bug
Elisa-Visentin Oct 6, 2023
6f8223c
Bug calib function
Elisa-Visentin Oct 6, 2023
d6ab37f
Bug magic-only
Elisa-Visentin Oct 6, 2023
52d6222
fixed test
Elisa-Visentin Oct 6, 2023
03f6e98
updated config files
ranieremenezes Oct 6, 2023
8de7c67
Info on high level analysis in README
ranieremenezes Oct 6, 2023
183da71
Tel. name exception
Elisa-Visentin Oct 6, 2023
7dce9ba
calib: test + bug fix
Elisa-Visentin Oct 6, 2023
15376a5
tel_positions
Elisa-Visentin Oct 7, 2023
ff8ee4a
Check if the input telescope list is fine
ranieremenezes Oct 9, 2023
b604768
New function to check telescope list
ranieremenezes Oct 9, 2023
cf92a25
Just fixing a typo
ranieremenezes Oct 9, 2023
48ae576
Minor fixes
Elisa-Visentin Oct 12, 2023
93c6dd7
Minor fixes+tests (check_list, tel-wise training)
Elisa-Visentin Oct 13, 2023
f4fde0f
Output file name
Elisa-Visentin Oct 13, 2023
4ff0885
Pyflakes error
Elisa-Visentin Oct 13, 2023
fff4a84
calib fixes
Elisa-Visentin Oct 13, 2023
df96e74
Check_input_list test
Elisa-Visentin Oct 16, 2023
ddfdcf3
Merge branch 'master' into Torino
Elisa-Visentin Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# A conda environment with all useful package for ctapipe developers
name: magic-lst1
name: magic-lst
channels:
- conda-forge
dependencies:
Expand Down
48 changes: 37 additions & 11 deletions magicctapipe/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from math import trunc
from ctapipe.utils.download import download_file_cached
from magicctapipe.utils import resource_file
import yaml

maxjoint = 13000
maxmonly = 500
Expand Down Expand Up @@ -42,11 +43,11 @@
"20201216_M2_05093711.014_Y_CrabNebula-W0.40+035.root",
]
DL1_LST_data = ["dl1_LST-1.Run03265.0094.h5"]

"""
Temporary paths
"""


@pytest.fixture(scope="session")
def temp_DL1_gamma(tmp_path_factory):
return tmp_path_factory.mktemp("DL1_gammas")
Expand Down Expand Up @@ -231,12 +232,10 @@ def temp_DL2_real_monly(tmp_path_factory):
def temp_DL3_monly(tmp_path_factory):
return tmp_path_factory.mktemp("DL3_monly")


"""
Custom data
"""


@pytest.fixture(scope="session")
def dl2_test(temp_DL2_test):
"""
Expand Down Expand Up @@ -270,12 +269,10 @@ def pd_test():
df = pd.DataFrame(np.array([[1, 2], [3, 4], [5, 6]]), columns=["a", "b"])
return df


"""
Remote paths (to download test files)
"""


@pytest.fixture(scope="session")
def base_url():
return "http://www.magic.iac.es/mcp-testdata"
Expand All @@ -286,12 +283,10 @@ def env_prefix():
# ENVIRONMENT VARIABLES TO BE CREATED
return "MAGIC_CTA_DATA_"


"""
Downloads: files
"""


@pytest.fixture(scope="session")
def dl0_gamma(base_url, env_prefix):
gamma_dl0 = []
Expand Down Expand Up @@ -375,21 +370,52 @@ def dl1_lst(base_url, env_prefix):

@pytest.fixture(scope="session")
def config():
config_path = resource_file("config.yaml")
config_path = resource_file("test_config.yaml")
return config_path


@pytest.fixture(scope="session")
def config_monly():
config_path = resource_file("config_monly.yaml")
return config_path
config_path = resource_file("test_config_monly.yaml")
return config_path


@pytest.fixture(scope="session")
def config_gen():
config_path = resource_file("test_config_general.yaml")
with open(config_path, "rb") as f:
config = yaml.safe_load(f)
return config


@pytest.fixture(scope="session")
def config_gen_4lst():
config_path = resource_file("test_config_general_4LST.yaml")
with open(config_path, "rb") as f:
config = yaml.safe_load(f)
return config


@pytest.fixture(scope="session")
def config_calib():
config_path = resource_file("test_config_calib.yaml")
with open(config_path, "rb") as f:
config = yaml.safe_load(f)
return config


@pytest.fixture(scope="session")
def config_check():
config_path = resource_file("test_check_list.yaml")
with open(config_path, "rb") as f:
config = yaml.safe_load(f)
return config


"""
Data processing
"""


@pytest.fixture(scope="session")
def gamma_l1(temp_DL1_gamma, dl0_gamma, config):
"""
Expand Down
6 changes: 3 additions & 3 deletions magicctapipe/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
get_num_islands_MAGIC,
clean_image_params,
)
from .leakage import get_leakage
from .calib import calibrate

from .leakage import (
get_leakage,
)

__all__ = [
"MAGICClean",
"PixelTreatment",
"get_num_islands_MAGIC",
"calibrate",
"clean_image_params",
"get_leakage",
]
140 changes: 140 additions & 0 deletions magicctapipe/image/calib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import numpy as np
from ctapipe.image import (
apply_time_delta_cleaning,
number_of_islands,
tailcuts_clean,
)
from ctapipe.instrument import CameraGeometry
from lstchain.image.cleaning import apply_dynamic_cleaning
from lstchain.image.modifier import (
add_noise_in_pixels,
random_psf_smearer,
set_numba_seed
)
from magicctapipe.image import MAGICClean


__all__ = [
"calibrate"
]


def calibrate(event, tel_id, config, calibrator, is_lst, obs_id=None, camera_geoms=None, magic_clean=None):

jsitarek marked this conversation as resolved.
Show resolved Hide resolved
"""
This function calibrates the camera image for a single event of a telescope

Parameters
----------
event: event
From an EventSource
tel_id: int
Telescope ID
config: dictionary
Parameters for image extraction and calibration
calibrator: CameraCalibrator (ctapipe.calib)
ctapipe object needed to calibrate the camera
is_lst: bool
Whether the telescope is a LST
obs_id: int
Observation ID. Unsed in case of LST telescope
camera_geoms: telescope.camera.geometry
Camera geometry. Used in case of LST telescope
magic_clean: dictionary (1 entry per MAGIC telescope)
Each entry is a MAGICClean object using the telescope camera geometry. Used in case of MAGIC telescope


Returns
-------
signal_pixels: boolean mask
Mask of the pixels selected by the cleaning
image: numpy array
Array of number of p.e. in the camera pixels
peak_time: numpy array
Array of the signal peak time in the camera pixels

"""
if (not is_lst) and (magic_clean==None):
raise ValueError("Check the provided parameters and the telescope type; MAGIC calibration not possible if magic_clean not provided")
if (is_lst) and (obs_id==None):
raise ValueError("Check the provided parameters and the telescope type; LST calibration not possible if obs_id not provided")
if (is_lst) and (camera_geoms==None):
raise ValueError("Check the provided parameters and the telescope type; LST calibration not possible if gamera_geoms not provided")
if (not is_lst) and (type(magic_clean[tel_id])!=MAGICClean):
raise ValueError("Check the provided magic_clean parameter; MAGIC calibration not possible if magic_clean not a dictionary of MAGICClean objects")
if (is_lst) and (type(camera_geoms[tel_id])!=CameraGeometry):
raise ValueError("Check the provided camera_geoms parameter; LST calibration not possible if camera_geoms not a dictionary of CameraGeometry objects")

calibrator._calibrate_dl0(event, tel_id)
calibrator._calibrate_dl1(event, tel_id)

image = event.dl1.tel[tel_id].image.astype(np.float64)
peak_time = event.dl1.tel[tel_id].peak_time.astype(np.float64)
gabemery marked this conversation as resolved.
Show resolved Hide resolved

if not is_lst:
use_charge_correction = config["charge_correction"]["use"]

if use_charge_correction:
# Scale the charges by the correction factor
image *= config["charge_correction"]["factor"]

# Apply the image cleaning
signal_pixels, image, peak_time = magic_clean[tel_id].clean_image(
event_image=image, event_pulse_time=peak_time
)

else:
increase_nsb = config["increase_nsb"].pop("use")
jsitarek marked this conversation as resolved.
Show resolved Hide resolved
increase_psf = config["increase_psf"]["use"]
use_time_delta_cleaning = config["time_delta_cleaning"].pop("use")
use_dynamic_cleaning = config["dynamic_cleaning"].pop("use")
use_only_main_island = config["use_only_main_island"]

if increase_nsb:
rng = np.random.default_rng(obs_id)
# Add extra noise in pixels
image = add_noise_in_pixels(rng, image, **config["increase_nsb"])

if increase_psf:
set_numba_seed(obs_id)

Check warning on line 99 in magicctapipe/image/calib.py

View check run for this annotation

Codecov / codecov/patch

magicctapipe/image/calib.py#L99

Added line #L99 was not covered by tests
# Smear the image
image = random_psf_smearer(

Check warning on line 101 in magicctapipe/image/calib.py

View check run for this annotation

Codecov / codecov/patch

magicctapipe/image/calib.py#L101

Added line #L101 was not covered by tests
image=image,
fraction=config["increase_psf"]["fraction"],
indices=camera_geoms[tel_id].neighbor_matrix_sparse.indices,
indptr=camera_geoms[tel_id].neighbor_matrix_sparse.indptr,
)

# Apply the image cleaning
signal_pixels = tailcuts_clean(
camera_geoms[tel_id], image, **config["tailcuts_clean"]
)

if use_time_delta_cleaning:
signal_pixels = apply_time_delta_cleaning(
geom=camera_geoms[tel_id],
mask=signal_pixels,
arrival_times=peak_time,
**config["time_delta_cleaning"],
)

if use_dynamic_cleaning:
signal_pixels = apply_dynamic_cleaning(
image, signal_pixels, **config["dynamic_cleaning"]
)

if use_only_main_island:
_, island_labels = number_of_islands(camera_geoms[tel_id], signal_pixels)
n_pixels_on_island = np.bincount(island_labels.astype(np.int64))

Check warning on line 128 in magicctapipe/image/calib.py

View check run for this annotation

Codecov / codecov/patch

magicctapipe/image/calib.py#L127-L128

Added lines #L127 - L128 were not covered by tests

# The first index means the pixels not surviving
# the cleaning, so should not be considered
n_pixels_on_island[0] = 0
max_island_label = np.argmax(n_pixels_on_island)
signal_pixels[island_labels != max_island_label] = False

Check warning on line 134 in magicctapipe/image/calib.py

View check run for this annotation

Codecov / codecov/patch

magicctapipe/image/calib.py#L132-L134

Added lines #L132 - L134 were not covered by tests

config["increase_nsb"]["use"]=increase_nsb
config["time_delta_cleaning"]["use"]=use_time_delta_cleaning
config["dynamic_cleaning"]["use"]=use_dynamic_cleaning

return signal_pixels, image, peak_time
Loading