From 9b3155fe84af7fe7678c9c98769fb28f1bd63092 Mon Sep 17 00:00:00 2001 From: camisowers <38049893+camisowers@users.noreply.github.com> Date: Wed, 26 Jul 2023 14:39:53 -0700 Subject: [PATCH] skip any previously rescaled images (#398) --- src/toffy/rosetta.py | 10 ++++++---- tests/rosetta_test.py | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/toffy/rosetta.py b/src/toffy/rosetta.py index 6e714a60..dcb512c2 100644 --- a/src/toffy/rosetta.py +++ b/src/toffy/rosetta.py @@ -677,14 +677,16 @@ def rescale_raw_imgs(img_out_dir, scale=200): fov_dir = os.path.join(img_out_dir, fov) # create subdirectory for the new images sub_dir = os.path.join(fov_dir, "rescaled") - os.makedirs(sub_dir) + if not os.path.exists(sub_dir): + os.makedirs(sub_dir) chans = io_utils.list_files(fov_dir) # rescale each channel image for chan in chans: - img = io.imread(os.path.join(fov_dir, chan)) - img = (img / scale).astype("float32") fname = os.path.join(sub_dir, chan) - image_utils.save_image(fname, img) + if not os.path.exists(fname): + img = io.imread(os.path.join(fov_dir, chan)) + img = (img / scale).astype("float32") + image_utils.save_image(fname, img) def generate_rosetta_test_imgs( diff --git a/tests/rosetta_test.py b/tests/rosetta_test.py index 428de007..83c84224 100644 --- a/tests/rosetta_test.py +++ b/tests/rosetta_test.py @@ -1,6 +1,7 @@ import copy import os import tempfile +import time from pathlib import Path import numpy as np @@ -639,10 +640,17 @@ def test_rescale_raw_imgs(): # test successful rescale of data rescaled_img_data = load_utils.load_imgs_from_tree(temp_dir, "rescaled") + create_time = Path(os.path.join(temp_dir, fovs[0], "rescaled")).stat().st_ctime assert rescaled_img_data.all() == (img_data / 200).all() assert rescaled_img_data.dtype == "float32" + # re-run function and check the files are not re-written + time.sleep(3) + rosetta.rescale_raw_imgs(temp_dir) + modify_time = Path(os.path.join(temp_dir, fovs[0], "rescaled")).stat().st_mtime + assert np.isclose(modify_time, create_time) + def create_rosetta_comp_structure( raw_data_dir,