Skip to content

Commit 8847a11

Browse files
committed
turn off pet2bids when required
1 parent 7c4eab6 commit 8847a11

File tree

4 files changed

+79
-37
lines changed

4 files changed

+79
-37
lines changed

docker-compose-nginx.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ services:
5353
environment:
5454
MONGO_CONNECTION_STRING: mongodb://mongodb:27017/ezbids
5555
PRESORT: ${PRESORT:-false}
56+
PET2BIDS_ENABLED: ${PET2BIDS_ENABLED:-true}
5657
networks:
5758
- ezbids
5859
tty: true #turn on color for bids-validator output

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ services:
6262
environment:
6363
MONGO_CONNECTION_STRING: mongodb://mongodb:27017/ezbids
6464
PRESORT: ${PRESORT:-false}
65+
PET2BIDS_ENABLED: ${PET2BIDS_ENABLED:-true}
6566
networks:
6667
- ezbids
6768
tty: true #turn on color for bids-validator output

example.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# insert your host name here, it should match your ssl certificate and/or the output
44
# of echo $HOSTNAME
55
SERVER_NAME=localhost
6-
6+
PET2BIDS_ENABLED=false
77

88
# Set the BRAINLIFE_USE_NGINX environment variable to true to use https"
99
# (this will launch the services on port 443) and run with nginx/production_nginx.conf"

handler/find_img_data.py

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
# presort can slow down ezBIDS as it examines every dicom, it's enabled/disabled
1010
# by setting the PRESORT environment variable to "true" or ""
11-
presort_enabled = bool(os.getenv('PRESORT', 'false').lower() == 'true')
12-
presort_enabled_pet = bool(os.getenv('PRESORT_PET', 'false').lower() == 'true')
11+
presort_enabled = bool(os.getenv("PRESORT", "false").lower() == "true")
12+
presort_enabled_pet = bool(os.getenv("PRESORT_PET", "false").lower() == "true")
13+
pet2bids_enabled = bool(os.getenv("PET2BIDS_ENABLED", "true").lower() == "true")
1314

1415

1516
# if pet2bids is installed we use it wherever the PET data live
@@ -19,37 +20,40 @@
1920
from pypet2bids import is_pet
2021
except (ImportError, ModuleNotFoundError):
2122
pet2bidsInstalled = False
22-
print('pet2bids is not installed, using dcm2niix on PET directories instead')
23+
print("pet2bids is not installed, using dcm2niix on PET directories instead")
2324
# Don't exit, just continue without PET support
2425
is_pet = None
2526

