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

Remove unnecessary for loop in save_pandas_data_in_table #101

Merged
merged 2 commits into from
Sep 30, 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
15 changes: 7 additions & 8 deletions magicctapipe/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,15 @@ def load_irf_files(input_dir_irf):
return irf_data, extra_header


def save_pandas_data_in_table(data, output_file, group_name, table_name, mode="w"):
def save_pandas_data_in_table(
input_data, output_file, group_name, table_name, mode="w"
):
"""
Saves a pandas data frame in a table.

Parameters
----------
data: pandas.core.frame.DataFrame
input_data: pandas.core.frame.DataFrame
Pandas data frame
output_file: str
Path to an output HDF file
Expand All @@ -947,13 +949,10 @@ def save_pandas_data_in_table(data, output_file, group_name, table_name, mode="w
"a" for appending the table to the file
"""

params = data.dtypes.index
dtypes = data.dtypes.values
values = [tuple(array) for array in input_data.to_numpy()]
dtypes = np.dtype(list(zip(input_data.dtypes.index, input_data.dtypes.values)))

data_array = np.array(
[tuple(array) for array in data.to_numpy()],
dtype=np.dtype([(param, dtype) for param, dtype in zip(params, dtypes)]),
)
data_array = np.array(values, dtype=dtypes)

with tables.open_file(output_file, mode=mode) as f_out:
f_out.create_table(group_name, table_name, createparents=True, obj=data_array)
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def dl1_stereo_to_dl2(input_file_dl1, input_dir_rfs, output_dir):
sim_config = pd.read_hdf(input_file_dl1, key="simulation/config")

save_pandas_data_in_table(
data=sim_config,
input_data=sim_config,
output_file=output_file,
group_name="/simulation",
table_name="config",
Expand Down
2 changes: 1 addition & 1 deletion magicctapipe/scripts/lst1_magic/lst1_magic_stereo_reco.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def stereo_reconstruction(input_file, output_dir, config, magic_only_analysis=Fa
sim_config = pd.read_hdf(input_file, key="simulation/config")

save_pandas_data_in_table(
data=sim_config,
input_data=sim_config,
output_file=output_file,
group_name="/simulation",
table_name="config",
Expand Down