Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A python script to wrap image read and write for Nifti Images. #60

Merged
merged 8 commits into from
Apr 30, 2024
24 changes: 20 additions & 4 deletions WrapImage/nifti_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import json
import os
import nibabel as nib
from src.wrappers.OsipiBase import OsipiBase
from utilities.data_simulation.GenerateData import GenerateData
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
import numpy as np
import random

def read_nifti_4d(input_file):
def read_nifti_file(input_file):
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
"""
For reading the 4d nifti image
"""
Expand All @@ -27,11 +30,11 @@ def read_json_file(json_file):

return json_data

def save_nifti_3d(data, output_file):
def save_nifti_3d(data, output_file, **kwargs):
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
"""
For saving the 3d nifti images of the output of the algorithm
"""
output_img = nib.Nifti1Image(data, np.eye(4))
output_img = nib.nifti1.Nifti1Image(data, np.eye(4), **kwargs)
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
nib.save(output_img, output_file)

if __name__ == "__main__":
Expand All @@ -45,7 +48,7 @@ def save_nifti_3d(data, output_file):

try:
# Read the 4D NIfTI file
data = read_nifti_4d(args.input_file)
data = read_nifti_file(args.input_file)

# Construct the full paths for the JSON, b-vector, and b-value files
json_file = os.path.join(args.bids_dir, "dataset_description.json")
Expand All @@ -58,7 +61,20 @@ def save_nifti_3d(data, output_file):
bvals = read_json_file(bval_file)

# Pass additional arguments to the algorithm
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
rng = np.random.RandomState(42)
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
fit = OsipiBase(algorithm=args.algorithm)
S0 = 1
gd = GenerateData(rng=rng)
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
D = data["D"]
f = data["f"]
Dp = data["Dp"]
# signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR, rician_noise)

# Passing the values to the selectect algorithm and saving it
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(signal, bvals)
Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
save_nifti_3d(f_fit, "f.nii.gz")
save_nifti_3d(Dp_fit, "dp.nii.gz")
save_nifti_3d(D_fit, "d.nii.gz")

Unique-Usman marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
print(f"Error: {e}")