Skip to content

Commit 51d78b9

Browse files
use a cache
1 parent 2ef42f7 commit 51d78b9

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
- removed System tab from the Pioreactor's "Control" dialog. You can see (most) of this data on the Inventory page
44
- added a Self-test tab back to the Pioreactor's "Control" dialog, and to the "Control all Pioreactors"
5-
6-
7-
## Agents
5+
- reused cached IR LED reference normalization per experiment when `ref_normalization=unity`, instead of always taking the first reading
86
- improved unit API retry safety by returning in-progress responses when Huey task locks are held
97
- improved API error messages with structured causes and remediation hints for agents and UI clients
108

core/pioreactor/background_jobs/od_reading.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
from pioreactor.utils import adcs as madcs
9595
from pioreactor.utils import argextrema
9696
from pioreactor.utils import local_intermittent_storage
97+
from pioreactor.utils import local_persistent_storage
9798
from pioreactor.utils import timing
9899
from pioreactor.utils.math_helpers import mean
99100
from pioreactor.utils.od_fusion import compute_fused_od
@@ -721,21 +722,36 @@ class PhotodiodeIrLedReferenceTrackerUnitInit(IrLedReferenceTracker):
721722
SIGNAL / f(REF).
722723
"""
723724

724-
def __init__(self, channel: pt.PdChannel) -> None:
725+
_PERSISTENT_CACHE_NAME = "ir_led_reference_normalization"
726+
727+
def __init__(self, channel: pt.PdChannel, experiment: pt.Experiment | None = None) -> None:
725728
super().__init__()
726729
self.channel = channel
727-
self.initial_ref: float | None = None
730+
self.experiment = experiment
731+
self.initial_ref = self._get_cached_initial_ref()
728732
self.led_output_ema = ExponentialMovingAverage(
729733
config.getfloat("od_reading.config", "pd_reference_ema")
730734
)
731735
self.led_output_emstd = ExponentialMovingStd(alpha=0.95, ema_alpha=0.8, initial_std_value=0.001)
732736

737+
def _get_cached_initial_ref(self) -> float | None:
738+
with local_persistent_storage(self._PERSISTENT_CACHE_NAME) as cache:
739+
cached = cache.get(self.experiment)
740+
741+
if cached is None:
742+
return None
743+
if isinstance(cached, bytes):
744+
try:
745+
cached = float(cached.decode())
746+
except ValueError:
747+
return None
748+
return float(cached)
749+
733750
def update(self, ir_output_reading: pt.Voltage) -> None:
734751
if self.initial_ref is None:
735752
self.initial_ref = ir_output_reading
736-
737-
if self.initial_ref <= 0.0:
738-
raise ValueError("Initial IR reference is 0.0. Is it connected correctly? Is the IR LED working?")
753+
with local_persistent_storage(self._PERSISTENT_CACHE_NAME) as cache:
754+
cache.set(self.experiment, self.initial_ref)
739755

740756
relative_ref = ir_output_reading / self.initial_ref
741757

@@ -1589,6 +1605,7 @@ def start_od_reading(
15891605
if ref_normalization == "unity":
15901606
ir_led_reference_tracker = PhotodiodeIrLedReferenceTrackerUnitInit(
15911607
ir_led_reference_channel,
1608+
experiment=experiment,
15921609
)
15931610
else:
15941611
ir_led_reference_tracker = PhotodiodeIrLedReferenceTrackerStaticInit(

core/tests/test_od_reading.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,12 @@ def test_PhotodiodeIrLedReferenceTrackerStaticInit() -> None:
13071307

13081308

13091309
def test_PhotodiodeIrLedReferenceTrackerUnitInit() -> None:
1310-
tracker = PhotodiodeIrLedReferenceTrackerUnitInit(channel="1")
1310+
experiment = "test_PhotodiodeIrLedReferenceTrackerUnitInit"
1311+
with local_persistent_storage("ir_led_reference_normalization") as cache:
1312+
if experiment in cache:
1313+
del cache[experiment]
1314+
1315+
tracker = PhotodiodeIrLedReferenceTrackerUnitInit(channel="1", experiment=experiment)
13111316

13121317
for i in range(1000):
13131318
v = 0.001 * np.random.randn() + 0.25
@@ -1324,6 +1329,22 @@ def test_PhotodiodeIrLedReferenceTrackerUnitInit() -> None:
13241329
assert abs(tracker.transform(1.0) - 1.25) < 0.1
13251330

13261331

1332+
def test_PhotodiodeIrLedReferenceTrackerUnitInit_uses_cached_reference() -> None:
1333+
experiment = "test_ir_led_reference_cache"
1334+
with local_persistent_storage("ir_led_reference_normalization") as cache:
1335+
cache[experiment] = 0.25
1336+
1337+
try:
1338+
tracker = PhotodiodeIrLedReferenceTrackerUnitInit(channel="1", experiment=experiment)
1339+
assert tracker.initial_ref == 0.25
1340+
tracker.update(0.50)
1341+
assert tracker.initial_ref == 0.25
1342+
finally:
1343+
with local_persistent_storage("ir_led_reference_normalization") as cache:
1344+
if experiment in cache:
1345+
del cache[experiment]
1346+
1347+
13271348
def test_ref_normalization_unity_uses_unit_init() -> None:
13281349
with temporary_config_change(config, "od_reading.config", "ref_normalization", "unity"):
13291350
with start_od_reading(

0 commit comments

Comments
 (0)