Skip to content

Commit 910e529

Browse files
committed
[cytation5] improve autofocus effiency by caching
1 parent 46d8eb2 commit 910e529

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

pylabrobot/plate_reading/biotek_backend.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
import time
77
from dataclasses import dataclass
8-
from typing import Any, Callable, Coroutine, List, Literal, Optional, Tuple, Union, cast
8+
from typing import Any, Callable, Coroutine, Dict, List, Literal, Optional, Tuple, Union, cast
99

1010
try:
1111
import cv2 # type: ignore
@@ -67,10 +67,17 @@ async def _golden_ratio_search(
6767
c = b - (b - a) / phi
6868
d = a + (b - a) / phi
6969

70+
cache: Dict[float, float] = {}
71+
72+
async def cached_func(x: float) -> float:
73+
if x not in cache:
74+
cache[x] = await func(x)
75+
return cache[x]
76+
7077
t0 = time.time()
7178
iteration = 0
7279
while abs(b - a) > tol:
73-
if (await func(c)) > (await func(d)):
80+
if (await cached_func(c)) > (await cached_func(d)):
7481
b = d
7582
else:
7683
a = c

0 commit comments

Comments
 (0)