diff --git a/examples/header.h5 b/examples/header.h5 index 55f849af30b411def5615dca45ce8811be91d386..82daee09f5d9bab4d4ef6cf1c24d6067e7457f8a 100644 Binary files a/examples/header.h5 and b/examples/header.h5 differ diff --git a/examples/projection_geometry.h5 b/examples/projection_geometry.h5 index 33fa0d029f9c06080eb024291a542da0e34bbf58..881fcc6f7961040df0d860fa0471cbe8ddce7644 100644 Binary files a/examples/projection_geometry.h5 and b/examples/projection_geometry.h5 differ diff --git a/pyproject.toml b/pyproject.toml index e4a6fac20989234d8cd845b72f1c7b54d7851af9..75d5612df15f3f36efbef06d73f9077675cde8c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "h5schemas" -version = "0.1.15" +version = "0.1.16" description = "Add your description here" readme = "README.md" authors = [ diff --git a/src/h5schemas/header/add_header.py b/src/h5schemas/header/add_header.py index d95bc9e83b0e82a3aa00cd697850e831840284e2..892043a47283c9f90820f832e1ecc30357785bb0 100644 --- a/src/h5schemas/header/add_header.py +++ b/src/h5schemas/header/add_header.py @@ -48,10 +48,12 @@ def uuid_time_from_json_header(json_header_obj: header.JsonHeader) -> tuple[str, if __name__ == "__main__": - test_file = File("./examples/header.h5", "w") + from h5schemas.header.overwrite_header import overwrite_header_at_index + test_file = File("./examples/header.h5", "a") init_header(test_file) add_header_sample(test_file, None) add_header_sample(test_file, None) add_header_sample(test_file, None) add_header_sample(test_file, *uuid_time_from_json_header(header.generate_header())) + overwrite_header_at_index(test_file, 0, 'test', 0.0) \ No newline at end of file diff --git a/src/h5schemas/header/overwrite_header.py b/src/h5schemas/header/overwrite_header.py new file mode 100644 index 0000000000000000000000000000000000000000..9e5ee8c587857e672055f1a896c5d94569d114df --- /dev/null +++ b/src/h5schemas/header/overwrite_header.py @@ -0,0 +1,14 @@ +from h5py import File, Group +from h5schemas.header.add_header import check_uuid_time_stamp + + +def overwrite_header_at_index( + h5_file: File | Group, index: int, uuid: str | None, timestamp: float | None = None +): + uuid, timestamp = check_uuid_time_stamp(uuid, timestamp) + + dst = h5_file["uuid"] + dst[index] = uuid + + dst = h5_file["timestamp"] + dst[index] = timestamp \ No newline at end of file diff --git a/src/h5schemas/image/overwrite_image.py b/src/h5schemas/image/overwrite_image.py new file mode 100644 index 0000000000000000000000000000000000000000..e9238c0f77a3c8bd91bbac0885c71e7f15418e12 --- /dev/null +++ b/src/h5schemas/image/overwrite_image.py @@ -0,0 +1,20 @@ +from h5py import File, Group +import numpy as np +from pathlib import Path + +from h5schemas.header.overwrite_header import overwrite_header_at_index + +def overwrite_image_at_index(h5_file: File | Group, + image: Path | np.ndarray, + index: int, + uuid: str | None = None, + timestamp: int | None = None, +): + if isinstance(image, Path): + dst = h5_file["image_path"] + dst[index] = str(image) + else: + dst = h5_file["image_array"] + dst[index] = image + + overwrite_header_at_index(h5_file["header"], index, uuid, timestamp) \ No newline at end of file diff --git a/src/h5schemas/projection_geometry/add_projection_geometry.py b/src/h5schemas/projection_geometry/add_projection_geometry.py index 45a608c89db6cd6af7f4dd368d0c727e68d6cab2..32f8bff68b369834e7456b3481179f37aa968bda 100644 --- a/src/h5schemas/projection_geometry/add_projection_geometry.py +++ b/src/h5schemas/projection_geometry/add_projection_geometry.py @@ -101,6 +101,7 @@ def add_projection_geometry_sample( if __name__ == "__main__": + from h5schemas.projection_geometry.overwrite_projection_geometry import overwrite_projection_geometry_at_index test_file = File("./examples/projection_geometry.h5", "w") init_projection_geometry(test_file) add_projection_geometry_sample( @@ -113,3 +114,13 @@ if __name__ == "__main__": add_projection_geometry_sample( test_file, [0.0, 1.0, 2.0], [3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 9.0] ) + + overwrite_projection_geometry_at_index( + test_file, + 0, + [1.0, 1.0, 1.0], + [3.0, 4.0, 5.0, 6.0], + [1.0, 1.0, 1.0], + [10.0, 11.0, 12.0, 13], + uuid='test', + ) \ No newline at end of file diff --git a/src/h5schemas/projection_geometry/overwrite_projection_geometry.py b/src/h5schemas/projection_geometry/overwrite_projection_geometry.py new file mode 100644 index 0000000000000000000000000000000000000000..3a216afdd2a4da5dc8817d4bf72e12fa7771d9dc --- /dev/null +++ b/src/h5schemas/projection_geometry/overwrite_projection_geometry.py @@ -0,0 +1,88 @@ +from h5py import File, Group +import numpy as np +from h5schemas.header.overwrite_header import overwrite_header_at_index + + + + +def overwrite_projection_geometry_at_index( + h5_file: File | Group, + index: int, + detector_center_position_mm: tuple[float, float, float], + detector_center_orientation_quat: tuple[float, float, float, float], + focal_spot_position_mm: tuple[float, float, float], + focal_spot_orientation_quat: tuple[float, float, float, float] | None = None, + uuid: str | None = None, + timestamp: float | None = None, +): + update_values = list() + + def insert_update(values): + dst, value = values + dst[index] = value + + update_values.append( + [h5_file["detector_center_position_mm/x"], detector_center_position_mm[0]] + ) + update_values.append( + [h5_file["detector_center_position_mm/y"], detector_center_position_mm[1]] + ) + update_values.append( + [h5_file["detector_center_position_mm/z"], detector_center_position_mm[2]] + ) + + update_values.append( + [ + h5_file["detector_center_orientation_quat/x"], + detector_center_orientation_quat[0], + ] + ) + update_values.append( + [ + h5_file["detector_center_orientation_quat/y"], + detector_center_orientation_quat[1], + ] + ) + update_values.append( + [ + h5_file["detector_center_orientation_quat/z"], + detector_center_orientation_quat[2], + ] + ) + update_values.append( + [ + h5_file["detector_center_orientation_quat/w"], + detector_center_orientation_quat[3], + ] + ) + + update_values.append( + [h5_file["focal_spot_position_mm/x"], focal_spot_position_mm[0]] + ) + update_values.append( + [h5_file["focal_spot_position_mm/y"], focal_spot_position_mm[1]] + ) + update_values.append( + [h5_file["focal_spot_position_mm/z"], focal_spot_position_mm[2]] + ) + + if focal_spot_orientation_quat is None: + focal_spot_orientation_quat = (0.0, 0.0, 0.0, 1.0) + + update_values.append( + [h5_file["focal_spot_orientation_quat/x"], focal_spot_orientation_quat[0]] + ) + update_values.append( + [h5_file["focal_spot_orientation_quat/y"], focal_spot_orientation_quat[1]] + ) + update_values.append( + [h5_file["focal_spot_orientation_quat/z"], focal_spot_orientation_quat[2]] + ) + update_values.append( + [h5_file["focal_spot_orientation_quat/w"], focal_spot_orientation_quat[3]] + ) + + for value in update_values: + insert_update(value) + + overwrite_header_at_index(h5_file['header'], index, uuid, timestamp) \ No newline at end of file