def test_load_heuristics(): test_heuristic = 'heur_test_acq' heuristic_output_filename = utils.load_heuristic(test_heuristic).filename assert test_heuristic in heuristic_output_filename with raises(Exception) as errorinfo: utils.load_heuristic('') assert 'Failed to import heuristic' in str(errorinfo.value)
def use_heuristic(heur_file, sub, ses, filename, outdir, record_label=''): utils.check_file_exists(heur_file) """ Import the heuristic file specified by the user and uses its output to rename the file. Input ----- heur_file: path Fullpath to heuristic file. sub: str or int Name of subject. ses: str or int or None Name of session. filename: path Name of the input of phys2bids. outdir: str or path Path to the directory that will become the "site" folder ("root" folder of BIDS database). record_label: str Optional label for the "record" entry of BIDS. Output ------- heurpath: str or path Returned fullpath to tsv.gz new file (post BIDS formatting). """ if sub[:4] != 'sub-': name = 'sub-' + sub else: name = sub fldr = outdir + '/' + name if ses: if ses[:4] != 'ses-': fldr = fldr + '/ses-' + ses name = name + '_ses-' + ses else: fldr = fldr + '/' + ses name = name + ses fldr = fldr + '/func' utils.path_exists_or_make_it(fldr) cwd = os.getcwd() os.chdir(outdir) heur = utils.load_heuristic(heur_file) name = heur.heur(Path(filename).stem, name) recording = '' if record_label: recording = f'_recording-{record_label}' heurpath = fldr + '/' + name + recording + '_physio' # for ext in ['.tsv.gz', '.json', '.log']: # move_file(outfile, heurpath, ext) os.chdir(cwd) return heurpath
def use_heuristic(heur_file, sub, ses, filename, outdir, run='', record_label=''): """ Import and use the heuristic specified by the user to rename the file. Parameters ---------- heur_file: path Fullpath to heuristic file. sub: str or int Name of subject. ses: str or int or None Name of session. filename: path Name of the input of phys2bids. outdir: str or path Path to the directory that will become the "site" folder ("root" folder of BIDS database). record_label: str Optional label for the "record" entry of BIDS. Returns ------- heurpath: str or path Returned fullpath to tsv.gz new file (post BIDS formatting). Raises ------ KeyError if `bids_keys['task']` is empty """ utils.check_file_exists(heur_file) # Initialise a dictionary of bids_keys that has already "recording" bids_keys = {'sub': '', 'ses': '', 'task': '', 'acq': '', 'ce': '', 'dir': '', 'rec': '', 'run': run, 'recording': record_label} # Start filling bids_keys dictionary and path with subject and session if sub.startswith('sub-'): bids_keys['sub'] = sub[4:] fldr = os.path.join(outdir, sub) else: bids_keys['sub'] = sub fldr = os.path.join(outdir, f'sub-{sub}') if ses: if ses.startswith('ses-'): bids_keys['ses'] = ses[4:] fldr = os.path.join(fldr, ses) else: bids_keys['ses'] = ses fldr = os.path.join(fldr, f'ses-{ses}') # Load heuristic and use it to fill dictionary heur = utils.load_heuristic(heur_file) bids_keys.update(heur.heur(Path(filename).stem, run)) # If bids_keys['task'] is still empty, stop the program if not bids_keys['task']: LGR.warning(f'The heuristic {heur_file} could not deal with' f'{Path(filename).stem}') raise KeyError('No "task" attribute found') # Compose name by looping in the bids_keys dictionary # and adding nonempty keys name = '' for key in bids_keys: if bids_keys[key] != '': name = f'{name}{key}-{bids_keys[key]}_' # Finish path, create it, add filename, export fldr = os.path.join(fldr, 'func') os.makedirs(fldr, exist_ok=True) heurpath = os.path.join(fldr, f'{name}physio') return heurpath
def test_load_heuristics(): test_heuristic = 'heur_test_acq' heuristic_output_filename = utils.load_heuristic(test_heuristic).filename assert test_heuristic in heuristic_output_filename