Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/maple/core/calibration/submodel_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -3080,7 +3080,9 @@ def validate_no_hardcoded_values_in_observation_code(self) -> "SubmodelTarget":

Allowed constants:
- 0, 1, 2 (indexing, basic arithmetic)
- 1.96 (95% CI convention)
- 1.96 (normal z for the 2.5th-97.5th / 95% CI range)
- 1.645 (normal z for the 5th-95th percentile range; box-plot whisker
convention, parallel to 1.96)

NOT allowed (should be inputs):
- Time conversions like 24.0, 60.0 (add as unit_conversion input)
Expand All @@ -3090,8 +3092,9 @@ def validate_no_hardcoded_values_in_observation_code(self) -> "SubmodelTarget":
"""
import ast

# Minimal set of truly necessary constants
ALLOWED_CONSTANTS = {0, 0.0, 1, 1.0, 2, 2.0, 1.96}
# Minimal set of truly necessary constants (normal z-values for the two
# standard percentile ranges used in SD-from-percentile reconstructions).
ALLOWED_CONSTANTS = {0, 0.0, 1, 1.0, 2, 2.0, 1.96, 1.645}

errors = []

Expand Down
26 changes: 26 additions & 0 deletions tests/unit/core/test_submodel_target_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2110,3 +2110,29 @@ def test_percent_with_normal_shape_raises(self):
def test_percent_with_logit_normal_passes(self):
# logit_normal on a median-in-(0,1) percent observable is the correct shape.
SubmodelTarget(**_percent_target("logit_normal"))


# ============================================================================
# Test that 1.645 (5th-95th percentile z) is an allowed observation_code constant,
# parallel to 1.96 (2.5th-97.5th). The authoring prompt instructs 1.645 for
# box-plot-whisker SD reconstruction, so the validator must not flag it.
# ============================================================================

CODE_WITH_1645 = """
def derive_observation(inputs, sample_size, rng, n_bootstrap):
import numpy as np
sd = inputs['test_value'] / (2 * 1.645)
return rng.normal(inputs['test_value'], sd / np.sqrt(sample_size), n_bootstrap)
"""


class TestAllowedConstant1645:
"""1.645 must be whitelisted like 1.96 in observation_code."""

def test_1645_not_flagged_as_hardcoded(self):
data = make_algebraic_target(input_value=10.0, measurement_error_code=CODE_WITH_1645)
try:
SubmodelTarget(**data)
except ValidationError as e:
msg = str(e)
assert "1.645" not in msg and "hardcoded" not in msg.lower(), msg
Loading