diff --git a/example1/resources/diffusion/NODDI.bval b/example1/resources/diffusion/NODDI.bval new file mode 100644 index 0000000000000000000000000000000000000000..b2e286ce85d8f6d04ed86d64b1d9ecb8f0064d7e --- /dev/null +++ b/example1/resources/diffusion/NODDI.bval @@ -0,0 +1 @@ +0 0 2000 2000 1000 1000 diff --git a/example1/resources/diffusion/NODDI.bvec b/example1/resources/diffusion/NODDI.bvec new file mode 100644 index 0000000000000000000000000000000000000000..22f1e8093c8d5d491252e2f30b4cf473ce4fea25 --- /dev/null +++ b/example1/resources/diffusion/NODDI.bvec @@ -0,0 +1,3 @@ +0 0 0.021828 -0.015425 -0.70918 -0.2465 +0 0 0.80242 0.22098 -0.00063106 0.1043 +0 0 -0.59636 0.97516 -0.70503 -0.96351 diff --git a/example1/resources/plugins/definitions.py b/example1/resources/plugins/definitions.py index 385f752491acc982a8bb5a7d3e7567513e06a65a..7b30193ed84785ac7fe46965a59801ec641e0e7c 100644 --- a/example1/resources/plugins/definitions.py +++ b/example1/resources/plugins/definitions.py @@ -1,8 +1,20 @@ import logging import os + +# defined this way, log messages will be formatted correctly +# and appear with this file-name logger = logging.getLogger(__name__) +# path to the root folder of plugin +# (bidscoin_example/example1/resources) +# usefull to retrieve auxiliary files +plugin_root = os.path.normpath( + os.path.join(os.path.dirname(__file__), "..")) + +# definition of correct session names and sequences +# usefull to check the completness and/or correct emplacement +# of scans Series = { "ses-LCL": ('localizer', 'cmrr_mbep2d_bold_mb2_invertpe', diff --git a/example1/resources/plugins/process_plugin.py b/example1/resources/plugins/process_plugin.py new file mode 100644 index 0000000000000000000000000000000000000000..207bec01c7a25e828e9fb01431a9983b86588041 --- /dev/null +++ b/example1/resources/plugins/process_plugin.py @@ -0,0 +1,171 @@ +import os +import shutil +import logging +import random + +from definitions import checkSeries, plugin_root + +logger = logging.getLogger(__name__) + +# global variables +rawfolder = "" +bidsfolder = "" +dry_run = False + +participants_table = None +rec_path = "" +countSeries = {} + + +def InitEP(source: str, destination: str, dry: bool) -> int: + global rawfolder + global bidsfolder + global dry_run + + rawfolder = source + bidsfolder = destination + dry_run = dry + + +def SubjectEP(scan): + scan.sub_values["handiness"] = random.choice([0, 1]) + + +def SessionEP(scan): + global series + global sid + sub = scan.subject + ses = scan.session + path = os.path.join(rawfolder, + sub, ses, + "MRI") + series = sorted(os.listdir(path)) + series = [s.split("-", 1)[1] for s in series] + sid = -1 + checkSeries(path, sub, ses, False) + # copytng behevioral data + aux_input = os.path.join(rawfolder, sub, ses, "auxiliary") + if ses in ("ses-LCL", "ses-HCL"): + if not os.path.isdir(aux_input): + logger.error("Session {}/{} do not contain auxiliary folder" + .format(sub, ses)) + return -1 + for old, new in (("FCsepNBack.tsv", "task-rest_events.tsv"), + ("FCsepNBack.json", "task-rest_events.json"), + ("VAS.tsv", "task-rest_beh.tsv"), + ("VAS.json", "task-rest_beh.json")): + source = "{}/{}".format(aux_input, old) + if not os.path.isfile(source): + logger.error("{}/{}: File {} not found" + .format(sub, ses, source)) + + +series = list() +sid = -1 +Intended = "" + + +def SequenceEP(recording): + global series + global sid + global Intended + Intended = "" + sid += 1 + recid = series[sid] + if recid != recording.recId(): + logger.warning("{}: Id mismatch folder {}" + .format(recording.recIdentity(False), + recid)) + if recid == "cmrr_mbep2d_bold_mb2_invertpe": + mod = series[sid + 1] + if mod.endswith("cmrr_mbep2d_bold_mb2_task_fat"): + Intended = "nBack" + elif mod.endswith("cmrr_mbep2d_bold_mb2_task_nfat"): + Intended = "nBack" + elif mod.endswith("cmrr_mbep2d_bold_mb2_rest"): + Intended = "rest" + else: + Intended = "invalid" + logger.warning("{}: Unknown session {}" + .format(recording.recIdentity(), + mod)) + elif recid == "gre_field_mapping": + if recording.sesId() in ("ses-HCL", "ses-LCL"): + Intended = "HCL/LCL" + elif recording.sesId() == "ses-STROOP": + Intended = "STROOP" + else: + logger.warning("{}: Unknown session {}" + .format(recording.recIdentity(), + recording.sesId())) + Intended = "invalid" + elif recid == "al_mtflash3d_sensArray": + det = series[sid + 2] + if det.endswith("al_mtflash3d_PDw"): + Intended = "PDw" + elif det.endswith("al_mtflash3d_T1w"): + Intended = "T1w" + recording.setAttribute("Intended", "T1w") + elif det.endswith("al_mtflash3d_MTw"): + Intended = "MTw" + else: + logger.warning("{}: Unable determine modality" + .format(recording.recIdentity())) + Intended = "invalid" + elif recid == "al_mtflash3d_sensBody": + det = series[sid + 1] + if det.endswith("al_mtflash3d_PDw"): + Intended = "PDw" + elif det.endswith("al_mtflash3d_T1w"): + Intended = "T1w" + elif det.endswith("al_mtflash3d_MTw"): + Intended = "MTw" + else: + logger.warning("{}: Unable determine modality" + .format(recording.recIdentity())) + Intended = "invalid" + + +def RecordingEP(recording): + if Intended != "": + recording.setAttribute("SeriesDescription", Intended) + + +def SequenceEndEP(outfolder, recording): + """ + Simulates 3D to 4D images conversion + """ + modality = recording.Modality() + + if modality not in ("func", "dwi"): + return + + f4D = os.path.join(outfolder, "4D") + if not os.path.isfile(f4D + ".nii"): + logger.info("{}: Converting {} MRI to 4D" + .format(recording.recIdentity(index=False), + modality)) + first_file = os.path.join(outfolder, recording.files[0]) + shutil.copy2(first_file, f4D + ".nii") + first_file = os.path.splitext(first_file)[0] + ".json" + shutil.copy2(first_file, f4D + ".json") + + # copying fake bval and bvec values + # during the bidsifications these files will + # be automatically picked up + if modality == "dwi": + shutil.copy2(os.path.join(plugin_root, + "diffusion", + "NODDI.bval"), + os.path.join(outfolder, + "4D.bval")) + shutil.copy2(os.path.join(plugin_root, + "diffusion", + "NODDI.bvec"), + os.path.join(outfolder, + "4D.bvec")) + for f_nii in recording.files: + f_nii = os.path.join(outfolder, f_nii) + f_json = os.path.splitext(f_nii)[0] + ".json" + os.remove(f_nii) + os.remove(f_json)