|
94 | 94 | from pioreactor.utils import adcs as madcs |
95 | 95 | from pioreactor.utils import argextrema |
96 | 96 | from pioreactor.utils import local_intermittent_storage |
| 97 | +from pioreactor.utils import local_persistent_storage |
97 | 98 | from pioreactor.utils import timing |
98 | 99 | from pioreactor.utils.math_helpers import mean |
99 | 100 | from pioreactor.utils.od_fusion import compute_fused_od |
@@ -721,21 +722,36 @@ class PhotodiodeIrLedReferenceTrackerUnitInit(IrLedReferenceTracker): |
721 | 722 | SIGNAL / f(REF). |
722 | 723 | """ |
723 | 724 |
|
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: |
725 | 728 | super().__init__() |
726 | 729 | self.channel = channel |
727 | | - self.initial_ref: float | None = None |
| 730 | + self.experiment = experiment |
| 731 | + self.initial_ref = self._get_cached_initial_ref() |
728 | 732 | self.led_output_ema = ExponentialMovingAverage( |
729 | 733 | config.getfloat("od_reading.config", "pd_reference_ema") |
730 | 734 | ) |
731 | 735 | self.led_output_emstd = ExponentialMovingStd(alpha=0.95, ema_alpha=0.8, initial_std_value=0.001) |
732 | 736 |
|
| 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 | + |
733 | 750 | def update(self, ir_output_reading: pt.Voltage) -> None: |
734 | 751 | if self.initial_ref is None: |
735 | 752 | 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) |
739 | 755 |
|
740 | 756 | relative_ref = ir_output_reading / self.initial_ref |
741 | 757 |
|
@@ -1589,6 +1605,7 @@ def start_od_reading( |
1589 | 1605 | if ref_normalization == "unity": |
1590 | 1606 | ir_led_reference_tracker = PhotodiodeIrLedReferenceTrackerUnitInit( |
1591 | 1607 | ir_led_reference_channel, |
| 1608 | + experiment=experiment, |
1592 | 1609 | ) |
1593 | 1610 | else: |
1594 | 1611 | ir_led_reference_tracker = PhotodiodeIrLedReferenceTrackerStaticInit( |
|
0 commit comments