Skip to content

Commit aa9fff6

Browse files
fix tests that were using the wrong od min/maxs
1 parent 9e98285 commit aa9fff6

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

pioreactor/background_jobs/od_reading.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -665,55 +665,43 @@ def hydate_models(self, calibration_data: structs.ODCalibration | None) -> None:
665665
)
666666

667667
def _hydrate_model(self, calibration_data: structs.ODCalibration) -> Callable[[pt.Voltage], pt.OD]:
668-
if calibration_data.curve_type == "poly":
669-
"""
670-
Finds the smallest root in the range [minOD, maxOD] calibrated against.
671-
672-
Note: if the calibration curve is non-monotonic, ie. has a maximum not on the boundary of the range,
673-
this procedure effectively ignores it.
674-
675-
"""
668+
if (calibration_data.x != "OD600") or (calibration_data.y != "Voltage"):
669+
self.logger.error(f"Calibration {calibration_data.calibration_name} is not for OD600.")
670+
raise exc.CalibrationError(f"Calibration {calibration_data.calibration_name} is not for OD600.")
676671

677-
def calibration(observed_voltage: pt.Voltage) -> pt.OD:
678-
assert calibration_data.x == "OD600", "calibration is wrong"
679-
assert calibration_data.y == "Voltage", "calibration is wrong"
680-
min_OD, max_OD = min(calibration_data.recorded_data["x"]), max(
681-
calibration_data.recorded_data["x"]
682-
)
683-
min_voltage, max_voltage = min(calibration_data.recorded_data["y"]), max(
684-
calibration_data.recorded_data["y"]
685-
)
672+
def _calibrate_signal(observed_voltage: pt.Voltage) -> pt.OD:
673+
min_OD, max_OD = min(calibration_data.recorded_data["x"]), max(
674+
calibration_data.recorded_data["x"]
675+
)
676+
min_voltage, max_voltage = min(calibration_data.recorded_data["y"]), max(
677+
calibration_data.recorded_data["y"]
678+
)
686679

687-
try:
688-
return calibration_data.y_to_x(observed_voltage, enforce_bounds=True)
689-
except exc.NoSolutionsFoundError:
690-
if observed_voltage <= min_voltage:
691-
return min_OD
692-
elif observed_voltage > max_voltage:
693-
return max_OD
694-
else:
695-
raise exc.NoSolutionsFoundError(
696-
f"No solution found for calibrated signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V, which would map outside the allowed values."
697-
)
698-
except exc.SolutionBelowDomainError:
699-
self.logger.warning(
700-
f"Signal below suggested calibration range. Trimming signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V, which would map outside the allowed values."
701-
)
702-
self.has_logged_warning = True
680+
try:
681+
return calibration_data.y_to_x(observed_voltage, enforce_bounds=True)
682+
except exc.NoSolutionsFoundError:
683+
if observed_voltage <= min_voltage:
703684
return min_OD
704-
except exc.SolutionAboveDomainError:
705-
self.logger.warning(
706-
f"Signal above suggested calibration range. Trimming signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V."
707-
)
708-
self.has_logged_warning = True
685+
elif observed_voltage > max_voltage:
709686
return max_OD
687+
else:
688+
raise exc.NoSolutionsFoundError(
689+
f"No solution found for calibrated signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V, which would map outside the allowed values."
690+
)
691+
except exc.SolutionBelowDomainError:
692+
self.logger.warning(
693+
f"Signal below suggested calibration range. Trimming signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V, which would map outside the allowed values."
694+
)
695+
self.has_logged_warning = True
696+
return min_OD
697+
except exc.SolutionAboveDomainError:
698+
self.logger.warning(
699+
f"Signal above suggested calibration range. Trimming signal. Calibrated for OD=[{min_OD:0.3g}, {max_OD:0.3g}], V=[{min_voltage:0.3g}, {max_voltage:0.3g}]. Observed {observed_voltage:0.3f}V."
700+
)
701+
self.has_logged_warning = True
702+
return max_OD
710703

711-
else:
712-
713-
def calibration(observed_voltage: pt.Voltage) -> pt.OD:
714-
return observed_voltage
715-
716-
return calibration
704+
return _calibrate_signal
717705

718706
def __call__(self, od_readings: structs.ODReadings) -> structs.ODReadings:
719707
od_readings = copy(od_readings)
@@ -1045,7 +1033,7 @@ def record_from_adc(self) -> structs.ODReadings | None:
10451033

10461034
try:
10471035
od_readings = self.calibration_transformer(raw_od_readings)
1048-
except exc.NoSolutionsFoundError:
1036+
except (exc.NoSolutionsFoundError, exc.CalibrationError):
10491037
# some calibration error occurred
10501038
od_readings = None
10511039

pioreactor/tests/test_od_reading.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def test_calibration_simple_linear_calibration_positive_slope() -> None:
876876
pause()
877877
pause()
878878
voltage = 10.0
879-
assert od.calibration_transformer.models["2"](voltage) == 1.0
879+
assert od.calibration_transformer.models["2"](voltage) == max(cal.recorded_data["x"])
880880
pause()
881881
pause()
882882
pause()
@@ -1055,7 +1055,7 @@ def float_to_od_readings_struct(ch: pt.PdChannel, v: float) -> structs.ODReading
10551055
ods={ch: structs.RawODReading(od=v, angle="90", channel=ch, timestamp=current_utc_datetime())},
10561056
)
10571057

1058-
assert cc(float_to_od_readings_struct("2", 0.001)).ods["2"].od == 0
1058+
assert cc(float_to_od_readings_struct("2", 0.001)).ods["2"].od == min(cal.recorded_data["x"])
10591059
assert cc(float_to_od_readings_struct("2", 0.002)).ods["2"].od == 0
10601060
assert abs(cc(float_to_od_readings_struct("2", 0.004)).ods["2"].od - 0.0032975807375385234) < 1e-5
10611061
assert abs(cc(float_to_od_readings_struct("2", 0.02)).ods["2"].od - 0.03639585015289039) < 1e-5

pioreactor/whoami.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def am_I_a_worker() -> bool:
153153

154154

155155
@cache
156-
def get_pioreactor_version() -> tuple[int, int]:
156+
def get_pioreactor_version() -> tuple[int, int, *tuple[int, ...]]:
157157
# pioreactor model version
158158
if os.environ.get("MODEL_VERSION"):
159159
return version_text_to_tuple(os.environ["MODEL_VERSION"])

0 commit comments

Comments
 (0)