Skip to content
Snippets Groups Projects
Commit 57014b2c authored by Gröne, Tjark Leon Raphael's avatar Gröne, Tjark Leon Raphael
Browse files

Update file maxwell_integrate_to_h5.py

parent 6a7aa2ee
Branches
No related tags found
No related merge requests found
...@@ -15,7 +15,8 @@ from watchdog.observers.polling import PollingObserver ...@@ -15,7 +15,8 @@ from watchdog.observers.polling import PollingObserver
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
from multiprocessing.pool import ThreadPool as Pool from multiprocessing.pool import ThreadPool as Pool
import pandas as pd import pandas as pd
from silx.io.dictdump import h5todict, dicttoh5 #from silx.io.dictdump import h5todict, dicttoh5
import h5py
import re import re
...@@ -124,198 +125,65 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"): ...@@ -124,198 +125,65 @@ def integrate_ims_in_dir(path_im, path_int, dtype_im=".tif", dtype_int=".dat"):
subdir_name = os.path.basename(os.path.normpath(subdir_path_int)) subdir_name = os.path.basename(os.path.normpath(subdir_path_int))
results_df.to_csv(os.path.join(subdir_path_int, f"{subdir_name}.csv"), index=False) results_df.to_csv(os.path.join(subdir_path_int, f"{subdir_name}.csv"), index=False)
# Create the HDF5 file
# Sort results_data by filename with h5py.File("output_silx_format.h5", "w") as f:
def natural_sort_key(item): # Create top-level group (e.g., subdir_name)
return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', item["filename"])] subdir_grp = f.create_group(subdir_name)
subdir_grp.attrs["NX_class"] = np.string_("NXentry")
results_data = sorted(results_data, key=natural_sort_key) subdir_grp.attrs["description"] = np.string_(subdir_name)
# Prepare data for HDF5 file using silx
hdf5_data = { for idx, result in enumerate(results_data, start=1):
subdir_name: { scan_name = f"{idx}.1"
"@NX_class": "NXentry", scan_grp = subdir_grp.create_group(scan_name)
"description": f"{subdir_name}",
"files": [ # --- Group-level attributes ---
{ scan_grp.attrs["NX_class"] = np.string_("NXentry")
"name": f"{idx}.1", scan_grp.attrs["default"] = np.string_("measurement")
"path": f"/{idx}.1", scan_grp.attrs["plotselect"] = np.string_("q,I")
"attributes": [
{ # --- Measurement group ---
"name": "NX_class", meas_grp = scan_grp.create_group("measurement")
"shape": [], meas_grp.attrs["NX_class"] = np.string_("NXcollection")
"type": {
"class": "String", # Datasets: q, I, dI
"charSet": "UTF-8", for name in ["q", "I", "dI"]:
"strPad": "null-terminated" data = result[name]
}, meas_grp.create_dataset(name, data=data, chunks=(min(len(data), 1000),), dtype='f8')
"value": "NXentry"
}, # --- Plotselect group ---
{ plotselect_grp = scan_grp.create_group("plotselect")
"name": "default", plotselect_grp.attrs["NX_class"] = np.string_("NXcollection")
"shape": [], plotselect_grp.attrs["axes"] = np.string_("q")
"type": { plotselect_grp.attrs["signal"] = np.string_("I")
"class": "String", # # Sort results_data by filename
"charSet": "UTF-8", # def natural_sort_key(item):
"strPad": "null-terminated" # return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', item["filename"])]
},
"value": "measurement" # results_data = sorted(results_data, key=natural_sort_key)
}, # # Prepare data for HDF5 file using silx
{ # hdf5_data = {}
"name": "plotselect", # for idx, result in enumerate(results_data, start=1):
"shape": [], # hdf5_data[f"{idx}.1"] = {
"type": { # "@NX_class": "NXentry",
"class": "String", # "measurement": {
"charSet": "UTF-8", # "@NX_class": "NXcollection",
"strPad": "null-terminated" # "q": result["q"].tolist(), # Convert numpy arrays to lists for HDF5 compatibility
}, # "I": result["I"].tolist(),
"value": "q,I" # "dI": result["dI"].tolist(),
} # },
], # "plotselect": {
"kind": "group", # "@NX_class": "NXcollection",
"children": [ # "axes": "q",
{ # "signal": "I",
"name": "measurement", # },
"path": f"/{idx}.1/measurement", # }
"attributes": [
{ print(f"Results for subdirectory {subdir_name} saved to HDF5 file using h5py.")
"name": "NX_class",
"shape": [],
"type": {
"class": "String",
"charSet": "UTF-8",
"strPad": "null-terminated"
},
"value": "NXcollection"
}
],
"kind": "group",
"children": [
{
"name": "q",
"path": f"/{idx}.1/measurement/q",
"attributes": [],
"kind": "dataset",
"shape": [len(result["q"])],
"type": {
"class": "Float",
"endianness": "little-endian",
"size": 64
},
"chunks": [min(len(result["q"]), 1000)],
"filters": [],
"rawType": {
"signed": False,
"type": 1,
"vlen": False,
"littleEndian": True,
"size": 8,
"total_size": len(result["q"])
},
"value": result["q"]
},
{
"name": "I",
"path": f"/{idx}.1/measurement/I",
"attributes": [],
"kind": "dataset",
"shape": [len(result["I"])],
"type": {
"class": "Float",
"endianness": "little-endian",
"size": 64
},
"chunks": [min(len(result["I"]), 1000)],
"filters": [],
"rawType": {
"signed": False,
"type": 1,
"vlen": False,
"littleEndian": True,
"size": 8,
"total_size": len(result["I"])
},
"value": result["I"]
},
{
"name": "dI",
"path": f"/{idx}.1/measurement/dI",
"attributes": [],
"kind": "dataset",
"shape": [len(result["dI"])],
"type": {
"class": "Float",
"endianness": "little-endian",
"size": 64
},
"chunks": [min(len(result["dI"]), 1000)],
"filters": [],
"rawType": {
"signed": False,
"type": 1,
"vlen": False,
"littleEndian": True,
"size": 8,
"total_size": len(result["dI"])
},
"value": result["dI"]
}
]
},
{
"name": "plotselect",
"path": f"/{idx}.1/plotselect",
"attributes": [
{
"name": "NX_class",
"shape": [],
"type": {
"class": "String",
"charSet": "UTF-8",
"strPad": "null-terminated"
},
"value": "NXcollection"
},
{
"name": "axes",
"shape": [1],
"type": {
"class": "String",
"charSet": "UTF-8",
"strPad": "null-terminated"
},
"value": "q"
},
{
"name": "signal",
"shape": [],
"type": {
"class": "String",
"charSet": "UTF-8",
"strPad": "null-terminated"
},
"value": "I"
}
],
"kind": "group"
}
]
}
for idx, result in enumerate(results_data, start=1)
]
}
}
# Save to HDF5 file using silx
hdf5_file_path = os.path.join(subdir_path_int, f"{subdir_name}.h5")
dicttoh5(hdf5_data, hdf5_file_path, mode="w")
print(f"Results for subdirectory {subdir_name} saved to HDF5 file using silx.")
# Save to HDF5 file using silx # Save to HDF5 file using silx
hdf5_file_path = os.path.join(subdir_path_int, f"{subdir_name}.h5") # hdf5_file_path = os.path.join(subdir_path_int, f"{subdir_name}.h5")
dicttoh5(hdf5_data, hdf5_file_path, mode="w") # dicttoh5(hdf5_data, hdf5_file_path, mode="w")
print(f"Results for subdirectory {subdir_name} saved to CSV and HDF5 files using silx.")
del results_df del results_df
else: else:
print(f"No images were integrated in subdirectory {subdir}. No results DataFrame created.") print(f"No images were integrated in subdirectory {subdir}. No results DataFrame created.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment