1+ import os
2+ import numpy as np
3+ import nibabel as nib
4+
5+ # Adjust this import to match your local PyDesigner structure
6+ from pydesigner .fitting .dwipy import DWI
7+ from pydesigner .system .utils import writeNii
8+
9+ basePath = os .path .join ('/Volumes' ,'Flashy' ,'HIE_FBI_003' ,'FBWM_b4000' )
10+
11+ dwi_path = os .path .join (basePath , "dwi_preprocessed.nii" )
12+ bvec_path = os .path .join (basePath ,"dwi_preprocessed.bvec" )
13+ bval_path = os .path .join (basePath ,"dwi_preprocessed.bval" )
14+ mask_path = os .path .join (basePath ,"brain_mask.nii" )
15+
16+ out_dir = os .path .join (basePath , "debug_fodf_flips" )
17+ os .makedirs (out_dir , exist_ok = True )
18+
19+
20+ def apply_gradient_flip (dwi_obj , flips ):
21+ """
22+ flips = (fx, fy, fz), each either +1 or -1.
23+ """
24+ flips = np .asarray (flips , dtype = float )
25+ dwi_obj .grad [:, :3 ] = dwi_obj .grad [:, :3 ] * flips [None , :]
26+ return dwi_obj
27+
28+
29+ flip_tests = {
30+ "none" : ( 1 , 1 , 1 ),
31+ "xflip" : (- 1 , 1 , 1 ),
32+ "yflip" : ( 1 , - 1 , 1 ),
33+ "zflip" : ( 1 , 1 , - 1 ),
34+ "xyflip" : (- 1 , - 1 , 1 ),
35+ "xzflip" : (- 1 , 1 , - 1 ),
36+ "yzflip" : ( 1 , - 1 , - 1 ),
37+ "xyzflip" : (- 1 , - 1 , - 1 ),
38+ }
39+
40+
41+ for label , flips in flip_tests .items ():
42+ print (f"\n Running flip test: { label } { flips } " )
43+
44+ dwi = DWI (
45+ imPath = dwi_path ,
46+ bvecPath = bvec_path ,
47+ bvalPath = bval_path ,
48+ mask = mask_path ,
49+ nthreads = 1 ,
50+ )
51+
52+ dwi = apply_gradient_flip (dwi , flips )
53+
54+ # FBI/fODF only. Do not run full FBWM.
55+ zeta , faa , fodf , fodf_mrtrix , min_awf , Da , De_mean , De_ax , De_rad , De_fa , min_cost , min_cost_fn = dwi .fbi (
56+ l_max = 6 ,
57+ fbwm = False ,
58+ rectify = True ,
59+ res = "low" ,
60+ )
61+
62+ # Save the fODF SH coefficients or fODF output returned by dwi.fbi().
63+ # Depending on your downstream viewer, this may be the file to inspect.
64+ out_path = os .path .join (out_dir , f"fodf_{ label } .nii" )
65+ writeNii (np .asarray (fodf ).real , dwi .hdr , out_path )
66+
67+ writeNii (
68+ np .asarray (fodf_mrtrix ).real ,
69+ dwi .hdr ,
70+ os .path .join (out_dir , f"fodf_mrtrix_{ label } .nii" ),
71+ )
72+
73+ # # Save FAA too as a scalar QC map.
74+ # faa_path = os.path.join(out_dir, f"faa_{label}.nii.gz")
75+ # writeNii(np.asarray(faa), dwi.hdr, faa_path)
76+
77+ print (f"Saved: { out_path } " )
78+ # print(f"Saved: {faa_path}")
0 commit comments