Skip to content

Commit 97a0f77

Browse files
introduce benchmarking
1 parent e55aac3 commit 97a0f77

File tree

5 files changed

+116
-7
lines changed

5 files changed

+116
-7
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
import numpy as np
3+
import itertools
4+
import deeptrack as dt
5+
6+
u = dt.units
7+
8+
9+
def create_pipeline(output_region=(0, 0, 128, 128), num_particles=1):
10+
11+
optics = dt.Brightfield(output_region=output_region)
12+
13+
mie = dt.MieSphere(
14+
radius=0.5e-6,
15+
refractive_index=1.45,
16+
z=lambda: np.random.randn() * 10,
17+
position=lambda: output_region[2:] * np.random.randn(2),
18+
L=10,
19+
)
20+
21+
field = optics(mie ^ num_particles)
22+
return field
23+
24+
25+
def test_simulate_mie_128_1(benchmark):
26+
pipeline = create_pipeline(output_region=(0, 0, 128, 128), num_particles=1)
27+
benchmark(
28+
lambda: pipeline.update()(),
29+
)
30+
31+
32+
def test_simulate_mie_128_5(benchmark):
33+
pipeline = create_pipeline(output_region=(0, 0, 128, 128), num_particles=5)
34+
benchmark(
35+
lambda: pipeline.update()(),
36+
)
37+
38+
39+
def test_simulate_mie_512_1(benchmark):
40+
pipeline = create_pipeline(output_region=(0, 0, 512, 512), num_particles=1)
41+
benchmark(
42+
lambda: pipeline.update()(),
43+
)
44+
45+
46+
def test_simulate_mie_512_5(benchmark):
47+
pipeline = create_pipeline(output_region=(0, 0, 512, 512), num_particles=5)
48+
benchmark(
49+
lambda: pipeline.update()(),
50+
)
51+
52+
53+
def test_simulate_mie_1024_1(benchmark):
54+
pipeline = create_pipeline(output_region=(0, 0, 1024, 1024), num_particles=1)
55+
benchmark(
56+
lambda: pipeline.update()(),
57+
)
58+
59+
60+
def test_simulate_mie_1024_5(benchmark):
61+
pipeline = create_pipeline(output_region=(0, 0, 1024, 1024), num_particles=5)
62+
benchmark(
63+
lambda: pipeline.update()(),
64+
)

deeptrack/features.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,18 @@ class BindUpdate(StructuralFeature):
10311031
__distributed__ = False
10321032

10331033
def __init__(self, feature: Feature, **kwargs):
1034-
self.feature = feature
1034+
import warnings
1035+
1036+
warnings.warn(
1037+
"BindUpdate is deprecated and may be removed in a future release. Please use BindResolve in conjunction with Arguments.",
1038+
DeprecationWarning,
1039+
)
1040+
self.feature = self.add_feature(feature)
10351041
super().__init__(**kwargs)
10361042

1037-
def _update(self, **kwargs):
1038-
super()._update(**kwargs)
1039-
self.feature._update(**{**kwargs, **self.properties})
1043+
def update(self, **kwargs):
1044+
super().update(**kwargs)
1045+
self.feature.update(**{**kwargs, **self.properties})
10401046

10411047
def get(self, image, **kwargs):
10421048
return self.feature.resolve(image, **kwargs)

deeptrack/image.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@
1616
Transforms.
1717
"""
1818

19+
import warnings
1920
import numpy as np
2021
import numpy.lib.mixins
2122
import operator as ops
2223
from tensorflow import Tensor
2324
from .backend.tensorflow_bindings import TENSORFLOW_BINDINGS
2425

26+
CUPY_INSTALLED = False
27+
try:
28+
import cupy as cp
29+
30+
CUPY_INSTALLED = True
31+
except Exception as e:
32+
CUPY_INSTALLED = False
33+
warnings.warn(
34+
"cupy not installed. GPU-accelerated simulations will not be possible"
35+
)
36+
2537

2638
def _binary_method(op):
2739
"""Implement a forward binary method with a noperator, e.g., __add__."""
@@ -257,9 +269,16 @@ def __array_function__(self, func, types, args, kwargs):
257269
else:
258270
return NotImplemented
259271

272+
elif not (
273+
isinstance(self._value, (np.ndarray, tuple, list))
274+
or np.isscalar(self._value)
275+
) and not hasattr(self._value, "__array_function__"):
276+
print(func, type(self._value), self._value)
277+
return NotImplemented
278+
260279
out = func(*values, **kwargs)
261280

262-
if isinstance(out, bool):
281+
if isinstance(out, (bool, int, float)):
263282
return out
264283

265284
out = Image(out)
@@ -270,7 +289,7 @@ def __array_function__(self, func, types, args, kwargs):
270289
return out
271290

272291
def __array__(self):
273-
return np.array(self._value)
292+
return np.array(self._value)
274293

275294
def __getattr__(self, key):
276295
return getattr(self._value, key)

deeptrack/test/test_elementwise.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .. import elementwise, features, Image
1313

1414
import numpy as np
15+
import cupy as cp
1516
import numpy.testing
1617
import inspect
1718

@@ -42,6 +43,12 @@ def grid_test_features(
4243

4344
expected_result = expected_result_function(f_a_input)
4445

46+
if isinstance(output._value, cp.ndarray):
47+
output = output.get()
48+
49+
if isinstance(expected_result, cp.ndarray):
50+
expected_result = expected_result.get()
51+
4552
if isinstance(output, list) and isinstance(expected_result, list):
4653
[
4754
np.testing.assert_almost_equal(np.array(a), np.array(b))
@@ -68,7 +75,15 @@ def test(self):
6875
grid_test_features(
6976
self,
7077
cl,
71-
[-1, 0, 1, (np.random.rand(50, 500) - 0.5) * 100, np.inf, np.nan],
78+
[
79+
-1,
80+
0,
81+
1,
82+
(np.random.rand(50, 500) - 0.5) * 100,
83+
(cp.random.rand(50, 500) - 0.5) * 100,
84+
np.inf,
85+
np.nan,
86+
],
7287
np.__dict__[cl.__name__.lower()],
7388
)
7489

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tool.pytest.ini_options]
2+
minversion = "6.0"
3+
testpaths = [
4+
"deeptrack/benchmarks",
5+
]

0 commit comments

Comments
 (0)