Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sitic committed Sep 20, 2023
1 parent c986d27 commit b57b87b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 102 deletions.
9 changes: 5 additions & 4 deletions optimap/image/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ def load_image(filename, as_gray=False, **kwargs):
_print(f'loading image from {fn.absolute()} ... ')

if fn.suffix == '.npy':
image = np.load(filename, **kwargs)
image = np.load(fn, **kwargs)
else:
# image = skimage.io.imread(filename, as_gray=as_gray, **kwargs)
image = cv2.imread(filename, cv2.IMREAD_ANYDEPTH | cv2.IMREAD_UNCHANGED)
image = cv2.imread(str(fn),
cv2.IMREAD_ANYDEPTH | cv2.IMREAD_UNCHANGED)
if as_gray and image.ndim == 3:
if image.shape[2] == 3:
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Expand Down Expand Up @@ -131,7 +132,7 @@ def save_image(image, filename, **kwargs):
_print(f"saving image to {Path(filename).absolute()}")
fn = Path(filename)
if fn.suffix == '.npy':
np.save(filename, image, **kwargs)
np.save(fn, image, **kwargs)
else:
# skimage.io.imsave(filename, image, **kwargs)
cv2.imwrite(filename, image, **kwargs)
cv2.imwrite(str(fn), image, **kwargs)
8 changes: 4 additions & 4 deletions optimap/video/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def load_tiff(filename, start_frame=0, end_frame=None, step=1, use_mmap=False):
)
return video

def load_image_folder(folder_path, prefix="", start_frame=0, end_frame=None, step=1):
def load_image_folder(path, prefix="", start_frame=0, end_frame=None, step=1):
"""
Loads a sequences of images from a folder as a video.
Expand All @@ -53,7 +53,7 @@ def load_image_folder(folder_path, prefix="", start_frame=0, end_frame=None, ste
Parameters
----------
folder_path : str or pathlib.Path
path : str or pathlib.Path
Path to folder containing .tiff images
prefix : str, optional
Prefix of the files to load
Expand All @@ -69,8 +69,8 @@ def load_image_folder(folder_path, prefix="", start_frame=0, end_frame=None, ste
np.ndarray
3D numpy array containing the loaded images
"""
_print(f"loading video from series of .tif/.tiff-images in folder '{folder_path}'")
path = Path(folder_path)
_print(f"loading video from series of images files in folder '{path}'")
path = Path(path)
if not path.is_dir():
raise ValueError(f"'{path}' is not a directory")

Expand Down
44 changes: 22 additions & 22 deletions tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import optimap as om
import numpy as np
from tempfile import TemporaryDirectory
from pathlib import Path

def test_image_export():
def test_image_export(tmpdir):
img = np.random.rand(100, 100).astype(np.float32)
with TemporaryDirectory() as tmpdir:
fn = tmpdir + "/test.npy"
om.image.save_image(img, fn)
img2 = om.image.load_image(fn)
assert np.allclose(img, img2)

# Test if 16-bit PNG and TIFF export works
with TemporaryDirectory() as tmpdir:
img = (img * 65535).astype(np.uint16)
fn = tmpdir + "/test.png"
om.image.save_image(img, fn)
img2 = om.image.load_image(fn)
assert img.dtype == img2.dtype
assert np.allclose(img, img2)
fn = tmpdir / "test.npy"
om.image.save_image(img, Path(fn))
img2 = om.image.load_image(Path(fn))
assert np.allclose(img, img2)

with TemporaryDirectory() as tmpdir:
img = (img * 65535).astype(np.uint16)
fn = tmpdir + "/test.tiff"
om.image.save_image(img, fn)
img2 = om.image.load_image(fn)
assert img.dtype == img2.dtype
assert np.allclose(img, img2)
def test_16bit_export(tmpdir):
"""Test if 16-bit PNG and TIFF export works."""
img = np.random.rand(100, 100).astype(np.float32)
img = (img * 65535).astype(np.uint16)

fn = tmpdir / "test.png"
om.image.save_image(img, fn)
img2 = om.image.load_image(fn)
assert img.dtype == img2.dtype
assert np.allclose(img, img2)

