@@ -38,6 +38,23 @@ def save_nifti_file(data, output_file, affine=None, **kwargs):
38
38
output_img = nib .nifti1 .Nifti1Image (data , affine , ** kwargs )
39
39
nib .save (output_img , output_file )
40
40
41
+ def loop_over_first_n_minus_1_dimensions (arr ):
42
+ """
43
+ Loops over the first n-1 dimensions of a numpy array.
44
+
45
+ Args:
46
+ arr: A numpy array.
47
+
48
+ Yields:
49
+ A tuple containing the indices for the current iteration and a flattened view of the remaining dimensions.
50
+ """
51
+ n = arr .ndim
52
+ for idx in np .ndindex (* arr .shape [:n - 1 ]):
53
+ flat_view = arr [idx ].flatten ()
54
+ yield idx , flat_view
55
+
56
+
57
+
41
58
if __name__ == "__main__" :
42
59
parser = argparse .ArgumentParser (description = "Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files." )
43
60
parser .add_argument ("input_file" , type = str , help = "Path to the input 4D NIfTI file." )
@@ -64,13 +81,17 @@ def save_nifti_file(data, output_file, affine=None, **kwargs):
64
81
65
82
# Pass additional arguments to the algorithm
66
83
fit = OsipiBase (algorithm = args .algorithm )
67
- # signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR, rician_noise)
68
-
69
- # Passing the values to the selectect algorithm and saving it
70
- [f_fit , Dp_fit , D_fit ] = fit .osipi_fit (signal , bvals )
71
- save_nifti_file (f_fit , "f.nii.gz" , args .affine )
72
- save_nifti_file (Dp_fit , "dp.nii.gz" , args .affline )
73
- save_nifti_file (D_fit , "d.nii.gz" , args .affline )
84
+ f_image = np .zeros_like (data .shape [:data .ndim - 1 ])
85
+ D_image = np .zeros_like (data .shape [:data .ndim - 1 ])
86
+ Dp_image = np .zeros_like (data .shape [:data .ndim - 1 ])
87
+ for idx , view in loop_over_first_n_minus_1_dimensions (data ):
88
+ [f_fit , Dp_fit , D_fit ] = fit .osipi_fit (view , bvals )
89
+ f_image [idx ]= f_fit
90
+ Dp_image [idx ]= Dp_fit
91
+ D_image [idx ]= D_fit
92
+ save_nifti_file (f_image , "f.nii.gz" , args .affine )
93
+ save_nifti_file (Dp_image , "dp.nii.gz" , args .affline )
94
+ save_nifti_file (D_image , "d.nii.gz" , args .affline )
74
95
75
96
except Exception as e :
76
97
print (f"Error: { e } " )
0 commit comments