Download scripts/view_ct.py from ppak10/ORNL-LPBF-Cylinders: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/datasets/ppak10/ORNL-LPBF-Cylinders/resolve/main/scripts/view_ct.py
- Command line
-
hf download hf://datasets/ppak10/ORNL-LPBF-Cylinders/scripts/view_ct.py
-
curl -L -o view_ct.py https://huggingface.co/datasets/ppak10/ORNL-LPBF-Cylinders/resolve/main/scripts/view_ct.py
2.49 kB
| #!/usr/bin/env python3 | |
| """ | |
| Napari viewer for CT data from ORNL LPBF Cylinders dataset. | |
| Usage: | |
| uv run scripts/view_ct.py [--source PATH] | |
| Loads the X-ray CT data and flaw segmentation lazily using dask, | |
| then opens an interactive napari viewer. | |
| """ | |
| import argparse | |
| from pathlib import Path | |
| import dask.array as da | |
| import h5py | |
| import napari | |
| def load_ct_lazy(hdf5_path: Path) -> tuple[da.Array, da.Array, h5py.File]: | |
| """Load CT datasets lazily using dask. | |
| Returns dask arrays that only load data on demand, plus the open file handle. | |
| """ | |
| f = h5py.File(hdf5_path, "r") | |
| ct_data = f["slices/registered_data/x-ray_ct_data"] | |
| ct_flaw = f["slices/registered_data/x-ray_ct_flaw"] | |
| # Wrap as dask arrays for lazy loading | |
| # Use chunks aligned with the data for efficient access | |
| ct_data_dask = da.from_array(ct_data, chunks=(1, 1024, 1024)) | |
| ct_flaw_dask = da.from_array(ct_flaw, chunks=(1, 1024, 1024)) | |
| return ct_data_dask, ct_flaw_dask, f | |
| def main(): | |
| parser = argparse.ArgumentParser(description="View CT data with napari") | |
| parser.add_argument( | |
| "--source", | |
| type=Path, | |
| default=Path("source/2024-05-01 M2 AMMTO Fatigue Blanks 05.hdf5"), | |
| help="Path to HDF5 file", | |
| ) | |
| args = parser.parse_args() | |
| if not args.source.exists(): | |
| print(f"Error: {args.source} not found") | |
| return 1 | |
| print(f"Loading CT data from {args.source}...") | |
| print(" (Data loads lazily - initial startup is fast)") | |
| ct_data, ct_flaw, file_handle = load_ct_lazy(args.source) | |
| print(f" CT data shape: {ct_data.shape}") | |
| print(f" CT flaw shape: {ct_flaw.shape}") | |
| print() | |
| print("Opening napari viewer...") | |
| print(" - Scroll mouse wheel to move through layers") | |
| print(" - Use layer controls to toggle CT/flaw visibility") | |
| print(" - Adjust contrast with the layer controls") | |
| viewer = napari.Viewer(title="ORNL LPBF CT Viewer") | |
| # Add raw CT data | |
| viewer.add_image( | |
| ct_data, | |
| name="X-ray CT", | |
| colormap="gray", | |
| contrast_limits=[0, 65535], | |
| blending="translucent", | |
| ) | |
| # Add flaw segmentation as labels | |
| viewer.add_labels( | |
| ct_flaw, | |
| name="Flaw Segmentation", | |
| opacity=0.5, | |
| ) | |
| # Set initial view to middle slice | |
| viewer.dims.current_step = (ct_data.shape[0] // 2, 0, 0) | |
| napari.run() | |
| # Clean up | |
| file_handle.close() | |
| return 0 | |
| if __name__ == "__main__": | |
| exit(main()) | |