This folder contains a workflow for single cell z-drift QC. It uses local z-stack - Register the z-stack to FOV, match each ROI in FOV to z-stack, and calculate intensity change across planar z-drift.
register_fov_local_zstack.py- Main pipeline for one plane path (no DataFrame input, no parallel processing)
register_fov_local_zstack_methods.py- Method-level registration math/utilities shared by the pipeline
register_fov_local_zstack_viz.py- Result loading and method-comparison visualization
register_fov_local_zstack_qc.py- QC metrics, ROI/neuropil overlays, and GIF generation
For a single plane_path:
- Finds local z-stack data:
local_zstack_reg = cdu.get_local_zstack_reg(plane_path)
- Registers local z-stack between planes:
reg_stack_imgs, shift_all = zstack.reg_between_planes(local_zstack_reg, ref_ind=0)
- Saves registered z-stack TIFF (intermediate result)
- Registers local z-stack to FOV mean image (translation/affine/nonrigid cascade)
- Saves flat result files:
*_results.h5*_metadata.json
- Generates QC outputs:
- Using the ROI table
registration_method_comparison.png- ROI overlay PNG
- final QC GIF
from pathlib import Path
import sys
if '/root/capsule/code' not in sys.path:
sys.path.append('/root/capsule/code')
import register_fov_local_zstack as reg_mod
import register_fov_local_zstack_qc as qc_mod
import register_fov_local_zstack_viz as viz_mod
plane_path = Path('/root/capsule/data/<processed_session>/<plane_id>')
out_dir = Path('/root/capsule/results')
qc_dir = out_dir / 'qc'
# End-to-end registration + save
run_out = reg_mod.run_single_plane(
plane_path=plane_path,
output_dir=out_dir,
reg_ref_ind=0,
save_tiff=True,
file_suffix='local_zstack_to_fov',
pad=3,
)
result = run_out['result']
saved_paths = run_out['saved_paths']
# Method comparison figure
viz_mod.save_method_comparison_figure(
result=result,
row_i=0,
out_path=qc_dir / 'registration_method_comparison.png',
)
# QC (ROI overlay + GIF)
qc_out = qc_mod.run_qc(result, qc_dir)
print(saved_paths)
print(qc_out['overlay_image_path'])
print(qc_out['gif_path'])- Not much of a gain for now (8 planes, 8 workers, with 16 cores)
- Serial processing takes about 8 minutes
from pathlib import Path
import register_fov_local_zstack_parallel as par_mod
plane_paths = [
Path("/root/capsule/data/session/plane_0"),
Path("/root/capsule/data/session/plane_1"),
# ... up to plane_7
]
results = par_mod.run_planes_parallel(
plane_paths,
output_dir=Path("/root/capsule/results"),
n_workers=8,
)
par_mod.print_parallel_results(results)prepare_local_zstack(plane_path, reg_ref_ind=0, save_tiff=True, tiff_save_dir=None)prepare_plane_data(plane_path, zstack_data)register_local_zstack_to_fov(plane_path, ..., pad=3)save_result(result, output_dir, file_suffix='local_zstack_to_fov')run_(plane_path, output_dir, ...)list_saved_result_files(output_dir)get_registration_benchmark_summary(result)
load_result_from_bundle(result_path, h5_path=None)save_method_comparison_figure(result, row_i, out_path)save_method_comparison_figure_from_result_file(result_file, out_path, ...)
get_valid_roi_table(result)make_roi_overlay_image(result, valid_roi_table, ...)save_roi_overlay_image(result, valid_roi_table, save_path)save_qc_gif(result, valid_roi_table, save_path, fps=3)run_qc(result, save_dir, gif_fps=3)
<session>_<plane>_<suffix>_results.h5<session>_<plane>_<suffix>_metadata.json
The H5 file includes:
registered_zstackmatched_plane_indicespadded_plane_indicescrop_y_inds,crop_x_indsfov_mean,fov_mean_cropped- transform groups per method
registration_method_comparison.png<session>_<plane>_registered_mean_zstack_roi_overlay.png<session>_<plane>_registered_zstack_with_roi_outlines.gif
Color convention in ROI overlay image:
- ROI contours: red
- Neuropil contours: yellow
A runnable test notebook is provided at:
test_notebook.ipynb
It covers:
- preprocessing
- registration
- benchmark summary
- save/load roundtrip
- visualization
- QC output generation
- Read-only filesystem error when saving TIFF
- Set
tiff_save_dirto a writable path (for example under/root/capsule/scratch/...).
- Set
- Missing local z-stack file
- Ensure
plane_pathcontains*_z_stack_local_reg.h5.
- Ensure
- No result files found
- Check
output_dirand filename suffix.
- Check
/root/capsule/scratch/_run/
results/
zstack_tiff/
*_results.h5
*_metadata.json
qc/
registration_method_comparison.png
*_registered_mean_zstack_roi_overlay.png
*_registered_zstack_with_roi_outlines.gif