From 550f1e12f2ad0aff9aff64285b07b6028bd0d7b3 Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 18 Jun 2019 11:28:49 -0500 Subject: [PATCH 1/2] add --overwrite-nii --- xnat_downloader/cli/run.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/xnat_downloader/cli/run.py b/xnat_downloader/cli/run.py index 18b38a6..f7179e1 100755 --- a/xnat_downloader/cli/run.py +++ b/xnat_downloader/cli/run.py @@ -97,6 +97,8 @@ def parse_cmdline(): parser.add_argument('--scan-non-fmt', action="store_true", help="if subject and session use BIDS formatting, " "but the scans do not") + parser.add_argument('--overwrite-nii', action='store_true', + help='overwrite the nifti file if it exists') # Required arguments required_args = parser.add_argument_group('Required arguments') required_args.add_argument('-i', '--input_json', @@ -268,7 +270,8 @@ def get_scans(self, ses_label, scan_labels=None): self.scan_dict[key] = scan_obj def download_scan_unformatted(self, scan, dest, scan_repl_dict, bids_num_len, - sub_repl_dict=None, sub_label_prefix=None): + sub_repl_dict=None, sub_label_prefix=None, + overwrite_nii=False): """ Downloads a particular scan session @@ -281,9 +284,17 @@ def download_scan_unformatted(self, scan, dest, scan_repl_dict, bids_num_len, Directory where the zip file will be saved. The actual dicoms will be saved under the general scheme /scans//resources/DICOM/files - scan_dict: dictionary + bids_num_len: int + the number of integers to use to represent the subject label + scan_repl_dict: dict Dictionary containing terms to match the scan name on xnat with the reproin name of the scan + sub_label_prefix: string + prefix to add to the subject label (e.g. "AMBI") + sub_repl_dict: dict + dictionary to change the subject label based on its representation on xnat + overwrite_nii: bool + overwrite the output nifti file if it already exists """ from glob import glob if scan not in self.scan_dict.keys(): @@ -405,12 +416,13 @@ def download_scan_unformatted(self, scan, dest, scan_repl_dict, bids_num_len, fname=fname, dcm_dir=dcm_dir) bids_outfile = os.path.join(bids_dir, fname + '.nii.gz') - if not os.path.exists(bids_outfile): + if not os.path.exists(bids_outfile) or overwrite_nii: call(dcm2niix, shell=True) else: print('It appears the nifti file already exists for {scan}'.format(scan=scan)) - def download_scan(self, scan, dest, sub_label_prefix=None, scan_repl_dict=None): + def download_scan(self, scan, dest, sub_label_prefix=None, scan_repl_dict=None, + overwrite_nii=False): """ Downloads a particular scan session @@ -430,6 +442,8 @@ def download_scan(self, scan, dest, sub_label_prefix=None, scan_repl_dict=None): the scan names may not be (e.g. PU: anat-T1w). This dictionary converts the scan names to their BIDS formatted counterparts. (e.g. "PU: anat-T1w" -> "anat-T1w_rec-pu") + overwrite_nii: bool + overwrite the output nifti file if it already exists """ from glob import glob if scan not in self.scan_dict.keys(): @@ -560,7 +574,7 @@ def download_scan(self, scan, dest, sub_label_prefix=None, scan_repl_dict=None): fname=fname, dcm_dir=dcm_dir) bids_outfile = os.path.join(bids_dir, fname + '.nii.gz') - if not os.path.exists(bids_outfile): + if not os.path.exists(bids_outfile) or overwrite_nii: call(dcm2niix, shell=True) else: print('It appears the nifti file already exists for {scan}'.format(scan=scan)) @@ -641,7 +655,8 @@ def main(): for scan in sub_class.scan_dict.keys(): # download the scan if scan_repl_dict and opts.scan_non_fmt: - sub_class.download_scan(scan, dest, sub_label_prefix, scan_repl_dict) + sub_class.download_scan(scan, dest, sub_label_prefix, + scan_repl_dict, overwrite_nii=opts.overwrite_nii) elif scan_repl_dict: sub_class.download_scan_unformatted(scan, dest, scan_repl_dict, bids_num_len, sub_repl_dict, From 456a4f8d350abaaeca9a17e64698871c09354b58 Mon Sep 17 00:00:00 2001 From: James Kent Date: Tue, 18 Jun 2019 11:31:51 -0500 Subject: [PATCH 2/2] pass --overwrite-nii option into class functions --- xnat_downloader/cli/run.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/xnat_downloader/cli/run.py b/xnat_downloader/cli/run.py index f7179e1..360efc0 100755 --- a/xnat_downloader/cli/run.py +++ b/xnat_downloader/cli/run.py @@ -660,9 +660,11 @@ def main(): elif scan_repl_dict: sub_class.download_scan_unformatted(scan, dest, scan_repl_dict, bids_num_len, sub_repl_dict, - sub_label_prefix) + sub_label_prefix, + overwrite_nii=opts.overwrite_nii) else: - sub_class.download_scan(scan, dest, sub_label_prefix) + sub_class.download_scan(scan, dest, sub_label_prefix, + overwrite_nii=opts.overwrite_nii) if __name__ == "__main__":