Skip to content

Commit bb7c4bf

Browse files
committed
add progress bar to add-nifti-info
1 parent 35d8750 commit bb7c4bf

File tree

1 file changed

+66
-59
lines changed

1 file changed

+66
-59
lines changed

cubids/cubids.py

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,63 +31,6 @@
3131
from cubids.metadata_merge import check_merging_operations, group_by_acquisition_sets
3232

3333

34-
def _extract_metadata_single_nifti(nifti_path):
35-
"""Extract metadata from a single NIfTI and write to its sidecar JSON.
36-
37-
Parameters
38-
----------
39-
nifti_path : :obj:`str`
40-
Path to a NIfTI file.
41-
"""
42-
try:
43-
img = nb.load(str(nifti_path))
44-
except Exception:
45-
print("Empty Nifti File: ", str(nifti_path))
46-
return
47-
48-
# get important info from niftis
49-
obliquity = np.any(nb.affines.obliquity(img.affine) > 1e-4)
50-
voxel_sizes = img.header.get_zooms()
51-
matrix_dims = img.shape
52-
# add nifti info to corresponding sidecars
53-
sidecar = utils.img_to_new_ext(str(nifti_path), ".json")
54-
if Path(sidecar).exists():
55-
try:
56-
with open(sidecar) as f:
57-
data = json.load(f)
58-
except Exception:
59-
print("Error parsing this sidecar: ", sidecar)
60-
return
61-
62-
if "Obliquity" not in data.keys():
63-
data["Obliquity"] = str(obliquity)
64-
if "VoxelSizeDim1" not in data.keys():
65-
data["VoxelSizeDim1"] = float(voxel_sizes[0])
66-
if "VoxelSizeDim2" not in data.keys():
67-
data["VoxelSizeDim2"] = float(voxel_sizes[1])
68-
if "VoxelSizeDim3" not in data.keys():
69-
data["VoxelSizeDim3"] = float(voxel_sizes[2])
70-
if "Dim1Size" not in data.keys():
71-
data["Dim1Size"] = matrix_dims[0]
72-
if "Dim2Size" not in data.keys():
73-
data["Dim2Size"] = matrix_dims[1]
74-
if "Dim3Size" not in data.keys():
75-
data["Dim3Size"] = matrix_dims[2]
76-
if "NumVolumes" not in data.keys():
77-
if img.ndim == 4:
78-
data["NumVolumes"] = matrix_dims[3]
79-
elif img.ndim == 3:
80-
data["NumVolumes"] = 1
81-
if "ImageOrientation" not in data.keys():
82-
orient = nb.orientations.aff2axcodes(img.affine)
83-
orient = [str(orientation) for orientation in orient]
84-
joined = "".join(orient) + "+"
85-
data["ImageOrientation"] = joined
86-
87-
with open(sidecar, "w") as file:
88-
json.dump(data, file, indent=4)
89-
90-
9134
warnings.simplefilter(action="ignore", category=FutureWarning)
9235
bids.config.set_option("extension_initial_dot", True)
9336

@@ -417,9 +360,16 @@ def add_nifti_info(self, n_cpus=1):
417360

418361
if n_cpus > 1 and len(nifti_paths) > 0:
419362
with ProcessPoolExecutor(n_cpus) as executor:
420-
list(executor.map(_extract_metadata_single_nifti, nifti_paths))
363+
list(
364+
tqdm(
365+
executor.map(_extract_metadata_single_nifti, nifti_paths),
366+
total=len(nifti_paths),
367+
desc="Processing NIfTI files",
368+
unit="file",
369+
)
370+
)
421371
else:
422-
for nifti_path in nifti_paths:
372+
for nifti_path in tqdm(nifti_paths, desc="Processing NIfTI files", unit="file"):
423373
_extract_metadata_single_nifti(nifti_path)
424374

425375
if self.use_datalad:
@@ -1607,3 +1557,60 @@ def get_fieldmap_lookup(self):
16071557
def get_layout(self):
16081558
"""Get layout."""
16091559
return self.layout
1560+
1561+
1562+
def _extract_metadata_single_nifti(nifti_path):
1563+
"""Extract metadata from a single NIfTI and write to its sidecar JSON.
1564+
1565+
Parameters
1566+
----------
1567+
nifti_path : :obj:`str`
1568+
Path to a NIfTI file.
1569+
"""
1570+
try:
1571+
img = nb.load(str(nifti_path))
1572+
except Exception:
1573+
print("Empty Nifti File: ", str(nifti_path))
1574+
return
1575+
1576+
# get important info from niftis
1577+
obliquity = np.any(nb.affines.obliquity(img.affine) > 1e-4)
1578+
voxel_sizes = img.header.get_zooms()
1579+
matrix_dims = img.shape
1580+
# add nifti info to corresponding sidecars
1581+
sidecar = utils.img_to_new_ext(str(nifti_path), ".json")
1582+
if Path(sidecar).exists():
1583+
try:
1584+
with open(sidecar) as f:
1585+
data = json.load(f)
1586+
except Exception:
1587+
print("Error parsing this sidecar: ", sidecar)
1588+
return
1589+
1590+
if "Obliquity" not in data.keys():
1591+
data["Obliquity"] = str(obliquity)
1592+
if "VoxelSizeDim1" not in data.keys():
1593+
data["VoxelSizeDim1"] = float(voxel_sizes[0])
1594+
if "VoxelSizeDim2" not in data.keys():
1595+
data["VoxelSizeDim2"] = float(voxel_sizes[1])
1596+
if "VoxelSizeDim3" not in data.keys():
1597+
data["VoxelSizeDim3"] = float(voxel_sizes[2])
1598+
if "Dim1Size" not in data.keys():
1599+
data["Dim1Size"] = matrix_dims[0]
1600+
if "Dim2Size" not in data.keys():
1601+
data["Dim2Size"] = matrix_dims[1]
1602+
if "Dim3Size" not in data.keys():
1603+
data["Dim3Size"] = matrix_dims[2]
1604+
if "NumVolumes" not in data.keys():
1605+
if img.ndim == 4:
1606+
data["NumVolumes"] = matrix_dims[3]
1607+
elif img.ndim == 3:
1608+
data["NumVolumes"] = 1
1609+
if "ImageOrientation" not in data.keys():
1610+
orient = nb.orientations.aff2axcodes(img.affine)
1611+
orient = [str(orientation) for orientation in orient]
1612+
joined = "".join(orient) + "+"
1613+
data["ImageOrientation"] = joined
1614+
1615+
with open(sidecar, "w") as file:
1616+
json.dump(data, file, indent=4)

0 commit comments

Comments
 (0)