-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_xnat2bids.py
748 lines (582 loc) · 26.7 KB
/
run_xnat2bids.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
import argparse
import asyncio
import copy
from collections import defaultdict
import datetime
import difflib
from enum import Enum
from getpass import getpass
import glob
import logging
import os
import pathlib
import requests
import shlex
import time
import re
from toml import load
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.NOTSET)
logging.basicConfig(level=logging.INFO)
logging.getLogger('asyncio').setLevel(logging.WARNING)
# Disable DEBUG logging for urllib3
urllib3_logger = logging.getLogger('urllib3')
urllib3_logger.setLevel(logging.WARNING)
# PARAM_VAL (--param val)
# MULTI_VAL (-param 1, --param 2, --param n)
# FLAG_ONLU (--param)
class ParamType(Enum):
PARAM_VAL = 0
MULTI_VAL = 1
FLAG_ONLY = 2
MULTI_FLAG = 3
# param_name: (param_type, needs_binding)
xnat2bids_params = {
"bidsmap-file": (ParamType.PARAM_VAL, True),
"bids_root": (ParamType.PARAM_VAL, True),
"cleanup": (ParamType.FLAG_ONLY, False),
"dicomfix-config":(ParamType.PARAM_VAL, True),
"export-only": (ParamType.FLAG_ONLY, False),
"host": (ParamType.PARAM_VAL, False),
"includeseq": (ParamType.MULTI_VAL, False),
"log-id": (ParamType.PARAM_VAL, False),
"overwrite": (ParamType.FLAG_ONLY, False),
"sessions": (ParamType.MULTI_VAL, False),
"skip-export": (ParamType.FLAG_ONLY, False),
"skipseq": (ParamType.MULTI_VAL, False),
"validate_frames": (ParamType.FLAG_ONLY, False),
"version": (ParamType.PARAM_VAL, False),
"verbose": (ParamType.MULTI_FLAG, False),
}
config_params = {
"project": (ParamType.PARAM_VAL, False),
"subjects": (ParamType.MULTI_VAL, False),
}
def suggest_similar(input_value, valid_options):
suggestions = difflib.get_close_matches(input_value, valid_options, n=1, cutoff=0.6)
return suggestions[0] if suggestions else None
def verify_parameters(config):
config_dict = load(config)
x2b_params = config_dict['xnat2bids-args']
for k, v in x2b_params.items():
if not (k in xnat2bids_params or k in config_params):
logging.info(f"Invalid parameter in configuration file: {k} ")
logging.info("Please resolve invalid parameters before running.")
suggestion = suggest_similar(k, list(xnat2bids_params.keys()) + list(config_params.keys()))
if suggestion:
print(f"Did you mean: {suggestion}?")
exit()
def get_user_credentials():
user = input('Enter XNAT Username: ')
password = getpass('Enter Password: ')
return user, password
def merge_default_params(config_path, default_params):
if config_path is None:
return default_params
user_params = load(config_path)
return merge_config_files(user_params, default_params)
def parse_cli_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('bids_root', nargs='?', default=None)
parser.add_argument('--diff', action=argparse.BooleanOptionalAction, help="diff report between bids_root and remote XNAT")
parser.add_argument('--update', action=argparse.BooleanOptionalAction, help="diff report between bids_root and remote XNAT")
parser.add_argument("--config", help="path to user config")
args = parser.parse_args()
return args
def prompt_user_for_sessions(arg_dict):
docs = "https://docs.ccv.brown.edu/bnc-user-manual/xnat-to-bids-intro/using-oscar/oscar-utility-script"
logging.warning("No sessions were provided in the configuration file. Please specify session(s) to process.")
logging.info("For helpful guidance, check out our docs at %s", docs)
sessions_input = input("Enter Session(s) (comma-separated): ")
arg_dict['xnat2bids-args']['sessions'] = [s.strip() for s in sessions_input.split(',')]
def get(connection, url, **kwargs):
r = connection.get(url, **kwargs)
r.raise_for_status()
return r
def get_project_subject_session(connection, host, session):
"""Get project ID and subject ID from session JSON
If calling within XNAT, only session is passed"""
r = get(
connection,
host + "/data/experiments/%s" % session,
params={"format": "json", "handler": "values", "columns": "project,subject_ID,label"},
)
sessionValuesJson = r.json()["ResultSet"]["Result"][0]
project = sessionValuesJson["project"]
subjectID = sessionValuesJson["subject_ID"]
r = get(
connection,
host + "/data/subjects/%s" % subjectID,
params={"format": "json", "handler": "values", "columns": "label"},
)
subject = r.json()["ResultSet"]["Result"][0]["label"]
return project, subject
def get_sessions_from_project_subjects(connection, host, project, subjects):
sessions = []
for subj in subjects:
r = get(
connection,
host + f"/data/projects/{project}/subjects/{subj}/experiments",
params={"format": "json"},
)
projectValues = r.json()["ResultSet"]["Result"]
sessions.extend(extractSessions(projectValues))
return sessions
def get_sessions_from_project(connection, host, project):
r = get(
connection,
host + f"/data/projects/{project}/experiments",
params={"format": "json"},
)
return r.json()["ResultSet"]["Result"]
def prepare_path_prefixes(project, subject):
# get PI from project name
pi_prefix = project.split("_")[0]
# Paths to export source data in a BIDS friendly way
study_prefix = "study-" + project.split("_")[1]
return pi_prefix.lower(), study_prefix.lower()
def set_logging_level(x2b_arglist: list):
if "--verbose" in x2b_arglist:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)
def fetch_latest_simg(container_name):
list_of_versions = glob.glob(f'/oscar/data/bnc/simgs/*/*{container_name}*')
# Regular expression to extract version numbers from filenames
version_regex = re.compile(r'(?:v)?(\d+\.\d+\.\d+)\.sif')
# Dictionary to hold images and their parsed versions
image_versions = {}
for image in list_of_versions:
match = version_regex.search(image)
if match:
image_version = match.group(1)
image_versions[image] = tuple(map(int, image_version.split('.')))
# Find the image with the highest version number
most_recent_image = max(image_versions, key=image_versions.get)
return most_recent_image
def extract_params(param, value):
arg = []
# if includeseq or skipseq parameter, check whether a
# range is specified (a string with a dash), and parse
# accordingly
if param in ['includeseq','skipseq'] and type(value)==str:
val_list = value.replace(" ", "").split(",")
for val in val_list:
if "-" in val:
startval,stopval = val.split("-")
expanded_val = list(range(int(startval),int(stopval)+1))
for v in expanded_val:
arg.append(f"--{param} {v}")
else:
arg.append(f"--{param} {val}")
else:
for v in value:
arg.append(f"--{param} {v}")
return ' '.join(arg)
def extractSessions(results):
sessions = []
for experiment in results:
sessions.append(experiment['ID'])
return sessions
def fetch_job_ids(stdout):
jobs = []
if isinstance(stdout, bytes):
stdout = [stdout]
for line in stdout:
job_id = line.replace(b'Submitted batch job ', b'' ).strip().decode('utf-8')
jobs.append(job_id)
return jobs
def get_datetime(date):
year, month, day = date.split(" ")[0].split("-")
return datetime.datetime(int(year), int(month), int(day))
def diff_data_directory(bids_root, user, password):
missing_sessions = []
# Establish connection
connection = requests.Session()
connection.verify = True
connection.auth = (user, password)
host = "https://xnat.bnc.brown.edu"
# Gather list of existing PIs in data directory
pis = [proj.name for proj in os.scandir(bids_root) if proj.is_dir()]
for pi in pis:
# Gather list of studies for every project
studies = [stu.name.split("-")[1] for stu in os.scandir(f"{bids_root}/{pi}")]
for study in studies:
# Request all sessions in PROJECT_STUDY from XNAT.
pi_study = f"{pi}_{study}".upper()
sessions = get_sessions_from_project(connection, host, pi_study)
for session in sessions:
# Get date of most recent change for every session
latest_date = get_datetime(session['date'])
date_added = get_datetime(session['insert_date'])
# Sessions with label format SUBJECT_SESSION are one among many, named ses-SESSION
if "_" in session['label']:
subj, sess = session['label'].lower().split("_")
# Sessions with SUBJECT as label are the only session. Default to ses-01
else:
subj = session['label']
sess = "01"
ses_path = f"{bids_root}/{pi}/study-{study}/bids/sub-{subj}/ses-{sess}"
# Add to list of sessions to sync if path does not exist.
if not (os.path.exists(ses_path)):
missing_sessions.append({'pi': pi, 'study': study, 'subject': subj, 'session': sess, 'ID': session['ID']} )
else:
# For existing paths, check for more recent changes via date of last export and XNAT's session date.
c_time = os.path.getctime(ses_path)
l_time = time.localtime(c_time)
data_date = datetime.datetime(l_time.tm_year, l_time.tm_mon, l_time.tm_mday)
if (date_added > data_date or latest_date > data_date):
missing_sessions.append({'pi': pi, 'study': study, 'subject': subj, 'session': sess, 'ID': session['ID']} )
connection.close()
return missing_sessions
def generate_diff_report(sessions_to_update):
for session_data in sessions_to_update:
project = session_data['pi']
study = session_data['study']
subject = session_data['subject']
session = session_data['session']
ID = session_data['ID']
if session == '':
logging.info(f"Missing session information for {project}/{study}/{subject} (ID: {ID})")
else:
logging.info(f"Session {session} found for {project}/{study}/{subject} (ID: {ID})")
def fetch_requested_sessions(arg_dict, user, password):
# Initialize sessions list
sessions = []
# Establish connection
connection = requests.Session()
connection.verify = True
connection.auth = (user, password)
host = arg_dict["xnat2bids-args"]["host"]
if 'project' in arg_dict['xnat2bids-args']:
project = arg_dict['xnat2bids-args']['project']
if 'subjects' in arg_dict['xnat2bids-args']:
subjects = arg_dict['xnat2bids-args']['subjects']
sessions = get_sessions_from_project_subjects(connection, host, project, subjects)
else:
sessions = extractSessions(get_sessions_from_project(connection, host, project))
connection.close()
if "sessions" in arg_dict['xnat2bids-args']:
sessions.extend(arg_dict['xnat2bids-args']['sessions'])
return sessions
def merge_config_files(user_cfg, default_cfg):
user_slurm = user_cfg['slurm-args']
default_slurm = default_cfg['slurm-args']
default_x2b = default_cfg['xnat2bids-args']
if "xnat2bids-args" in user_cfg:
user_x2b = user_cfg['xnat2bids-args']
# Assemble merged dictionary with default values.
merged_dict = defaultdict(dict)
merged_dict['xnat2bids-args'].update(default_x2b)
merged_dict['slurm-args'].update(default_slurm)
# Update merged dictionary with user provided arguments.
merged_dict['slurm-args'].update(user_slurm)
if "xnat2bids-args" in user_cfg:
merged_dict['xnat2bids-args'].update(user_x2b)
# Add session specific parameter blocks
for key in user_cfg.keys():
if key == 'slurm-args' or key == 'xnat2bids-args':
continue
merged_dict[key].update(user_cfg[key])
return merged_dict
def parse_x2b_params(xnat2bids_dict, session, bindings):
x2b_param_list = []
positional_args = ["sessions", "bids_root"]
# Handle positional argments SESSION and BIDS_ROOT
x2b_param_list.append(session)
if "bids_root" in xnat2bids_dict:
bids_root = xnat2bids_dict["bids_root"]
arg = f"{bids_root}"
bindings.append(arg)
x2b_param_list.append(arg)
for param, value in xnat2bids_dict.items():
if not (param in xnat2bids_params or param in config_params):
logging.info(f"Invalid parameter in configuration file: {param}")
logging.info("Please resolve invalid parameters before running.")
suggestion = suggest_similar(k, list(xnat2bids_params.keys()) + list(config_params.keys()))
if suggestion:
print(f"Did you mean: {suggestion}?")
exit()
if value == "" or value is None:
continue
if param in positional_args or param in config_params:
continue
param_type = xnat2bids_params[param][0]
if param_type == ParamType.PARAM_VAL:
arg = f"--{param} {value}"
x2b_param_list.append(arg)
elif param_type == ParamType.MULTI_VAL:
arg = extract_params(param, value)
x2b_param_list.append(arg)
elif param_type == ParamType.FLAG_ONLY:
arg = f"--{param}"
x2b_param_list.append(arg)
elif param_type == ParamType.MULTI_FLAG:
arg = f"--{param}"
for i in range(value):
x2b_param_list.append(arg)
needs_binding = xnat2bids_params[param][1]
if needs_binding:
bindings.append(value)
return x2b_param_list
def compile_slurm_list(arg_dict, user):
slurm_param_list = []
for param, value in arg_dict["slurm-args"].items():
if value != "" and value is not None:
arg = f"--{param} {value}"
slurm_param_list.append(arg)
return slurm_param_list
def compile_xnat2bids_list(session, arg_dict, user):
"""Create command line argument list from TOML dictionary."""
# Create copy of dictionary, so as not to update
# the original object reference while merging configs.
arg_dict_copy = copy.deepcopy(arg_dict)
bindings = []
# Compile list of appended arguments
x2b_param_dict = {}
for section_name, section_dict in arg_dict_copy.items():
# Extract xnat2bids-args from original dictionary
if section_name == "xnat2bids-args":
x2b_param_dict = section_dict
# If a session key exist for the current session being
# processed, update final config with session block.
elif section_name == session:
x2b_param_dict.update(section_dict)
# Transform session config dictionary into a parameter list.
x2b_param_list = parse_x2b_params(x2b_param_dict, session, bindings)
return x2b_param_list, bindings
def assemble_argument_lists(arg_dict, user, password, bids_root, argument_lists=[]):
# Compose argument lists for each session
for session in arg_dict['xnat2bids-args']['sessions']:
# Compile list of slurm parameters.
slurm_param_list = compile_slurm_list(arg_dict, user)
# Fetch compiled xnat2bids and slurm parameter lists
x2b_param_list, bindings = compile_xnat2bids_list(session, arg_dict, user)
# Insert username and password into x2b_param_list
x2b_param_list.insert(2, f"--user {user}")
x2b_param_list.insert(3, f"--pass {password}")
# Define output for logs
if not ('output' in arg_dict['slurm-args']):
output = f"/gpfs/scratch/{user}/logs/%x-{session}-%J.txt"
arg = f"--output {output}"
slurm_param_list.append(arg)
else:
output = arg_dict['slurm-args']['output']
if not (os.path.exists(os.path.dirname(output))):
os.makedirs(os.path.dirname(output))
# Define bids root directory
if 'bids_root' in arg_dict['xnat2bids-args']:
bids_root = x2b_param_list[1]
else:
x2b_param_list.insert(1, bids_root)
bindings.append(bids_root)
if not (os.path.exists(bids_root)):
os.makedirs(bids_root)
# Store xnat2bids, slurm, and binding paramters as tuple.
argument_lists.append((x2b_param_list, slurm_param_list, bindings))
# Set logging level per session verbosity.
set_logging_level(x2b_param_list)
# Remove the password parameter from the x2b_param_list
x2b_param_list_without_password = [param for param in x2b_param_list if not param.startswith('--pass')]
logging.debug({
"message": "Argument List",
"session": session,
"slurm_param_list": slurm_param_list,
"x2b_param_list": x2b_param_list_without_password,
})
return argument_lists, bids_root
async def launch_x2b_jobs(argument_lists, simg, tasks=[], output=[]):
# Loop over argument lists for provided sessions.
needs_validation = False
for args in argument_lists:
# Compilie slurm and xnat2bids args
xnat2bids_param_list = args[0]
slurm_param_list = args[1]
bindings_paths = args[2]
xnat2bids_options = ' '.join(xnat2bids_param_list)
slurm_options = ' '.join(slurm_param_list)
# Compile bindings into formated string
bindings = ' '.join(f"-B {path}" for path in bindings_paths)
# Set needs_validation if --export-only does not exist
if "--export-only" not in xnat2bids_param_list: needs_validation = True
# Build shell script for sbatch
sbatch_script = f'\'$(cat << EOF #!/bin/sh\n \
apptainer exec --no-home {bindings} {simg} \
xnat2bids {xnat2bids_options}\nEOF\n)\''
# Escape any '$' characters and format to comply with sbatch script syntax
sbatch_escaped_script = "$" + sbatch_script[2:-1].replace('$', '\$')
formatted_script = f'\'{sbatch_escaped_script}\''
# Process command string for SRUN
sbatch_cmd = shlex.split(f"sbatch {slurm_options} \
--wrap {formatted_script}")
# Set logging level per session verbosity.
set_logging_level(xnat2bids_param_list)
# Remove the password from sbatch command before logging
xnat2bids_options_without_password = []
exclude_next_opt = False
for opt in xnat2bids_options.split():
if exclude_next_opt:
exclude_next_opt = False
elif opt == "--pass":
exclude_next_opt = True
continue
else:
xnat2bids_options_without_password.append(opt)
sbatch_script_without_password = f"apptainer exec --no-home {bindings} {simg} \
xnat2bids {xnat2bids_options_without_password}"
sbatch_cmd_without_password = shlex.split(f"sbatch {slurm_options} \
--wrap {sbatch_script_without_password}")
logging.debug({
"message": "Executing xnat2bids",
"session": xnat2bids_param_list[0],
"command": sbatch_cmd_without_password
})
# Run xnat2bids
proc = await asyncio.create_subprocess_exec(*sbatch_cmd, stdout=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
output.append(stdout)
return output, needs_validation
async def launch_bids_validator(arg_dict, user, password, bids_root, job_deps):
bids_experiments = []
output = []
# Establish connection
connection = requests.Session()
connection.verify = True
connection.auth = (user, password)
# Fetch pi and study prefixes for BIDS path
host = arg_dict["xnat2bids-args"]["host"]
for session in arg_dict["xnat2bids-args"]["sessions"]:
proj, subj = get_project_subject_session(connection, host, session)
pi_prefix, study_prefix = prepare_path_prefixes(proj, subj)
# Define bids_experiment_dir
bids_dir = f"{bids_root}/{pi_prefix}/{study_prefix}/bids"
if bids_dir not in bids_experiments:
bids_experiments.append(bids_dir)
# Close connection
connection.close()
# Define bids-validator singularity image path
simg=fetch_latest_simg("validator")
for bids_experiment_dir in bids_experiments:
# Build shell script for sbatch
sbatch_bids_val_script = f"\"$(cat << EOF #!/bin/sh\n \
apptainer exec --no-home -B {bids_experiment_dir} {simg} \
bids-validator {bids_experiment_dir}\nEOF\n)\""
# Compile list of slurm parameters.
bids_val_slurm_params = compile_slurm_list(arg_dict, user)
if not ('output' in arg_dict['slurm-args']):
val_output = f"/gpfs/scratch/{user}/logs/%x-%J.txt"
arg = f"--output {val_output}"
bids_val_slurm_params.append(arg)
else:
x2b_output = arg_dict['slurm-args']['output'].split("/")
x2b_output[-1] = "%x-%J.txt"
val_output = "/".join(x2b_output)
bids_val_slurm_params = [f"--output {val_output}" if "output" in item else item for item in bids_val_slurm_params]
bids_val_slurm_params.append("--kill-on-invalid-dep=yes")
slurm_options = ' '.join(bids_val_slurm_params)
# Process command string for SRUN
slurm_options = slurm_options.replace("--job-name xnat2bids", "--job-name bids-validator")
# Fetch JOB-IDs of xnat2bids jobs to wait upon
afterok_ids = ":".join(job_deps)
sbatch_bids_val_cmd = shlex.split(f"sbatch -d afterok:{afterok_ids} {slurm_options} \
--wrap {sbatch_bids_val_script}")
# Run bids-validator
proc = await asyncio.create_subprocess_exec(*sbatch_bids_val_cmd, stdout=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
output.append(stdout)
return output
async def main():
# Instantiate argument parser
args = parse_cli_arguments()
# Fetch user credentials
user, password = get_user_credentials()
if (args.config):
verify_parameters(args.config)
# Load default config file into dictionary
script_dir = pathlib.Path(__file__).parent.resolve()
default_params = load(f'{script_dir}/x2b_default_config.toml')
# Set arg_dict. If user provides config, merge dictionaries.
arg_dict = merge_default_params(args.config, default_params)
# Initialize bids_root for non-local use
bids_root = f"/users/{user}/bids-export/"
# Initialize version and singularity image for non-local use
try:
version = arg_dict['xnat2bids-args']['version']
simg=f"/oscar/data/bnc/simgs/brownbnc/xnat-tools-{version}.sif"
# we have to delete the argument so that it doesn't get
# passed to xnat2bids
del arg_dict['xnat2bids-args']['version']
except KeyError:
simg = fetch_latest_simg('xnat-tools')
if any(key in arg_dict['xnat2bids-args'] for key in ['project', 'subjects', 'sessions']):
sessions = fetch_requested_sessions(arg_dict, user, password)
if len(sessions) == 0:
logging.info("There are no sessions to export. Please check your configuration file for errors.")
exit()
else:
if 'sessions' in arg_dict['xnat2bids-args']:
for session in sessions:
if session not in arg_dict['xnat2bids-args']['sessions']:
arg_dict['xnat2bids-args']['sessions'].append(session)
else:
arg_dict['xnat2bids-args']['sessions'] = sessions
if (args.diff):
data_dir = bids_root
if args.bids_root:
data_dir = args.bids_root
elif "bids_root" in arg_dict['xnat2bids-args']:
data_dir = arg_dict['xnat2bids-args']["bids_root"]
sessions_to_update = diff_data_directory(data_dir, user, password)
generate_diff_report(sessions_to_update)
return
if args.update:
data_dir = bids_root
if args.bids_root:
data_dir = args.bids_root
elif "bids_root" in arg_dict['xnat2bids-args']:
data_dir = arg_dict['xnat2bids-args']["bids_root"]
sessions_to_update = diff_data_directory(data_dir, user, password)
session_list = [ses['ID'] for ses in sessions_to_update]
if len(session_list) == 0:
logging.info("Your data directory is synced. Exiting.")
exit()
else:
logging.info("Launching jobs for the following sessions:")
generate_diff_report(sessions_to_update)
while True:
confirm = input("Would you like to proceed with the update? (y/n) \n")
if confirm == "y" or confirm == "Y":
break
elif confirm == "n" or confirm == "N":
exit()
else:
logging.info("Your input was not a valid option.")
continue
if 'sessions' in arg_dict['xnat2bids-args']:
for session in session_list:
if session not in arg_dict['xnat2bids-args']['sessions']:
arg_dict['xnat2bids-args']['sessions'].append(session)
else:
arg_dict['xnat2bids-args']['sessions'] = session_list
try:
sessions = arg_dict['xnat2bids-args']['sessions']
except KeyError:
sessions = []
if not sessions:
prompt_user_for_sessions(arg_dict)
argument_lists, bids_root = assemble_argument_lists(arg_dict, user, password, bids_root)
# Launch xnat2bids
x2b_output, needs_validation = await launch_x2b_jobs(argument_lists, simg)
x2b_jobs = fetch_job_ids(x2b_output)
# Launch bids-validator
if needs_validation:
validator_output = await launch_bids_validator(arg_dict, user, password, bids_root, x2b_jobs)
validator_jobs = fetch_job_ids(validator_output)
# Summary Logging
logging.info("Launched %d xnat2bids %s", len(x2b_jobs), "jobs" if len(x2b_jobs) > 1 else "job")
logging.info("Job %s: %s", "IDs" if len(x2b_jobs) > 1 else "ID", ' '.join(x2b_jobs))
if needs_validation:
logging.info("Launched %d bids-validator %s to check BIDS compliance", len(validator_jobs), "jobs" if len(validator_jobs) > 1 else "job")
logging.info("Job %s: %s", "IDs" if len(validator_jobs) > 1 else "ID", ' '.join(validator_jobs))
logging.info("Processed Scans Located At: %s", bids_root)
if __name__ == "__main__":
asyncio.run(main())