27+
2628
def find_img_data(dir):
27-
'''
29+
"""
2830
Finds all directories that contain DICOM (or other) raw imaging data.
2931
If dcm2niix output (NIfTI, JSON files) uploaded instead, ezBIDS has separate process for detecting those files.
3032
3133
Parameters
3234
----------
3335
dir : string
3436
root-level directory of uploaded data
35-
'''
37+
"""
3638
global mri_dcm_dirs_list
3739
hasImgData = False
3840

3941
# Define common DICOM file extensions (lowercase)
40-
dicom_extensions = ('.dcm', '.ima', '.img', '')
42+
dicom_extensions = (".dcm", ".ima", ".img", "")
4143

42-
with open('find_img_data.log', 'w') as log:
44+
with open("find_img_data.log", "w") as log:
4345
# MRI (raw only)
4446
for root, dirs, files in os.walk(dir):
4547
for f in sorted(files):
4648
# Case-insensitive extension check
4749
if any(f.lower().endswith(ext) for ext in dicom_extensions):
4850
try:
49-
log.write(f"Trying to read DICOM file: {os.path.join(root, f)}\n")
51+
log.write(
52+
f"Trying to read DICOM file: {os.path.join(root, f)}\n"
53+
)
5054
read_file = dcmread(os.path.join(root, f))
5155
log.write(f"DICOM Modality: {read_file.Modality}\n")
52-
if read_file.Modality == 'MR':
56+
if read_file.Modality == "MR":
5357
if root not in mri_dcm_dirs_list:
5458
log.write(f"Found MRI directory: {root}\n")
5559
mri_dcm_dirs_list.append(root)
@@ -66,6 +70,7 @@ def find_img_data(dir):
6670
if os.path.isdir(full_path):
6771
find_img_data(full_path)
6872

73+
6974
# change to input directory
7075
root = sys.argv[1]
7176
os.chdir(root)
@@ -78,80 +83,115 @@ def find_img_data(dir):
7883
root_full_path = str(Path(root).absolute())
7984

8085
# Actually call find_img_data with the root directory
81-
find_img_data('.')
86+
find_img_data(".")
8287

8388
# PET
84-
pet_folders = [str(folder) for folder in is_pet.pet_folder(Path(root).resolve(), skim=True, njobs=4)]
85-
pet_folders = [os.path.relpath(x, root) for x in pet_folders if x != '']
86-
pet_folders = [os.path.join('.', x) for x in pet_folders]
89+
pet_folders = [
90+
str(folder)
91+
for folder in is_pet.pet_folder(Path(root).resolve(), skim=True, njobs=4)
92+
]
93+
pet_folders = [os.path.relpath(x, root) for x in pet_folders if x != ""]
94+
pet_folders = [os.path.join(".", x) for x in pet_folders]
8795

8896
if pet_folders:
8997
for pet in pet_folders:
9098
# See if we're dealing ECAT-formatted file(s)
91-
ecats = [x for x in os.listdir(pet) if x.endswith(tuple(['.v', '.v.gz']))]
99+
ecats = [x for x in os.listdir(pet) if x.endswith(tuple([".v", ".v.gz"]))]
92100
if len(ecats):
93101
for ecat in ecats:
94102
if ecat not in pet_ecat_files_list:
95-
pet_ecat_files_list.append(f'{pet}/{ecat}')
103+
pet_ecat_files_list.append(f"{pet}/{ecat}")
96104
# See if we're dealing with DICOM files
97105
dcms = [
98-
x for x in os.listdir(pet)
99-
if not x.endswith(tuple(['.nii', '.nii.gz', '.v', '.v.gz', '.json', '.tsv']))
106+
x
107+
for x in os.listdir(pet)
108+
if not x.endswith(
109+
tuple([".nii", ".nii.gz", ".v", ".v.gz", ".json", ".tsv"])
110+
)
100111
]
101112
if len(dcms) and pet not in pet_dcm_dirs_list:
102113
pet_dcm_dirs_list.append(pet)
103114

104115
# MEG
105-
MEG_extensions = ['*.ds', '*.fif', '*.sqd', '*.con', '*.raw', '*.ave', '*.mrk', '*.kdf', '*.mhd', '*.trg', '*.chn', '*.dat']
116+
MEG_extensions = [
117+
"*.ds",
118+
"*.fif",
119+
"*.sqd",
120+
"*.con",
121+
"*.raw",
122+
"*.ave",
123+
"*.mrk",
124+
"*.kdf",
125+
"*.mhd",
126+
"*.trg",
127+
"*.chn",
128+
"*.dat",
129+
]
106130
for meg_ext in MEG_extensions:
107-
if meg_ext == '*.ds':
108-
type_search = 'd'
131+
if meg_ext == "*.ds":
132+
type_search = "d"
109133
else:
110-
type_search = 'f'
134+
type_search = "f"
111135

112-
find_cmd = os.popen(f"find . -maxdepth 9 -type {type_search} -name '{meg_ext}'").read()
113-
if find_cmd != '':
136+
find_cmd = os.popen(
137+
f"find . -maxdepth 9 -type {type_search} -name '{meg_ext}'"
138+
).read()
139+
if find_cmd != "":
114140
meg_data_list.append(find_cmd)
115141

116142
if len(meg_data_list):
117143
# TODO - won't this remove different extensions?
118-
meg_data_list = [x for x in meg_data_list[0].split('\n') if x != '' and 'hz.ds' not in x]
144+
meg_data_list = [
145+
x for x in meg_data_list[0].split("\n") if x != "" and "hz.ds" not in x
146+
]
119147

120148
# Save the MRI, PET, MEG, and NIfTI lists (if they exist) to separate files
121-
file = open(f'{root}/dcm2niix.list', 'w')
149+
file = open(f"{root}/dcm2niix.list", "w")
122150
if len(mri_dcm_dirs_list):
123151
# Sort the list before writing
124152
sorted_mri_dirs = sorted(mri_dcm_dirs_list)
125153
for dcm in sorted_mri_dirs:
126154
if presort_enabled:
127155
presorted_dicoms = sorted(presort(dcm)) # Sort presorted results too
128156
for pre in presorted_dicoms:
129-
file.write(str(pre) + '\n')
157+
file.write(str(pre) + "\n")
158+
else:
159+
file.write(dcm + "\n")
160+
161+
# Add PET directories to dcm2niix.list when pet2bids is disabled
162+
if len(pet_dcm_dirs_list) and not pet2bids_enabled:
163+
# Sort the list before writing
164+
sorted_pet_dirs = sorted(pet_dcm_dirs_list)
165+
for dcm in sorted_pet_dirs:
166+
if presort_enabled:
167+
presorted_folders = sorted(presort(dcm)) # Sort presorted results too
168+
for pre in presorted_folders:
169+
file.write(str(pre) + "\n")
130170
else:
131-
file.write(dcm + '\n')
171+
file.write(dcm + "\n")
132172
file.close()
133173

134-
if len(pet_dcm_dirs_list):
135-
file = open(f'{root}/pet2bids_dcm.list', 'w')
174+
if len(pet_dcm_dirs_list) and pet2bids_enabled:
175+
file = open(f"{root}/pet2bids_dcm.list", "w")
136176
# Sort the list before writing
137177
sorted_pet_dirs = sorted(pet_dcm_dirs_list)
138178
for dcm in sorted_pet_dirs:
139179
if presort_enabled:
140180
presorted_folders = sorted(presort(dcm)) # Sort presorted results too
141181
for pre in presorted_folders:
142-
file.write(str(pre) + '\n')
182+
file.write(str(pre) + "\n")
143183
else:
144-
file.write(dcm + '\n')
184+
file.write(dcm + "\n")
145185
file.close()
146186

147-
if len(pet_ecat_files_list):
148-
file = open(f'{root}/pet2bids_ecat.list', 'w')
187+
if len(pet_ecat_files_list) and pet2bids_enabled:
188+
file = open(f"{root}/pet2bids_ecat.list", "w")
149189
for ecat in pet_ecat_files_list:
150-
file.write(ecat + '\n')
190+
file.write(ecat + "\n")
151191
file.close()
152192

153193
if len(meg_data_list):
154-
file = open(f'{root}/meg.list', 'w')
194+
file = open(f"{root}/meg.list", "w")
155195
for meg in meg_data_list:
156-
file.write(meg + '\n')
196+
file.write(meg + "\n")
157197
file.close()

0 commit comments

Comments
 (0)