Skip to content

Commit ade52fd

Browse files
committed
Added read_bval_file and read_bvec_file to read the bvec and the bval data
1 parent 5ad4cbc commit ade52fd

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

WrapImage/nifti_wrapper.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ def read_json_file(json_file):
2929

3030
return json_data
3131

32+
def read_bval_file(bval_file):
33+
"""
34+
For reading the bval file
35+
"""
36+
if not os.path.exists(bval_file):
37+
raise FileNotFoundError(f"File '{bval_file}' not found.")
38+
39+
with open(bval_file, "r") as f:
40+
try:
41+
bval_data = [int(val) for val in f.read().split()]
42+
except ValueError as e:
43+
raise ValueError(f"Error reading bval file '{bval_file}': {e}")
44+
45+
return bval_data
46+
47+
def read_bvec_file(bvec_file):
48+
"""
49+
For reading the bvec file
50+
"""
51+
if not os.path.exists(bvec_file):
52+
raise FileNotFoundError(f"File '{bvec_file}' not found.")
53+
54+
with open(bvec_file, "r") as f:
55+
try:
56+
lines = f.readlines()
57+
bvec_data = []
58+
for line in lines:
59+
bvec_data.append([float(val) for val in line.split()])
60+
except ValueError as e:
61+
raise ValueError(f"Error reading bvec file '{bvec_file}': {e}")
62+
63+
return bvec_data
64+
3265
def save_nifti_file(data, output_file, affine=None, **kwargs):
3366
"""
3467
For saving the 3d nifti images of the output of the algorithm
@@ -58,9 +91,10 @@ def loop_over_first_n_minus_1_dimensions(arr):
5891
if __name__ == "__main__":
5992
parser = argparse.ArgumentParser(description="Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files.")
6093
parser.add_argument("input_file", type=str, help="Path to the input 4D NIfTI file.")
61-
parser.add_argument("bids_dir", type=str, help="Path to the BIDS directory.")
94+
parser.add_argument("bvec_file", type=str, help="Path to the b-vector file.")
95+
parser.add_argument("bval_file", type=str, help="Path to the b-value file.")
6296
parser.add_argument("--affine", type=float, nargs="+", help="Affine matrix for NIfTI image.")
63-
parser.add_argument("--algorithm", type=str, choices=["algorithm1", "algorithm2"], default="algorithm1", help="Select the algorithm to use.")
97+
parser.add_argument("--algorithm", type=str, choices=["algorithm1", "algorithm2"], default="OJ_GU_seg", help="Select the algorithm to use.")
6498
parser.add_argument("algorithm_args", nargs=argparse.REMAINDER, help="Additional arguments for the algorithm.")
6599

66100
args = parser.parse_args()
@@ -69,15 +103,9 @@ def loop_over_first_n_minus_1_dimensions(arr):
69103
# Read the 4D NIfTI file
70104
data, _ = read_nifti_file(args.input_file)
71105

72-
# Construct the full paths for the JSON, b-vector, and b-value files
73-
json_file = os.path.join(args.bids_dir, "dataset_description.json")
74-
bvec_file = os.path.join(args.bids_dir, "bvecs.json")
75-
bval_file = os.path.join(args.bids_dir, "bvals.json")
76-
77-
# Read the JSON, b-vector, and b-value files
78-
json_data = read_json_file(json_file)
79-
bvecs = read_json_file(bvec_file)
80-
bvals = read_json_file(bval_file)
106+
# Read the b-vector, and b-value files
107+
bvecs = read_bvec_file(args.bvec_file)
108+
bvals = read_bval_file(args.bval_file)
81109

82110
# Pass additional arguments to the algorithm
83111
fit = OsipiBase(algorithm=args.algorithm)

0 commit comments

Comments
 (0)