Skip to content

Commit

Permalink
Update to match neurodatascience/nipoppy:main (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
michellewang authored Feb 29, 2024
2 parents 8a9e50b + f438b73 commit f7a7d8d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
34 changes: 29 additions & 5 deletions nipoppy/workflow/proc_pipe/fmriprep/run_fmriprep.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python
import argparse
import json
import subprocess
import sys
import os
from pathlib import Path
import nipoppy.workflow.logger as my_logger
Expand All @@ -16,6 +18,7 @@
SINGULARITY_FS_DIR = "/fsdir/"
SINGULARITY_TEMPLATEFLOW_DIR = "/templateflow"
SINGULARITY_FS_LICENSE = "/fsdir/license.txt"
DNAME_BIDS_DB = 'bids_db_fmriprep'
os.environ['SINGULARITYENV_SUBJECTS_DIR'] = SINGULARITY_FS_DIR
os.environ['SINGULARITYENV_FS_LICENSE'] = SINGULARITY_FS_LICENSE
os.environ['SINGULARITYENV_TEMPLATEFLOW_HOME'] = SINGULARITY_TEMPLATEFLOW_DIR
Expand All @@ -31,15 +34,31 @@ def run_fmriprep(participant_id: str,
SINGULARITY_CONTAINER: str,
use_bids_filter: bool,
anat_only: bool,
logger: logging.Logger):
logger: logging.Logger,
regenerate_bids_db: bool = False):
""" Launch fmriprep container"""

fmriprep_out_dir = f"{fmriprep_dir}/output/"
fmriprep_home_dir = f"{fmriprep_out_dir}/fmriprep_home_{participant_id}/"
Path(f"{fmriprep_home_dir}").mkdir(parents=True, exist_ok=True)

# BIDS DB created for fmriprep by run_nipoppy.py
bids_db_dir = f"/fmripre_proc/bids_db_fmriprep"
bids_db_dir = f"/fmripre_proc/{DNAME_BIDS_DB}"

bids_db_dir_outside_container = f"{proc_dir}/{DNAME_BIDS_DB}"
if not Path(bids_db_dir_outside_container).exists():
logger.warning(f"Creating the BIDS database directory because it does not exist: {bids_db_dir_outside_container}")
Path(bids_db_dir_outside_container).mkdir(parents=True, exist_ok=True)
else:
logger.info(f"Using existing BIDS database directory: {bids_db_dir_outside_container}")

if regenerate_bids_db:
for path in Path(bids_db_dir_outside_container).glob("*"):
if path.is_file():
logger.warning(f"Removing existing BIDS database file: {path}")
path.unlink()
else:
logger.error(f"Expected to find only files in {bids_db_dir_outside_container} but found directory {path}")
sys.exit(1)

# Singularity CMD
SINGULARITY_CMD=f"singularity run \
Expand Down Expand Up @@ -101,6 +120,7 @@ def run(participant_id: str,
output_dir: str,
use_bids_filter: bool,
anat_only: bool,
regenerate_bids_db: bool = False,
logger=None):
""" Runs fmriprep command
"""
Expand Down Expand Up @@ -156,7 +176,9 @@ def run(participant_id: str,
SINGULARITY_FMRIPREP,
use_bids_filter,
anat_only,
logger)
logger,
regenerate_bids_db=regenerate_bids_db,
)

if __name__ == '__main__':
# argparse
Expand All @@ -173,6 +195,7 @@ def run(participant_id: str,
help='specify custom output dir (if None --> <DATASET_ROOT>/derivatives)')
parser.add_argument('--use_bids_filter', action='store_true', help='use bids filter or not')
parser.add_argument('--anat_only', action='store_true', help='run only anatomical workflow or not')
parser.add_argument('--regenerate_bids_db', action='store_true', help='regenerate PyBIDS database (default: use existing if available)')

args = parser.parse_args()

Expand All @@ -182,9 +205,10 @@ def run(participant_id: str,
output_dir = args.output_dir # Needed on BIC (QPN) due to weird permissions issues with mkdir
use_bids_filter = args.use_bids_filter
anat_only = args.anat_only
regenerate_bids_db = args.regenerate_bids_db

# Read global configs
with open(global_config_file, 'r') as f:
global_configs = json.load(f)

run(participant_id, global_configs, session_id, output_dir, use_bids_filter, anat_only)
run(participant_id, global_configs, session_id, output_dir, use_bids_filter, anat_only, regenerate_bids_db=regenerate_bids_db)
37 changes: 28 additions & 9 deletions nipoppy/workflow/proc_pipe/mriqc/run_mriqc.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env python
import subprocess
import sys
import argparse
import json
import nipoppy.workflow.logger as my_logger
from pathlib import Path
import os

DNAME_BIDS_DB = 'bids_db_mriqc'
SINGULARITY_TEMPLATEFLOW_DIR = "/templateflow"
os.environ['SINGULARITYENV_TEMPLATEFLOW_HOME'] = SINGULARITY_TEMPLATEFLOW_DIR

def run(participant_id, global_configs, session_id, output_dir, modalities, bids_db_dir=None, logger=None):
def run(participant_id, global_configs, session_id, output_dir, modalities, logger=None, regenerate_bids_db=False):
""" Runs mriqc command
"""
DATASET_ROOT = global_configs["DATASET_ROOT"]
Expand All @@ -28,10 +31,24 @@ def run(participant_id, global_configs, session_id, output_dir, modalities, bids
log_file = f"{log_dir}/mriqc.log"
logger = my_logger.get_logger(log_file)

if bids_db_dir is None:
bids_db_dir = f"/mriqc_proc/bids_db_mriqc"

logger.info(f"bids_db_dir: {bids_db_dir}")
# path inside the container
bids_db_dir = f"/mriqc_proc/{DNAME_BIDS_DB}"

bids_db_dir_outside_container = f"{proc_dir}/{DNAME_BIDS_DB}"
if not Path(bids_db_dir_outside_container).exists():
logger.warning(f"Creating the BIDS database directory because it does not exist: {bids_db_dir_outside_container}")
Path(bids_db_dir_outside_container).mkdir(parents=True, exist_ok=True)
else:
logger.info(f"Using existing BIDS database directory: {bids_db_dir_outside_container}")

if regenerate_bids_db:
for path in Path(bids_db_dir_outside_container).glob("*"):
if path.is_file():
logger.warning(f"Removing existing BIDS database file: {path}")
path.unlink()
else:
logger.error(f"Expected to find only files in {bids_db_dir_outside_container} but found directory {path}")
sys.exit(1)

if output_dir is None:
output_dir = f"{DATASET_ROOT}/derivatives"
Expand All @@ -57,6 +74,7 @@ def run(participant_id, global_configs, session_id, output_dir, modalities, bids
-B {mriqc_output_dir}:/out \
-B {mriqc_work_dir}:/work \
-B {TEMPLATEFLOW_DIR}:{SINGULARITY_TEMPLATEFLOW_DIR} \
--cleanenv \
{SINGULARITY_CONTAINER} "

# Compose mriqc command
Expand All @@ -68,7 +86,7 @@ def run(participant_id, global_configs, session_id, output_dir, modalities, bids
--no-sub \
--work-dir /work \
--bids-database-dir {bids_db_dir}"
# --bids-database-wipe" # wiping and regerating bids db with catalog.py
# --bids-database-wipe" # wiping and regenerating bids db with catalog.py

CMD_ARGS = SINGULARITY_CMD + MRIQC_CMD
CMD = CMD_ARGS.split()
Expand Down Expand Up @@ -102,15 +120,16 @@ def run(participant_id, global_configs, session_id, output_dir, modalities, bids
parser.add_argument('--session_id', type=str, required=True, help='session id for the participant')
parser.add_argument('--output_dir', type=str, default=None, help='specify custom output dir (if None, output is saved at <DATASET_ROOT>/derivatives/mriqc)')
parser.add_argument('--modalities', nargs="*", required=True, help='specify modalities (i.e. suffixes) to QC. Possible options: T1w, T2w, bold, dwi')
parser.add_argument('--bids_db_path', type=str, default=None, help='path pybids layout db')
parser.add_argument('--regenerate_bids_db', action='store_true', help='regenerate PyBIDS database (default: use existing if available)')

args = parser.parse_args()

global_config_file = args.global_config
participant_id = args.participant_id
session_id = args.session_id
output_dir = args.output_dir
modalities = args.modalities
bids_db_path = args.bids_db_path
regenerate_bids_db = args.regenerate_bids_db

# Read global configs
with open(global_config_file, 'r') as f:
Expand All @@ -120,4 +139,4 @@ def run(participant_id, global_configs, session_id, output_dir, modalities, bids
participant_id = args.participant_id
session_id = args.session_id

run(participant_id, global_configs, session_id, output_dir, modalities, bids_db_path)
run(participant_id, global_configs, session_id, output_dir, modalities, regenerate_bids_db=regenerate_bids_db)

0 comments on commit f7a7d8d

Please sign in to comment.