Skip to content

Commit 5ad4cbc

Browse files
committed
Update the pull request
1 parent 6db42c1 commit 5ad4cbc

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

WrapImage/nifti_wrapper.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ def save_nifti_file(data, output_file, affine=None, **kwargs):
3838
output_img = nib.nifti1.Nifti1Image(data, affine , **kwargs)
3939
nib.save(output_img, output_file)
4040

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+
4158
if __name__ == "__main__":
4259
parser = argparse.ArgumentParser(description="Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files.")
4360
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):
6481

6582
# Pass additional arguments to the algorithm
6683
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)
7495

7596
except Exception as e:
7697
print(f"Error: {e}")

0 commit comments

Comments
 (0)