Skip to content

Commit 207c30e

Browse files
added small changes
1 parent a6613f4 commit 207c30e

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

micaflow/scripts/compute_fa_md.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,28 @@ def compute_fa_md(bias_corr_path, mask_path, moving_bval, moving_bvec, fa_path,
472472
print(f" New shape: {dwi_data.shape}")
473473
print(f" Total volumes: {len(bvals)}")
474474

475+
# Filter out invalid diffusion volumes (b > 50 but bvec is 0,0,0)
476+
# bvecs is shaped (3, N) at this point
477+
bvec_norms = np.linalg.norm(bvecs, axis=0)
478+
# Identify indices that are in a shell (b>50) but have no direction
479+
bad_indices = (bvals > 50) & (bvec_norms < 1e-6)
480+
481+
if np.any(bad_indices):
482+
n_removed = np.sum(bad_indices)
483+
print(f"{YELLOW}Warning: Found {n_removed} volumes in diffusion shell (b>50) with zero b-vectors (0,0,0).{RESET}")
484+
print(f"{YELLOW} Excluding invalid volumes to prevent fitting errors...{RESET}")
485+
486+
# Create mask of volumes to keep
487+
keep_mask = ~bad_indices
488+
489+
# Apply filtering
490+
dwi_data = dwi_data[..., keep_mask]
491+
bvals = bvals[keep_mask]
492+
bvecs = bvecs[:, keep_mask]
493+
494+
print(f" New DWI shape after filtering: {dwi_data.shape}")
495+
print(f" New B-values count: {len(bvals)}")
496+
475497
# Count b0 volumes
476498
b0_count = np.sum(bvals <= 50)
477499
dwi_count = len(bvals) - b0_count

micaflow/scripts/synth_b0.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -656,26 +656,6 @@ def main():
656656
os.makedirs(temp_dir, exist_ok=True)
657657
print(f"\n{CYAN}Temporary directory:{RESET} {temp_dir}")
658658

659-
# --- FIX START: Handle 4D input dimensions for SynB0-DISCO compatibility ---
660-
# Ensure inputs are strictly 3D. If 4D with N=1 (e.g. 240x240x220x1),
661-
# squeeze them to prevent NumPy "inhomogeneous shape" errors during inference.
662-
663-
t1_img_check = nib.load(t1_path)
664-
if len(t1_img_check.shape) > 3 and t1_img_check.shape[3] == 1:
665-
print(f"{YELLOW}Warning: T1w input is 4D {t1_img_check.shape}. Squeezing to 3D for inference compatibility...{RESET}")
666-
t1_3d_path = os.path.join(temp_dir, "t1_3d_input.nii.gz")
667-
# Squeeze data and save to temp file
668-
nib.save(nib.Nifti1Image(t1_img_check.get_fdata().squeeze(), t1_img_check.affine), t1_3d_path)
669-
t1_path = t1_3d_path
670-
671-
b0_img_check = nib.load(b0_path)
672-
if len(b0_img_check.shape) > 3 and b0_img_check.shape[3] == 1:
673-
print(f"{YELLOW}Warning: B0 input is 4D {b0_img_check.shape}. Squeezing to 3D for inference compatibility...{RESET}")
674-
b0_3d_path = os.path.join(temp_dir, "b0_3d_input.nii.gz")
675-
nib.save(nib.Nifti1Image(b0_img_check.get_fdata().squeeze(), b0_img_check.affine), b0_3d_path)
676-
b0_path = b0_3d_path
677-
# --- FIX END ---
678-
679659
# Find models
680660
models_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'models')
681661
print(f"{CYAN}Models directory:{RESET} {models_dir}")

0 commit comments

Comments
 (0)