File: benchmark_transform_warp.py

package info (click to toggle)
skimage 0.26.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,720 kB
  • sloc: python: 61,600; cpp: 2,592; ansic: 1,591; xml: 1,342; javascript: 1,267; makefile: 135; sh: 16
file content (75 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
from skimage.transform import SimilarityTransform, warp, resize_local_mean
import warnings
import functools
import inspect

try:
    from skimage.util.dtype import _convert as convert
except ImportError:
    from skimage.util.dtype import convert


class WarpSuite:
    params = (
        [np.uint8, np.uint16, np.float32, np.float64],
        [128, 1024, 4096],
        [0, 1, 3],
        # [np.float32, np.float64]
    )
    # param_names = ['dtype_in', 'N', 'order', 'dtype_tform']
    param_names = ['dtype_in', 'N', 'order']

    # def setup(self, dtype_in, N, order, dtype_tform):
    def setup(self, dtype_in, N, order):
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "Possible precision loss")
            self.image = convert(np.random.random((N, N)), dtype=dtype_in)
        self.tform = SimilarityTransform(
            scale=1, rotation=np.pi / 10, translation=(0, 4)
        )
        self.tform.params = self.tform.params.astype('float32')
        self.order = order

        if 'dtype' in inspect.signature(warp).parameters:
            self.warp = functools.partial(warp, dtype=self.image.dtype)
        else:
            # Keep a call to functools to have the same number of python
            # function calls
            self.warp = functools.partial(warp)

    # def time_same_type(self, dtype_in, N, order, dtype_tform):
    def time_same_type(self, dtype_in, N, order):
        """Test the case where the users wants to preserve their same low
        precision data type."""
        result = self.warp(
            self.image, self.tform, order=self.order, preserve_range=True
        )

        # convert back to input type, no-op if same type
        result = result.astype(dtype_in, copy=False)

    # def time_to_float64(self, dtype_in, N, order, dtype_form):
    def time_to_float64(self, dtype_in, N, order):
        """Test the case where want to upvert to float64 for continued
        transformations."""
        warp(self.image, self.tform, order=self.order, preserve_range=True)


class ResizeLocalMeanSuite:
    params = (
        [np.float32, np.float64],
        [(512, 512), (2048, 2048), (48, 48, 48), (192, 192, 192)],
        [(512, 512), (2048, 2048), (48, 48, 48), (192, 192, 192)],
    )
    param_names = ['dtype', 'shape_in', 'shape_out']

    timeout = 180

    def setup(self, dtype, shape_in, shape_out):
        if len(shape_in) != len(shape_out):
            raise NotImplementedError("shape_in, shape_out must have same dimension")
        self.image = np.zeros(shape_in, dtype=dtype)

    def time_resize_local_mean(self, dtype, shape_in, shape_out):
        resize_local_mean(self.image, shape_out)