Skip to content

fix: to()/array_as() same dtype-alias bug as the constructor; astype() double-cast - #87

Open
petercorke wants to merge 1 commit into
fix/infer-dtype-alias-mismatchfrom
fix/to-array-as-astype-dtype-alias
Open

fix: to()/array_as() same dtype-alias bug as the constructor; astype() double-cast#87
petercorke wants to merge 1 commit into
fix/infer-dtype-alias-mismatchfrom
fix/to-array-as-astype-dtype-alias

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Stacked on #86 (needs DTYPE_ALIASES from that branch). Same root cause found while auditing every dtype-resolving entry point after #86:

  • Image.to() / Image.array_as() both called plain np.dtype(dtype) with no DTYPE_ALIASES lookup, so dtype='float'/'int' resolved to NumPy's own float64/platform-int rather than the Toolbox's float32/uint8 convention. 'double' and 'half' never showed this bug -- NumPy's own np.dtype('double')/('half') already equal float64/float16, coincidentally matching DTYPE_ALIASES for those two.
  • to() also did its own separate premature dtype = np.dtype(dtype) before calling array_as(dtype) -- simplified to just pass dtype through unchanged now that array_as() and the Image constructor both resolve it themselves.
  • astype() had no test-visible bug -- its final output was already correct, self-corrected by the constructor's now-fixed dtype= handling in self.__class__(self._A.astype(dtype), dtype=dtype) -- but did an unnecessary intermediate cast to NumPy's raw (wrong) interpretation of the alias before the constructor cast it again to the right one, e.g. astype('float') cast uint8->float64->float32 instead of directly uint8->float32. Resolved the alias before the first cast to avoid the redundant round-trip.

Test plan

  • Extended tests/test_dtype_resolution.py's shared parametrized matrix with test methods for to()/array_as()/astype(), rather than new one-off tests
  • Verified against pre-fix code: to()/array_as() fail for exactly the 'int'/'float' cases (not 'double'/'half', matching the analysis above); astype() correctly shows no output-level regression
  • Full tests/test_image_core.py + tests/test_dtype_resolution.py suites pass (119 passed)

…) double-cast

Same root cause as the constructor/convert() fix this branch is based
on: Image.to()/.array_as() called plain np.dtype(dtype) with no
DTYPE_ALIASES lookup, so dtype='float'/'int' resolved to NumPy's own
float64/platform-int rather than the Toolbox's float32/uint8
convention. 'double' and 'half' never showed this bug -- NumPy's own
np.dtype('double')/('half') already equal float64/float16, coincidentally
matching DTYPE_ALIASES for those two.

to() also did its own separate premature dtype = np.dtype(dtype) before
calling array_as(dtype) -- simplified to just pass dtype through
unchanged now that array_as() and the Image constructor both resolve
it themselves; no need to resolve it three times.

astype() had no test-visible bug (its final output was already correct,
self-corrected by the constructor's now-fixed dtype= handling in
self.__class__(self._A.astype(dtype), dtype=dtype)) but did an
unnecessary intermediate cast to NumPy's raw (wrong) interpretation of
the alias before the constructor cast it again to the right one --
e.g. astype('float') cast uint8->float64->float32 instead of directly
uint8->float32. Resolved the alias before the first cast to avoid the
redundant round-trip.

Extended tests/test_dtype_resolution.py's shared parametrized matrix
with test methods for these three entry points, rather than new
one-off tests -- confirms to()/array_as() fail against pre-fix code
for the 'int'/'float' cases specifically (not 'double'/'half', matching
the analysis above), and that astype() has no output-level regression
to catch.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 3 high

Alerts:
⚠ 3 issues (≤ 0 issues of at least minor severity)

Results:
3 new issues

Category Results
Security 3 high

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant