Skip to content

Commit

Permalink
Refactoring + deepcopy bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Sep 23, 2024
1 parent 2c4c9eb commit 5f7dc24
Show file tree
Hide file tree
Showing 20 changed files with 888 additions and 809 deletions.
13 changes: 6 additions & 7 deletions bidscoin/bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ def test_bidsmap(bidsmapfile: str):
return bidsmap.validate(1)


def test_bidscoin(bidsmapfile: Path, options: dict=None, testplugins: bool=True, testgui: bool=True, testtemplate: bool=True) -> int:
def test_bidscoin(bidsmapfile, options: dict=None, testplugins: bool=True, testgui: bool=True, testtemplate: bool=True) -> int:
"""
Performs a bidscoin installation test
:param bidsmapfile: The bidsmap or the full pathname/basename of the bidsmap yaml-file
:param bidsmapfile: The full pathname/basename of the bidsmap yaml-file or the bidsmap object itself
:param options: The bidscoin options. If empty, the default options are used
:return: 0 if the test was successful, otherwise 1
"""
Expand Down Expand Up @@ -568,16 +568,15 @@ def test_bidscoin(bidsmapfile: Path, options: dict=None, testplugins: bool=True,
list_executables(True)

# Test the plugins
options = bidsmap['Options'] if not options and bidsmap else {}
if not options.get('plugins'):
if not bidsmap.plugins:
LOGGER.warning('No plugins found in the bidsmap (BIDScoin will likely not do anything)')
if testplugins:

# Show an overview of the plugins and show the test results
list_plugins(True)
for plugin in pluginfolder.glob('*.py'):
if plugin.stem != '__init__':
errorcode = test_plugin(plugin.stem, options['plugins'].get(plugin.stem,{}) if options else {})
errorcode = test_plugin(plugin.stem, bidsmap.plugins.get(plugin.stem, {}))
success = not errorcode and success
if errorcode:
LOGGER.warning(f"Failed test: {plugin.stem}")
Expand Down Expand Up @@ -705,9 +704,9 @@ def main():
settracking(value=args.tracking)
reportcredits(args=args.credits)

except Exception:
except Exception as error:
trackusage('bidscoin_exception')
raise
raise error


if __name__ == "__main__":
Expand Down
345 changes: 196 additions & 149 deletions bidscoin/bids.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bidscoin/bidsapps/deface.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ def main():
try:
deface(**vars(args))

except Exception:
except Exception as error:
trackusage('deface_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions bidscoin/bidsapps/echocombine.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ def main():
try:
echocombine(**vars(args))

except Exception:
except Exception as error:
trackusage('echocombine_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions bidscoin/bidsapps/fixmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def fixmeta(bidsfolder: str, pattern: str, metadata: dict, participant: list, bi
LOGGER.info(f"Command: fixmeta {' '.join(sys.argv[1:])}")

# Load the bidsmap data (-> plugins)
bidsmap, _ = bids.BidsMap(Path(bidsmap or 'bidsmap.yaml'), bidsdir/'code'/'bidscoin', checks=(False, False, False))
if not bidsmap:
bidsmap, _ = bids.BidsMap(bidsmap_template, checks=(False, False, False))
bidsmap = bids.BidsMap(Path(bidsmap or 'bidsmap.yaml'), bidsdir/'code'/'bidscoin', checks=(False, False, False))
if not bidsmap.filepath:
bidsmap = bids.BidsMap(bidsmap_template, checks=(False, False, False))
plugins = bidsmap.plugins
provdata = bids.bidsprov(bidsdir)

Expand Down Expand Up @@ -117,9 +117,9 @@ def main():
try:
fixmeta(**vars(args))

except Exception:
except Exception as error:
trackusage('fixmeta_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions bidscoin/bidsapps/medeface.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ def main():
try:
medeface(**vars(args))

except Exception:
except Exception as error:
trackusage('medeface_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions bidscoin/bidsapps/skullstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ def main():
try:
skullstrip(**vars(args))

except Exception:
except Exception as error:
trackusage('skullstrip_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
14 changes: 7 additions & 7 deletions bidscoin/bidsapps/slicereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_options(options: list) -> str:
for n, option in enumerate(options):
if options[n-1] == 'l': continue # Skip checking the LUT string
if not (option in ('L','l','i','e','t','n','u','s','c') or option.replace('.','').replace('-','').isdecimal()):
print(f"Invalid OPTIONS: {' '.join(options)}"); sys.exit(2)
raise SystemExit(f"[ERROR] Invalid OPTIONS: {' '.join(options)}")
if option.isalpha():
options[n] = '-' + options[n]

Expand All @@ -68,18 +68,18 @@ def parse_outputs(outputargs: list, name: str) -> tuple:
isnumber = lambda arg: arg.replace('.','').replace('-','').isdecimal()
for n, outputarg in enumerate(outputargs):
if not (outputarg in ('x', 'y', 'z', 'a', 'A', 'S', 'LF') or isnumber(outputarg)):
print(f"Invalid {name}: '{outputarg}' in '{' '.join(outputargs)}'"); sys.exit(2)
raise SystemExit(f"[ERROR] Invalid {name}: '{outputarg}' in '{' '.join(outputargs)}'")
if outputarg.isalpha() and outputarg != 'LF':
slices += f"{'' if n==0 else '-' if outputargs[n-1]=='LF' else '+'} slice_tmp{n}.png "
if outputarg == 'a':
outputs += f"-{outputarg} slice_tmp{n}.png "
elif outputarg in ('x', 'y', 'z', 'A'):
if not isnumber(outputargs[n+1]):
print(f"Invalid {name}: '{outputargs[n+1]}' in '{' '.join(outputargs)}'"); sys.exit(2)
raise SystemExit(f"[ERROR] Invalid {name}: '{outputargs[n+1]}' in '{' '.join(outputargs)}'")
outputs += f"-{outputarg} {outputargs[n+1]} slice_tmp{n}.png "
elif outputarg == 'S':
if not (isnumber(outputargs[n+1]) and isnumber(outputargs[n+2])):
print(f"Invalid {name}: {outputarg} >> '{' '.join(outputargs)}'"); sys.exit(2)
raise SystemExit(f"[ERROR] Invalid {name}: {outputarg} >> '{' '.join(outputargs)}'")
outputs += f"-{outputarg} {outputargs[n+1]} {outputargs[n+2]} slice_tmp{n}.png "

return outputs, slices
Expand Down Expand Up @@ -135,7 +135,7 @@ def slicer_append(inputimage: Path, operations: str, outlineimage: Path, mainopt
if process.stderr or process.returncode != 0:
LOGGER.warning(f"{command}\nErrorcode {process.returncode}:\n{process.stdout}\n{process.stderr}")
if process.returncode != 0:
sys.exit(process.returncode)
raise SystemExit(process.returncode)


def slicereport(bidsfolder: str, pattern: str, outlinepattern: str, outlineimage: str, participant: list, reportfolder: str, xlinkfolder: str, qcscores: list, cluster: str, operations: str, suboperations: str, options: list, outputs: list, suboptions: list, suboutputs: list):
Expand Down Expand Up @@ -330,9 +330,9 @@ def main():
try:
slicereport(**vars(args))

except Exception:
except Exception as error:
trackusage('slicereport_exception')
raise
raise error


if __name__ == '__main__':
Expand Down
21 changes: 10 additions & 11 deletions bidscoin/bidscoiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def bidscoiner(sourcefolder: str, bidsfolder: str, participant: list=(), force:
bidscoinfolder = bidsfolder/'code'/'bidscoin'
bidscoinfolder.mkdir(parents=True, exist_ok=True)
if not rawfolder.is_dir():
print(f"Rawfolder '{rawfolder}' not found")
return
raise SystemExit(f"\n[ERROR] Exiting the program because your sourcefolder argument '{sourcefolder}' was not found")

# Start logging
bcoin.setup_logging(bidscoinfolder/'bidscoiner.log')
Expand Down Expand Up @@ -81,7 +80,7 @@ def bidscoiner(sourcefolder: str, bidsfolder: str, participant: list=(), force:

# Create a README file if it does not exist
readme_file = bidsfolder/'README'
if not (readme_file.is_file() or next(bidsfolder.glob('README.*'))):
if not (readme_file.is_file() or next(bidsfolder.glob('README.*'), None)):
LOGGER.info(f"Creating a template README file (adjust it to your needs): {readme_file}")
try:
urllib.request.urlretrieve('https://raw.githubusercontent.com/bids-standard/bids-starter-kit/main/templates/README.MD', readme_file)
Expand Down Expand Up @@ -150,7 +149,7 @@ def bidscoiner(sourcefolder: str, bidsfolder: str, participant: list=(), force:

# Check if we should skip the session-folder
datasource = bids.get_datasource(subject, bidsmap.plugins)
subid,_ = datasource.subid_sesid(bidsmap[datasource.dataformat]['subject'], bidsmap[datasource.dataformat]['session'])
subid,_ = datasource.subid_sesid(bidsmap.dataformat(datasource.dataformat).subject, bidsmap.dataformat(datasource.dataformat).session)
if (bidsfolder/subid).is_dir() and not force:
LOGGER.info(f">>> Skipping already processed subject: {bidsfolder/subid} (you can use the -f option to overrule)")
continue
Expand Down Expand Up @@ -263,15 +262,15 @@ def bidscoiner(sourcefolder: str, bidsfolder: str, participant: list=(), force:
if not datasource.dataformat:
LOGGER.info(f">>> No datasources found in '{sesfolder}'")
continue
subid = bidsmap[datasource.dataformat]['subject']
sesid = bidsmap[datasource.dataformat]['session']
subid = bidsmap.dataformat(datasource.dataformat).subject
sesid = bidsmap.dataformat(datasource.dataformat).session
subid, sesid = datasource.subid_sesid(subid, sesid or '')
bidssession = bidsfolder/subid/sesid # TODO: Support DICOMDIR with multiple subjects (as in PYDICOMDIR)
if not force and bidssession.is_dir():
datatypes = set()
for dataformat in dataformats:
for datatype in lsdirs(bidssession): # See what datatypes we already have in the bids session-folder
if list(datatype.iterdir()) and bidsmap[dataformat].get(datatype.name): # See if we are going to add data for this datatype
for datatype in [dtype for dtype in lsdirs(bidssession) if next(dtype.iterdir())]: # See what non-empty datatypes we already have in the bids session-folder
for dataformat in bidsmap.dataformats:
if datatype.name in dataformat.datatypes: # See if we are going to add data for this datatype
datatypes.add(datatype.name)
if datatypes:
LOGGER.info(f">>> Skipping processed session: {bidssession} already has {datatypes} data (you can carefully use the -f option to overrule)")
Expand Down Expand Up @@ -320,9 +319,9 @@ def main():
try:
bidscoiner(**vars(args))

except Exception:
except Exception as error:
trackusage('bidscoiner_exception')
raise
raise error


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 5f7dc24

Please sign in to comment.