From bffd5e9581169e3629310a7276d6b00e96cbcdc3 Mon Sep 17 00:00:00 2001 From: Taylor Salo Date: Wed, 26 May 2021 00:41:48 -0400 Subject: [PATCH] [ENH] Add overwrite option to transform_images (#509) * Add overwrite option to transform_images. * Apply suggestions from code review --- nimare/transforms.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nimare/transforms.py b/nimare/transforms.py index b64525c16..e85d26750 100644 --- a/nimare/transforms.py +++ b/nimare/transforms.py @@ -18,7 +18,7 @@ LGR = logging.getLogger(__name__) -def transform_images(images_df, target, masker, metadata_df=None, out_dir=None): +def transform_images(images_df, target, masker, metadata_df=None, out_dir=None, overwrite=False): """Generate images of a given type from other image types and write out to files. Parameters @@ -37,6 +37,8 @@ def transform_images(images_df, target, masker, metadata_df=None, out_dir=None): out_dir : :obj:`str` or :obj:`None`, optional Path to output directory. If None, use folder containing first image for each study in ``images_df``. + overwrite : :obj:`bool`, optional + Whether to overwrite existing files or not. Default is False. Returns ------- @@ -84,7 +86,11 @@ def transform_images(images_df, target, masker, metadata_df=None, out_dir=None): # Get converted data img = resolve_transforms(target, available_data, new_masker) if img is not None: - img.to_filename(new_file) + if overwrite or not op.isfile(new_file): + img.to_filename(new_file) + else: + LGR.debug("Image already exists. Not overwriting.") + images_df.loc[images_df["id"] == id_, target] = new_file else: images_df.loc[images_df["id"] == id_, target] = None