def test_get_source_dependent_parameters_observed(observed_dl1_files): from lstchain.reco.dl1_to_dl2 import get_source_dependent_parameters # on observation data srcdep_config['observation_mode'] = 'on' dl1_params = pd.read_hdf(observed_dl1_files["dl1_file1"], key=dl1_params_lstcam_key) src_dep_df_on = get_source_dependent_parameters(dl1_params, srcdep_config) # wobble observation data srcdep_config['observation_mode'] = 'wobble' dl1_params['alt_tel'] += np.deg2rad(0.4) src_dep_df_wobble = get_source_dependent_parameters( dl1_params, srcdep_config) assert 'alpha' in src_dep_df_on['on'].columns assert 'dist' in src_dep_df_on['on'].columns assert 'time_gradient_from_source' in src_dep_df_on['on'].columns assert 'skewness_from_source' in src_dep_df_on['on'].columns assert (src_dep_df_on['on']['expected_src_x'] == 0).all() assert (src_dep_df_on['on']['expected_src_y'] == 0).all() np.testing.assert_allclose(src_dep_df_wobble['on']['expected_src_x'], -0.195, atol=1e-2) np.testing.assert_allclose(src_dep_df_wobble['on']['expected_src_y'], 0., atol=1e-2) np.testing.assert_allclose(src_dep_df_wobble['off_180']['expected_src_x'], 0.195, atol=1e-2) np.testing.assert_allclose(src_dep_df_wobble['off_180']['expected_src_y'], 0., atol=1e-2)
def test_get_source_dependent_parameters_mc(simulated_dl1_file): from lstchain.reco.dl1_to_dl2 import get_source_dependent_parameters # for gamma MC dl1_params = pd.read_hdf(simulated_dl1_file, key=dl1_params_lstcam_key) src_dep_df_gamma = get_source_dependent_parameters(dl1_params, srcdep_config) # for proton MC dl1_params.mc_type = 101 src_dep_df_proton = get_source_dependent_parameters( dl1_params, srcdep_config) assert 'alpha' in src_dep_df_gamma['on'].columns assert 'dist' in src_dep_df_gamma['on'].columns assert 'time_gradient_from_source' in src_dep_df_gamma['on'].columns assert 'skewness_from_source' in src_dep_df_gamma['on'].columns assert ( src_dep_df_gamma['on']['expected_src_x'] == dl1_params['src_x']).all() assert ( src_dep_df_gamma['on']['expected_src_y'] == dl1_params['src_y']).all() np.testing.assert_allclose(src_dep_df_proton['on']['expected_src_x'], 0.195, atol=1e-2) np.testing.assert_allclose(src_dep_df_proton['on']['expected_src_y'], 0., atol=1e-2)
def main(): args = parser.parse_args() dl1_filename = os.path.abspath(args.input_file) config = get_standard_config() if args.config_file is not None: try: config = read_configuration_file(os.path.abspath(args.config_file)) except ("Custom configuration could not be loaded !!!"): pass dl1_params = pd.read_hdf(dl1_filename, key=dl1_params_lstcam_key) subarray_info = SubarrayDescription.from_hdf(dl1_filename) tel_id = config["allowed_tels"][0] if "allowed_tels" in config else 1 focal_length = subarray_info.tel[tel_id].optics.equivalent_focal_length src_dep_df = pd.concat(get_source_dependent_parameters( dl1_params, config, focal_length=focal_length), axis=1) metadata = global_metadata() write_dataframe(src_dep_df, dl1_filename, dl1_params_src_dep_lstcam_key, config=config, meta=metadata)
def main(): dl1_filename = os.path.abspath(args.input_file) config = {} if args.config_file is not None: try: config = read_configuration_file(os.path.abspath(args.config_file)) except ("Custom configuration could not be loaded !!!"): pass dl1_params = pd.read_hdf(dl1_filename, key=dl1_params_lstcam_key) src_dep_df = get_source_dependent_parameters(dl1_params, config) write_dataframe(src_dep_df, dl1_filename, dl1_params_src_dep_lstcam_key)
def main(): args = parser.parse_args() custom_config = {} if args.config_file is not None: try: custom_config = read_configuration_file( os.path.abspath(args.config_file)) except ("Custom configuration could not be loaded !!!"): pass config = replace_config(standard_config, custom_config) data = pd.read_hdf(args.input_file, key=dl1_params_lstcam_key) if 'lh_fit_config' in config.keys(): lhfit_data = pd.read_hdf(args.input_file, key=dl1_likelihood_params_lstcam_key) if np.all(lhfit_data['obs_id'] == data['obs_id']) & np.all( lhfit_data['event_id'] == data['event_id']): lhfit_data.drop({'obs_id', 'event_id'}, axis=1, inplace=True) lhfit_keys = lhfit_data.keys() data = pd.concat([data, lhfit_data], axis=1) # if real data, add deltat t to dataframe keys data = add_delta_t_key(data) # Dealing with pointing missing values. This happened when `ucts_time` was invalid. if 'alt_tel' in data.columns and 'az_tel' in data.columns \ and (np.isnan(data.alt_tel).any() or np.isnan(data.az_tel).any()): # make sure there is a least one good pointing value to interp from. if np.isfinite(data.alt_tel).any() and np.isfinite(data.az_tel).any(): data = impute_pointing(data) else: data.alt_tel = -np.pi / 2. data.az_tel = -np.pi / 2. # Get trained RF path for reconstruction: file_reg_energy = os.path.join(args.path_models, 'reg_energy.sav') file_cls_gh = os.path.join(args.path_models, 'cls_gh.sav') if config['disp_method'] == 'disp_vector': file_disp_vector = os.path.join(args.path_models, 'reg_disp_vector.sav') elif config['disp_method'] == 'disp_norm_sign': file_disp_norm = os.path.join(args.path_models, 'reg_disp_norm.sav') file_disp_sign = os.path.join(args.path_models, 'cls_disp_sign.sav') subarray_info = SubarrayDescription.from_hdf(args.input_file) tel_id = config["allowed_tels"][0] if "allowed_tels" in config else 1 focal_length = subarray_info.tel[tel_id].optics.equivalent_focal_length # Apply the models to the data # Source-independent analysis if not config['source_dependent']: data = filter_events( data, filters=config["events_filters"], finite_params=config['energy_regression_features'] + config['disp_regression_features'] + config['particle_classification_features'] + config['disp_classification_features'], ) if config['disp_method'] == 'disp_vector': dl2 = dl1_to_dl2.apply_models(data, file_cls_gh, file_reg_energy, reg_disp_vector=file_disp_vector, focal_length=focal_length, custom_config=config) elif config['disp_method'] == 'disp_norm_sign': dl2 = dl1_to_dl2.apply_models(data, file_cls_gh, file_reg_energy, reg_disp_norm=file_disp_norm, cls_disp_sign=file_disp_sign, focal_length=focal_length, custom_config=config) # Source-dependent analysis if config['source_dependent']: # if source-dependent parameters are already in dl1 data, just read those data. if dl1_params_src_dep_lstcam_key in get_dataset_keys(args.input_file): data_srcdep = get_srcdep_params(args.input_file) # if not, source-dependent parameters are added now else: data_srcdep = pd.concat(dl1_to_dl2.get_source_dependent_parameters( data, config, focal_length=focal_length), axis=1) dl2_srcdep_dict = {} srcindep_keys = data.keys() srcdep_assumed_positions = data_srcdep.columns.levels[0] for i, k in enumerate(srcdep_assumed_positions): data_with_srcdep_param = pd.concat([data, data_srcdep[k]], axis=1) data_with_srcdep_param = filter_events( data_with_srcdep_param, filters=config["events_filters"], finite_params=config['energy_regression_features'] + config['disp_regression_features'] + config['particle_classification_features'] + config['disp_classification_features'], ) if config['disp_method'] == 'disp_vector': dl2_df = dl1_to_dl2.apply_models( data_with_srcdep_param, file_cls_gh, file_reg_energy, reg_disp_vector=file_disp_vector, focal_length=focal_length, custom_config=config) elif config['disp_method'] == 'disp_norm_sign': dl2_df = dl1_to_dl2.apply_models(data_with_srcdep_param, file_cls_gh, file_reg_energy, reg_disp_norm=file_disp_norm, cls_disp_sign=file_disp_sign, focal_length=focal_length, custom_config=config) dl2_srcdep = dl2_df.drop(srcindep_keys, axis=1) dl2_srcdep_dict[k] = dl2_srcdep if i == 0: dl2_srcindep = dl2_df[srcindep_keys] os.makedirs(args.output_dir, exist_ok=True) output_file = os.path.join( args.output_dir, os.path.basename(args.input_file).replace('dl1', 'dl2', 1)) if os.path.exists(output_file): raise IOError(output_file + ' exists, exiting.') dl1_keys = get_dataset_keys(args.input_file) if dl1_images_lstcam_key in dl1_keys: dl1_keys.remove(dl1_images_lstcam_key) if dl1_params_lstcam_key in dl1_keys: dl1_keys.remove(dl1_params_lstcam_key) if dl1_params_src_dep_lstcam_key in dl1_keys: dl1_keys.remove(dl1_params_src_dep_lstcam_key) if dl1_likelihood_params_lstcam_key in dl1_keys: dl1_keys.remove(dl1_likelihood_params_lstcam_key) metadata = global_metadata() write_metadata(metadata, output_file) with open_file(args.input_file, 'r') as h5in: with open_file(output_file, 'a') as h5out: # Write the selected DL1 info for k in dl1_keys: if not k.startswith('/'): k = '/' + k path, name = k.rsplit('/', 1) if path not in h5out: grouppath, groupname = path.rsplit('/', 1) g = h5out.create_group(grouppath, groupname, createparents=True) else: g = h5out.get_node(path) h5in.copy_node(k, g, overwrite=True) # need container to use lstchain.io.add_global_metadata and lstchain.io.add_config_metadata if not config['source_dependent']: if 'lh_fit_config' not in config.keys(): write_dl2_dataframe(dl2, output_file, config=config, meta=metadata) else: dl2_onlylhfit = dl2[lhfit_keys] dl2.drop(lhfit_keys, axis=1, inplace=True) write_dl2_dataframe(dl2, output_file, config=config, meta=metadata) write_dataframe(dl2_onlylhfit, output_file, dl2_likelihood_params_lstcam_key, config=config, meta=metadata) else: write_dl2_dataframe(dl2_srcindep, output_file, config=config, meta=metadata) write_dataframe(pd.concat(dl2_srcdep_dict, axis=1), output_file, dl2_params_src_dep_lstcam_key, config=config, meta=metadata)
def test_get_source_dependent_parameters(): from lstchain.reco.dl1_to_dl2 import get_source_dependent_parameters dl1_params = pd.read_hdf(dl1_file, key=dl1_params_lstcam_key) src_dep_df = get_source_dependent_parameters(dl1_params, standard_config)