Skip to content

Commit

Permalink
do not use deprecated methods in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Jul 23, 2023
1 parent a154a5b commit f0c789a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
4 changes: 3 additions & 1 deletion impy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,9 @@ def big_imread(
-------
BigImgArray
"""
out = lazy_imread(path, chunks=chunks, name=name, squeeze=squeeze)
from impy.lazy import imread as lazy_imread_

out = lazy_imread_(path, chunks=chunks, name=name, squeeze=squeeze)
return BigImgArray(out.value, out.name, out.axes, out.source, out.metadata)

def read_meta(path: str) -> dict[str, Any]:
Expand Down
8 changes: 4 additions & 4 deletions impy/lazy/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def standard_exponential(
def random(
self,
size: int | tuple[int, ...] | None = None,
dtype = None,
dtype = np.float32,
*,
axes: AxesLike | None = None,
name: str | None = None,
Expand All @@ -175,10 +175,10 @@ def random(
) -> LazyImgArray:
size, name, axes = _normalize_like(size, name, axes, like)

arr = self._rng.random(size=size, dtype=dtype, chunks=chunks)
arr = self._rng.random(size=size, dtype=np.float64, chunks=chunks)
if np.isscalar(arr):
return arr
return asarray(arr, axes=axes, name=name)
return asarray(arr.astype(dtype, copy=False), axes=axes, name=name)

def normal(
self,
Expand All @@ -196,7 +196,7 @@ def normal(
arr = self._rng.normal(loc=loc, scale=scale, size=size, chunks=chunks)
if np.isscalar(arr):
return arr
return asarray(arr, axes=axes, name=name)
return asarray(arr.astype(np.float32), axes=axes, name=name)

def poisson(
self,
Expand Down
11 changes: 6 additions & 5 deletions impy/viewer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import napari
import os

from ..arrays import *
from .._const import Const
from ..core import imread, lazy_imread
from impy.arrays import *
from impy._const import Const
from impy.core import imread
from impy.lazy import imread as lazy_imread

if TYPE_CHECKING:
from ..frame import TrackFrame, PathFrame, AxesFrame
from ..roi import Roi
from impy.frame import TrackFrame, PathFrame, AxesFrame
from impy.roi import Roi
from napari.layers import Shapes


Expand Down
12 changes: 7 additions & 5 deletions impy/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
make_world_scale,
)

from ..collections import *
from ..arrays import *
from ..core import array as ip_array, aslazy as ip_aslazy
from ..axes import ScaleView, AxisLike, Axes, Axis
from .._const import Const
from impy.collections import *
from impy.arrays import *
from impy.core import array as ip_array
from impy.lazy import asarray as ip_aslazy
from impy.axes import ScaleView, AxisLike, Axes, Axis
from impy._const import Const

if TYPE_CHECKING:
from napari.components import LayerList
Expand All @@ -44,6 +45,7 @@ class napariViewers:
def __init__(self):
self._viewers: WeakValueDictionary[str, "napari.Viewer"] = WeakValueDictionary()
self._front_viewer: str = None
self._axes: Axes = None

def __repr__(self):
w = "".join([f"<{k}>" for k in self._viewers.keys()])
Expand Down
4 changes: 2 additions & 2 deletions rst/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ memory map of the image file that is split into smaller chunks, and passes it to
read" state. The image data is therefore loaded only when it is needed. Many useful functions in ``ImgArray``
are also implemented in ``LazyImgArray`` so that you can easily handle large datasets.

To read large images as ``LazyImgArray``, call ``lazy_imread`` instead. You can specify its chunk size using
To read large images as ``LazyImgArray``, call ``impy.lazy.imread`` instead. You can specify its chunk size using
``chunks`` parameter.

.. code-block:: python
img = ip.lazy_imread("path/to/image.tif", chunks=(1, "auto", "auto", "auto"))
img = ip.lazy.imread("path/to/image.tif", chunks=(1, "auto", "auto", "auto"))
img
.. code-block::
Expand Down
13 changes: 7 additions & 6 deletions tests/test_lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def test_functions_and_slicing(resource):
with ip.SetConst(RESOURCE=resource):
path = Path(__file__).parent / "_test_images" / "image_tzcyx.tif"
img = ip.lazy_imread(path, chunks=(4, 5, 2, 32, 32))
img = ip.lazy.imread(path, chunks=(4, 5, 2, 32, 32))
sl = "y=20:40;x=30:50;c=0;z=2,4"
assert_allclose(img[sl].compute(), img.compute()[sl])
assert_allclose(
Expand All @@ -27,7 +27,7 @@ def test_filters(fn, resource):
return
with ip.SetConst(RESOURCE=resource):
path = Path(__file__).parent / "_test_images" / "image_tzcyx.tif"
img = ip.lazy_imread(path, chunks=(4, 5, 2, 32, 32))
img = ip.lazy.imread(path, chunks=(4, 5, 2, 32, 32))

assert_allclose(
getattr(img, fn)().compute(),
Expand All @@ -37,7 +37,8 @@ def test_filters(fn, resource):

def test_numpy_function():
from dask.array.core import Array as DaskArray
img = ip.aslazy(ip.random.random_uint16((2, 3, 4)))
rng = ip.lazy.random.default_rng(0)
img = rng.random_uint16((2, 3, 4))
assert img.axes == "tyx"
assert isinstance(np.mean(img).compute(), float)
proj = np.mean(img, axis="y")
Expand All @@ -49,9 +50,9 @@ def test_numpy_function():
@pytest.mark.parametrize("opname", ["gt", "lt", "ge", "le", "eq", "ne"])
def test_operator(opname):
op = getattr(operator, opname)
rng = ip.random.default_rng(0)
img1 = ip.aslazy(rng.random_uint16((2, 3, 4)))
img2 = ip.aslazy(rng.random_uint16((2, 3, 4)))
rng = ip.lazy.random.default_rng(0)
img1 = rng.random_uint16((2, 3, 4))
img2 = rng.random_uint16((2, 3, 4))
assert_allclose(
op(img1, img2).compute(),
op(img1.compute(), img2.compute()),
Expand Down
12 changes: 12 additions & 0 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ def test_tiled(resource):
img.tiled(chunks=(40, 50, 50)).dog_filter(low_sigma=1.0, fourier=True)
img.tiled(chunks=(40, 50, 50)).log_filter(sigma=1.0)

def test_lazy_tiled(resource):
with ip.SetConst(RESOURCE=resource):
rng = ip.lazy.random.default_rng(1111)

img = rng.random(size=(120, 120, 120), axes="zyx")
img.tiled(chunks=(40, 50, 50)).lowpass_filter()
img.tiled(chunks=(40, 50, 50)).gaussian_filter(sigma=1.0)
img.tiled(chunks=(40, 50, 50)).gaussian_filter(sigma=1.0, fourier=True)
img.tiled(chunks=(40, 50, 50)).dog_filter(low_sigma=1.0)
img.tiled(chunks=(40, 50, 50)).dog_filter(low_sigma=1.0, fourier=True)
img.tiled(chunks=(40, 50, 50)).log_filter(sigma=1.0)

@pytest.mark.parametrize("order", [1, 3])
def test_drift_correction(order: int):
img = ip.random.normal(size=(5, 10, 3, 120, 120), axes="tzcyx")
Expand Down

0 comments on commit f0c789a

Please sign in to comment.