Repository navigation
Write signed zero as 0.0 in every written float (#105) - #110
Conversation
Owner decision 17 November 2026 (plan N71): every float the package writes goes through one helper, float_text.canonical_zero, so a plane whose pixel_min or pixel_max is -0.0 on one platform and +0.0 on another writes the same bytes. Covers the float cells of plane_histograms.csv, Parameters.to_columns(), ImageSummary.to_row() and percentile_name. Computed values keep their sign; no algorithm, schema version or message changes. tools/dump_outputs.py digests are unchanged and no recorded golden holds -0.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
guijacquemet
left a comment
There was a problem hiding this comment.
Independent review: #110 (head 19590b3, fixes #105)
Verdict: CHANGES NEEDED. The fix is small and the core change is correct. The main changes needed: one to_row byte change on non-zero values that isn't documented, and a claim scope that's wider than what the code covers.
Verified
- Fresh clone, venv,
pip install --no-cache-dir -e .[dev,histology], NumPy 2.5.5, Python 3.13. At head the suite ran in two halves: 2094 passed / 2 skipped, then 679 passed. The 2 skips aretests/test_rgb.py:82, which needscsbdeep(not installed).python tools/check.pypassed: code rules, ruff, 2773 passed / 2 skipped. - Ran
tests/test_signed_zero.pyagainstorigin/main(30992e9), with the two helpers inlined as a shim: 29 failed, 4 passed, as claimed. tools/dump_outputs.pygives the same digests on main and on head:032b395b…1102(1788 lines) and--corpus0111f8bd…009e(4764 lines). These match the CHANGELOG.canonical_zero: under round-to-nearest,x + 0.0 == xbitwise for every float except-0.0. NaN still printsnan, ±inf and subnormals are unchanged.- No byte change for any value the library computes.
HistogramTail(integrity.py:110) andParameters(parameters.py:99-100) already convert to Pythonfloat._wide_histogramproduces Python numbers, and_key_valueuses.item().PooledHistogram.percentile/medianreturnfloat(...)(pooling.py:179), and so does_saturated_fraction. Sorepr(canonical_zero(x)) == repr(x)on every non-zero path the library can reach. - No JSON writer: there is no
jsoninsrc/. The only file the package writes iscsv_io.py:257. - The only
-0.0grep hit in the goldens (tests/data/stain_v060_parity.json) is-0.06…, a false positive. The "no golden holds-0.0" claim holds. - Plan numbering (item 65, N71 after N70) and the contract sections (§6.1, §7.1, §8.2) are correct. Not bumping
schema_versionis justified:0.0 == -0.0and readers parse both.
Findings
-
ImageSummary.to_row()changes text and type for non-zero values when the caller supplies the values (summaries.py:158-167). Must fix or document.canonical_zerorunsfloat()on every stored value. On a directly builtImageSummary, this changes what a caller writes:np.float32(0.1)becomes0.10000000149011612.csv.writerused to write0.1.5becomes5.0.np.float64becomesfloat.
Rows from
image_summaries()are unchanged, because they already hold Python floats. But the CHANGELOG says "no API … change", and this is a byte change on non-zero values for public-constructor input. Simplest fix: canonicalise only zeros, e.g.0.0 if value == 0 else value(orvalue + 0.0only whenvalue == 0). That keeps the type and text of every other value. Alternatively, document thefloat()conversion as intended. -
csv_io._text(csv_io.py:585-590) only catchesfloatsubclasses. APlaneHistogrambuilt directly withnp.float32(-0.0)limits still writes-0.0, both before and after this PR (reproduced).compute_*never produces this, because it yields Python floats. Separately,np.float64limits used to writenp.float64(-0.0)/np.float64(0.1), which can't be read back. They now write0.0/0.1. That's a side fix the PR doesn't mention. Suggest one of:- make
PlaneHistogram(orcheck_histogram) convert limits to Pythonint/floatat construction, asHistogramTailandParametersalready do; - extend
_texttonp.floatingwith zero-only handling.
Either way, add a regression test.
- make
-
The claim "every float the package writes" is wider than the code. These
to_dict()methods are documented as JSON-compatible dicts for the run configuration, the same role asto_row, and they still pass a user-given-0.0through:NormalizationSettings.to_dict()(settings.py:172-188,percentile)HistogramSettings.to_dict()(ranges.py:111-118,robust_quantiles)IntensityCheckSettings.to_dict()(checks.py:135-142,saturation_limit)
Verified:
json.dumps(NormalizationSettings(percentile=(-0.0, 99.8)).to_dict())gives[-0.0, 99.8], whilemethod_namefor the same settings givespercentile 0-99.8. This echoes user input, so it is deterministic and is not the platform problem in #105. Either send these floats throughcanonical_zerotoo, or narrow the wording in the CHANGELOG, plan N71 and thefloat_textmodule docstring so they list exactly what is covered. -
Note, no change needed: message text can still show
-0(checks._text.6g,ranges._percent:g, the!rvalues inintegrity.py). The PR states that messages are out of scope, which is consistent. -
Nit: the line edited at
docs/contract.md:991is now about 120 characters. The rest of the file wraps at about 100.
Not merged or pushed.
Generated by Claude Code
to_row() ran float() on every stored value, so a caller-built ImageSummary holding np.float32(0.1) or 5 wrote 0.10000000149011612 or 5.0. The new float_text.positive_zero replaces a float zero of either sign (Python or NumPy) with 0.0 and returns every other value as given, so non-zero values keep their v0.7.0 type and text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
…view 2) csv_io._text only canonicalised float subclasses, so a caller-built PlaneHistogram with np.float32(-0.0) limits still wrote -0.0. _text now passes every value through positive_zero first. Side fix, documented in the CHANGELOG: np.float64 limits used to be written as np.float64(...), which the reader refuses; they are now written as the Python float's repr. np.float32 and integer limits keep their str text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
NormalizationSettings.to_dict() (percentile), HistogramSettings.to_dict() (robust_quantiles) and IntensityCheckSettings.to_dict() (saturation_limit) are the JSON run configuration a caller writes; a user-given -0.0 now becomes 0.0 through positive_zero, zeros only. The CHANGELOG, plan N71 and the float_text docstring list these paths. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
|
Review findings addressed, one commit each (head
🤖 Generated with Claude Code Generated by Claude Code |
guijacquemet
left a comment
There was a problem hiding this comment.
Re-review: #110 at head 045d25e (4 commits on top of 19590b3)
Verdict: MERGE. Each of the four earlier findings is fixed, and I found no byte change that the PR doesn't document.
What I checked
Fresh clone, venv, pip install --no-cache-dir -e .[dev,histology], NumPy 2.5.5, Python 3.13.
-
Test suite at head, run in two halves: 2094 passed / 2 skipped, then 692 passed. The 2 skips are
test_rgb.py:82, which needscsbdeep(not installed).python tools/check.pypasses: code rules, ruff, 2786 passed / 2 skipped. -
New tests fail on the pre-fix code. I ran the head's
tests/test_signed_zero.pyagainst19590b3, withpositive_zeroinlined as a shim. Result: 5 failed, 41 passed. Each code finding has a failing test:to_rowkeeping its values:test_summary_rows_keep_every_other_value_as_givennp.float32(-0.0)limits:test_numpy_negative_zero_limits_write_0_0[float32]- the three settings dictionaries:
test_settings_dictionaries_write_0_0×3
The
np.float64cases already passed on19590b3, which had fixed them. -
tools/dump_outputs.pydigests are the same on main and on head:032b395b…1102(1788 lines) and--corpus0111f8bd…009e(4764 lines). -
Written output compared, main against head, for every value type, with the same probe script on both commits:
- Limits in
plane_histograms.csv(Python float,np.float64,np.float32,np.float16, int,np.int64/int32/uint16):- Non-zero values give identical text. The one exception is
np.float64, which main wrote asnp.float64(0.25). The reader refuses that text, so no valid file contained it. Head writes0.25, and the CHANGELOG documents this as a side fix. - Integers and NumPy integers give identical text, including
0. - Every float zero of either sign gives
0.0, for all four float types.
- Non-zero values give identical text. The one exception is
ImageSummary.to_row():np.float32(0.1),np.float64(0.1),5,np.int64(0),Noneand NaN come back as the same objects as on main, and csv writes the same text for them.-0.0in Python,np.float64,np.float32ornp.float16becomes the Python float0.0.
to_dict()ofNormalizationSettings(percentile),HistogramSettings(robust_quantiles) andIntensityCheckSettings(saturation_limit): JSON for-0.0in Python,np.float64ornp.float32input is now0.0. A non-zero value such as0.25is unchanged.__post_init__already converts these values to Python floats, so no type change is possible.
- Limits in
Handling NumPy limits in _text rather than at construction
This is acceptable. Converting limits at construction would also convert the NumPy integer limit types that #104 keeps. positive_zero in _text (csv_io.py:585-594) changes only float zeros. isinstance(value, float) then sends np.float64 to repr(float) and leaves np.float32 on str, as before. The np.float32 str text does not round-trip to the exact float32 value, but that was already true on main and is outside this PR.
Docs
- The CHANGELOG, plan N71 and the
float_textmodule docstring now separate the two kinds of output:- text the package writes itself goes through
canonical_zero; - values handed to a caller to write go through
positive_zero.
- text the package writes itself goes through
- The CHANGELOG states the
np.float64side fix. - Not bumping
schema_versionis still justified.
Nits (non-blocking)
docs/contract.md§7.1: after the rewrap, the line holding only`summary_columns(channel_ids)` the namesis short. Cosmetic.- §7.1 still says "numbers as floats".
to_rownow passes a value the caller supplied through with its own type, e.g. an int. That's fine for summaries made byimage_summaries(); the wording could mention it later.
Not merged or pushed.
Generated by Claude Code
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
… nits) Only a float zero of either sign is replaced by 0.0; floats come from image_summaries and a caller-given value keeps its type. Rewraps the short line left by #110. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G3qp9AxjpGPw9xfWAUJNqc
Closes #105. Owner decision of 17 November 2026: canonicalise signed zero to
0.0(rule 2 of the issue). Recorded as plan N71. Targeted for v0.7.1.What changes
float_textwithcanonical_zero(x) = float(x) + 0.0andfloat_text(x) = repr(canonical_zero(x)). This is the one place a float becomes written text.x + 0.0changes no float except-0.0, which becomes0.0under round-to-nearest. NaN and ±inf are written as before.csv_io._text, used forIntensity_Min/MaxIntensity_<ID>andHistogram_BinningMin/Max_<ID>.csv_io._tail_text, used for the underflow and overflow mean and std.Parameters.to_columns(), used forNormalization_Offset/Scale_<ID>.ImageSummary.to_row(), for its float values. The package writes no file here, but these rows are the §7.1 columns a calling brick writes.normalization._exact_decimal(percentile_name). This path already did+ 0.0and now uses the helper.tests/dataare test goldens written by test scripts.PlaneHistogram,Parameters,ImageSummaryfields) keep their sign. No algorithm, API, schema version or message changes. The contract (§6.1, §7.1, §8.2), plan N71 and CHANGELOG (Unreleased) are updated.Goldens and bytes
tests/data/*.json,golden_pixels.npz, spec fixtures) contains-0.0. An exact-regex search found none.tools/dump_outputs.pygives the same digests before and after: default032b395b…,--corpus0111f8bd…. Neither dump contains-0.0.-0.0change, and only in those cells. The new value reads back as an equal float.Tests
tests/test_signed_zero.py(33 tests) covers:-0.0float32 plane, written as0.0in every float column;[-0.0, 0.0, -0.0, 0.0]plus three mixed planes, each written in three element orders, which must give identical bytes;-0.0(the round trip still works);percentile_name;On v0.7.0 /
origin/main(helper-only tests removed so the file imports), the suite gives 29 failed, 3 passed. Every writer path fails there. With this change all 33 pass.Full suite, run in two halves (Python 3.13,
[dev,histology]):csbdeepis not installed);tools/check.py --rulesandruff format --checkare clean.Release note
Draft release PR #109 merges cleanly with this branch in git terms. Its v0.7.1 summary says "every output bitwise that of v0.7.0" and "No … file-format change", so it needs a small edit to mention #105 after this merges.
Generated by Claude Code