Skip to content

148 exception fail fast #178

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

Merged
merged 37 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b1bf9c6
Removed try/except from the code for it to fail fast.
t-sanchez Aug 8, 2022
7904302
Try reducing the level of verbosity in python files.
t-sanchez Aug 8, 2022
7da4413
WIP: Reduce verbose from cpp files
t-sanchez Aug 8, 2022
71fa43f
WIP: Reduce verbosity of cpp
t-sanchez Aug 8, 2022
2efed71
Merge branch '147_153_input_mgmt_preprocessing' into 148_exception_fa…
t-sanchez Aug 8, 2022
5fecde7
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Aug 23, 2022
38b4f85
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Aug 23, 2022
0932490
Tests on verbosity in c++ files.
t-sanchez Aug 23, 2022
135e31e
WIP: Keep reducing verbosity level, and putting verbose variables in …
t-sanchez Aug 23, 2022
bedcdef
WIP: Added verbose management to most of the c++ code with Get/SetVer…
t-sanchez Aug 24, 2022
9a688a0
WIP: Added verbose management to most of the c++ code with Get/SetVer…
t-sanchez Aug 24, 2022
b5424dc
WIP: Added verbose management to most of the c++ code with Get/SetVer…
t-sanchez Aug 24, 2022
3490327
WIP: Keep updating verbosity.
t-sanchez Aug 29, 2022
3e153ae
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Aug 29, 2022
83e5a78
Added a more transparent exception in StacksOrdering when the mask is…
t-sanchez Aug 29, 2022
c3624b3
Added a logging of failed runs: a run fails quickly, its error is log…
t-sanchez Aug 29, 2022
9075474
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Sep 1, 2022
bf15c0f
Fix issues due to merging; add missing prints to verbose-only.
t-sanchez Sep 1, 2022
cb5c729
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Sep 5, 2022
df0014e
Merge branch 'v2.1.0' into 148_exception_fail_fast
t-sanchez Sep 6, 2022
8b98f78
WP: Integrate verbosity control in labelmaps, as well as fast failure…
t-sanchez Sep 6, 2022
23f54ec
Added documentation for do_multi_parameters, do_srr_assessment. Set v…
t-sanchez Sep 6, 2022
b649c93
Refined printing of messages in successful or failed runs. Processed …
t-sanchez Sep 6, 2022
6a44969
STYLE: PEP8 compliance.
t-sanchez Sep 6, 2022
5ff8b15
STYLE: PEP8 compliance.
t-sanchez Sep 6, 2022
7d361a2
Merge branch '148_exception_fail_fast' of github.com:Medical-Image-An…
t-sanchez Sep 6, 2022
f9a7c4d
DOC: Update usage.rst [skip ci]
t-sanchez Sep 6, 2022
7ab59dd
DOC: update documentation of create_recon_stage and create_preproc_stage
t-sanchez Sep 6, 2022
a02fdc6
WIP: Started refactoring inputs in reconstruction.py to not have all …
t-sanchez Sep 6, 2022
b9051d9
WIP: Change print to exception for missing reorientation inputs.
t-sanchez Sep 1, 2022
29a3ce0
Fixed some merging issues to have multiparameterTV work well: moved b…
t-sanchez Sep 6, 2022
332f253
STYLE: Fix PEP8 issues.
t-sanchez Sep 6, 2022
631ae2f
BUG: Add absolute path to commands to prevent filenotfound error.
t-sanchez Sep 6, 2022
d98862e
PEP8 issues
t-sanchez Sep 6, 2022
037a9d0
DOC: Codacy requests - added docstrings to check_participants_params,…
t-sanchez Sep 6, 2022
907f452
BUG: add EXEC_PATH
t-sanchez Sep 6, 2022
a7f38ab
This is a combination of 2 commits.
t-sanchez Sep 6, 2022
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
125 changes: 94 additions & 31 deletions docker/bidsapp/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ def check_and_return_valid_nb_of_cores(openmp_nb_of_cores, nipype_nb_of_cores, o
return openmp_nb_of_cores, nipype_nb_of_cores


def check_participants_params(participants_params):
"""Check the participants_params input json to see
if the custom_interfaces have only allowed keys, i.e.
keys that are used somewhere in mialsrtk.

Parameters
----------
pariticipants_params: dict
A dictionary corresponding to the json config file.
"""
allowed_keys = [
"skip_svr",
"do_refine_hr_mask",
"do_nlm_denoising",
"skip_stacks_ordering",
"do_reconstruct_labels",
"do_anat_orientation",
"do_multi_parameters",
"do_srr_assessment"
]
for sub, sub_list in participants_params.items():
for config in sub_list:
if "custom_interfaces" in config.keys():
for k in config["custom_interfaces"].keys():
if k not in allowed_keys:
raise RuntimeError(
f"Invalid custom interface key {k} for subject "
f"{sub}. Valid keys are \n" +
", ".join(allowed_keys)
)


def main(bids_dir, output_dir,
subject,
session,
Expand All @@ -170,6 +202,7 @@ def main(bids_dir, output_dir,
labels_derivatives_dir='',
masks_desc=None,
dict_custom_interfaces=None,
verbose=None,
nipype_number_of_cores=1,
openmp_number_of_cores=1,
memory=0
Expand Down Expand Up @@ -241,7 +274,6 @@ def main(bids_dir, output_dir,

if sr_id is None:
sr_id = 1

# Initialize an instance of AnatomicalPipeline
if run_type == "sr":
pipeline = SRReconPipeline(
Expand All @@ -257,6 +289,7 @@ def main(bids_dir, output_dir,
labels_derivatives_dir,
masks_desc,
p_dict_custom_interfaces=dict_custom_interfaces,
p_verbose=verbose,
p_openmp_number_of_cores=openmp_number_of_cores,
p_nipype_number_of_cores=nipype_number_of_cores
)
Expand All @@ -272,6 +305,7 @@ def main(bids_dir, output_dir,
masks_derivatives_dir,
masks_desc,
p_dict_custom_interfaces=dict_custom_interfaces,
p_verbose=verbose,
p_openmp_number_of_cores=openmp_number_of_cores,
p_nipype_number_of_cores=nipype_number_of_cores,
)
Expand Down Expand Up @@ -307,28 +341,29 @@ def main(bids_dir, output_dir,
print("INFO: Environment variable OMP_NUM_THREADS set to: "
"{}".format(os.environ["OMP_NUM_THREADS"]))

print(args.param_file)
with open(args.param_file, "r") as f:
with open(args.param_file, 'r') as f:
participants_params = json.load(f)
print(participants_params)
print(participants_params.keys())
print()

check_participants_params(participants_params)
subjects_to_analyze = []
# only for a subset of subjects
if args.participant_label:
subjects_to_analyze = args.participant_label
# for all subjects
else:
subject_dirs = glob(os.path.join(args.bids_dir, "sub-*"))
subjects_to_analyze = [
subjects_to_analyze = sorted([
subject_dir.split("-")[-1] for subject_dir in subject_dirs
]
])
success_dict = {}
failed_dict = {}

for sub in subjects_to_analyze:
success_dict[sub] = []
failed_dict[sub] = []
if sub in participants_params.keys():

sr_list = participants_params[sub]
print(f"Processing subject {sub}.")
print(sr_list)

for sr_params in sr_list:
Expand All @@ -349,29 +384,57 @@ def main(bids_dir, output_dir,
if "custom_interfaces" in sr_params.keys() else None

if sr_id is None:
print(f"WARNING: Do not process subjects {sub} "
"because of missing parameters.")
e = f'Subject {sub} was not processed ' \
f'because of missing parameters.'
failed_dict[sub] += [e]
print(e)
continue

res = main(bids_dir=args.bids_dir,
output_dir=args.output_dir,
subject=sub,
session=ses,
run_type=args.run_type,
p_ga=ga,
p_stacks=stacks,
param_TV=param_TV,
sr_id=sr_id,
masks_derivatives_dir=args.masks_derivatives_dir,
labels_derivatives_dir=args.labels_derivatives_dir,
masks_desc=masks_desc,
dict_custom_interfaces=dict_custom_interfaces,
nipype_number_of_cores=nipype_nb_of_cores,
openmp_number_of_cores=openmp_nb_of_cores,
memory=args.memory
)
try:
res = main(
bids_dir=args.bids_dir,
output_dir=args.output_dir,
subject=sub,
session=ses,
run_type=args.run_type,
p_ga=ga,
p_stacks=stacks,
param_TV=param_TV,
sr_id=sr_id,
masks_derivatives_dir=args.masks_derivatives_dir,
labels_derivatives_dir=args.labels_derivatives_dir,
masks_desc=masks_desc,
dict_custom_interfaces=dict_custom_interfaces,
verbose=args.verbose,
nipype_number_of_cores=nipype_nb_of_cores,
openmp_number_of_cores=openmp_nb_of_cores,
memory=args.memory
)
out = f"Subject {sub} with parameters {sr_params} " \
f"succeeded."
success_dict[sub] += [out]
except Exception as e:
e = f"Subject {sub} with parameters {sr_params} failed "\
f"with message \n\t {e}"
failed_dict[sub] += [e]
print(e)
else:
print(f"WARNING: Do not process subjects {sub} "
"because of missing configuration.")
e = f"Subject {sub} was not processed because of missing "\
f"configuration."
failed_dict[sub] += [e]
print(e)

if not all(v == [] for v in success_dict.values()):
print("\n" + "-"*15 + "\nSUCCESSFUL RUNS\n" +
"-"*15)
for sub, v in success_dict.items():
for out in v:
print("-> ", out)

if not all(v == [] for v in failed_dict.values()):
print("\n" + "-"*24 + "\nWARNING: SOME RUN FAILED\n" +
"-"*24)
for sub, v in failed_dict.items():
for error in v:
print("-> ", error)

sys.exit(0)
10 changes: 8 additions & 2 deletions documentation/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ where:
* ``"session"`` (optional) It MUST be specified if you have a BIDS dataset composed of multiple sessions with the *sub-XX/ses-YY* structure.

* ``"ga"`` (optional but mandatory when ``do_anat_orientation`` is true) subject's gestational age in weeks.

* ``"run_type"`` (optional): defines the type of run that should be done. It can be set between `sr` (super-resolution) and `preprocessing` (preprocessing-only). (default is ``"sr"``)

* ``"custom_interfaces"`` (optional): indicates weither optional interfaces of the pipeline should be performed.

* ``"skip_svr"`` (optional) the Slice-to-Volume Registration should be skipped in the image reconstruction. (default is False)
Expand All @@ -89,7 +89,13 @@ where:
If True, path to a directory containing STA atlas (Gholipour et al., 2017 [1]_, [2]_) must be mounted to `/sta`.

* ``"preproc_do_registration"`` (optional) indicates whether the Slice-to-Volume Registration should be computed in the ``"preprocessing"`` run (default is False).

* ``"do_multi_parameters"`` (optional) enables running the super-resolution reconstruction with lists of parameters. The algorithm will
then run a grid search over all combinations of parameters. (default is False)

* ``"do_srr_assessment"`` (optional) enables comparing the quality of the super-resolution reconstruction with a reference image. (default is False)
If True, it will require a reference isotropic T2w image, mask and labels located in the data folder.

References
----------
.. [1] Gholipour et al.; A normative spatiotemporal MRI atlas of the fetal brain for automatic segmentation and analysis of early brain growth, Scientific Reports 7, Article number: 476 (2017). `(link to article)<http://www.nature.com/articles/s41598-017-00525-w>`_ .
Expand Down
12 changes: 3 additions & 9 deletions pymialsrtk/cli/mialsuperresolutiontoolkit_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,8 @@ def main():
tracker.start()

# Execute the docker run command
try:
print(f'... cmd: {cmd}')
run(cmd)
exit_code = 0
except Exception as e:
print('Failed')
print(e)
exit_code = 1
print(f'... cmd: {cmd}')
run(cmd)

if args.track_carbon_footprint:
emissions: float = tracker.stop()
Expand All @@ -139,7 +133,7 @@ def main():
print("Results can be visualized with the codecarbon visualization tool using following command:\n")
print(f'\t$ carbonboard --filepath="{args.bids_dir}/code/emissions.csv" --port=9999\n')

return exit_code
return 0


if __name__ == '__main__':
Expand Down
12 changes: 3 additions & 9 deletions pymialsrtk/cli/mialsuperresolutiontoolkit_singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,8 @@ def main():
tracker.start()

# Execute the singularity run command
try:
print(f'... cmd: {cmd}')
run(cmd)
exit_code = 0
except Exception as e:
print('Failed')
print(e)
exit_code = 1
print(f'... cmd: {cmd}')
run(cmd)

if args.track_carbon_footprint:
emissions: float = tracker.stop()
Expand All @@ -138,7 +132,7 @@ def main():
print("Results can be visualized with the codecarbon visualization tool using following command:\n")
print(f'\t$ carbonboard --filepath="{args.bids_dir}/code/emissions.csv" --port=9999\n')

return exit_code
return 0


if __name__ == '__main__':
Expand Down
Empty file added pymialsrtk/git
Empty file.
Loading