Skip to content

Allow NumPy scalar values in entropy and Hjorth functions - #63

Open
evadiva3 wants to merge 1 commit into
raphaelvallat:masterfrom
evadiva3:fix/antropy-1-numpy-scalar-arguments-rejected
Open

evadiva3 wants to merge 1 commit into
raphaelvallat:masterfrom
evadiva3:fix/antropy-1-numpy-scalar-arguments-rejected

Conversation

@evadiva3

@evadiva3 evadiva3 commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

Some valid NumPy scalar inputs get rejected by the entropy and Hjorth functions because the type checks only look for Python int and float values. This uses numbers.Real so NumPy real scalars are accepted too. The added tests cover those inputs and make sure invalid strings still raise an error.

Reproduction

import numpy as np
import antropy as ant
x = np.random.default_rng(0).normal(size=100)
print(ant.sample_entropy(x, tolerance=np.float32(0.2)))
print(ant.app_entropy(x, tolerance=np.float32(0.2)))
print(ant.hjorth_params(x, sf=np.int64(100)))

Before: the first call raises TypeError: tolerance must be a float or int, got float32.

After, with the seed above:

2.1832383353614793
0.6185074856803912
(np.float64(133.82668601970298), np.float64(1.2985206760088817))

The regression tests also compare NumPy scalar results with equivalent Python scalar arguments and retain the invalid-string checks.

Impact

Rating: P2

Why: Valid NumPy real scalar tolerances and sampling frequencies are rejected in otherwise supported calls. This affects scientific-code inputs such as a tolerance computed from float32 data; the fix changes validation, not the entropy or Hjorth formulas.

Testing

python -m pytest -q: 34 passed. Ruff lint and format checks pass across src and tests. The three affected regression tests fail against the original implementation and pass with the patch.

AI assistance

Model used: GPT-6.1 Sol via OpenAI Codex.

I used it to inspect the saved patch, review the code, run the reported checks, and help with this description. The contribution starts from an existing saved patch.

Checklist

  • The impact rating is stated and justified.
  • The branch is current with the target branch, and open PRs were checked for duplicates.
  • The change is small and focused.
  • The description starts with my supplied summary.
  • Regression tests and the full test suite pass.
  • Code style checks pass.
  • The changelog was not modified.

If AI-assisted:

  • The PR was opened as a draft.
  • The exact model used is reported.
  • An AI review was run and its summary was posted.

@evadiva3

Copy link
Copy Markdown
Author

AI review — GPT-6.1 Sol via OpenAI Codex

  • Checked all three changed validation sites: approximate entropy tolerance, sample entropy tolerance, and Hjorth sampling frequency. The numerical formulas are unchanged.
  • Checked the regression coverage for NumPy float32/float64/int64 arguments, equivalence with Python scalars, and rejection of invalid strings. The original implementation fails the affected tests.
  • Full suite: 34 passed. Ruff lint and format checks pass on source and tests.
  • No additional defects were found in this scoped review, so no extra fixes were added or deferred.
  • Simplicity review: one standard-library numeric abstraction replaces repeated built-in-only checks; no new helper, dependency, or numerical implementation was introduced. Claude Code /review and /simplify commands were not used.

@evadiva3
evadiva3 marked this pull request as ready for review November 17, 2026 03:58

This branch has not been deployed

No deployments
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