From b4747508a9a6187e31fd94cf20632fe3e525f506 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 13 Mar 2024 06:42:39 +0000 Subject: [PATCH 1/6] Rename `io.imwrite` to `io.imsave` --- superdsm/batch.py | 8 ++++---- superdsm/export.py | 8 ++++---- superdsm/io.py | 2 +- tests/testsuite.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/superdsm/batch.py b/superdsm/batch.py index 1862a49a..72a3a684 100644 --- a/superdsm/batch.py +++ b/superdsm/batch.py @@ -2,7 +2,7 @@ from .objects import _compute_objects from ._aux import mkdir, is_subpath, copy_dict from .output import get_output, Text -from .io import imread, imwrite +from .io import imread, imsave from .render import rasterize_labels, render_ymap, render_atoms, render_adjacencies, render_result_over_image from .automation import create_config from .config import Config @@ -74,7 +74,7 @@ def write_adjacencies_image(name, data): ymap = render_ymap(data) ymap = render_atoms(data, override_img=ymap, border_color=(0,0,0), border_radius=1) img = render_adjacencies(data, override_img=ymap, edge_color=(0,1,0), endpoint_color=(0,1,0)) - imwrite(adj_filepath, img) + imsave(adj_filepath, img) atomic_stage = pipeline.stages[pipeline.find('c2f-region-analysis')] atomic_stage.add_callback('end', write_adjacencies_image) @@ -86,12 +86,12 @@ def write_adjacencies_image(name, data): if seg_border is None: seg_border = 8 img_overlay = render_result_over_image(result_data, border_width=seg_border) mkdir(pathlib.Path(overlay_filepath).parents[0]) - imwrite(overlay_filepath, img_overlay) + imsave(overlay_filepath, img_overlay) if seg_filepath is not None: seg_result = rasterize_labels(result_data, **rasterize_kwargs) mkdir(pathlib.Path(seg_filepath).parents[0]) - imwrite(seg_filepath, seg_result) + imsave(seg_filepath, seg_result) return result_data, timings diff --git a/superdsm/export.py b/superdsm/export.py index 3cc9fb95..bef09906 100644 --- a/superdsm/export.py +++ b/superdsm/export.py @@ -1,7 +1,7 @@ from .render import colorize_labels, normalize_image, render_ymap, render_result_over_image, render_atoms, render_adjacencies from .batch import Task, _resolve_timings_key from .output import get_output -from .io import imread, imwrite +from .io import imread, imsave import numpy as np import gzip, dill, pathlib @@ -93,7 +93,7 @@ img = imread(im_filepath) if args.enhance: img = normalize_image(img) outputfile.parents[0].mkdir(parents=True, exist_ok=True) - imwrite(str(outputfile), img) + imsave(str(outputfile), img) elif args.mode in ('seg', 'fgc', 'adj', 'atm'): if args.mode in ('fgc', 'adj', 'atm'): task.last_stage = 'c2f-region-analysis' @@ -105,7 +105,7 @@ ymap_legend = np.vstack([ymap_legend] * 10) ymap_legendfile = outdir / f'ymap_legend.png' out.write(f'\nWriting legend: {ymap_legendfile}') - imwrite(str(ymap_legendfile), ymap_legend) + imsave(str(ymap_legendfile), ymap_legend) data = task.run(one_shot=True, force=True, evaluation='none', out=out) out.write('\nRunning export:') for image_id in task.file_ids: @@ -124,7 +124,7 @@ img = render_adjacencies(dataframe, override_img=ymap, edge_color=(0,1,0), endpoint_color=(0,1,0)) elif args.mode == 'atm': img = render_atoms(dataframe, border_color=(0,1,0), border_radius=border_width // 2, normalize_img=args.enhance) - imwrite(str(outputfile), img) + imsave(str(outputfile), img) out.write(f' Exported {outputfile}') out.write('\n') out.write(f'Exported {len(task.file_ids)} files') diff --git a/superdsm/io.py b/superdsm/io.py index 34111b0a..4e0f0621 100644 --- a/superdsm/io.py +++ b/superdsm/io.py @@ -2,7 +2,7 @@ import os, warnings -def imwrite(filepath, img, shape=None, antialias=False): +def imsave(filepath, img, shape=None, antialias=False): """Writes an image to a file. :param filepath: The path of the file to be written. diff --git a/tests/testsuite.py b/tests/testsuite.py index f4489d43..ae4603c7 100755 --- a/tests/testsuite.py +++ b/tests/testsuite.py @@ -66,7 +66,7 @@ def validate_image(test, name, img): except: actual_path = root_dir / 'actual' / name actual_path.parent.mkdir(parents=True, exist_ok=True) - superdsm.io.imwrite(str(actual_path), img) + superdsm.io.imsave(str(actual_path), img) raise From c62bc8040df10573ca5b82f34ca38cec86e3bb21 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 13 Mar 2024 06:51:05 +0000 Subject: [PATCH 2/6] Add `force_filetype` argument for `io.imread` --- superdsm/io.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/superdsm/io.py b/superdsm/io.py index 4e0f0621..cd16e290 100644 --- a/superdsm/io.py +++ b/superdsm/io.py @@ -3,7 +3,8 @@ def imsave(filepath, img, shape=None, antialias=False): - """Writes an image to a file. + """ + Writes an image to a file. :param filepath: The path of the file to be written. :param img: A ``numpy.ndarray`` object corresponding to the image data. @@ -32,23 +33,40 @@ def imsave(filepath, img, shape=None, antialias=False): skimage.io.imsave(filepath, img) -def imread(filepath, **kwargs): - """Loads an image from file. +def imread(filepath, force_filetype=None, **kwargs): + """ + Loads an image from file. Supported file extensions are PNG, TIF, and TIFF. + + :param force_filetype: Pretend that the file has a specific extension. """ + + if force_filetype is not None: + force_filetype = force_filetype.lower() + assert force_filetype in ('png', 'tif', 'tiff') + filetype = force_filetype + + else: + filepath_parts = str(filepath).split('.') + assert len(filepath_parts) >= 2, f'Failed to determine file extension: {filepath}' + filetype = filepath_parts[-1].lower() + filepath = os.path.expanduser(filepath) if not os.path.exists(filepath) or not os.path.isfile(filepath): - raise ValueError('not a file: %s' % filepath) - fp_lowercase = filepath.lower() - if 'as_gray' not in kwargs: kwargs['as_gray'] = True - if fp_lowercase.endswith('.png'): + raise ValueError(f'Not a file: {filepath}') + + if 'as_gray' not in kwargs: + kwargs['as_gray'] = True + + if filetype == 'png': img = skimage.io.imread(filepath, **kwargs) - elif fp_lowercase.endswith('.tif') or fp_lowercase.endswith('.tiff'): + + elif filetype in ('tif', 'tiff'): with warnings.catch_warnings(): warnings.filterwarnings('ignore', category=RuntimeWarning) img = skimage.io.imread(filepath, plugin='tifffile', **kwargs) + else: - raise ValueError('unknown file extension: %s' % filepath) + raise ValueError(f'Unknown file extension: {filepath}') return img - From d3d5f2a69a1c227a9072cdc7890c572805dc0291 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 13 Mar 2024 13:12:02 +0100 Subject: [PATCH 3/6] Increase timeout for `validate_conditional_checks` --- .github/workflows/validate_pr.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/validate_pr.yml b/.github/workflows/validate_pr.yml index 441ba0a3..accaaa41 100644 --- a/.github/workflows/validate_pr.yml +++ b/.github/workflows/validate_pr.yml @@ -20,6 +20,7 @@ jobs: validate_conditional_checks: name: Validate conditional checks runs-on: ubuntu-latest + timeout-minutes: 1440 steps: From 13e5bca359f26cb1d4817c085c6679e204318311 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 13 Mar 2024 14:00:54 +0100 Subject: [PATCH 4/6] Increase `blend/require-conditional-status-checks` timeout --- .github/workflows/validate_pr.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/validate_pr.yml b/.github/workflows/validate_pr.yml index accaaa41..55a484a3 100644 --- a/.github/workflows/validate_pr.yml +++ b/.github/workflows/validate_pr.yml @@ -27,6 +27,7 @@ jobs: - uses: blend/require-conditional-status-checks@2022.02.04 with: interval: 20s + timeout: 1440m checks-yaml: | - job: 'Test: U2OS' paths: From d13164f3a21be8dc7ff40be84b5c1f151c4bdadb Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 10 Jul 2024 06:57:10 +0000 Subject: [PATCH 5/6] Revert "Increase timeout for `validate_conditional_checks`" This reverts commit d3d5f2a69a1c227a9072cdc7890c572805dc0291. --- .github/workflows/validate_pr.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/validate_pr.yml b/.github/workflows/validate_pr.yml index 55a484a3..c91f29f7 100644 --- a/.github/workflows/validate_pr.yml +++ b/.github/workflows/validate_pr.yml @@ -20,7 +20,6 @@ jobs: validate_conditional_checks: name: Validate conditional checks runs-on: ubuntu-latest - timeout-minutes: 1440 steps: From 4f1e4febb968967d2761447c80cba40e3f76a297 Mon Sep 17 00:00:00 2001 From: Leonid Kostrykin Date: Wed, 10 Jul 2024 06:57:47 +0000 Subject: [PATCH 6/6] Revert "Increase `blend/require-conditional-status-checks` timeout" This reverts commit 13e5bca359f26cb1d4817c085c6679e204318311. --- .github/workflows/validate_pr.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/validate_pr.yml b/.github/workflows/validate_pr.yml index c91f29f7..441ba0a3 100644 --- a/.github/workflows/validate_pr.yml +++ b/.github/workflows/validate_pr.yml @@ -26,7 +26,6 @@ jobs: - uses: blend/require-conditional-status-checks@2022.02.04 with: interval: 20s - timeout: 1440m checks-yaml: | - job: 'Test: U2OS' paths: