fix: introduce explicit conversion to scalar to avoid implicit conversion issues after numpy 2.0.0#475
Open
marat-davudov wants to merge 1 commit into
Conversation
89ffedb to
311c897
Compare
…sion issues after numpy 2.0.0
311c897 to
09307b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: NumPy 2.0.0 Compatibility - Explicit Scalar Conversion. Issue #474
Summary
This PR fixes compatibility issues with NumPy 2.0.0 by adding explicit scalar conversions before type casting operations.
Problem
Starting with NumPy 2.0.0, implicit conversion of NumPy scalar types to Python built-in types has been deprecated and raises errors. Operations like
int(np.array([1])[0])orint(np.argmax(...))no longer work automatically.Error example:
Solution
Use the
.item()method to explicitly convert NumPy scalars to Python scalars before type conversion:Changes Made
Fixed 5 locations across 2 files where NumPy scalars were implicitly converted:
dice_ml/explainer_interfaces/explainer_base.pynp.argmax(original_pred)→ add.item()beforeint()conversionself.target_cf_class[0][0]→ add.item()beforeint()conversionis_cf_valid()→ add.item()beforeint()conversiondice_ml/explainer_interfaces/dice_genetic.pytup[0]frompopulation_fitness→ add.item()beforeint()conversiontup[0]frompopulation_fitness→ add.item()beforeint()conversionAll changes use defensive programming with
hasattr()checks to maintain backward compatibility with older NumPy versions.Testing
Related
This addresses the NumPy 2.0 migration guide recommendations:
https://numpy.org/devdocs/numpy_2_0_migration_guide.html#changes-to-numpy-data-type-promotion