def fw_heudiconv_download(): """Use fw-heudiconv to download BIDS data. Returns True if success or False if there are no dwi files.""" subjects = [subject_container.label] if not use_all_sessions: # find session object origin sessions = [session_container.label] else: sessions = None # Do the download! bids_root.parent.mkdir(parents=True, exist_ok=True) downloads = export.gather_bids(fw, project_label, subjects, sessions) export.download_bids(fw, downloads, str(bids_dir.resolve()), dry_run=False) # Download the extra T1w or T2w if extra_t1 is not None: get_external_bids(extra_t1, extra_t1_path) if extra_t2 is not None: get_external_bids(extra_t2, extra_t2_path) dwi_files = [fname for fname in bids_root.glob("**/*") if "dwi/" in str(fname)] if not len(dwi_files): logger.warning("No DWI files found in %s", bids_root) return False return True
logger.info("Dry run: {}".format(dry_run)) # action if action == "Curate": curate.convert_to_bids(fw, project_label, heuristic, subjects, sessions, dry_run=dry_run) elif action == "Export": downloads = export.gather_bids(fw, project_label, subjects, sessions) export.download_bids(fw, downloads, "/flywheel/v0/input", dry_run=dry_run) if not dry_run: pass # tidy up # output_dir = "/flywheel/v0/output" # os.system("zip -r {}_BIDSexport.zip output/*".format(destination['id'])) # os.system("mv *.zip output") # to_remove = os.listdir(output_dir) # to_remove = ["{}/{}".format(output_dir, x) for x in to_remove if ".zip" not in x] # for x in to_remove: # if os.path.isfile(x): # os.remove(x) # else: # shutil.rmtree(x)
subject_container = fw.get(session_container.parents['subject']) subjects = [subject_container.label] else: sessions = None subjects = None # logging stuff logger.info("Running fw-heudiconv with the following settings:") logger.info("Project: {}".format(project_label)) logger.info("Subject(s): {}".format(subjects)) logger.info("Session(s): {}".format(sessions)) logger.info("Action: {}".format(action)) logger.info("Dry run: {}".format(dry_run)) # action if action == "Curate": print("This gear does not do BIDS Curation!") elif action == "Export": downloads = export.gather_bids(fw, project_label, subjects, sessions) export.download_bids(fw, downloads, "/flywheel/v0/input", dry_run=dry_run, folders_to_download=['anat', 'func']) if not dry_run: pass elif action == "Tabulate": print("This gear does not do BIDS Tabulation!") else: raise Exception('Action not specified correctly!')
def fw_heudiconv_download(): """Use fw-heudiconv to download BIDS data.""" subjects = [subject_container.label] sessions = [session_container.label] # Use manually specified T1 if it exists if manual_t1 is not None: anat_input = manual_t1_path subject_label = subject_container.label.replace('_', 'x').replace(' ', 'x') session_label = session_container.label.replace('_', 'x').replace(' ', 'x') prefix = 'sub-{}_ses-{}_'.format(subject_label, session_label) return True, anat_input, prefix # Do the download! bids_root.parent.mkdir(parents=True, exist_ok=True) downloads = export.gather_bids(fw, project_label, subjects, sessions) export.download_bids(fw, downloads, str(bids_dir.resolve()), dry_run=False, folders_to_download=['anat']) bids.config.set_option('extension_initial_dot', True) # suppress warning layout = bids.BIDSLayout(bids_root) filters = {} if bids_filter_path: logger.info("Using {} as BIDS filter.".format(bids_filter_path)) with open(bids_filter_path) as f: data = json.load(f) try: filters = data["t1w"] except KeyError as ke: print(ke) logger.warning("Could not find 't1w' field in json.") logger.info("BIDS filter file not formatted correctly.") return False else: if bids_sub: filters["subject"] = [bids_sub] else: filters["subject"] = subjects if bids_ses: filters["session"] = [bids_ses] else: filters["session"] = sessions if bids_acq: filters["acquisition"] = bids_acq if bids_run: filters["run"] = bids_run anat_list = layout.get(return_type='file', suffix='T1w', extension=['.nii', '.nii.gz'], **filters) # if there are multiple files or no files, error out # otherwise just use the one if len(anat_list) > 1: logger.warning( "Multiple anatomical files found in %s. If you want to process multiple images, use the longitudinal gear.", bids_root) return False elif not len(anat_list) or len(anat_list) == 0: logger.warning("No anatomical files found in %s", bids_root) return False else: anat_input = anat_list[0] logger.info("Using {} as input anatomical image.".format(anat_input)) # Generate prefix from bids layout basename = os.path.basename(anat_input).split('.')[0] prefix = basename.replace('_T1w', '') + '_' return True, anat_input, prefix
def fw_heudiconv_download(): """Use fw-heudiconv to download BIDS data.""" subjects = [subject_container.label] sessions = [session_container.label] # Use manually specified T1 if it exists if manual_t1 is not None: anat_input = manual_t1_path subject_label = subject_container.label.replace('_', 'x') session_label = session_container.label.replace('_', 'x') prefix = 'sub-{}_ses-{}_'.format(subject_label, session_label) return True, anat_input, prefix # Do the download! bids_root.parent.mkdir(parents=True, exist_ok=True) downloads = export.gather_bids(fw, project_label, subjects, sessions) export.download_bids(fw, downloads, str(bids_dir.resolve()), dry_run=False, folders_to_download=['anat']) layout = BIDSLayout(bids_root) # Get subject and session label # subject_label = layout.get(return_type='id', target='subject')[0].strip("[']") # session_label = layout.get(return_type='id', target='session')[0].strip("[']") filters = {} if bids_sub: filters["subject"] = [bids_sub] else: filters["subject"] = subjects if bids_ses: filters["session"] = [bids_ses] else: filters["session"] = sessions if bids_acq: filters["acquisition"] = bids_acq if bids_run: filters["run"] = bids_run anat_list = layout.get(return_type='file', extension=['.nii', '.nii.gz'], **filters) # if there are multiple files or no files, error out # otherwise just use the one if len(anat_list) > 1: logger.warning( "Multiple anatomical files found in %s. If you want to process multiple images, use the longtidunal gear.", bids_root) return False elif not len(anat_list) or len(anat_list) == 0: logger.warning("No anatomical files found in %s", bids_root) return False else: anat_input = anat_list[0] logger.info("Using {} as input anatomical image.".format(anat_input)) # Generate prefix from bids layout basename = os.path.basename(anat_input).split('.')[0] prefix = basename.replace('_T1w', '') + '_' return True, anat_input, prefix