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

Console scripts #16

Merged
merged 16 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ The following command will set up a conda virtual environment, add the necessary

*magic-cta-pipe* scripts to perform the analysis of MAGIC only and MAGIC+LST1 data. Within the *scripts* folder we have the subdirectories:

* *magic*: scripts for the analysis of MAGIC data (currently under heavy restructuring in order to be used with ctapipe v0.12)
* *lst1_magic_real*: scripts for the MAGIC+LST1 analysis (updated to be used with ctapipe v0.12)
* *magic*: scripts for the analysis of MAGIC data (**currently under heavy restructuring in order to be used with ctapipe v0.12**)
* *lst1_magic*: scripts for the MAGIC+LST1 analysis (updated to be used with ctapipe v0.12)

<!--
A brief description:
Expand Down
2 changes: 2 additions & 0 deletions magicctapipe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import irfs
from . import train
from . import utils
from . import scripts

from .version import __version__

Expand All @@ -10,5 +11,6 @@
"irfs",
"train",
"utils",
"scripts",
"__version__",
]
21 changes: 21 additions & 0 deletions magicctapipe/scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .lst1_magic import (
dl1_to_dl2,
event_coincidence,
mc_dl0_to_dl1,
stereo_reco,
train_rf_regressor,
train_rf_classifier,
magic_cal_to_dl1,
merge_hdf_files,
)

__all__ = [
"dl1_to_dl2",
"event_coincidence",
"mc_dl0_to_dl1",
"stereo_reco",
"train_rf_regressor",
"train_rf_classifier",
"magic_cal_to_dl1",
"merge_hdf_files",
]
21 changes: 21 additions & 0 deletions magicctapipe/scripts/lst1_magic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .lst1_magic_dl1_to_dl2 import dl1_to_dl2
from .lst1_magic_event_coincidence import event_coincidence
from .lst1_magic_mc_dl0_to_dl1 import mc_dl0_to_dl1
from .lst1_magic_stereo_reco import stereo_reco
from .lst1_magic_train_rfs import (
train_rf_regressor,
train_rf_classifier,
)
from .magic_data_cal_to_dl1 import magic_cal_to_dl1
from .merge_hdf_files import merge_hdf_files

__all__ = [
"dl1_to_dl2",
"event_coincidence",
"mc_dl0_to_dl1",
"stereo_reco",
"train_rf_regressor",
"train_rf_classifier",
"magic_cal_to_dl1",
"merge_hdf_files",
]
24 changes: 10 additions & 14 deletions ...apipe/scripts/lst1_magic_real/config.yaml → magicctapipe/scripts/lst1_magic/config.yaml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mc_allowed_tels:

LST:

# The following settings are derived from lstchain v0.8.4:
# The following settings are derived from lstchain v0.8.4:
# - lstchain_standard_config.json
# - lstchain/data/lstchain_dl1ab_tune_MC_to_Crab_config.json

Expand All @@ -21,7 +21,7 @@ LST:
extra_bias_in_dim_pixels: 0.5
transition_charge: 8
extra_noise_in_bright_pixels: 1.2

increase_psf:
use: true
smeared_light_fraction: 0.125
Expand Down Expand Up @@ -65,14 +65,14 @@ MAGIC:
pedestalType: 'FromExtractorRndm'


event_coincidence:
type_lst_time: 'dragon_time'
event_coincidence:
type_lst_time: 'dragon_time'
window_width: 6.0e-7 # unit: [sec]
offset_start: -5.0e-6 # unit: [sec]
offset_start: -5.0e-6 # unit: [sec]
offset_stop: 0.0 # unit: [sec]


stereo_reco:

stereo_reco:
quality_cuts: '(intensity > 50) & (width > 0)'


Expand All @@ -97,7 +97,7 @@ energy_regressor:
max_samples: null

features: [
'intensity',
'intensity',
'length',
'width',
'skewness',
Expand Down Expand Up @@ -128,7 +128,7 @@ direction_regressor:
warm_start: false
ccp_alpha: 0.0
max_samples: null

features: [
'intensity',
'length',
Expand All @@ -141,7 +141,7 @@ direction_regressor:
'impact'
]


event_classifier:
settings:
n_estimators: 100
Expand Down Expand Up @@ -174,7 +174,3 @@ event_classifier:
'h_max',
'impact'
]




47 changes: 23 additions & 24 deletions .../lst1_magic_real/lst1_magic_dl1_to_dl2.py → ...ripts/lst1_magic/lst1_magic_dl1_to_dl2.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# coding: utf-8

