Repository navigation
fix(analysis): refuse non-integral md.csv step values instead of truncating (AUD-001) - #76
Conversation
|
Additional real-artifact evidence (not synthetic fixtures): Loaded every real mliport run under All of them have integral |
hydrogen1222
left a comment
There was a problem hiding this comment.
Audit review — changes requested before merge (precision edge, not caught by CI).
The finite/fractional validation is a real improvement and the red→green tests are persuasive. However _validated_steps begins with np.asarray(values, dtype=float), so TrajectoryDataset.from_frames(steps=[9007199254740993, ...]) silently changes the valid integer 2**53+1 into 2**53 before the integral/range checks. The CSV parser _read_thermodynamics likewise converts the step column through float before validation. Also -2**63 is a representable signed int64 value but the symmetric abs >= 2**63 bound rejects it (consider whether negative steps are allowed at all).
Please add a failing regression showing that an integer input is either preserved exactly or explicitly rejected (never silently rounded); validate text step columns before lossy parsing, or document/reject values beyond the exactly representable float range. Keep the fix narrow; the separately identified nonfinite-CSV fallback can remain a follow-up if explicitly tracked. CI is green, but this is a scientific identity contract.
…cating AUD-001 (tracker #75). TrajectoryDataset.load cast the finite raw/md.csv "step" column with astype(int), so steps 0.5, 1.5, 2.5, 3.5 loaded as 0, 1, 2, 3. The fabricated indices were then re-exported as the step axis (thermodynamic_diagnostics "step" column) and used to recompute frame_stride_steps in analysis_view()/slice_frames(). A step beyond the int64 range wrapped as well, with a numpy "invalid value encountered in cast" RuntimeWarning. Step indices are integers by contract: mliport writes int steps and _uniform_integer_step already treats non-integral values as unknown. Add _validated_steps(), which requires finite, integral, in-range values, and use it in both cast sites (TrajectoryDataset.load and TrajectoryDataset.from_frames). Invalid input now fails closed with a clear ValueError instead of being silently reinterpreted. Tests (red before the fix, green after): test_fractional_md_csv_steps_are_rejected_not_truncated test_out_of_range_md_csv_steps_are_rejected test_from_frames_rejects_fractional_steps test_integral_float_md_csv_steps_still_load (guard) DATA IMPACT: no scientific record invalidated. No stored trajectory is rewritten; only the handling of an already-invalid step column changes, from silent truncation to an explicit error. Tracking: #75
…float AUD-001 review follow-up (tracker #75, PR #76). _validated_steps() began with np.asarray(values, dtype=float), so an integer input such as steps=[2**53 + 1] silently became 2**53 before the integrality/range checks could see it, and the md.csv step column was parsed through float() for the same reason. The symmetric abs() >= 2**63 bound also rejected the representable int64 minimum -2**63. Dispatch on the input dtype instead: * integer input is never routed through float; only the asymmetric int64 range is enforced (so -2**63 is accepted, 2**63 is rejected); * float input must be finite, integral and within the exactly representable float range (|v| <= 2**53); * a CSV step column is parsed exactly from its text with Decimal, so "9007199254740993" and "9007199254740993.0" both keep their exact integer value. A finite non-integral cell is rejected; blank, unparseable or non-finite cells keep the pre-existing "axis unknown" fallback (unchanged, tracked separately). Regressions added (red before this commit, green after): test_from_frames_preserves_large_integer_steps_exactly test_from_frames_accepts_int64_min_and_rejects_overflow test_md_csv_huge_integer_step_text_is_parsed_exactly test_md_csv_float_literal_step_keeps_its_exact_integer_value test_md_csv_unparseable_step_makes_the_axis_unknown DATA IMPACT: no scientific record invalidated. No stored trajectory is rewritten; only the handling of a step column changes. Tracking: #75
6e7cab9 to
ff71072
Compare
|
Review follow-up pushed as Integer input is no longer routed through
The CSV step column is validated on its text. New regressions (verified red on the previous commit, green now):
Evidence: |
hydrogen1222
left a comment
There was a problem hiding this comment.
Round-2 independent code review — one precision hole remains; please fix before merge.
Rev 2 correctly preserves homogeneous integer inputs and parses CSV step text exactly. However _validated_steps still starts with array = np.asarray(values). A heterogeneous Python sequence with a large integer and an otherwise legitimate integral float silently rounds before the dtype checks:
import numpy as np
steps = [2**53 + 1, 2.0]
array = np.asarray(steps)
assert array.dtype == np.float64
assert array[0] == 2**53 # integer identity was already lost
# _validated_steps(steps, source="test") returns [2**53, 2]
# without raising an errorPlease add a red→green regression for TrajectoryDataset.from_frames(steps=[2**53+1, 2.0]) (use 2 frames) and preserve exact integer values or reject mixed numeric input before coercion. For array inputs, distinguish np.ndarray numeric integer vs float dtypes without rebuilding a mixed array. This is precisely the silent data-identity failure AUD-001 is intended to prevent.
A secondary hardening note (not necessarily blocking): in _parse_step_column, compare the finite Decimal to the int64 bounds before int(integral), so an input like 1e999999999 is rejected cheaply without attempting a massive integer conversion.
All current GitHub checks are green; this is a source-reviewed uncovered edge, not a claim that tests failed. Please keep #76 open until the regression is added.
…coercion AUD-001 round-2 review follow-up (tracker #75, PR #76). _validated_steps still began with np.asarray(values), so a heterogeneous Python sequence such as [2**53 + 1, 2.0] was promoted to float64 and the integer was silently rounded to 2**53 before any dtype check could see it -- exactly the data-identity failure AUD-001 exists to prevent. * An np.ndarray keeps the vectorised dtype path (_validated_step_array). * Any other sequence is validated element by element via the new _step_index(), which checks Python int first (exact, never through float), then float (finite / integral / |v| <= 2**53), and rejects everything else. The int64 array is only built afterwards, so numpy promotion can no longer round a value. Secondary hardening: _parse_step_column now compares the Decimal against the int64 bounds before int(integral). This is a real robustness fix, not just a micro-optimisation -- on the previous commit int(Decimal("1e999999999")) tried to build a ~10**9-digit integer and the process hung. Regressions (red before, green after): test_from_frames_preserves_mixed_integer_and_float_steps_exactly test_from_frames_rejects_invalid_mixed_sequences (4 parametrised cases) test_md_csv_astronomical_step_exponent_is_rejected DATA IMPACT: no scientific record invalidated. No stored trajectory is rewritten; only the handling of a step axis changes. Tracking: #75
|
Round-2 follow-up pushed as Heterogeneous sequences are validated element-wise, before any coercion.
Your example now preserves the identity: Secondary hardening done. Regressions (verified red on
Evidence: |
hydrogen1222
left a comment
There was a problem hiding this comment.
Round-3 source/CI review of head 2adfc2ef: the previously blocking AUD-001 precision reproductions are resolved. Specifically, ordinary mixed Python numeric inputs [2**53 + 1, 2.0] are now element-validated before array coercion and preserve 9007199254740993; the exact-text CSV Decimal parser bounds the value before integer allocation, with a regression for 1e999999999. Full test matrix, lint, docs and package build all report success on this head, including Python 3.10–3.13. No further blocking concern for the documented ordinary Python int/float and CSV-text use cases; owner may consider merging #76. No real model GPU job was performed in this review.
A non-blocking, uncommon follow-up for tracker #75: from_frames currently wraps steps in list(steps), so the _validated_step_array vectorized branch is not reached through that public entry point, and the scalar _step_index casts np.longdouble to Python float. On platforms where longdouble has extended mantissa, a list/array of np.longdouble(2**53+1) may thus still lose integer identity. If such nonstandard dtype inputs are within the supported contract, add a separate regression/fix; do not re-expand this already substantial PR without an actual requirement. Likewise float16/float32 input contracts should be spelled out separately rather than assuming a float64 exactness threshold. These do not affect standard int/float CSV pathways fixed here.
Tracking: #75 (Living code audit & exhaustive scientific test campaign)
AUD-001 — trajectory
stepintegerity / data integritySeverity: P0 hypothesis → confirmed data-integrity defect (no scientific
miscomputation demonstrated; see "Impact" below).
Defect (red, reproduced on baseline
main@1ef9a1be)TrajectoryDataset.loadread the finiteraw/md.csvstepcolumn and cast itwith
astype(int)without checking integrality:Consequences reproduced:
dataset.stepswas silently fabricated: a file with0.5, 1.5, 2.5, 3.5loaded as
0, 1, 2, 3.thermodynamic_diagnostics(
columns["step"]), so an output artifact disagreed with its input.analysis_view()/slice_frames()recomputeframe_stride_stepsfromself.steps, so a fabricated integral column can override the trustedmetadata stride.
int64range wrapped (astype) with a numpyRuntimeWarning: invalid value encountered in cast.TrajectoryDataset.from_frames(steps=[0.5, 1.5, ...])had the samenp.asarray(..., int)truncation.The module already guards this elsewhere:
_uniform_integer_stepreturnsNonefor non-integral values. The twoastype(int)sites were the gap.Fix (green)
_validated_steps()requires finite, integral, in-range values and returnsint64; it is used by both cast sites. Invalid input now fails closed with aclear
ValueErrornaming the offending values, instead of inventing a stepaxis that is not in the file (AGENTS.md rules 2 and 3).
Non-finite
md.csvstep columns keep the pre-existing "step axis unknown"fallback; only the audited finite-truncation path changed.
Tests
Red before the fix, green after (
tests/mliport/mliport/analysis/test_dataset_validation.py):test_fractional_md_csv_steps_are_rejected_not_truncatedtest_out_of_range_md_csv_steps_are_rejectedtest_from_frames_rejects_fractional_stepstest_integral_float_md_csv_steps_still_load(guard)Evidence
main@1ef9a1bea5033dc1a1664d8450f4b4f9edfbd91d, Python 3.12.13,pytest 9.1.1, numpy 2.5.4, CPU-only
.venv-dev.1649 passed, 33 skipped.1653 passed, 33 skipped(pytest tests -q -p no:cacheprovider -rsx).pytest tests/mliport/mliport/analysis -q→130 passed, 23 skipped.ruff check mliport/mliport tests scripts→ All checks passed;ruff format --check→ clean.scripts/check_repository_hygiene.py→ passed.scripts/generate_docs.py --check→ up to date.test_docs_contract.py→56 passed.DATA IMPACT
No scientific record invalidated. No stored trajectory,
md.csvor evidencefile is rewritten or regenerated. Only the handling of an already-invalid step
column changes, from silent truncation to an explicit rejection.
Residual / not covered
NaN/Inf)stepcolumns still fall back toinfo/arange(nframes) * striderather than failing closed. That is aseparate, pre-existing behaviour and was left untouched to keep this PR minimal.
Rev 2 — review follow-up (commit
ff71072b, rebased ontomain@77a94afd)Addresses the "precision edge" review. Integer input is never routed through
floatagain, and the CSVstepcolumn is validated on its text:int64range isenforced (
-2**63accepted,2**63rejected);|v| <= 2**53;_parse_step_columnreads each CSV cell withdecimal.Decimal, so"9007199254740993"and"9007199254740993.0"both keep their exact value;fallback (still a tracked follow-up, not expanded here).
Regressions added:
test_from_frames_preserves_large_integer_steps_exactly,test_from_frames_accepts_int64_min_and_rejects_overflow,test_md_csv_huge_integer_step_text_is_parsed_exactly,test_md_csv_float_literal_step_keeps_its_exact_integer_value,test_md_csv_unparseable_step_makes_the_axis_unknown. Full suite: 1661passed, 33 skipped; analysis suite 135 passed / 23 skipped; ruff clean; CI
green on 3.10–3.13.
Rev 3 — round-2 review follow-up (commit
2adfc2ef)Fixes the remaining precision hole: a heterogeneous Python sequence such as
[2**53 + 1, 2.0]was promoted tofloat64bynp.asarrayand the integer wasrounded to
2**53before any dtype check could see it.np.ndarrayinput keeps the vectorised dtype path (_validated_step_array)._step_index()(Python
intfirst — exact, never through float — then float withfinite / integral /
|v| <= 2**53), and theint64array is only builtafterwards.
_parse_step_columncompares theDecimalto the int64 boundsbefore
int(integral). On the previous commit1e999999999made theprocess hang (the regression run timed out at 300 s); it is now rejected in
< 1 ms.
Regressions:
test_from_frames_preserves_mixed_integer_and_float_steps_exactly,test_from_frames_rejects_invalid_mixed_sequences(4 cases),test_md_csv_astronomical_step_exponent_is_rejected. Full suite: 1667passed, 33 skipped; analysis suite 141 passed / 23 skipped; ruff clean.