Skip to content
Snippets Groups Projects
Commit 1d34c0c3 authored by Andi Gerken's avatar Andi Gerken
Browse files

Fixed issue #21 (when an existing file was opened with mode "w", it was not initialized)

parent 0608dd55
No related branches found
No related tags found
1 merge request!25Fixed issue #21 (when an existing file was opened with mode "w", it was not initialized)
Pipeline #42624 passed
...@@ -176,8 +176,19 @@ class File(h5py.File): ...@@ -176,8 +176,19 @@ class File(h5py.File):
# x Create file, fail if exists # x Create file, fail if exists
# a Read/write if exists, create otherwise # a Read/write if exists, create otherwise
logging.info(f"Opening File {path}") logging.info(f"Opening File {path}")
initialize = not Path(path).exists()
assert mode in ["r", "r+", "w", "x", "a"], f"Unknown mode {mode}."
# If the file does not exist or if it should be truncated with mode=w, initialize it.
if Path(path).exists() and mode != "w":
initialize = False
else:
initialize = True
try:
super().__init__(path, mode, libver=("earliest", "v110")) super().__init__(path, mode, libver=("earliest", "v110"))
except OSError as e:
raise OSError(f"Could not open file {path} with mode {mode}.\n{e}")
if initialize: if initialize:
assert world_size_cm is not None and format_version is not None assert world_size_cm is not None and format_version is not None
...@@ -264,13 +275,30 @@ class File(h5py.File): ...@@ -264,13 +275,30 @@ class File(h5py.File):
sampling = self["samplings"].create_group(name) sampling = self["samplings"].create_group(name)
if frequency_hz is not None:
sampling.attrs["frequency_hz"] = (np.float32)(frequency_hz)
if monotonic_time_points_us is not None: if monotonic_time_points_us is not None:
monotonic_time_points_us = np.array(
monotonic_time_points_us, dtype=np.int64
)
sampling.create_dataset( sampling.create_dataset(
"monotonic_time_points_us", "monotonic_time_points_us", data=monotonic_time_points_us
data=np.array(monotonic_time_points_us, dtype=np.int64),
) )
if frequency_hz is None:
diff = np.diff(monotonic_time_points_us)
if np.all(diff == diff[0]) and diff[0] > 0:
frequency_hz = 1e6 / diff[0]
warnings.warn(
f"The frequency_hz of {frequency_hz:.2f}hz was calculated automatically by robofish.io. The safer variant is to pass it using frequency_hz.\nThis is important when using fish_models with the files."
)
else:
warnings.warn(
"The frequency_hz could not be calculated automatically. When using fish_models, the file will access frequency_hz."
)
if frequency_hz is not None:
sampling.attrs["frequency_hz"] = (np.float32)(frequency_hz)
if calendar_time_points is not None: if calendar_time_points is not None:
def format_calendar_time_point(p): def format_calendar_time_point(p):
...@@ -314,8 +342,9 @@ class File(h5py.File): ...@@ -314,8 +342,9 @@ class File(h5py.File):
@property @property
def default_sampling(self): def default_sampling(self):
if not "samplings" in self: assert (
print("Wassss?", self) "samplings" in self
), "The file does not have a group 'sampling' which is required."
if "default" in self["samplings"].attrs: if "default" in self["samplings"].attrs:
return self["samplings"].attrs["default"] return self["samplings"].attrs["default"]
return None return None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment