Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] babs-init runs without babs_singularity_run section #64

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions babs/babs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,38 @@ def babs_merge(self, chunk_size, trial_run):
+ " not to push merging actions to output RIA.")
print("\n`babs-merge` did not fully finish yet!")

def babs_unzip(container_config_yaml_file):
"""
This function unzips results and extract desired files.
This is done in 3 steps:
1. Generate scripts used by `babs-unzip`
2. Run scripts to unzip data
3. Merge all branches of unzipping

Parameters:
--------------
config: dict
loaded container config yaml file
"""

# ====================================================
# Generate scripts used by `babs-unzip`
# ====================================================

# Prepare input_ds_unzip:
# Call `babs_bootstrap()`:
# !!!! using babs_proj_unzip, instead current `self`!!!

print("TODO")

# ====================================================
# Run scripts to unzip data
# ====================================================

# ====================================================
# Merge all branches of unzipping
# ====================================================


class Input_ds():
"""This class is for input dataset(s)"""
Expand Down Expand Up @@ -2298,13 +2330,23 @@ def generate_bash_run_bidsapp(self, bash_path, input_ds, type_session):

# check if `self.config` from the YAML file contains information we need:
if "babs_singularity_run" not in self.config:
print("The key 'babs_singularity_run' was not included "
"in the `container_config_yaml_file`. "
"Therefore we will not refer to the yaml file for `singularity run` arguments, "
"but will use regular `singularity run` command.")
# "command of singularity run will be read from information "
# "saved by `call-fmt` when `datalad containers-add.`")
cmd_singularity_flags = "\n\t"
# sanity check: there should be only one input ds
# otherwise need to specify in this section:
assert input_ds.num_ds == 1, \
"Section 'babs_singularity_run' is missing in the provided" \
+ " `container_config_yaml_file`. As there are more than one" \
+ " input dataset, you must include this section to specify" \
+ " to which argument that each input dataset will go."
# if there is only one input ds, fine:
print("Section 'babs_singularity_run' was not included "
"in the `container_config_yaml_file`. ")
cmd_singularity_flags = "" # should be empty
# Make sure other returned variables from `generate_cmd_singularityRun_from_config`
# also have values:
# as "--fs-license-file" or "$FREESURFER_LICENSE" is not provided:
flag_fs_license = None
# copied from `generate_cmd_singularityRun_from_config`:
singuRun_input_dir = input_ds.df["path_data_rel"][0]
else:
# print("Generate singularity run command from `container_config_yaml_file`")
# # contain \ for each key-value
Expand Down
58 changes: 57 additions & 1 deletion babs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,62 @@ def babs_merge_main():
babs_proj.babs_merge(args.chunk_size, args.trial_run)


def babs_unzip_cli():
""" CLI for babs-unzip """

parser = argparse.ArgumentParser(
description="``babs-unzip`` unzips results zip files and extracts desired files",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--project_root", "--project-root",
help="Absolute path to the root of BABS project."
" For example, '/path/to/my_BABS_project/'.",
required=True)
parser.add_argument(
'--container_config_yaml_file', '--container-config-yaml-file',
help="Path to a YAML file of the BIDS App container that contains information of"
" what files to unzip etc.")

return parser


def babs_unzip_main():
"""
This is the core function of babs-unzip, which unzip results zip files
and extracts desired files.

project_root: str
Absolute path to the root of BABS project.
For example, '/path/to/my_BABS_project/'.
container_config_yaml_file: str
path to container's configuration YAML file.
These two sections will be used:
1. 'unzip_desired_filenames' - must be included
2. 'rename_conflict_files' - optional
"""

# Get arguments:
args = babs_unzip_cli().parse_args()
project_root = args.project_root
container_config_yaml_file = args.container_config_yaml_file

# container config:
config = read_yaml(container_config_yaml_file)
# ^^ not to use filelock here - otherwise will create `*.lock` file in user's folder

# Sanity checks:
if "unzip_desired_filenames" not in config:
raise Exception("Section 'unzip_desired_filenames' is not included"
" in `--container_config_yaml_file`. This section is required."
" Path to this YAML file: '" + container_config_yaml_file + "'.")

# Get class `BABS` based on saved `analysis/code/babs_proj_config.yaml`:
babs_proj, _ = get_existing_babs_proj(project_root)

# Call method `babs_unzip()`:
babs_proj.babs_unzip(config)


def get_existing_babs_proj(project_root):
"""
This is to get `babs_proj` (class `BABS`) and `input_ds` (class `Input_ds`)
Expand Down Expand Up @@ -730,7 +786,7 @@ def get_existing_babs_proj(project_root):
babs_proj_config_yaml = op.join(project_root,
"analysis/code/babs_proj_config.yaml")
if op.exists(babs_proj_config_yaml) is False:
raise Exception( \
raise Exception(
"`babs-init` was not successful;"
+ " there is no 'analysis/code/babs_proj_config.yaml' file!"
+ " Please rerun `babs-init` to finish the setup.")
Expand Down
6 changes: 6 additions & 0 deletions docs/source/preparation_config_yaml_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ Sections in the configuration YAML file

Among these sections, these sections are optional:

* **babs_singularity_run**

* Only if you are sure that besides arguments handled by BABS, you don't need any other argument,
you may exclude this section from the YAML file.
* You must include this section if there are more one input dataset.

* **required_files**
* **keywords_alert**

Expand Down
44 changes: 44 additions & 0 deletions notebooks/example_container_fmriprepfake.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is an example config yaml file for fmriprep-fake version 0.1.0.
# fmriprep-fake's Docker image is publicly available at: https://hub.docker.com/r/djarecka/fmriprep_fake
# fmriprep-fake generates fMRIPrep outputs without running fMRIPrep itself. You may use it to check fmriprep outputs.
# More details please see: https://github.com/djarecka/fmriprep-fake
# Thanks to Dorota Jarecka for preparing and sharing this BIDS App!

# Warning!!!
# This is only an example, which may not necessarily fit your purpose,
# or be an optimized solution for your case,
# or be compatible to the toy BIDS App version you're using.
# Therefore, please change and tailor it for your case before use it!!!

# 'babs_singularity_run': There is no such section for fmriprep-fake
# as all commands needed by fmriprep-fake have been handled by BABS.

babs_zip_foldername: # the foldername(s) to get zipped and the version string(s)
fmriprepfake: "0-1-1" # should be the same as in `babs-init`'s argument `container_name`; better using "-" instead of "."

cluster_resources:
interpreting_shell: /bin/bash # "-S /bin/bash" on cubic
hard_memory_limit: 2G # "-l h_vmem=25G" on cubic
temporary_disk_space: 20G # "-l tmpfree=50G" on cubic # this is highly-recommended on cubic
hard_runtime_limit: "24:00:00" # needed by cubic for PennLINC lab members; otherwise jobs will be sent to generic nodes
customized_text: |
#$ -R y

# Users need to add their customized bash command below,
# they will be used as preambles in `participant_job.sh`
# the commands should not be quoted!
script_preamble: |
source ${CONDA_PREFIX}/bin/activate mydatalad # Penn Med CUBIC cluster; replace 'mydatalad' with your conda env name

# Where to run the jobs:
job_compute_space: "${CBICA_TMPDIR}" # Penn Med CUBIC cluster tmp space

# Below is to filter out subjects (or sessions)
# right now we only filter based on unzipped dataset
required_files:
$INPUT_DATASET_#1:
- "func/*_bold.nii*"
- "anat/*_T1w.nii*"

# Keywords for alerting messages in log files:
# 'keywords_alert': not to include this section for now.
Loading