Datasets:
Add reconstruct.py
Browse files- reconstruct.py +119 -0
reconstruct.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reconstruct: beamform one rotation frame of the eSAF rotational 3D US dataset.
|
| 2 |
+
|
| 3 |
+
Loads a zea .hdf5 acquisition (raw RF channel data, one normal plane-wave
|
| 4 |
+
transmit per rotation frame), reads the acquisition parameters, and runs the
|
| 5 |
+
delay-and-sum beamforming pipeline configured in pipeline.yaml. The resulting
|
| 6 |
+
B-mode image is saved as a PNG, together with a plot of the per-frame probe
|
| 7 |
+
rotation angle (this dataset's special data: the linear array rotates 180
|
| 8 |
+
degrees about its axial axis, 1 degree per frame) so downstream users know how
|
| 9 |
+
to interpret the frame axis.
|
| 10 |
+
|
| 11 |
+
Usage:
|
| 12 |
+
python reconstruct.py
|
| 13 |
+
python reconstruct.py --input data/acq_0016_point_d130_r4.hdf5
|
| 14 |
+
python reconstruct.py --frame-index 45
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
# Default to the jax backend so the script runs without exporting KERAS_BACKEND.
|
| 20 |
+
os.environ.setdefault("KERAS_BACKEND", "jax")
|
| 21 |
+
os.environ.setdefault("MPLBACKEND", "Agg")
|
| 22 |
+
|
| 23 |
+
import argparse
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
|
| 26 |
+
import matplotlib.pyplot as plt
|
| 27 |
+
import numpy as np
|
| 28 |
+
import zea
|
| 29 |
+
from zea import Config, File, Pipeline
|
| 30 |
+
|
| 31 |
+
HERE = Path(__file__).parent
|
| 32 |
+
DEFAULT_INPUT = HERE / "data" / "acq_0008_point_d60_r4.hdf5"
|
| 33 |
+
DEFAULT_OUTPUT = HERE / "outputs" / "reconstruct_example.png"
|
| 34 |
+
CONFIG = HERE / "pipeline.yaml"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def main():
|
| 38 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 39 |
+
parser.add_argument("--input", type=Path, default=DEFAULT_INPUT)
|
| 40 |
+
parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT)
|
| 41 |
+
parser.add_argument(
|
| 42 |
+
"--frame-index",
|
| 43 |
+
type=int,
|
| 44 |
+
default=None,
|
| 45 |
+
help="rotation frame to beamform (default: frame closest to 90 deg, "
|
| 46 |
+
"where an off-axis target lies in the imaging plane)",
|
| 47 |
+
)
|
| 48 |
+
args = parser.parse_args()
|
| 49 |
+
|
| 50 |
+
if not args.input.exists():
|
| 51 |
+
raise FileNotFoundError(f"{args.input} not found.")
|
| 52 |
+
|
| 53 |
+
zea.init_device()
|
| 54 |
+
|
| 55 |
+
# Load the whole processing chain + parameters from pipeline.yaml
|
| 56 |
+
config = Config.from_path(str(CONFIG))
|
| 57 |
+
|
| 58 |
+
# Load file: acquisition parameters (with config overrides), one raw RF frame,
|
| 59 |
+
# and this dataset's special data — the probe rotation angle per frame.
|
| 60 |
+
with File(str(args.input)) as f:
|
| 61 |
+
parameters = f.load_parameters(**config.parameters)
|
| 62 |
+
rotation_angles_deg = np.asarray(
|
| 63 |
+
{c.name: c for c in f.custom}["rotation_angles_deg"].data
|
| 64 |
+
).ravel()
|
| 65 |
+
n_frames = f.data.raw_data.shape[0]
|
| 66 |
+
frame = args.frame_index
|
| 67 |
+
if frame is None: # default: frame where the probe has rotated ~90 deg
|
| 68 |
+
frame = int(np.argmin(np.abs(rotation_angles_deg - 90.0)))
|
| 69 |
+
raw = f.data.raw_data[frame : frame + 1] # (1, n_tx, n_ax, n_el, 1) — RF
|
| 70 |
+
|
| 71 |
+
print(f"raw_data frame : {raw.shape} ({n_frames} frames in file)")
|
| 72 |
+
|
| 73 |
+
# Build and run the beamforming pipeline defined in pipeline.yaml
|
| 74 |
+
pipeline = Pipeline.from_config(config)
|
| 75 |
+
inputs = pipeline.prepare_parameters(parameters)
|
| 76 |
+
outputs = pipeline(data=raw, **inputs)
|
| 77 |
+
|
| 78 |
+
recon = np.array(outputs["data"]) # (1, grid_z, grid_x), log-compressed dB
|
| 79 |
+
image = zea.display.to_8bit(
|
| 80 |
+
recon[0], dynamic_range=parameters.dynamic_range, pillow=False
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# B-mode + rotation-angle plot (how to interpret the frame axis)
|
| 84 |
+
zea.visualize.set_mpl_style()
|
| 85 |
+
fig, axes = plt.subplots(
|
| 86 |
+
1, 2, figsize=(11, 5), gridspec_kw={"width_ratios": [1, 1.3]}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
mm = plt.FuncFormatter(lambda v, _: f"{v * 1e3:.0f}")
|
| 90 |
+
axes[0].imshow(image, extent=parameters.extent_imshow, cmap="gray", aspect="equal")
|
| 91 |
+
axes[0].xaxis.set_major_formatter(mm)
|
| 92 |
+
axes[0].yaxis.set_major_formatter(mm)
|
| 93 |
+
axes[0].set(
|
| 94 |
+
title=f"B-mode frame {frame} @ {rotation_angles_deg[frame]:.0f} deg",
|
| 95 |
+
xlabel="Lateral [mm]",
|
| 96 |
+
ylabel="Depth [mm]",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
axes[1].plot(np.arange(n_frames), rotation_angles_deg, color="tab:blue")
|
| 100 |
+
axes[1].axvline(frame, color="tab:red", linewidth=1.2, label="beamformed frame")
|
| 101 |
+
axes[1].set(
|
| 102 |
+
title="Probe rotation angle per frame\n(array rotates about its axial axis)",
|
| 103 |
+
xlabel="Frame index",
|
| 104 |
+
ylabel="Rotation angle [deg]",
|
| 105 |
+
)
|
| 106 |
+
axes[1].grid(True, color="0.85", linewidth=0.8)
|
| 107 |
+
axes[1].legend(loc="upper left", fontsize=8, frameon=False)
|
| 108 |
+
|
| 109 |
+
fig.suptitle(f"OpenH-RF eSAF rotational 3D US — {args.input.name}", y=0.98)
|
| 110 |
+
plt.tight_layout()
|
| 111 |
+
args.output.parent.mkdir(parents=True, exist_ok=True)
|
| 112 |
+
plt.savefig(str(args.output), bbox_inches="tight", dpi=150)
|
| 113 |
+
|
| 114 |
+
print(f"Reconstructed : {recon.shape}")
|
| 115 |
+
print(f"Saved : {args.output}")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|