From 36492df42bd89718030469eb799bb07d115f1f56 Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Fri, 29 Sep 2023 15:24:17 +0200 Subject: [PATCH] Also track exceptions --- bidscoin/bcoin.py | 23 +++++++++++------ bidscoin/bidsapps/deface.py | 25 ++++++++++-------- bidscoin/bidsapps/echocombine.py | 23 ++++++++++------- bidscoin/bidsapps/medeface.py | 27 ++++++++++++-------- bidscoin/bidsapps/skullstrip.py | 25 ++++++++++-------- bidscoin/bidsapps/slicereport.py | 35 +++++++++++++++----------- bidscoin/bidscoiner.py | 16 +++++++----- bidscoin/bidseditor.py | 12 ++++++--- bidscoin/bidsmapper.py | 30 ++++++++++++---------- bidscoin/utilities/bidsparticipants.py | 19 ++++++++------ bidscoin/utilities/dicomsort.py | 29 +++++++++++++-------- bidscoin/utilities/rawmapper.py | 29 ++++++++++++--------- 12 files changed, 177 insertions(+), 116 deletions(-) diff --git a/bidscoin/bcoin.py b/bidscoin/bcoin.py index 4038244a..a452111a 100755 --- a/bidscoin/bcoin.py +++ b/bidscoin/bcoin.py @@ -524,6 +524,7 @@ def test_bidscoin(bidsmapfile: Union[Path,dict], options: dict=None, testplugins if not success: LOGGER.warning('Not all tests finishd successfully (this may be ok, but check the output above)') + trackusage('bidscoin_error') else: LOGGER.success('All tests finished successfully :-)') @@ -557,6 +558,7 @@ def pulltutorialdata(tutorialfolder: str) -> None: LOGGER.success(f"Done") except Exception as unpackerror: LOGGER.error(f"Could not unpack: {tutorialtargz}\n{unpackerror}") + trackusage('bidscoin_error') def settracking(value: str): @@ -596,14 +598,19 @@ def main(): # Parse the input arguments and run bidscoiner(args) args = get_parser().parse_args(None if sys.argv[1:] else ['--help']) - list_executables(show=args.list) - list_plugins(show=args.plugins) - uninstall_plugins(filenames=args.uninstall) - install_plugins(filenames=args.install) - pulltutorialdata(tutorialfolder=args.download) - test_bidscoin(bidsmapfile=args.test) - test_bidsmap(bidsmapfile=args.bidsmaptest) - settracking(value=args.tracking) + try: + list_executables(show=args.list) + list_plugins(show=args.plugins) + uninstall_plugins(filenames=args.uninstall) + install_plugins(filenames=args.install) + pulltutorialdata(tutorialfolder=args.download) + test_bidscoin(bidsmapfile=args.test) + test_bidsmap(bidsmapfile=args.bidsmaptest) + settracking(value=args.tracking) + + except Exception: + trackusage('bidscoin_exception') + raise if __name__ == "__main__": diff --git a/bidscoin/bidsapps/deface.py b/bidscoin/bidsapps/deface.py index e49497df..e2b7a28f 100755 --- a/bidscoin/bidsapps/deface.py +++ b/bidscoin/bidsapps/deface.py @@ -152,17 +152,22 @@ def main(): from bidscoin.cli._deface import get_parser - trackusage('deface') - args = get_parser().parse_args() - deface(bidsdir = args.bidsfolder, - pattern = args.pattern, - subjects = args.participant_label, - force = args.force, - output = args.output, - cluster = args.cluster, - nativespec = args.nativespec, - kwargs = args.args) + + trackusage('deface') + try: + deface(bidsdir = args.bidsfolder, + pattern = args.pattern, + subjects = args.participant_label, + force = args.force, + output = args.output, + cluster = args.cluster, + nativespec = args.nativespec, + kwargs = args.args) + + except Exception: + trackusage('deface_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidsapps/echocombine.py b/bidscoin/bidsapps/echocombine.py index 6c535812..addd49ae 100755 --- a/bidscoin/bidsapps/echocombine.py +++ b/bidscoin/bidsapps/echocombine.py @@ -200,17 +200,22 @@ def main(): from bidscoin.cli._echocombine import get_parser - trackusage('echocombine') - # Parse the input arguments and run bidscoiner(args) args = get_parser().parse_args() - echocombine(bidsdir = args.bidsfolder, - pattern = args.pattern, - subjects = args.participant_label, - output = args.output, - algorithm = args.algorithm, - weights = args.weights, - force = args.force) + + trackusage('echocombine') + try: + echocombine(bidsdir = args.bidsfolder, + pattern = args.pattern, + subjects = args.participant_label, + output = args.output, + algorithm = args.algorithm, + weights = args.weights, + force = args.force) + + except Exception: + trackusage('echocombine_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidsapps/medeface.py b/bidscoin/bidsapps/medeface.py index b84ef6ac..87994cdc 100755 --- a/bidscoin/bidsapps/medeface.py +++ b/bidscoin/bidsapps/medeface.py @@ -200,18 +200,23 @@ def main(): from bidscoin.cli._medeface import get_parser - trackusage('medeface') - args = get_parser().parse_args() - medeface(bidsdir = args.bidsfolder, - pattern = args.pattern, - maskpattern = args.maskpattern, - subjects = args.participant_label, - force = args.force, - output = args.output, - cluster = args.cluster, - nativespec = args.nativespec, - kwargs = args.args) + + trackusage('medeface') + try: + medeface(bidsdir = args.bidsfolder, + pattern = args.pattern, + maskpattern = args.maskpattern, + subjects = args.participant_label, + force = args.force, + output = args.output, + cluster = args.cluster, + nativespec = args.nativespec, + kwargs = args.args) + + except Exception: + trackusage('medeface_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidsapps/skullstrip.py b/bidscoin/bidsapps/skullstrip.py index 0a4e9b9c..799e4654 100755 --- a/bidscoin/bidsapps/skullstrip.py +++ b/bidscoin/bidsapps/skullstrip.py @@ -208,17 +208,22 @@ def main(): from bidscoin.cli._skullstrip import get_parser - trackusage('skullstrip') - args = get_parser().parse_args() - skullstrip(bidsdir = args.bidsfolder, - pattern = args.pattern, - subjects = args.participant_label, - masked = args.masked, - output = args.output, - force = args.force, - args = args.args, - cluster = args.cluster) + + trackusage('skullstrip') + try: + skullstrip(bidsdir = args.bidsfolder, + pattern = args.pattern, + subjects = args.participant_label, + masked = args.masked, + output = args.output, + force = args.force, + args = args.args, + cluster = args.cluster) + + except Exception: + trackusage('skullstrip_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidsapps/slicereport.py b/bidscoin/bidsapps/slicereport.py index cf62ec17..479ed13f 100755 --- a/bidscoin/bidsapps/slicereport.py +++ b/bidscoin/bidsapps/slicereport.py @@ -278,22 +278,27 @@ def main(): from bidscoin.cli._slicereport import get_parser - trackusage('slicereport') - args = get_parser().parse_args() - slicereport(bidsdir = args.bidsfolder, - pattern = args.pattern, - outlinepattern = args.outlinepattern, - outlineimage = args.outlineimage, - subjects = args.participant_label, - reportdir = args.reportfolder, - crossdirs = args.xlinkfolder, - qccols = args.qcscores, - cluster = args.cluster, - options = args.options, - outputs = args.outputs, - suboptions = args.suboptions, - suboutputs = args.suboutputs) + + trackusage('slicereport') + try: + slicereport(bidsdir = args.bidsfolder, + pattern = args.pattern, + outlinepattern = args.outlinepattern, + outlineimage = args.outlineimage, + subjects = args.participant_label, + reportdir = args.reportfolder, + crossdirs = args.xlinkfolder, + qccols = args.qcscores, + cluster = args.cluster, + options = args.options, + outputs = args.outputs, + suboptions = args.suboptions, + suboutputs = args.suboutputs) + + except Exception: + trackusage('slicereport_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidscoiner.py b/bidscoin/bidscoiner.py index 88f442ec..b7aeabd6 100755 --- a/bidscoin/bidscoiner.py +++ b/bidscoin/bidscoiner.py @@ -346,12 +346,16 @@ def main(): args = get_parser().parse_args() trackusage('bidscoiner') - - bidscoiner(rawfolder = args.sourcefolder, - bidsfolder = args.bidsfolder, - subjects = args.participant_label, - force = args.force, - bidsmapfile = args.bidsmap) + try: + bidscoiner(rawfolder = args.sourcefolder, + bidsfolder = args.bidsfolder, + subjects = args.participant_label, + force = args.force, + bidsmapfile = args.bidsmap) + + except Exception: + trackusage('bidscoiner_exception') + raise if __name__ == "__main__": diff --git a/bidscoin/bidseditor.py b/bidscoin/bidseditor.py index 52234696..f9ebe355 100755 --- a/bidscoin/bidseditor.py +++ b/bidscoin/bidseditor.py @@ -1693,10 +1693,14 @@ def main(): args = get_parser().parse_args() trackusage('bidseditor') - - bidseditor(bidsfolder = args.bidsfolder, - bidsmapfile = args.bidsmap, - templatefile = args.template) + try: + bidseditor(bidsfolder = args.bidsfolder, + bidsmapfile = args.bidsmap, + templatefile = args.template) + + except Exception: + trackusage('bidseditor_exception') + raise if __name__ == '__main__': diff --git a/bidscoin/bidsmapper.py b/bidscoin/bidsmapper.py index 927ef90e..84a853f4 100755 --- a/bidscoin/bidsmapper.py +++ b/bidscoin/bidsmapper.py @@ -240,19 +240,23 @@ def main(): args = get_parser().parse_args() trackusage('bidsmapper') - - bidsmapper(rawfolder = args.sourcefolder, - bidsfolder = args.bidsfolder, - bidsmapfile = args.bidsmap, - templatefile = args.template, - plugins = args.plugins, - subprefix = args.subprefix, - sesprefix = args.sesprefix, - unzip = args.unzip, - store = args.store, - noeditor = args.automated, - force = args.force, - noupdate = args.no_update) + try: + bidsmapper(rawfolder = args.sourcefolder, + bidsfolder = args.bidsfolder, + bidsmapfile = args.bidsmap, + templatefile = args.template, + plugins = args.plugins, + subprefix = args.subprefix, + sesprefix = args.sesprefix, + unzip = args.unzip, + store = args.store, + noeditor = args.automated, + force = args.force, + noupdate = args.no_update) + + except Exception: + trackusage('bidsmapper_exception') + raise if __name__ == "__main__": diff --git a/bidscoin/utilities/bidsparticipants.py b/bidscoin/utilities/bidsparticipants.py index 6a7a22a6..6c9b2342 100755 --- a/bidscoin/utilities/bidsparticipants.py +++ b/bidscoin/utilities/bidsparticipants.py @@ -182,14 +182,19 @@ def main(): from bidscoin.cli._bidsparticipants import get_parser - trackusage('bidsparticipants') - args = get_parser().parse_args() - bidsparticipants(rawfolder = args.sourcefolder, - bidsfolder = args.bidsfolder, - keys = args.keys, - bidsmapfile = args.bidsmap, - dryrun = args.dryrun) + + trackusage('bidsparticipants') + try: + bidsparticipants(rawfolder = args.sourcefolder, + bidsfolder = args.bidsfolder, + keys = args.keys, + bidsmapfile = args.bidsmap, + dryrun = args.dryrun) + + except Exception: + trackusage('bidsparticipants_exception') + raise if __name__ == "__main__": diff --git a/bidscoin/utilities/dicomsort.py b/bidscoin/utilities/dicomsort.py index edce5960..56c4af8d 100755 --- a/bidscoin/utilities/dicomsort.py +++ b/bidscoin/utilities/dicomsort.py @@ -40,6 +40,7 @@ def construct_name(scheme: str, dicomfile: Path, force: bool) -> str: value = int(value.replace('.','')) # Convert the SeriesInstanceUID to an int if not value and value != 0 and not force: LOGGER.error(f"Missing '{field}' DICOM field specified in the '{scheme}' folder/naming scheme, cannot find a safe name for: {dicomfile}\n") + trackusage('dicomsort_error') return '' else: schemevalues[field] = value @@ -112,6 +113,7 @@ def sortsession(sessionfolder: Path, dicomfiles: List[Path], folderscheme: str, subfolder = construct_name(folderscheme, dicomfile, force) if not subfolder: LOGGER.error('Cannot create subfolders, aborting dicomsort()...') + trackusage('dicomsort_error') return if subfolder not in subfolders: subfolders.append(subfolder) @@ -162,7 +164,7 @@ def sortsessions(sourcefolder: Path, subprefix: str='', sesprefix: str='', folde LOGGER.error(f"Unexpected dicomsource argument '{sourcefolder}', aborting dicomsort()...") return [] elif not sourcefolder.is_dir(): - print(f"Sourcefolder '{sourcefolder}' not found") + LOGGER.error(f"Sourcefolder '{sourcefolder}' not found") return [] if (folderscheme and not validscheme(folderscheme)) or (namescheme and not validscheme(namescheme)): LOGGER.error('Wrong scheme input argument(s), aborting dicomsort()...') @@ -214,20 +216,25 @@ def main(): from bidscoin.cli._dicomsort import get_parser - trackusage('dicomsort') + args = get_parser().parse_args() # Set-up logging bcoin.setup_logging() - args = get_parser().parse_args() - sortsessions(sourcefolder = args.dicomsource, - subprefix = args.subprefix, - sesprefix = args.sesprefix, - folderscheme = args.folderscheme, - namescheme = args.namescheme, - pattern = args.pattern, - force = args.force, - dryrun = args.dryrun) + trackusage('dicomsort') + try: + sortsessions(sourcefolder = args.dicomsource, + subprefix = args.subprefix, + sesprefix = args.sesprefix, + folderscheme = args.folderscheme, + namescheme = args.namescheme, + pattern = args.pattern, + force = args.force, + dryrun = args.dryrun) + + except Exception: + trackusage('dicomsort_exception') + raise if __name__ == "__main__": diff --git a/bidscoin/utilities/rawmapper.py b/bidscoin/utilities/rawmapper.py index 61cf0e98..07b08a11 100755 --- a/bidscoin/utilities/rawmapper.py +++ b/bidscoin/utilities/rawmapper.py @@ -141,19 +141,24 @@ def main(): from bidscoin.cli._rawmapper import get_parser - trackusage('rawmapper') - args = get_parser().parse_args() - rawmapper(rawfolder = args.sourcefolder, - outfolder = args.outfolder, - sessions = args.sessions, - rename = args.rename, - force = args.clobber, - dicomfield = args.field, - wildcard = args.wildcard, - subprefix = args.subprefix, - sesprefix = args.sesprefix, - dryrun = args.dryrun) + + trackusage('rawmapper') + try: + rawmapper(rawfolder = args.sourcefolder, + outfolder = args.outfolder, + sessions = args.sessions, + rename = args.rename, + force = args.clobber, + dicomfield = args.field, + wildcard = args.wildcard, + subprefix = args.subprefix, + sesprefix = args.sesprefix, + dryrun = args.dryrun) + + except Exception: + trackusage('rawmapper_exception') + raise if __name__ == "__main__":