From cfc494596fd0300ff5362dad33d54aa55cb0452b Mon Sep 17 00:00:00 2001 From: swittl <simon.wittl@th-deg.de> Date: Mon, 24 Mar 2025 15:23:41 +0100 Subject: [PATCH] group setter added --- examples/header.h5 | Bin 800 -> 24928 bytes pyproject.toml | 2 +- src/h5schemas/h5_types/base_dataset.py | 12 ++++++++++++ src/h5schemas/h5_types/image.py | 2 -- src/h5schemas/header/add_header.py | 18 ++++++++++++------ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/examples/header.h5 b/examples/header.h5 index 82daee09f5d9bab4d4ef6cf1c24d6067e7457f8a..ccb68dce024d304dd03651a1779a648511df20db 100644 GIT binary patch literal 24928 zcmeI3-A)rh6vxlDKq_LS(F^cuuS|><w;w{wjco+vf{1|<x$KtRl1*rZ(k48D`WoJN z<9nF+1ipcf;2oYbbB2`4mjPo^{tMmnF*9doesg9>w(Q5%rImY=k0wd3Ooqm2+Nkb! ztXCtn9%sAqhAN)Q4=TT;tm2XU5Y4DRtMP~H)i;)xiEFy}PHHm>#;oSzI6vg=Ah5bz zdn+^D*6I2xziFPSNxh^UANIobzB>B-Bofu-XW3P}qV#Cew$%WhX0qp5ho<F8-ln-+ zO0xb-%9uW?RJu#0<FxH?oV`+>N~cVKjK)i+QbEO5;XSBL*`A_R<Mmj3$nGdU-03F! zz2tqz_-A<>vSa0AM`U}oI?)rUWnHFxTKC`4xD*imw1V&ciFT2)`DT5|qm&CtzRv5s zHg3N*CeEvIJN;8+(wW!f4n1a?-T0uNbYqjNLS%;XTGnuy*Uqi$N^NbE4@s(TY>6-h zZk>(Iw<)R`)#I3wFYTn0wEJzkEKf`xYo5rK`Xf$w^w)7DH9VRy<Im_oFW*0Qe>~v& z0^@@H;{K*ox!MW0>+Y8<2Xo0SOm2FehSy8F`=8hASL<tLOmcwm<;G{u4Yup<JbmWJ zbGvhXA;_0=`Ft*4tQUes{w-FX2Ek$wnAUu*sWkb-dZ)j)CoXqP4EvpJxO2U&PIAzX z{UnMzy`+`IU1>aUZQma-pn?DhfB*=900@8p2!H?xfB*=900>-(fIN4XC-m-lyOT6~ zcJshL?XRjCop!?l0T2KI5C8!X7$E{Z{&w|WPp4ccl$!Z`DHoKRP5!c>733;WRLHeT z^HH%CH^WwO{<>q3KH_F&VW5w4b0@m#K3<f<g`I&u!XUiad0dwR+JgWHfB*=900@8p z2!H?xfB*=900@8p2!H?xfB*=900_vtH|PAngnu@jMs3KiFFfe0HbaFlK>!3m00ck) z1V)L#*H1Ig-~IYgD}S~!^WDl(nj_`{0w4eaAOHd&00JNY0w4eaAOHd&00JNY0w4ea WAOHd&00JNY0w4eaAOHfRO5itIt;{<B delta 63 zcmaEGh;aei1Pvwy=80M<j0_X|B^RIMV4A#wNkEZ<0Rm1iK^ahr5lRP4+~^Jf$(Rce diff --git a/pyproject.toml b/pyproject.toml index c4ca58a..38f999c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "h5schemas" -version = "0.1.20" +version = "0.1.21" description = "Add your description here" readme = "README.md" authors = [ diff --git a/src/h5schemas/h5_types/base_dataset.py b/src/h5schemas/h5_types/base_dataset.py index 4ab919b..df8b869 100644 --- a/src/h5schemas/h5_types/base_dataset.py +++ b/src/h5schemas/h5_types/base_dataset.py @@ -9,12 +9,24 @@ class BaseDataset: maximum: int | float | None = None dtype: str | np.float64 | np.float32 | np.uint16 | None = None + _group: str | None = None + def __init__(self, definition: GroupDefinition): self.path = definition.path self.dataset_name = definition.path.rsplit("/")[-1] self.group = definition.path[1 : -len(self.dataset_name)] self.description = definition.description + @property + def group(self) -> str | None: + return self._group + + @group.setter + def group(self, group_value: str | None): + if len(group_value) == 0: + group_value = '/' + self._group = group_value + @classmethod def with_file(cls, definition: GroupDefinition, h5_file: h5py.File): instance = cls(definition) diff --git a/src/h5schemas/h5_types/image.py b/src/h5schemas/h5_types/image.py index ccdb79b..8a844fc 100644 --- a/src/h5schemas/h5_types/image.py +++ b/src/h5schemas/h5_types/image.py @@ -11,8 +11,6 @@ class ImageDataset(BaseDataset): super().__init__(definition) def add_to_file(self, h5_file: h5py.File): - if len(self.group) == 0: - self.group = '/' group = h5_file.require_group(self.group) self.dataset = group.create_dataset( name=self.dataset_name, diff --git a/src/h5schemas/header/add_header.py b/src/h5schemas/header/add_header.py index 892043a..4c1605d 100644 --- a/src/h5schemas/header/add_header.py +++ b/src/h5schemas/header/add_header.py @@ -3,6 +3,7 @@ from thd_json import header from h5schemas.scan_schema import open_schema from h5schemas.initialize_dataset import initialize_dataset from datetime import datetime +import numpy as np def init_header( @@ -28,17 +29,22 @@ def check_uuid_time_stamp(uuid: str | None = None, timestamp: int | None = None) def add_header_sample( - h5_file: File | Group, uuid: str | None, timestamp: float | None = None + h5_file: File | Group, uuid: str | np.ndarray | None, timestamp: float | np.ndarray | None = None ): uuid, timestamp = check_uuid_time_stamp(uuid, timestamp) + if isinstance(uuid, np.ndarray): + uuid_shape = uuid.shape[0] + else: + uuid_shape = 1 + dst = h5_file["uuid"] - dst.resize((dst.shape[0] + 1,)) - dst[-1,] = uuid + dst.resize((dst.shape[0] + uuid_shape,)) + dst[-uuid_shape,] = uuid dst = h5_file["timestamp"] - dst.resize((dst.shape[0] + 1,)) - dst[-1,] = timestamp + dst.resize((dst.shape[0] + uuid_shape,)) + dst[-uuid_shape,] = timestamp def uuid_time_from_json_header(json_header_obj: header.JsonHeader) -> tuple[str, float]: @@ -49,7 +55,7 @@ def uuid_time_from_json_header(json_header_obj: header.JsonHeader) -> tuple[str, if __name__ == "__main__": from h5schemas.header.overwrite_header import overwrite_header_at_index - test_file = File("./examples/header.h5", "a") + test_file = File("./examples/header.h5", "w") init_header(test_file) add_header_sample(test_file, None) -- GitLab