Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update superdsm.io module #10

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/validate_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ jobs:
validate_conditional_checks:
name: Validate conditional checks
runs-on: ubuntu-latest
timeout-minutes: 1440

steps:

- uses: blend/[email protected]
with:
interval: 20s
timeout: 1440m
checks-yaml: |
- job: 'Test: U2OS'
paths:
Expand Down
8 changes: 4 additions & 4 deletions superdsm/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions superdsm/export.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
Expand All @@ -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:
Expand All @@ -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')
Expand Down
40 changes: 29 additions & 11 deletions superdsm/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os, warnings


def imwrite(filepath, img, shape=None, antialias=False):
"""Writes an image to a file.
def imsave(filepath, img, shape=None, antialias=False):
"""
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.
Expand Down Expand Up @@ -32,23 +33,40 @@ def imwrite(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

2 changes: 1 addition & 1 deletion tests/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading