-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_master.py
1195 lines (931 loc) · 53.8 KB
/
project_master.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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import copy
import subprocess
import os
import glob
import shutil
from functools import wraps
import logging
import datetime
import paramiko
import argparse
from backend.analysis import mri_preprocessing_wrappers
from backend.utils import utils
import nipype.pipeline.engine as pe
from nipype.interfaces.dcm2nii import Dcm2niix
class ProjectMaster():
"""
USEAGE: subclass this and overwrite all attributes with those relevant to your project.
See projects_config.py for an example. Run with run_project.py that calls
class methods to download scans from WBIC to hivemind and organise folder
structure to BIDS.
For logging to work, the logger must be initialised (self.init_logging)
with the scan information. Logs are saved to /docs/logs/
Key public methods download_scans_from_hpc() and move_raw_to_preprocessing()
handle downloading and moving scans. Methods on this class typically work on an
per-scan basis e.g. download a single scan, copy a single scan.
All existing methods should function well but it is also possible to
customise functionality by subclassing this class's methods.
NOTES:
Access of all mutable class attributes should be through getters which return
copies.
No need to initialise the __init__() on this class when subclassing.
"""
def __init__(self):
self.raw_scans_path = ""
self.docs_path = ""
self.logs_path = ""
self.download_logs_path = ""
self.slurm_logs_path = ""
self.data_path = ""
self.raw_scans_path = ""
self.preprocessing_path = ""
self.base_path = ""
self.project_code = ""
self.account = ""
self.server_to_download_to = ""
self.mrs_scan_details = None
self.func_scan_details = None
self.anat_scan_details = None
self.mpm_scan_details = None
self.b0_scan_details = None
self.b1_scan_details = None
# Scan Parameters ------------------------------------------------------------------------------------------------
self.num_expected_func_files = None
self.num_expected_anat_files = None
self.num_expected_mrs_files = None
self.num_expected_mpm_files = None
self.num_expected_b0_files = None
self.num_expected_b1_files = None
# Participants ---------------------------------------------------------------------------------------------------
self._participant_log = {
"XXXXX": {"sub_id": "sub-XXX",
"lab_id": "XXXX",
"zk_id": "zkXXwX_XXX"
},
}
# ----------------------------------------------------------------------------------------------------------------------
# Public Methods
# ----------------------------------------------------------------------------------------------------------------------
# Pulling Data from HPC and organising Project Structure
# ----------------------------------------------------------------------------------------------------------------------
def download_scans_from_hpc(self, wbic_id, scan_info):
"""
Download a scan from the WBIC to the HPC with WBIC's dcmconv.pl function. This requires a folder
called "wbic-data" on your rds-ds/user/username/hpc-work HPC directory.
This data is then this data from the HPC to the hivemind, under the project dir /raw_scans
and delete from the HPC.
Testing logs the nubmer of files in each downloadchecks none of the folders are empty.
All download / copy processes are logged to the /docs/logs log for this scan (see init_logging).
"""
if self.scan_already_downloaded(scan_info["zk_id"]):
return False
self.log(None, "Pulling scans from HPC...")
self._pull_scans_from_wbic_to_hpc(wbic_id,
scan_info["date"])
self._pull_scans_from_hpc_to_hivemind(wbic_id,
scan_info["date"])
self._extract_wbic_data_to_zk_folder(wbic_id,
scan_info["zk_id"])
download_failed, __ = self._test_download(scan_info["zk_id"],
save_to_log=True)
if download_failed:
return False
return True
def move_raw_to_preprocessing(self, wbic_id, sub_info, scan_info):
"""
Move the relevant raw scans (as specified in self.XXX_scan_details) for a scan
from the raw_scans dir to the preprocessing/sub/ses dir. If a ses dir already exists
in preprocessing/sub, it will be skipped.
Make a session directory in the preprocessing/sub dir for the
session if it does not exist. Then, the raw_scans dir is searched with
glob.glob (search string specified in the XXX_scan_details attribute)
and matches copied to the relevant folder.
Any runs specified in the "flags" entry of the "scan" dict in
the participant log will be ignored (see self.participant_log in
project_configs.py).
"""
ses_exists = self._check_ses_exists_mkdir_if_not(sub_info["sub_id"], scan_info, log=True)
for scan_type in ["mrs", "func", "anat", "mpm", "b0", "b1"]: # TODO: MOVE TO CONFIGS
if self.check_if_scan_type_is_in_ses_folder(sub_info["sub_id"], scan_info["ses_id"], scan_type):
continue
self._copy_data_to_preprocessing(scan_type,
scan_info,
sub_info)
self._dump_info_file_in_session_dir(wbic_id,
scan_info,
sub_info)
return True
def run_scan_sub_order_tests(self, assert_=False):
"""
Test all datetimes and sub ids in the session text files
match throughout the project.
Subjects should be in date order (e.g. the scan datetime for
sub-001 ses-001 should not be before sub-002 ses-001)
Sessions for a sub should also be in date order (e.g. sub-001
ses-001 should not be before sub-001 ses-002)
"""
error_log = self._test_project_scan_and_ses_ids_match_date_order()
error_log = "All Tests Passed" if not any(error_log) else error_log
self.log("Testing sub and ses IDS match datetimes",
error_log)
if assert_:
assert error_log == "All Tests Passed", error_log
# Helpers / Getters
# ----------------------------------------------------------------------------------------------------------------------
def get_participant_log(self):
"""
Test the participant log to ensure all inputs are formatted correctly
before returning a copy.
"""
participant_log = copy.deepcopy(self._participant_log)
self._test_participant_log(participant_log)
return participant_log
def is_initialised(self):
"""
Check the project directory structure has been initialised
"""
return os.path.isdir(self.base_path)
def init_project_directory_tree(self):
"""
Make all the base directories for the project.
NOTE: _mkdir will init dir tree, but verbose for clarity here.
"""
for path in [self.base_path, self.docs_path, self.logs_path,
self.download_logs_path, self.slurm_logs_path, self.data_path,
self.raw_scans_path, self.preprocessing_path]:
self._mkdir(path)
def init_logging(self, date_, zk_id, logging_path=None, log_filename=None): # TODO: this was originally for downloading only but has been extended. could be neated up with download init calling the relevant filename rather than it assumed as default ehre
"""
Initialise the logger for the current scan. All logging
(self.log()) will then be saved to the log in /docs/logs
with filename formatted "date_zk_id.log".
"""
if not logging_path:
logging_path = self.download_logs_path
if not log_filename:
log_filename = "_".join([date_, zk_id]) + ".log"
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
log_filename = "_".join([date_, zk_id]) + ".log"
logging.basicConfig(filename=os.path.join(self.download_logs_path, log_filename),
format="%(message)s",
level=logging.DEBUG)
self.log(None, "Logger Initialised...")
def log(self, title, message):
"""
Log the message, if title is not None inserted it with banner
seperated by line breaks. See init_logging for setup.
INPUTS: title (str) or None, message (str)
"""
if title:
now = datetime.datetime.now()
title = " ".join(["\n",
now.ctime(),
title])
message = title + " -------------------------------------------------------------------------------------" \
"\n\n" + message
logging.debug(message)
def scan_already_downloaded(self, zk_id):
"""
Download considered successful if data is moved from WBIC
code folder to zk folder after download as it is the last
process in self.download_scans_from_hpc()
"""
putative_zkid_scan_path = os.path.join(self.raw_scans_path,
zk_id)
return os.path.isdir(putative_zkid_scan_path)
def get_all_subs_and_ses_in_preprocessing(self):
"""
Return dict in format {sub-001: [ses-001, ses-002...],
sub-002, [ses-...]}
"""
result = {}
all_subs_paths = glob.glob(
os.path.join(self.preprocessing_path, "sub*"))
for sub_path in all_subs_paths:
all_session_paths = glob.glob(
os.path.join(sub_path, "ses-*"))
sessions = [os.path.basename(ses_path) for ses_path in all_session_paths]
sub = os.path.basename(sub_path)
result.update({sub: sessions})
return result
# ----------------------------------------------------------------------------------------------------------------------
# Private Methods
# ----------------------------------------------------------------------------------------------------------------------
# Pulling Data from HPC
# ----------------------------------------------------------------------------------------------------------------------
def _pull_scans_from_wbic_to_hpc(self, wbic_id, date_):
"""
SSH connect to to the HPC and use dcmconv.pl to download scans from WBIC to
a HPC folder /rds-d5/user/USERNAME/hpc-work/wbic-data.
"""
command = "module load wbic && " \
"cd /rds-d5/user/{0}/hpc-work/wbic-data && " \
"/usr/local/software/wbic/bin/dcmconv.pl " \
"-remoteae {1} -id {2} -date {3} -makedir -outtype dicom10 -direct -info -all".format(self.account,
self.project_code,
wbic_id,
date_)
stdout = self._run_ssh_to_hpc(command)
self.log("pulled scans from wbic to hpc ",
command)
self.log(None,
"project: {0}, wbic_id {1}, date: {2} \n {3}".format(self.project_code,
wbic_id,
self.account,
stdout))
def _pull_scans_from_hpc_to_hivemind(self, wbic_id, date_):
"""
SSH connect to HPC and download scans to hivemind. See
_pull_scans_from_wbic_to_hpc()
"""
command = "rsync -rsh /rds-d5/user/{0}/hpc-work/wbic-data/{1} {0}@{2}:{3} && " \
"rm -rf /rds-d5/user/{0}/hpc-work/wbic-data/{1}".format(self.account,
wbic_id,
self.server_to_download_to,
self.raw_scans_path)
stdout = self._run_ssh_to_hpc(command)
self.log("pulled scans from wbic to hpc ", # NEATEN
command)
self.log(None,
"wbic_id {0}, date: {1}, folder: {2} \n {3}".format(wbic_id,
date_,
self.raw_scans_path,
stdout))
def _run_ssh_to_hpc(self, command):
"""
Run the command on an SSH connection to the HPC. Try 5 times to connect
and if not sucessful, assert. If successful, return the stdout from the
ssh connection.
"""
max_attempts = 5
for attempt in range(max_attempts):
client = self._setup_ssh_to_hpc()
stdin, stdout, stderr = client.exec_command(command)
stdout_byte = stdout.read()
exit_code = stdout.channel.recv_exit_status() # must come after stdout.read() for large output
client.close()
if exit_code == 0:
return stdout_byte.decode("utf-8")
else:
if attempt == 4:
error = "subprocess failed in {0} attempts " \
"for command: {1} with error {2}".format(max_attempts,
command,
stderr.read().decode("utf-8"))
self.log("SSH ERROR", error)
assert False, error
def _setup_ssh_to_hpc(self):
"""
Use paramiko to generate an ssh connection from hivemind to HPC.
The SSH keys must already be setup and reside in /home/account/.ssh.
"""
key = paramiko.RSAKey.from_private_key_file("".join(["/home/",
self.account,
"/.ssh/id_rsa"]))
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname="login.hpc.cam.ac.uk", username=self.account, pkey=key)
return client
def _extract_wbic_data_to_zk_folder(self, wbic_id, zk_id):
"""
Extract data after downloaded from wbic to ABL standard form with
zk_id for backups. This is /raw_scans/zk_id/zk_id/scan_dirs.
For full backup, the common protocol sheet must be included in the
first level zk_id dir and the second level zk_id dir zipped.
"""
zk_id_path = os.path.join(self.raw_scans_path, zk_id, zk_id)
wbic_scan_files_path = glob.glob(os.path.join(self.raw_scans_path, wbic_id, "*"))[0]
self.log("Extract wbic data to zk folder",
"zk_id_path: " + zk_id_path + "\n "
"wbic_scan_files_path: " + wbic_scan_files_path)
self._mkdir(zk_id_path)
self._move(wbic_scan_files_path,
zk_id_path,
move_contents_only=True)
shutil.rmtree(os.path.join(self.raw_scans_path, wbic_id))
# Copying data from raw scans to preprocessing
# ----------------------------------------------------------------------------------------------------------------------
def _check_ses_exists_mkdir_if_not(self, sub_id, scan_info, log=False):
"""
Session is assumed to exist if a ses-XXX dir is found in preprocessing/sub-XXX dire
return True / False if session does / does not exist.
Log result if flag TRUE.
"""
ses_path = os.path.join(self.preprocessing_path,
sub_id,
scan_info["ses_id"])
ses_exists = os.path.isdir(ses_path)
if log:
if ses_exists :
pass
# self.log("Session already exists", TODO: think about this, can't log beause we want to skip scans when calling --move_to_preprocessing silently. But it is useful to have this log?
# "{0} for {1} was not created as it already exists".format(scan_info["ses_id"],
# sub_id))
else:
self.log("Creating new session file",
"existing {0} dir for {1}, {2} was not found. Creating dir.".format(sub_id,
scan_info["ses_id"],
scan_info["zk_id"]))
return ses_exists
def check_if_scan_type_is_in_ses_folder(self, sub_id, ses_id, scan_type):
"""
Check if the scan is already copied into the ses path e.g. preprocessing/sub-001/ses-001/func
Used to skip already-downloaded scans
"""
scan_type_path = os.path.join(self.preprocessing_path,
sub_id,
ses_id,
scan_type)
return os.path.isdir(scan_type_path)
def _copy_data_to_preprocessing(self, scan_type, scan_info, sub_info):
"""
Function to coordinate data copying from raw_scans to BIDS in preprocessing.
see self.move_raw_to_preprocessing()
scan_type: "mrs", "func", "anat" or "mpm", "b0", "b1"
TODO: bit repetitive as if raw scans dir is not present it will log the same response
many times, but do not want to take this a level up to download_and_copy as bnecomes too verbose.
"""
scan_details, num_expected_files = self._get_scan_details_and_expeced_num(scan_type)
if scan_details:
raw_scan_folder = os.path.join(self.raw_scans_path, scan_info["zk_id"])
if os.path.isdir(raw_scan_folder):
self.log("Copying raw {0} data to preprocessing folder".format(scan_type),
"Copying: {0}, {1}, {2}, {3}\n".format(scan_type,
scan_info["zk_id"],
sub_info["sub_id"],
scan_info["ses_id"]))
self._copy_data_from_raw_scans_to_preprocessing(scan_info,
sub_info["sub_id"],
scan_details,
scan_type,
num_expected_files)
else:
self.log("Copying raw {0} data to preprocessing folder".format(scan_type),
"no raw scans found for {0}, no data copied".format(scan_info["zk_id"]))
def check_for_duplicate_str_in_list(self, list_):
"""
Set() function will remove any duplicates
"""
return len(list_) == len(set(list_))
def _copy_data_from_raw_scans_to_preprocessing(self, # TODO: will pprobably end up needing to write a function to unpack scan details
scan_info,
sub_id,
scan_details,
data_name,
num_expected_files):
"""
see see self.move_raw_to_preprocessing()
"""
preprocessing_raw_data_path = os.path.join(self.preprocessing_path,
sub_id,
scan_info["ses_id"],
data_name, "raw")
for scan_name in scan_details.keys():
sequence_search_str = scan_details[scan_name]["search_str"]
task_name = scan_details[scan_name]["task_name"]
search_path_str = os.path.join(self.raw_scans_path,
scan_info["zk_id"], scan_info["zk_id"], # zk_id twice for backups organisation
sequence_search_str)
ordered_scan_run_paths = sorted(glob.glob(search_path_str))
if any(ordered_scan_run_paths) and \
self.check_for_duplicate_str_in_list(ordered_scan_run_paths):
self.log(None, "WARNING! Duplicate run detected in raw scans for " + scan_name)
saved_run_idx = 0
for true_run_idx, raw_data_to_copy in enumerate(ordered_scan_run_paths): # TODO: own function?
if "flags" in scan_info:
if self._skip_run_based_on_flags(scan_info, true_run_idx, data_name):
continue
bids_file_name = self._get_bids_filename(sub_id, scan_info["ses_id"], task_name,
saved_run_idx, scan_name)
destination_path = os.path.join(preprocessing_raw_data_path,
bids_file_name)
self._copy_dir_contents(raw_data_to_copy,
destination_path)
self._test_and_log_expected_file_number(destination_path,
num_expected_files)
saved_run_idx += 1
def _skip_run_based_on_flags(self, scan_info, run_idx, data_name): # TEST!!!!
"""
Runs to skip copying are set in the "flags" entry of the "scan" dict field
in self.participant log. All
"""
scan_specific_flags = [flag for flag in scan_info["flags"] if data_name in flag]
if any(scan_specific_flags):
runs_to_ignore = [flag.split("_")[2].lstrip("0") for flag in scan_specific_flags] # ignore leading zeros in case the user input as "001" format
if str(run_idx + 1) in runs_to_ignore: # TODO: does this fail the case where run=1 and runs to ingore contains ["10"]. Shouldnt but check
self.log(None,
"Did not copy run {0} for scan {1} "
"based on the flags {2}".format(run_idx + 1,
scan_info["zk_id"],
scan_specific_flags))
return True
return False
def _dump_info_file_in_session_dir(self, wbic_id, scan_info, sub_info):
"""
Write a file to a ses-XXX dir containing all information about the
sub / session. This file is used for tests on the scan datetime.
"""
ses_path = os.path.join(self.preprocessing_path,
sub_info["sub_id"],
scan_info["ses_id"])
if os.path.isdir(ses_path):
info = "project_code: {0}\n" \
"wbic_id: {1}\n" \
"sub_id: {2}\n" \
"ses_id {3}\n" \
"zk_id: {4}\n" \
"scan_date: {5}\n" \
"scan_start_time: {6}\n" \
"\nDo not edit this file".format(self.project_code,
wbic_id,
sub_info["sub_id"],
scan_info["ses_id"],
scan_info["zk_id"],
scan_info["date"],
scan_info["time_start"])
filename = scan_info["ses_id"] + "_info.txt"
with open(os.path.join(
ses_path, filename), "w") as file:
file.write(info)
# ----------------------------------------------------------------------------------------------------------------------
# Preprocessing - Run Commands
# ----------------------------------------------------------------------------------------------------------------------
def run_recon_all(self, sub_ids, ses_ids, run_ids, scan_names, scan_types, **kwargs):
"""
"""
run_func = self.get_recon_all_func(kwargs)
self._run_preprocessing_job(run_func, sub_ids, ses_ids, run_ids, scan_names, scan_types)
def run_dcm2niix(self, sub_ids, ses_ids, run_ids, scan_names, scan_types, **kwargs): # need to be careful specified keyworks do not overlap with nipype keywords
"""
"""
run_func = self.get_dcm2niix_func(kwargs)
self._run_preprocessing_job(run_func, sub_ids, ses_ids, run_ids, scan_names, scan_types)
def get_recon_all_func(self, kwargs): # TODO: use ke and mengxin options!
"""
"""
def run_recon_all_func(preprocessing_path, sub_id, ses_id, scan_types, bids_name, kwargs=kwargs): # TODO: ensure scan type is anat
from nipype.interfaces.freesurfer import ReconAll
source_dir = os.path.join(preprocessing_path, sub_id, ses_id, scan_types, 'nii', bids_name) # TODO: check if file already exists! for all!
reconall_node = pe.Node(name='reconall_node',
interface=ReconAll(subject_id=sub_id,
directive="all",
subjects_dir=source_dir,
T1_files=os.path.join(source_dir, bids_name + ".nii.gz")))
workflow = pe.Workflow(name='reconall')
workflow.base_dir = source_dir
workflow.add_nodes([reconall_node])
workflow.run(plugin="SLURMGraph", plugin_args = {'dont_resubmit_completed_jobs': True})
return run_recon_all_func
def get_dcm2niix_func(self, kwargs):
"""
"""
def run_dcm2niix_func(preprocessing_path, sub_id, ses_id, scan_types, bids_name, kwargs=kwargs):
source_dir = os.path.join(preprocessing_path, sub_id, ses_id, scan_types, 'raw', bids_name)
output_dir = os.path.join(preprocessing_path, sub_id, ses_id, scan_types, 'nii', bids_name)
self._mkdir(output_dir)
out_filename = bids_name if "out_filename" not in kwargs else kwargs["out_filename"]
dcm2niix_node = pe.Node(name='dcm2niix_node',
interface=Dcm2niix(out_filename=out_filename,
source_dir=source_dir,
output_dir=output_dir))
workflow = pe.Workflow(name='dcm2niix')
workflow.base_dir = output_dir
workflow.add_nodes([dcm2niix_node])
workflow.run(plugin="SLURMGraph", plugin_args = {'dont_resubmit_completed_jobs': True})
return run_dcm2niix_func
# ---------------------------------------------------------------------------------------------------------------------- # TODO: unit test
# Preprocessing - Run Commands
# ----------------------------------------------------------------------------------------------------------------------
def _run_preprocessing_job(self, command_func, sub_ids, ses_ids, run_ids, scan_names, scan_types, slurm=True, parallel=False, log=False, **kwargs): # TODO: rename scan_type to scan_types
"""
note will ignore sessions that do not exist. Make a log?
"""
# TODO: cannot run parallel and without slurm
sub_ids, ses_ids, run_ids, scan_names, scan_types = self._process_all_job_args(sub_ids, ses_ids, run_ids, scan_names, scan_types)
nii_or_raw = "raw" if "dcm2nii" in command_func.__name__ else "nii"
for sub_id in sub_ids:
if ses_ids == ["all"]:
ses_ids = self.get_all_ses_for_sub(sub_id)
for ses_id in ses_ids:
if not self.sub_has_ses(sub_id, ses_id): # TEST
continue
for scan_type in scan_types:
for scan_name in scan_names:
if not self.ses_has_at_least_one_scan_name_run(sub_id, ses_id, scan_type, nii_or_raw, scan_name):
continue
if run_ids == ["all"]:
run_ids = self.get_all_runs_in_folder(sub_id, ses_id, scan_type, nii_or_raw, scan_name)
for run_id in run_ids:
if not self.ses_has_run(sub_id, ses_id, scan_type, nii_or_raw, scan_name, run_id):
continue
scan_details, __ = self._get_scan_details_and_expeced_num(scan_type)
task_name = scan_details[scan_name]["task_name"] # mixing tasks and scan_names is not supported
run_idx = int(run_id[4:]) - 1 # TODO: fix _get_bids_filename?
bids_name = self._get_bids_filename(sub_id, ses_id, task_name, run_idx, scan_name)
##### NEW FUNCTION
command = command_func(self.preprocessing_path, sub_id, ses_id, scan_type, bids_name) # TODO: pipe output
# if log_filepath:
# open_type = "w" if not os.path.isfile(log_filepath) else "a"
# with open(log_filepath, open_type) as f:
# f.write(output_str)
# f.write("\n")
# f.close() # necessary?
# else:
# print(output_str)
# if slurm:
# all_commands_to_run += command + " &\n"
# ntasks += 1
# else:
# log_filepath = os.path.join(self.preprocessing_path, sub_id, "mri_command_logs.log") if log else None
# self._run_subprocess(command, log_filepath)
# if slurm:
# job_name = "recon_all" + "_" + datetime.datetime.now().strftime("%m%d%Y_%H%M") # TODO: FIX NAME !!
# utils.run_command_with_slurm(job_name, self.slurm_logs_path, self.slurm_logs_path, ntasks, all_commands_to_run)
def _run_subprocess(self, command, log_filepath=False): # TODO: MOVE TO UTILS, CHANGE NAME OF UTILS MODULE TO ONE BASED AROUND RUNNING COMMANDS.
"""
DOC where from
"""
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
output_str = output.strip().decode()
if log_filepath:
open_type = "w" if not os.path.isfile(log_filepath) else "a"
with open(log_filepath, open_type) as f:
f.write(output_str)
f.write("\n")
f.close() # necessary?
else:
print(output_str)
rc = process.poll()
return rc
# Check Arguments ------------------------------------------------------------------------------------------------------
def _process_all_job_args(self, sub_ids, ses_ids, run_ids, scan_names, scan_types): # CHANGE RUN NUM TO RUN_ID
"""
"""
for id in [sub_ids, ses_ids, run_ids, scan_names]:
assert type(id) == list, "Input type must be list for sub, ses, run and scan names"
sub_ids = self.check_and_process_sub_args(sub_ids)
ses_ids = self.process_mixed_list_of_ids(ses_ids, "ses-")
run_ids = self.process_mixed_list_of_ids(run_ids, "run-")
if scan_types == ["all"]:
scan_types = ["func", "anat", "b0", "b1"] # TODO: move to configs
return sub_ids, ses_ids, run_ids, scan_names, scan_types
def check_and_process_sub_args(self, sub_ids):
all_subs = [val["sub_id"] for val in self._participant_log.values()]
all_subs.reverse() # comes out high-to-low from dict
if sub_ids[0] == "all":
sub_ids = all_subs
all_sub_ids = self.process_mixed_list_of_ids(sub_ids, "sub-")
assert set(all_subs).intersection(set(all_sub_ids)), "Ensure specified sub id are in the participant log"
return all_sub_ids
def process_mixed_list_of_ids(self, list_of_ids, prefix):
"""
process mixed list e.g. list_of_ids=["sub-001", "2", "5:10"]
prefix is the id prefix e.g. "sub-", "ses-", "run-"
if id is in the form "3" or "5:6" expand_range_job_input returns
the fully formatted id e.g [sub-003] or ["sub-005", "sub-006"]
otherwise the user-input str (e..g "sub-010" is checked for formatting
igore "all" keyword as this is handlede later
"""
if list_of_ids[0] == "all":
return list_of_ids
all_ids = []
for id in list_of_ids:
if ":" in id or id.isnumeric():
id = self.expand_range_job_input(id, prefix)
else:
assert self._ids_properly_formatted([id], prefix), "Ensure " + prefix + "are properly formatted"
all_ids.append(id) if type(id) != list else all_ids.extend(id) # user input is str, format input is list
return all_ids
def _ids_properly_formatted(self, test_ids, prefix):
"""
Check that all ids in a list are properly formatted, could be:
prefix len = num chars in prefix i.e. "sub-" == 4
num_start:num_start + 2 == expected number, hard coded at 3 e.g. sub-001
sub_id e.g. sub-001, first_4_chars="sub-"
ses_id e.g ses-001, first_4_chars="ses-"
"""
prefix_len = len(prefix)
id_len = len(prefix) + 3
num_start = prefix_len + 1
is_formatted = []
for id in test_ids:
is_formatted.append(len(id) == id_len and id[0:prefix_len] == prefix and id[num_start:num_start+2].isnumeric())
return all(is_formatted)
def expand_range_job_input(self, id_range, prefix):
"""
id_range: specified as list "X:X" e.g. ["1:5"] or single nuimber "!"
prefix: prefix for the info type, e.g. "sub-", "ses-", "run-"
also works for single number input.
"""
if ":" in id_range:
partitioned_ids = id_range.partition(":")
assert len(partitioned_ids) == 3, "ensure input contains only a single : e.g. 1:5"
first_id, __, last_id = partitioned_ids
else:
first_id = last_id = id_range # if it is just a number
ids = [] # cannot use list comp because of prefix var scope (I don't think)
for num in range(int(first_id), int(last_id) + 1):
ids.append(prefix + "{:03}".format(num))
return ids
# Check sessions / runs exist ------------------------------------------------------------------------------------------
def sub_has_ses(self, sub_id, ses_id):
return os.path.isdir(
os.path.join(self.preprocessing_path, sub_id, ses_id))
def ses_has_run(self, sub_id, ses_id, scan_type, nii_or_raw, scan_name, run_id):
run_ids = self.get_all_runs_in_folder(sub_id, ses_id, scan_type, nii_or_raw, scan_name)
return run_id in run_ids
def get_all_runs_in_folder(self, sub_id, ses_id, scan_type, nii_or_raw, scan_name):
scans_dir = os.path.join(self.preprocessing_path, sub_id, ses_id, scan_type, nii_or_raw) # DUPLY
runs_fullpath = glob.glob(scans_dir + "/*" + scan_name)
runs_filenames = [os.path.split(fullpaths)[-1] for fullpaths in runs_fullpath]
run_ids = [filename.split("_")[3] for filename in runs_filenames] # bids means that run num is 4th entry in filename separated by _
return run_ids
def ses_has_at_least_one_scan_name_run(self, sub_id, ses_id, scan_type, nii_or_raw, scan_name):
"""
Check a run of scans has at least oen scan with the scan name e.g.
scan_type = "func"
scan_name = "vaso"
Check there is at least one run with bids foldername ending in vaso if the dir
preprocessing/sub_id/ses_id/scan_type/..
"""
scans_dir = os.path.join(self.preprocessing_path, sub_id, ses_id, scan_type, nii_or_raw)
runs = glob.glob(scans_dir + "/*" + scan_name) # DUPLY
return any(runs)
def get_all_ses_for_sub(self, sub_id):
ses_full_filepaths = glob.glob(os.path.join(self.preprocessing_path, sub_id, "*"))
ses_folders = [os.path.split(filepath)[-1] for filepath in ses_full_filepaths]
ses_folders.reverse()
return ses_folders
# ----------------------------------------------------------------------------------------------------------------------
# Utils - Can move these to dedicated module when large enough
# ----------------------------------------------------------------------------------------------------------------------
def process_args(self):
"""
Process the flags for run_project.py. See project README.md for details on usage.
TODO: can move
"""
parser = argparse.ArgumentParser()
parser.add_argument("-download_from_hpc", "--download_from_hpc",
action="store_true",
help="Flag to download raw scans from HPC to the raw scans folder and format for ABL backups")
parser.add_argument("-move_to_preprocessing", "--move_to_preprocessing",
action="store_true",
help="Flag to copy relevant scan files from raw scans to preprocessing directory, "
"see project configs, 'scan_details' fields for more info. ")
parser.add_argument("-run_recon_all", "--run_recon_all",
action="store_true",
help="Flag to run Freesurfers recon_all command on anatomical scans")
parser.add_argument("-run_dcm2niix", "--run_dcm2niix",
action="store_true",
help="Flag to run dcm2niix on all scans")
args_dict = parser.parse_args()
args = [v for __, v in sorted(vars(args_dict).items())] # sort dict alphabetically, be careful with order if adding new flags
download_from_hpc, move_to_preprocessing, run_dcm2niix, run_recon_all = args # could * expand, but better to be explicit about output order
return download_from_hpc, move_to_preprocessing, run_dcm2niix, run_recon_all
def _get_scan_details_and_expeced_num(self, scan_type):
scans_infos = {"mrs": [self.mrs_scan_details, self.num_expected_mrs_files],
"func": [self.func_scan_details, self.num_expected_func_files],
"anat": [self.anat_scan_details, self.num_expected_anat_files],
"mpm": [self.mpm_scan_details, self.num_expected_anat_files],
"b0": [self.b0_scan_details, self.num_expected_b0_files],
"b1": [self.b1_scan_details, self.num_expected_b1_files],
}
scan_details, expected_num_files = scans_infos[scan_type]
return scan_details, expected_num_files
def _copy_dir_contents(self, source_path, destination_path, log=True):
"""
Call linux os directly to copy files and log the process.
Could not get shutil.copy / copytree to work, "operation not permitted".
"""
self._mkdir(destination_path)
source_path_contents = source_path + "/*"
subprocess.run(" ".join(["cp", source_path_contents, destination_path]),
shell=True)
if log:
self.log(None,
"copied from: {0} \ncopied to: {1}".format(source_path_contents,
destination_path))
def _move(self, dir_to_move, destination_path, move_contents_only=False):
"""
Call linux os directly to move files and log the results.
"""
if move_contents_only:
dir_to_move = dir_to_move + "/*"
subprocess.run([" ".join(["mv", dir_to_move, destination_path])],
shell=True)
self.log(None,
"moved from: " + dir_to_move +
"\nmoved to: " + destination_path)
def _mkdir(self, dir):
if not os.path.isdir(dir):
os.makedirs(dir)
def _get_bids_filename(self, sub_id, ses_id, task_name, run_idx, scan_name):
"""
Return a filename in BIDS format e.g:
sub-002_task-ori_run-1_bold_ptx
"""
run_name = "run-{:03}".format(int(run_idx) + 1)
bids_file_name = "_".join([sub_id, ses_id, "task-" + task_name, run_name, scan_name])
return bids_file_name
def _extract_date_time_from_sub_info_file(self, full_filepath):
"""
Date the date and time from session ses-XXX_info.txt file and return as python datetime
"""
with open(full_filepath, "r") as file:
data = file.read()
search_date = re.search(r"\d{8}", data)
search_time = re.search(r"\d\d:\d\d", data)
combined_date_time = search_date.group() + " " + search_time.group()
scan_datetime = datetime.datetime.strptime(combined_date_time, "%Y%m%d %H:%M")
return scan_datetime
def _glob_one_result(self, search_str):
"""
Return glob checked for only one result - log and error if less or more.
"""
path = glob.glob(search_str)
if len(path) != 1:
error_message =" ".join(["ERROR: less / more than one file found for ",
search_str])
self.log("ERROR",
error_message)
assert False, error_message
return path[0]
# ----------------------------------------------------------------------------------------------------------------------
# Tests
# ----------------------------------------------------------------------------------------------------------------------
def _test_participant_log(self, participant_log): # TODO: why not on class attribute?
"""
Test all entries on the participant are correct format
"""
self._test_no_scan_id_are_duplicate(participant_log)
for wbic_id in participant_log.keys():
assert wbic_id.isnumeric(), "WBIC ID must be all numbers for wbic_id: " + wbic_id
assert len(wbic_id) == 5, "WBIC ID must be 5 digits for wbic_id: " + wbic_id
sub_id = participant_log[wbic_id]["sub_id"]
assert len(sub_id) == 7, "sub ID is too long (should be sub-XXX) for wbic_id: " + wbic_id
assert sub_id[0:4] == "sub-", "sub_id does not start with 'sub-' for wbic_id: " + wbic_id
assert sub_id[4:7].isnumeric(), "sub_id must end in three numbers for wbic_id: " + wbic_id
lab_id = participant_log[wbic_id]["lab_id"]
assert len(lab_id), "lab_id is not 4 digits for wbic_id: " + wbic_id
assert lab_id.isnumeric(), "lab_id is not intergers for wbic_id: " + wbic_id
for scan_info in participant_log[wbic_id]["scans"].values():
zk_id = scan_info["zk_id"]
assert len(zk_id) == 10, "zk_id is not 10 numbers / letters long for wbic_id: {0}, zk_id {1}".format(wbic_id,
zk_id)
assert zk_id[7:10].isnumeric(), "zk id does not end in 3 numbers for wbic_id: {0}, zk_id {1}".format(wbic_id,
zk_id)
pattern = re.compile("zk\d\dw[3, 7]_\d\d\d")
assert pattern.match(zk_id), "zk_id is in the wrong format for wbic_id: {0}, zk_id {1}".format(wbic_id,
zk_id)
ses_id = scan_info["ses_id"] # TODO: duplicated from new method ##############################################################################
assert ses_id[0:4] == "ses-", "ses id does not begin 'ses-' for wbic_id: {0}, zk_id {1}".format(wbic_id,
zk_id)
assert ses_id[4:7].isnumeric(), "last three digits for ses_id are not numeric for for wbic_id: {0}, zk_id {1}".format(wbic_id,
zk_id)