"""
Author: Yoshiki Ohtani (ICRR, ohtani@icrr.u-tokyo.ac.jp)
Author: Yoshiki Ohtani (ICRR, ohtani@icrr.u-tokyo.ac.jp)

Reconstruct the DL2 parameters (i.e., energy, direction and gammaness) with trained RFs.
The RFs will be applied per telescope combination and per telescope type.
If real data is input, the parameters in the Alt/Az coordinate will be transformed to the RA/Dec coordinate.
If real data is input, the parameters in the Alt/Az coordinate will be transformed to the RA/Dec coordinate.

Usage:
$ python lst1_magic_dl1_to_dl2.py
$ python lst1_magic_dl1_to_dl2.py
--input-file "./data/dl1_stereo/dl1_stereo_lst1_magic_run03265.0040.h5"
--output-file "./data/dl2/dl2_lst1_magic_run03265.0040.h5"
--energy-regressors "./data/rfs/energy_regressors_*.joblib"
Expand All @@ -19,7 +19,6 @@

import glob
import time
import yaml
import tables
import logging
import argparse
Expand Down Expand Up @@ -59,7 +58,7 @@ def apply_rfs(data, rfs_mask, estimator, tel_descriptions=None):
estimator.load(path)

tel_ids = list(estimator.telescope_rfs.keys())

df = data.query(f'(tel_id == {tel_ids}) & (multiplicity == {len(tel_ids)})')
df.dropna(subset=estimator.feature_names, inplace=True)
df['multiplicity'] = df.groupby(['obs_id', 'event_id']).size()
Expand All @@ -70,14 +69,14 @@ def apply_rfs(data, rfs_mask, estimator, tel_descriptions=None):
if n_events == 0:
logger.info('--> No corresponding events are found. Skipping.')
continue

logger.info(f'--> {n_events} events are found. Applying...')

if tel_descriptions != None:
if tel_descriptions is not None:
df_reco = estimator.predict(df, tel_descriptions)
else:
df_reco = estimator.predict(df)

reco_params = reco_params.append(df_reco)

reco_params.sort_index(inplace=True)
Expand All @@ -86,7 +85,7 @@ def apply_rfs(data, rfs_mask, estimator, tel_descriptions=None):


def dl1_to_dl2(
input_file, output_file,
input_file, output_file,
energy_regressors=None, direction_regressors=None, event_classifiers=None
):

Expand All @@ -100,16 +99,16 @@ def dl1_to_dl2(
subarray = SubarrayDescription.from_hdf(input_file)

# --- reconstruct energy ---
if energy_regressors != None:
if energy_regressors is not None:

estimator = EnergyRegressor()
estimator = EnergyRegressor()
logger.info('\nReconstucting the energy...')

reco_params = apply_rfs(data_joint, energy_regressors, estimator)
data_joint = data_joint.join(reco_params)

# --- reconstruct direction ---
if direction_regressors != None:
if direction_regressors is not None:

estimator = DirectionRegressor()
logger.info('\nReconstructing the direction...')
Expand All @@ -136,34 +135,34 @@ def dl1_to_dl2(
az=u.Quantity(data_joint['az_tel_mean'].values, u.rad),
timestamp=timestamps
)

reco_ra, reco_dec = transform_to_radec(
alt=u.Quantity(data_joint['reco_alt'].values, u.deg),
alt=u.Quantity(data_joint['reco_alt'].values, u.deg),
az=u.Quantity(data_joint['reco_az'].values, u.deg),
timestamp=timestamps
)
)

reco_ra_mean, reco_dec_mean = transform_to_radec(
alt=u.Quantity(data_joint['reco_alt_mean'].values, u.deg),
alt=u.Quantity(data_joint['reco_alt_mean'].values, u.deg),
az=u.Quantity(data_joint['reco_az_mean'].values, u.deg),
timestamp=timestamps
)
)

data_joint['ra_tel'] = ra_tel.to(u.deg).value
data_joint['dec_tel'] = dec_tel.to(u.deg).value
data_joint['ra_tel_mean'] = ra_tel_mean.to(u.deg).value
data_joint['dec_tel_mean'] = dec_tel_mean.to(u.deg).value
data_joint['reco_ra'] = reco_ra.to(u.deg).value
data_joint['reco_dec'] = reco_dec.to(u.deg).value
data_joint['reco_dec'] = reco_dec.to(u.deg).value
data_joint['reco_ra_mean'] = reco_ra_mean.to(u.deg).value
data_joint['reco_dec_mean'] = reco_dec_mean.to(u.deg).value

# --- classify event type ---
if event_classifiers != None:
if event_classifiers is not None:

estimator = EventClassifier()
logger.info('\nClassifying the event type...')

reco_params = apply_rfs(data_joint, event_classifiers, estimator)
data_joint = data_joint.join(reco_params)

Expand Down Expand Up @@ -195,7 +194,7 @@ def main():
parser = argparse.ArgumentParser()

parser.add_argument(
'--input-file', '-i', dest='input_file', type=str,
'--input-file', '-i', dest='input_file', type=str,
help='Path to an input DL1-stereo data file.'
)

Expand All @@ -222,7 +221,7 @@ def main():
args = parser.parse_args()

dl1_to_dl2(
args.input_file, args.output_file,
args.input_file, args.output_file,
args.energy_regressors, args.direction_regressors, args.event_classifiers
)

Expand Down
Loading