Skip to content

Commit

Permalink
override old files and log a warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Dec 14, 2023
1 parent 321705e commit eae59a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
30 changes: 8 additions & 22 deletions giga_connectome/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,27 +180,13 @@ def get_subject_lists(


def check_path(path: Path):
"""Check if given path (file or dir) already exists, and if so returns a
new path with _<n> appended (n being the number of paths with the same name
that exist already).
"""
path = path.resolve()
ext = path.suffix
path_parent = path.parent
"""Check if given path (file or dir) already exists.
If so, a warning is logged.
"""
path = path.absolute()
if path.exists():
similar_paths = [
str(p).replace(ext, "")
for p in path_parent.glob(f"{path.stem}_*{ext}")
]
existing_numbers = [
int(p.split("_")[-1])
for p in similar_paths
if p.split("_")[-1].isdigit()
]
n = str(max(existing_numbers) + 1) if existing_numbers else "1"
path = path_parent / f"{path.stem}_{n}{ext}"

gc_log.debug(f"Specified path already exists, using {path} instead.")

return path
gc_log.warning(
f"Specified path already exists:\n\t{path}\n"
"Old file will be overwritten"
)
4 changes: 2 additions & 2 deletions giga_connectome/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def workflow(args):
f"sub-{subject}_atlas-{atlas['name']}"
f"_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path)
utils.check_path(connectome_path)

gc_log.info(f"Generate subject level connectomes: sub-{subject}")

Expand All @@ -106,7 +106,7 @@ def workflow(args):
connectome_path = (
output_dir / f"atlas-{atlas['name']}_desc-{strategy['name']}.h5"
)
connectome_path = utils.check_path(connectome_path)
utils.check_path(connectome_path)

gc_log.info(connectome_path)
gc_log.info("Generate subject level connectomes")
Expand Down

0 comments on commit eae59a6

Please sign in to comment.