fn = tmpdir / "test.tiff"
om.image.save_image(img, fn)
img2 = om.image.load_image(fn)
assert img.dtype == img2.dtype
assert np.allclose(img, img2)
141 changes: 69 additions & 72 deletions tests/test_video_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,99 @@
import optimap as om


def test_npy_video():
def test_npy_video(tmpdir):
vid = np.random.random((8, 4, 6)).astype(np.float32)
with TemporaryDirectory() as tmpdir:
filename = tmpdir + "/test.npy"
om.save_video(vid, filename)
assert Path(filename).exists()

video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)
filename = tmpdir / "test.npy"
om.save_video(vid, filename)
assert Path(filename).exists()

video = om.load_video(filename, start_frame=4)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[4:])
video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(filename, frames=4)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[:4])
video = om.load_video(filename, start_frame=4)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[4:])

video = om.load_video(filename, step=2)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[::2])
video = om.load_video(filename, frames=4)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[:4])

video = om.load_video(filename, use_mmap=True)
assert np.all(video == vid)
assert video.flags["WRITEABLE"] == False
video = om.load_video(filename, step=2)
assert video.shape == (4, 4, 6)
assert np.all(video == vid[::2])

video = om.load_video(filename, use_mmap=True)
assert np.all(video == vid)
assert video.flags["WRITEABLE"] == False

def test_tiff_folder():

def test_tiff_folder(tmpdir):
vid = np.random.random((10, 4, 6)).astype(np.float32)

with TemporaryDirectory() as tmpdir:
om.save_image_sequence(vid, directory=tmpdir, suffix=".tiff")
assert len(list(Path(tmpdir).glob("*.tiff"))) == 10
om.save_image_sequence(vid, directory=tmpdir, suffix=".tiff")
assert len(list(Path(tmpdir).glob("*.tiff"))) == 10

video = om.load_video(tmpdir)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)
video = om.load_video(tmpdir)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(tmpdir, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])
video = om.load_video(tmpdir, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])


def test_png_folder():
def test_png_folder(tmpdir):
vid = np.random.random((10, 4, 6)) * 16535
vid = vid.astype(np.uint16)

with TemporaryDirectory() as tmpdir:
om.save_image_sequence(vid, directory=tmpdir, suffix=".png")
assert len(list(Path(tmpdir).glob("*.png"))) == 10
om.save_image_sequence(vid, directory=tmpdir, suffix=".png")
assert len(list(Path(tmpdir).glob("*.png"))) == 10

video = om.load_video(tmpdir)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)
video = om.load_video(tmpdir)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(tmpdir, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])
video = om.load_video(tmpdir, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])


def test_tiff_stack():
def test_tiff_stack(tmpdir):
vid = np.random.random((10, 4, 6)).astype(np.float32)
with TemporaryDirectory() as tmpdir:
filename = tmpdir + "/test.tiff"
om.save_video(vid, filename)
assert Path(filename).exists()
assert Path(filename).is_file()
filename = tmpdir / "test.tiff"
om.save_video(vid, filename)
assert Path(filename).exists()
assert Path(filename).is_file()

video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)
video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(filename, start_frame=1, frames=3, step=2, use_mmap=True)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])
assert video.flags["WRITEABLE"] == False
video = om.load_video(filename, start_frame=1, frames=3, step=2, use_mmap=True)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])
assert video.flags["WRITEABLE"] == False


def test_matlab():
def test_matlab(tmpdir):
vid = np.random.random((10, 4, 6)).astype(np.float32)
with TemporaryDirectory() as tmpdir:
filename = tmpdir + "/test.mat"
om.save_video(vid, filename)
assert Path(filename).exists()
assert Path(filename).is_file()

video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(filename, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])

filename = tmpdir / "test.mat"
om.save_video(vid, filename)
assert Path(filename).exists()
assert Path(filename).is_file()

video = om.load_video(filename)
assert video.shape == vid.shape
assert video.dtype == vid.dtype
assert np.all(video == vid)

video = om.load_video(filename, start_frame=1, frames=3, step=2)
assert video.shape == (3, 4, 6)
assert np.all(video == vid[1:6:2])

0 comments on commit b57b87b

Please sign in to comment.