|
| 1 | +""" |
| 2 | +Main entry point |
| 3 | +""" |
| 4 | +from pathlib import Path |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import gzip |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import nibabel as nb |
| 11 | + |
| 12 | +from pyretroicor.retroicor import retroicor_cardiac, retroicor_respiratory |
| 13 | +from pyretroicor.prepro import process_resp, process_cardiac |
| 14 | + |
| 15 | +def main(): |
| 16 | + parser = argparse.ArgumentParser() |
| 17 | + |
| 18 | + parser.add_argument( |
| 19 | + 'filename', metavar='path', |
| 20 | + help="Path to fMRI nifti files." |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + 'physio', metavar='physio', |
| 24 | + help="Path to physiology files in BIDS standard. \ |
| 25 | + Accept cardiac and/or respiratory recording." |
| 26 | + ) |
| 27 | + parser.add_argument( |
| 28 | + 'output', metavar='output', |
| 29 | + help="Path to save calculated derivatives." |
| 30 | + ) |
| 31 | + |
| 32 | + args = parser.parse_args() |
| 33 | + NII_FILE = args.filename |
| 34 | + PHYSIO_FILE = args.physio |
| 35 | + OUTDIR = args.output |
| 36 | + |
| 37 | + # check all files neesed exist: nifti, physio, accompanied jsons |
| 38 | + physio_json = PHYSIO_FILE.split(".tsv.gz")[0] + ".json" |
| 39 | + func_json = NII_FILE.split(".nii.gz")[0] + ".json" |
| 40 | + with open(func_json) as f: |
| 41 | + func_meta = json.load(f) |
| 42 | + |
| 43 | + if not Path(physio_json).is_file(): |
| 44 | + raise ValueError("Missing json file for physiology data. Is data in BIDS format?") |
| 45 | + else: |
| 46 | + with open(physio_json) as f: |
| 47 | + physio_meta = json.load(f) |
| 48 | + |
| 49 | + sampling_freq = physio_meta["SamplingFrequency"] |
| 50 | + start_time = physio_meta["StartTime"] |
| 51 | + columns = physio_meta["Columns"] |
| 52 | + |
| 53 | + |
| 54 | + func = nb.load(NII_FILE) # nibabel should do the test itself |
| 55 | + physio = np.loadtxt(PHYSIO_FILE) |
| 56 | + # create time |
| 57 | + time = np.arange(0, physio.shape[0]) * (1 / sampling_freq) + start_time |
| 58 | + if start_time < 0: |
| 59 | + time_zero = np.where(time < 0)[0][-1] + 1 |
| 60 | + time = time[time_zero:] |
| 61 | + physio = physio[time_zero:, :] |
| 62 | + |
| 63 | + cardiac = physio[:, columns.index("cardiac")] |
| 64 | + respiratory = physio[:, columns.index("respiratory")] |
| 65 | + |
| 66 | + # TR labels |
| 67 | + n_vol = func.shape[0] |
| 68 | + tr = func_meta["RepetitionTime"] |
| 69 | + tr_time = np.arange(1, n_vol + 1) * tr |
| 70 | + tr_idx = [] |
| 71 | + for t in tr_time: |
| 72 | + idx = np.where(time < t)[0][-1] + 1 |
| 73 | + tr_idx.append(idx) |
| 74 | + |
| 75 | + # peak detection and cleaning |
| 76 | + r_peak = process_cardiac(cardiac, sampling_freq) # r peak index |
| 77 | + r_peak = time[r_peak] # r peak time point |
| 78 | + retro_card = retroicor_cardiac(time, r_peak) |
| 79 | + |
| 80 | + # run retroicor |
| 81 | + respiratory = process_resp(respiratory, sampling_freq) |
| 82 | + retro_resp = retroicor_respiratory(respiratory) |
| 83 | + # rextract value per TR |
| 84 | + retroicor_regressors = np.hstack((retro_card, retro_resp))[tr_idx, :] |
| 85 | + |
| 86 | + # create output dir |
| 87 | + nii_file = Path(NII_FILE) |
| 88 | + subject = nii_file.name.split("_")[0].split("sub-")[-1] |
| 89 | + filename_root = nii_file.name.split("_bold")[0] |
| 90 | + if "ses" in NII_FILE: |
| 91 | + session = nii_file.name.split("_")[1].split("ses-")[-1] |
| 92 | + filepath = Path(OUTDIR) / "pyretroicor" / f"sub-{subject}" / f"ses-{session}" |
| 93 | + out_name = f"{filename_root}_desc-retroicor_regressors.tsv" |
| 94 | + else: |
| 95 | + filepath = Path(OUTDIR) / "pyretroicor" / f"sub-{subject}" |
| 96 | + out_name = f"{filename_root}_desc-retroicor_regressors.tsv" |
| 97 | + |
| 98 | + filepath.mkdir(parents=True, exist_ok=True) |
| 99 | + |
| 100 | + # save results as tsv |
| 101 | + header = [f"retroicor_cardiac_{i + 1}" for i in range(6)] \ |
| 102 | + + [f"retroicor_respiration_{i + 1}" for i in range(6)] |
| 103 | + |
| 104 | + np.savetxt(filepath / out_name, retroicor_regressors, |
| 105 | + delimiter="\t", header="\t".join(header), |
| 106 | + comments="") |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments