Skip to content

Update writing metadata to hdf5 logic to keep files from bloating in size.#342

Merged
kujaku11 merged 23 commits into
masterfrom
patches
Jun 5, 2026
Merged

Update writing metadata to hdf5 logic to keep files from bloating in size.#342
kujaku11 merged 23 commits into
masterfrom
patches

Conversation

@kujaku11

Copy link
Copy Markdown
Collaborator

There is a known issue with hdf5 when rewriting metadata attributes the file size will increase because new locations are created but the old ones are not deleted. one solution is to check metadata against old medadata and only write if anything changed. This PR applies that logic so there are no unnecessary rewrites to limit file size increase. Another method is to create a new file from the existing file which if bloated will reduce the file size. that is also included in mth5/utils.

tests are updated as is the action to only tun codecov on one python version which speeds up the tests.

kujaku11 and others added 9 commits March 27, 2026 15:45
There seems to be transient issue with requesting geomag data in that sometimes chunks return nan values when there are data, so trying retries.
Add guards to prevent unintended or redundant writes to HDF5 dataset attributes. Introduce detection of file mode and can_write check, plus _metadata_needs_write and _values_equal helpers (handling numpy scalars/arrays) to compare in-memory metadata with on-disk attributes before writing. Import read_attrs_to_dict and to_numpy_type helpers. Update TransferFunctionGroup to construct EstimateDataset with write_metadata=False to avoid side-effect writes when only reading, and add minor formatting cleanups.
Add guards to skip unnecessary HDF5 attribute writes when metadata is unchanged and to avoid writes on read-only files. Implemented _can_write, _values_equal and _metadata_needs_write helpers on dataset classes and BaseGroup to compare existing attrs (including numpy arrays / numpy scalars) to expected values before create/modify calls. Update write_metadata paths to use attr.modify only when values differ and to skip entirely if file is not writable. Also tidy some dataset slicing formatting and adjust imports. Add tests (tests/groups/test_metadata_write_guards.py) to assert unchanged metadata is not rewritten for BaseGroup, ChannelDataset, FeatureChannelDataset, FCChannelDataset and EstimateDataset.
Introduce repack_hdf5 in mth5.utils.h5_tools to copy HDF5 file-level attributes, groups, datasets, and their attributes into a new file (mirroring h5repack behavior). The function validates input existence, prevents accidental overwrite unless overwrite=True, and returns the output Path. Add tests to verify structure/data/attributes are copied, missing source raises FileNotFoundError, and overwrite behavior raises FileExistsError unless allowed. Uses h5py and pathlib.
Add a guard in BaseGroup before writing metadata that inspects the HDF5 file mode (via getattr on self.hdf5_group.file.mode). If the file is not opened writable (no 'w' or '+'), the code now logs a warning and returns early to avoid attempting writes on read-only files and raising errors.
Update .github/workflows/tests.yml to run pytest with coverage only when matrix.python-version is 3.13; for other Python versions the job runs tests without coverage. Also add an if condition to the Codecov upload step so coverage is uploaded only for the 3.13 job, avoiding duplicate or unnecessary coverage collection/uploads from other matrix entries.
@kujaku11 kujaku11 changed the title Conditionally run coverage and upload for Python 3.13 Update .github/workflows/tests.yml to run pytest with coverage only when matrix.python-version is 3.13; for other Python versions the job runs tests without coverage. Also add an if condition to the Codecov upload step so coverage is uploaded only for the 3.13 job, avoiding duplicate or unnecessary coverage collection/uploads from other matrix entries. Update writing metadata to hdf5 logic to keep files from bloating in size. May 26, 2026
@kujaku11 kujaku11 added the release:patch patch update label Jun 5, 2026
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

🚀 Version bumped to v0.6.8 and changelog updated. Ready to merge!

@kujaku11
kujaku11 merged commit 473476b into master Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:patch patch update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants