scikit-image
151 строка · 4.4 Кб
1# See "Writing benchmarks" in the asv docs for more information.
2# https://asv.readthedocs.io/en/latest/writing_benchmarks.html
3import numpy as np4
5from skimage import data, filters, color6from skimage.filters.thresholding import threshold_li7
8
9class FiltersSuite:10"""Benchmark for filter routines in scikit-image."""11
12def setup(self):13self.image = np.random.random((4000, 4000))14self.image[:2000, :2000] += 115self.image[3000:, 3000] += 0.516
17def time_sobel(self):18filters.sobel(self.image)19
20
21class FiltersSobel3D:22"""Benchmark for 3d sobel filters."""23
24def setup(self):25try:26filters.sobel(np.ones((8, 8, 8)))27except ValueError:28raise NotImplementedError("3d sobel unavailable")29self.image3d = data.binary_blobs(length=256, n_dim=3).astype(float)30
31def time_sobel_3d(self):32_ = filters.sobel(self.image3d)33
34
35class MultiOtsu:36"""Benchmarks for MultiOtsu threshold."""37
38param_names = ['classes']39params = [3, 4, 5]40
41def setup(self, *args):42self.image = data.camera()43
44def time_threshold_multiotsu(self, classes):45filters.threshold_multiotsu(self.image, classes=classes)46
47def peakmem_reference(self, *args):48"""Provide reference for memory measurement with empty benchmark.49
50Peakmem benchmarks measure the maximum amount of RAM used by a
51function. However, this maximum also includes the memory used
52during the setup routine (as of asv 0.2.1; see [1]_).
53Measuring an empty peakmem function might allow us to disambiguate
54between the memory used by setup and the memory used by target (see
55other ``peakmem_`` functions below).
56
57References
58----------
59.. [1]: https://asv.readthedocs.io/en/stable/writing_benchmarks.html#peak-memory
60"""
61pass62
63def peakmem_threshold_multiotsu(self, classes):64filters.threshold_multiotsu(self.image, classes=classes)65
66
67class ThresholdSauvolaSuite:68"""Benchmark for transform routines in scikit-image."""69
70def setup(self):71self.image = np.zeros((2000, 2000), dtype=np.uint8)72self.image3D = np.zeros((30, 300, 300), dtype=np.uint8)73
74idx = np.arange(500, 700)75idx3D = np.arange(10, 200)76
77self.image[idx[::-1], idx] = 25578self.image[idx, idx] = 25579
80self.image3D[:, idx3D[::-1], idx3D] = 25581self.image3D[:, idx3D, idx3D] = 25582
83def time_sauvola(self):84filters.threshold_sauvola(self.image, window_size=51)85
86def time_sauvola_3d(self):87filters.threshold_sauvola(self.image3D, window_size=51)88
89
90class ThresholdLi:91"""Benchmark for threshold_li in scikit-image."""92
93def setup(self):94try:95self.image = data.eagle()96except ValueError:97raise NotImplementedError("eagle data unavailable")98self.image_float32 = self.image.astype(np.float32)99
100def time_integer_image(self):101threshold_li(self.image)102
103def time_float32_image(self):104threshold_li(self.image_float32)105
106
107class RidgeFilters:108"""Benchmark ridge filters in scikit-image."""109
110def setup(self):111# Ensure memory footprint of lazy import is included in reference112self._ = filters.meijering, filters.sato, filters.frangi, filters.hessian113self.image = color.rgb2gray(data.retina())114
115def peakmem_setup(self):116"""peakmem includes the memory used by setup.117Peakmem benchmarks measure the maximum amount of RAM used by a
118function. However, this maximum also includes the memory used
119by ``setup`` (as of asv 0.2.1; see [1]_)
120Measuring an empty peakmem function might allow us to disambiguate
121between the memory used by setup and the memory used by slic (see
122``peakmem_slic_basic``, below).
123References
124----------
125.. [1]: https://asv.readthedocs.io/en/stable/writing_benchmarks.html#peak-memory
126"""
127pass128
129def time_meijering(self):130filters.meijering(self.image)131
132def peakmem_meijering(self):133filters.meijering(self.image)134
135def time_sato(self):136filters.sato(self.image)137
138def peakmem_sato(self):139filters.sato(self.image)140
141def time_frangi(self):142filters.frangi(self.image)143
144def peakmem_frangi(self):145filters.frangi(self.image)146
147def time_hessian(self):148filters.hessian(self.image)149
150def peakmem_hessian(self):151filters.hessian(self.image)152