hdf5-mojo: High level HDF5 bindings for Mojo (first release) ![]()
I’ve been working on porting a lot of my scientific stack and well-known computational & physics libraries to Mojo (look forward to some more library spam from me xD), and HDF5 is a core dependency in that workflow, so this happened ![]()
It’s honestly been really cool to see how much Mojo has evolved over the past ~2 years. Early on, porting scientific libraries was… painful
. Now it’s almost smooth sailing, with a few hiccups here and there. Super excited for what’s coming next!!!
This is an early release of hdf5-mojo, a set of high-level bindings to the HDF5 C library with a Mojo-friendly API. It’s usable today, but still evolving.
What this is right now
A simple two-layer design:
-
Low-level (
hdf5/bindings.mojo)
Thin FFI wrapper over the HDF5 C API (HDF5Lib) for cases where you need full control. -
High-level (
hdf5/api.mojo)
Ergonomic interface (H5File,NDArray) for most use cases.
In practice, you’ll almost always use the high-level API.
What works now
- Read 1D / 2D datasets without knowing shapes ahead of time
- Read scalar attributes from groups/datasets
- Create files and write datasets (row-major)
- Safe group creation via
require_group - Automatic HDF5 library discovery via
$CONDA_PREFIX(pixi-friendly)
Example
Check out the examples for a more complete walkthrough with sample data.
from hdf5 import H5File
def main() raises:
var f = H5File("data.h5")
# Read scalar attributes
var emin = f.read_scalar_attr[DType.float64]("/group", "min_energy")
var nnodes = f.read_scalar_attr[DType.int32]("/group", "number_energy_nodes")
# Read 1-D and 2-D datasets (sizes discovered automatically)
var xs = f.read_1d[DType.float64]("/group/dataset")
var mat = f.read_2d[DType.float64]("/group/matrix")
print(xs[0], mat[0, 0])
xs.free()
mat.free()
f.close()