-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataLoader.py
More file actions
43 lines (31 loc) · 1.02 KB
/
Copy pathdataLoader.py
File metadata and controls
43 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import nibabel as nib
def niiLoader(nii_path):
img = nib.load(str(nii_path))
entry = {
"data": img.get_fdata(),
"affine": img.affine
}
return entry
def dataLoader(root_dir, pattern="*preproc-quasiraw_T1w.nii.gz", maxImg=float('inf')):
results = []
anat_dirs = root_dir.glob("sub-*/ses-*/anat")
count = 0
for anat_dir in anat_dirs:
nii_files = list(anat_dir.glob(pattern))
if not nii_files:
continue
for nii_path in nii_files:
if count >= maxImg:
return results
parts = nii_path.name.split("_")
sub_id = next((p for p in parts if p.startswith("sub-")), None)
ses_id = next((p for p in parts if p.startswith("ses-")), None)
entry = {
"subject": sub_id,
"session": ses_id,
"filename": nii_path.name,
"path": str(nii_path),
}
results.append(entry)
count += 1
return results