From 860457cdc6189dd5313fc688d145afa14e36ea95 Mon Sep 17 00:00:00 2001 From: swittl <simon.wittl@th-deg.de> Date: Fri, 21 Mar 2025 10:54:27 +0100 Subject: [PATCH] added overwrite / inserts --- examples/header.h5 | Bin 24928 -> 800 bytes examples/projection_geometry.h5 | Bin 186352 -> 186352 bytes pyproject.toml | 2 +- src/h5schemas/header/add_header.py | 4 +- src/h5schemas/header/overwrite_header.py | 14 +++ src/h5schemas/image/overwrite_image.py | 20 ++++ .../add_projection_geometry.py | 11 +++ .../overwrite_projection_geometry.py | 88 ++++++++++++++++++ 8 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/h5schemas/header/overwrite_header.py create mode 100644 src/h5schemas/image/overwrite_image.py create mode 100644 src/h5schemas/projection_geometry/overwrite_projection_geometry.py diff --git a/examples/header.h5 b/examples/header.h5 index 55f849af30b411def5615dca45ce8811be91d386..82daee09f5d9bab4d4ef6cf1c24d6067e7457f8a 100644 GIT binary patch delta 63 zcmaEGh;aei1Pvwy=80M<j0_X|B^RIMV4A#wNkEZ<0Rm1iK^ahr5lRP4+~^Jf$(Rce literal 24928 zcmeI3-%k@k5Xa|QAQdq}^Z|IfCyddOEq84wiLs4<JP<KZ5*~ZBcjOXUp|p`FpY=cS zPw`*y4=^#l>nqOe4^k?>3>cI0y~54x?C#9&XJ?n>%6(c}UVSh=J5F-tax_AdT6MP* zd(|Ujab;Iuw~A-wM=L*PjN*}eh^DQ5%K9H{SKC}!A#UmXJ88R-8Znyl<Lr=kgTUHK z^{vcw$4=L`@{7$gTcmE<ijTTc>%ba%y|gK+%g;sE@S4)2ansj2@N6b~o-x?8Jk8rI z*GtWm|B`Y#Muo!r6rN;Fz)`kJeI}dH4szCCHj`0h(}ee+?aJi|TGLjKwa093#z&1# zdeBYZwY7bQ$00jbe$9wXRHG9;l~%^2>nH90J06t^qVGoV-9ObXQ?}l0EPIr3F3Gp^ zx~Pxaul2EuTHH<h1nG3<HMvJ~OtTXo_R>zQb5)2;b6(3DPV(BhbzQBlZ}A~Xwasl2 zy27oqF7s`IDq8i}r{qg3ZKthXi>}HOoyRs$WJ~?wCp!LXKav_Ajp^~{>_D&HKW2YC z;`RdLlKtZTrc}7uDYu*MmnnAUl3SSE_Bsu&mxTMD*Xvgs>v~LbfbiwUr_T*8*W7vf z+>ht?=KTd#Sj-m+`9e@zP)qz1l%FZJq?GQ>=bDu|pIC4A_V>l)j)_6LD{JoDZo8cx z_F_M6#_evJq;W?&_gy;=`wCD&00ck)1V8`;KmY_l00ck)1V8`;u0=qeyUP=L_q^Rn znmNC_^3U2Uw%9ytMhOH!00ck)1VCVj2!u&Pm4i6Ut70kPF9H&kkD_9bPr_0&NaEcn z2}(DeF2`61!a}1zMya80Jw|c2SnP{YR^{@)h*9c`5jA$hTa9s3*60rcAOHd&00JNY z0w4eaAOHd&00JNY0w4eaAOHd&00JN&@7`SS{}TTBbn4XsyM5uozG^d2i6RJq00@8p z2!Oya5cu|CZuaM|uhsYYCtCh^aaIn)95DwF009sH0T2KI5C8!X009sH0T2KI5C8!X a009sH0T2KI5C8!X009sH0T38=0>1&pSlXcg diff --git a/examples/projection_geometry.h5 b/examples/projection_geometry.h5 index 33fa0d029f9c06080eb024291a542da0e34bbf58..881fcc6f7961040df0d860fa0471cbe8ddce7644 100644 GIT binary patch delta 189 zcmeycp8Lak?hOa7v6Q41mrUH4AZ}=sW|W#_WU6amW}K*NXqaZ8Ymt(evf29jR)vWN zI5>`b&sO#Ne$#Pt;Prfo%>gm(?90E2GbV6-ux9`RB?p8ITu=d<pbV1W@^9A}8{o<r fP-M!P6c|5D7POP!oNzq>WMu>U_HWyne#rv>nc74N delta 208 zcmeycp8Lak?hOa7i5sRQCmUHB8tWREnw#nx8m1ZOS|+BNO}4)73zkbsNlQVNGcZiu zY<+#Jf<SL_VdwX6Hyv}<L+H(c*YhPd2gJ0qFaIXan82mvzyJnH4hR`81*o6`vY-r9 mPzFhG`M2wg4O|RR83q)Yawdh%3D*-qmMvi4{%t$cFL?m7J4f>X diff --git a/pyproject.toml b/pyproject.toml index e4a6fac..75d5612 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 d95bc9e..892043a 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 0000000..9e5ee8c --- /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 0000000..e9238c0 --- /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 45a608c..32f8bff 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 0000000..3a216af --- /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 -- GitLab