Skip to content

Commit

Permalink
Merge pull request #6 from BIDS-Apps/fsdir
Browse files Browse the repository at this point in the history
make freesurfer_dir optional
  • Loading branch information
fliem authored Aug 15, 2017
2 parents f6eef41 + cfd3b85 commit 0c89e96
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Your data has to be organized according to the
[BIDS standard](http://bids.neuroimaging.io) and each subject needs at
least one T1w image.
In a first step, BARACUS runs [FreeSurfer's](http://freesurfer.net)
`recon-all` command and saves the output in `--freesurfer_dir`.
`recon-all` command and saves the output in `{out_dir}/freesurfer/`
If the data has previously been analyzed with FreeSurfer version 5.3.0,
and BARACUS finds them in `--freesurfer_dir` this step ist skippen.

Expand Down Expand Up @@ -67,36 +67,44 @@ aseg files extracted via asegstats2table.
These examples demonstrate how to run the `bids/baracus` docker container.
For a brief introduction how to run BIDS Apps see
[this site](http://bids-apps.neuroimaging.io/tutorial/).
In the examples `/project/bids_sourcedata`, `/project/freesurfer` and
`/project/baracus` are directories
on your hard drive, which are mapped into the docker container directories
`/data/in`, `/data/freesurfer` and `/data/out`, respectively, via
the `-v` flag.
In the examples `/project/bids_sourcedata` and
`/project/baracus` are directories on your hard drive, which are mapped
into the docker container directories `/data/in` and `/data/out`,
respectively, via the `-v` flag.

#### Participants

docker run -ti --rm \
-v /project/bids_sourcedata/:/data/in \
-v /project/freesurfer/:/data/freesurfer \
-v /project/baracus:/data/out \
bids/baracus /data/in /data/out participant \
--license_key "XX" --freesurfer_dir /data/freesurfer
--license_key "XX"

#### Group

docker run -ti --rm \
-v /project/bids_sourcedata/:/data/in \
-v /project/freesurfer/:/data/freesurfer \
-v /project/baracus:/data/out \
bids/baracus /data/in /data/out group \
--license_key "XX"

#### Participants with previously processed FreeSurfer data
If FreeSurfer data is already available, for example at
`/project/freesurfer/` running the follwing command will use the
previously processed data:

docker run -ti --rm \
-v /project/bids_sourcedata/:/data/in \
-v /project/freesurfer/:/data/freesurfer \
-v /project/baracus:/data/out \
bids/baracus /data/in /data/out participant \
--license_key "XX" --freesurfer_dir /data/freesurfer

### Usage

docker run -ti --rm bids/${CIRCLE_PROJECT_REPONAME,,} -h
usage: run_brain_age_bids.py [-h]
[--participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...]]
--freesurfer_dir FREESURFER_DIR
[--freesurfer_dir FREESURFER_DIR]
[--models {Liem2016__OCI_norm,Liem2016__full_2samp_training} [{Liem2016__OCI_norm,Liem2016__full_2samp_training} ...]]
--license_key LICENSE_KEY [--n_cpus N_CPUS] [-v]
bids_dir out_dir {participant,group}
Expand All @@ -108,7 +116,7 @@ the `-v` flag.
positional arguments:
bids_dir The directory with the input dataset formatted
according to the BIDS standard.
out_dir Results are put here.
out_dir Results are put into {out_dir}/baracus.
{participant,group} Level of the analysis that will be performed.
"participant": predicts single subject brain age,
"group": collects single subject predictions.
Expand All @@ -125,7 +133,8 @@ the `-v` flag.
--freesurfer_dir FREESURFER_DIR
Folder with FreeSurfer subjects formatted according to
BIDS standard. If subject's recon-all folder cannot be
found, recon-all will be run.
found, recon-all will be run. If not specified
freesurfer data will be saved to {out_dir}/freesurfer
--models {Liem2016__OCI_norm,Liem2016__full_2samp_training} [{Liem2016__OCI_norm,Liem2016__full_2samp_training} ...]
--license_key LICENSE_KEY
FreeSurfer license key - letters and numbers after "*"
Expand All @@ -136,6 +145,7 @@ the `-v` flag.
-v, --version show program's version number and exit



## FILE mode
### Example
docker run -ti --rm \
Expand Down
10 changes: 5 additions & 5 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ test:
# ds114_test1
- docker run --rm -ti -v ${HOME}/data/ds114_test1:/data/in -v ${HOME}/data/ds114_test1_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test1:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out participant --freesurfer_dir /data/fs --license_key="~/test.key"
- docker run --rm -ti -v ${HOME}/data/ds114_test1:/data/in -v ${HOME}/data/ds114_test1_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test1:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out group --freesurfer_dir /data/fs --license_key="~/test.key"
- cat ${HOME}/outputs/ds114_test1/00_group/group_predicted_age.tsv
- cat ${HOME}/outputs/ds114_test1/baracus/00_group/group_predicted_age.tsv

# test models
- docker run --rm -ti -v ${HOME}/data/ds114_test1:/data/in -v ${HOME}/data/ds114_test1_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test1_models:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out participant --freesurfer_dir /data/fs --models Liem2016__OCI_norm Liem2016__full_2samp_training --license_key="~/test.key"
- docker run --rm -ti -v ${HOME}/data/ds114_test1:/data/in -v ${HOME}/data/ds114_test1_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test1_models:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out group --freesurfer_dir /data/fs --license_key="~/test.key"
- cat ${HOME}/outputs/ds114_test1_models/00_group/group_predicted_age.tsv
- cat ${HOME}/outputs/ds114_test1_models/baracus/00_group/group_predicted_age.tsv

# test file mode
- docker run --rm -ti -v ${HOME}/outputs/ds114_test1/sub-01/data/:/data/in -v ${HOME}/outputs/ds114_test1_file_mode:/data/out --entrypoint=run_brain_age_files.py bids/baracus /data/out --lh_thickness_file /data/in/lh.thickness.mgh --rh_thickness_file /data/in/rh.thickness.mgh --lh_area_file /data/in/lh.area.mgh --rh_area_file /data/in/rh.area.mgh --aseg_file /data/in/aseg --participant_label sub-01
- docker run --rm -ti -v ${HOME}/outputs/ds114_test1/baracus/sub-01/data/:/data/in -v ${HOME}/outputs/ds114_test1_file_mode:/data/out --entrypoint=run_brain_age_files.py bids/baracus /data/out --lh_thickness_file /data/in/lh.thickness.mgh --rh_thickness_file /data/in/rh.thickness.mgh --lh_area_file /data/in/lh.area.mgh --rh_area_file /data/in/rh.area.mgh --aseg_file /data/in/aseg --participant_label sub-01
- cat ${HOME}/outputs/ds114_test1_file_mode/sub-01/sub-01_predicted_age.tsv

# ds114_test2
- docker run --rm -ti -v ${HOME}/data/ds114_test2:/data/in -v ${HOME}/data/ds114_test2_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test2:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out participant --freesurfer_dir /data/fs --license_key="~/test.key"
- docker run --rm -ti -v ${HOME}/data/ds114_test2:/data/in -v ${HOME}/data/ds114_test2_freesurfer:/data/fs -v ${HOME}/outputs/ds114_test2:/data/out bids/${CIRCLE_PROJECT_REPONAME,,} /data/in /data/out group --freesurfer_dir /data/fs --license_key="~/test.key"
- cat ${HOME}/outputs/ds114_test2/00_group/group_predicted_age.tsv
- cat ${HOME}/outputs/ds114_test2/baracus/00_group/group_predicted_age.tsv

deployment:
hub:
Expand Down
29 changes: 21 additions & 8 deletions scripts/run_brain_age_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'is extracted automatiacally from that folder. ')
parser.add_argument('bids_dir', help='The directory with the input dataset '
'formatted according to the BIDS standard.')
parser.add_argument('out_dir', help='Results are put here.')
parser.add_argument('out_dir', help='Results are put into {out_dir}/baracus.')
parser.add_argument('analysis_level', help='Level of the analysis that will be performed. '
'"participant": predicts single subject brain age, '
'"group": collects single subject predictions.',
Expand All @@ -31,9 +31,11 @@
'provided all subjects should be analyzed. Multiple '
'participants can be specified with a space separated list.',
nargs="+")
parser.add_argument('--freesurfer_dir', required=True, help="Folder with FreeSurfer subjects formatted according "
"to BIDS standard. If subject's recon-all folder "
"cannot be found, recon-all will be run.")
parser.add_argument('--freesurfer_dir', help="Folder with FreeSurfer subjects formatted according "
"to BIDS standard. If subject's recon-all folder "
"cannot be found, recon-all will be run. "
"If not specified freesurfer data will be saved to {"
"out_dir}/freesurfer")
parser.add_argument('--models', choices=models_list, default=["Liem2016__OCI_norm"], help='',
nargs="+")
parser.add_argument('--license_key',
Expand All @@ -46,6 +48,17 @@
version='BARACUS version {}'.format(__version__))
args = parser.parse_args()

# set up output dirs
if args.freesurfer_dir:
freesurfer_dir = args.freesurfer_dir
else:
freesurfer_dir = os.path.join(args.out_dir, "freesurfer")
out_dir = os.path.join(args.out_dir, "baracus")
if not os.path.isdir(freesurfer_dir):
os.makedirs(freesurfer_dir)
if not os.path.isdir(out_dir):
os.makedirs(out_dir)

model_dir = resource_filename(Requirement.parse("baracus"), 'models')

run("bids-validator " + args.bids_dir)
Expand All @@ -58,11 +71,11 @@

if args.analysis_level == "participant":

data_files = run_prepare_all(args.bids_dir, args.freesurfer_dir, args.out_dir, subjects_to_analyze,
data_files = run_prepare_all(args.bids_dir, freesurfer_dir, out_dir, subjects_to_analyze,
sessions_to_analyze, args.n_cpus, args.license_key)

for subject, d in data_files.items():
d["out_dir"] = args.out_dir
d["out_dir"] = out_dir
d["model_dir"] = model_dir
d["models"] = args.models
d["subject_label"] = subject
Expand All @@ -72,10 +85,10 @@
print("Creating group table...")
df = pd.DataFrame([])
for subject in freesurfer_subjects_to_analyze:
in_file = os.path.join(args.out_dir, subject, subject + "_predicted_age.tsv")
in_file = os.path.join(out_dir, subject, subject + "_predicted_age.tsv")
df = df.append(pd.read_csv(in_file, sep="\t"))

group_out_dir = os.path.join(args.out_dir, "00_group")
group_out_dir = os.path.join(out_dir, "00_group")
if not os.path.isdir(group_out_dir):
os.makedirs(group_out_dir)
out_file = os.path.join(group_out_dir, "group_predicted_age.tsv")
Expand Down

0 comments on commit 0c89e96

Please sign in to comment.