From a999da570e9f93c050d6cc519222dc0639b1ac5c Mon Sep 17 00:00:00 2001 From: ralf Date: Mon, 5 Aug 2024 13:27:23 +0300 Subject: [PATCH] fixed iso to tag as is in the file, added function to print out attributes --- h5_data_organizer.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/h5_data_organizer.py b/h5_data_organizer.py index 425aa0e..b978b64 100644 --- a/h5_data_organizer.py +++ b/h5_data_organizer.py @@ -3,31 +3,41 @@ import pandas as pd file = "ltcc_current.h5" -# Dict to hold DFs 'sex', 'iso' & 'spid' -dfs_by_sex_iso_spid = {} + +def print_attrs(name, obj): + print(f"\nAttributes for {name}:") + for key, val in obj.attrs.items(): + print(f" {key}: {val}") + + +with h5py.File(file, "r") as h5_file: + h5_file.visititems(print_attrs) + +# Dict to hold DFs 'sex', 'tag' & 'spid' +dfs_by_sex_tag_spid = {} with h5py.File(file, "r") as h5_file: for eid in h5_file.keys(): attributes = h5_file[eid].attrs sex = attributes.get("sex") - iso = attributes.get("iso") + tag = attributes.get("tag") spid = attributes.get("spid") - # Creates a unique key for dict based on sex, iso & spid - key = f"{sex}_{iso}_{spid}" + # Creates a unique key for dict based on sex, tag & spid + key = f"{sex}_{tag}_{spid}" - if key not in dfs_by_sex_iso_spid: - dfs_by_sex_iso_spid[key] = pd.DataFrame() + if key not in dfs_by_sex_tag_spid: + dfs_by_sex_tag_spid[key] = pd.DataFrame() - row_data = {"experiment_id": eid, "sex": sex, "iso": iso, "spid": spid} + row_data = {"experiment_id": eid, "sex": sex, "tag": tag, "spid": spid} temp_df = pd.DataFrame([row_data]) # Append the DF to the appropriate dict entry - dfs_by_sex_iso_spid[key] = pd.concat( - [dfs_by_sex_iso_spid[key], temp_df], ignore_index=True + dfs_by_sex_tag_spid[key] = pd.concat( + [dfs_by_sex_tag_spid[key], temp_df], ignore_index=True ) -for key, df in dfs_by_sex_iso_spid.items(): +for key, df in dfs_by_sex_tag_spid.items(): print(f"DataFrame for {key}:") print(df) print()