|
a |
|
b/code/update_json.py |
|
|
1 |
import os |
|
|
2 |
import json |
|
|
3 |
import argparse |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
def get_parser(): |
|
|
7 |
parser = argparse.ArgumentParser( |
|
|
8 |
description="Adds spatial reference information to json sidecars." ) |
|
|
9 |
parser.add_argument('-d', |
|
|
10 |
required=True, help='Directory to process JSON files in') |
|
|
11 |
parser.add_argument('-r', |
|
|
12 |
default='0.8x0.8x0.8', |
|
|
13 |
help='Resampling factor to add to SpatialReference') |
|
|
14 |
|
|
|
15 |
return parser |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
def create_json_file(nii_gz_files, resampling_factor): |
|
|
19 |
"""Creates a JSON file listing all .nii.gz files and includes the resampling factor. |
|
|
20 |
|
|
|
21 |
Args: |
|
|
22 |
nii_gz_files (list): A list of paths to .nii.gz files. |
|
|
23 |
""" |
|
|
24 |
data = { |
|
|
25 |
#"SpatialReference": { |
|
|
26 |
# "Orientation": "RPI", |
|
|
27 |
# "Resampling": resampling_factor, |
|
|
28 |
# }, |
|
|
29 |
"GeneratedBy":[ |
|
|
30 |
{ |
|
|
31 |
"Name": "sct_dmri_moco", |
|
|
32 |
"Version": "SCT v6.1", |
|
|
33 |
#"Author": "Paul Bautin", |
|
|
34 |
#"Date": "2020-07-30 11:57:54", |
|
|
35 |
"Description": "Mean image across directions after motion correction" |
|
|
36 |
}] |
|
|
37 |
} |
|
|
38 |
for file in nii_gz_files: |
|
|
39 |
output_path = file.replace('.nii.gz', '.json') |
|
|
40 |
with open(output_path, 'w', encoding='utf-8') as file: |
|
|
41 |
json.dump(data, file, indent=4) |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
def add_spatial_reference(file_path, resampling_factor): |
|
|
45 |
"""Adds the SpatialReference field to a JSON file. |
|
|
46 |
|
|
|
47 |
Args: |
|
|
48 |
file_path (str): The path to the JSON file. |
|
|
49 |
""" |
|
|
50 |
with open(file_path, 'r+', encoding='utf-8') as file: |
|
|
51 |
# Load the existing data |
|
|
52 |
data = json.load(file) |
|
|
53 |
|
|
|
54 |
# Add the "SpatialReference" field |
|
|
55 |
data["SpatialReference"] = { |
|
|
56 |
"Resampling": resampling_factor, |
|
|
57 |
"Reorientation": "RPI", |
|
|
58 |
"Other": "root-mean square across 4th dimension (if it exists)" |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
# Reset file pointer to the beginning and truncate the file |
|
|
62 |
file.seek(0) |
|
|
63 |
file.truncate() |
|
|
64 |
|
|
|
65 |
# Write the modified data back to the file |
|
|
66 |
json.dump(data, file, indent=4) |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
def find_nii_gz_files(directory): |
|
|
70 |
"""Finds all .nii.gz files in a directory and its subdirectories. |
|
|
71 |
|
|
|
72 |
Args: |
|
|
73 |
directory (str): The root directory to start the search from. |
|
|
74 |
|
|
|
75 |
Returns: |
|
|
76 |
list: A list of paths to the .nii.gz files found. |
|
|
77 |
""" |
|
|
78 |
nii_gz_files = [] |
|
|
79 |
for root, _, files in os.walk(directory): |
|
|
80 |
for file in files: |
|
|
81 |
if ('rec-average_dwi.nii.gz' in file): |
|
|
82 |
nii_gz_files.append(os.path.join(root, file)) |
|
|
83 |
|
|
|
84 |
return nii_gz_files |
|
|
85 |
|
|
|
86 |
|
|
|
87 |
def process_directory(directory, resampling_factor): |
|
|
88 |
"""Recursively processes directories to find and update JSON files. |
|
|
89 |
|
|
|
90 |
Args: |
|
|
91 |
directory (str): The root directory to start the search from. |
|
|
92 |
""" |
|
|
93 |
for root, _, files in os.walk(directory): |
|
|
94 |
for file in files: |
|
|
95 |
if ('T2w' in file) and ('.json' in file): |
|
|
96 |
file_path = os.path.join(root, file) |
|
|
97 |
add_spatial_reference(file_path, resampling_factor) |
|
|
98 |
print(f"Updated {file_path}") |
|
|
99 |
|
|
|
100 |
def main(): |
|
|
101 |
parser = get_parser() |
|
|
102 |
args = parser.parse_args() |
|
|
103 |
#process_directory(args.d, args.r) |
|
|
104 |
nii_gz_files = find_nii_gz_files(args.d) |
|
|
105 |
create_json_file(nii_gz_files, args.r) |
|
|
106 |
|
|
|
107 |
if __name__ == "__main__": |
|
|
108 |
main() |