Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] : Find more comprehensive and easier to update method of checking dictionaries are almost equal #913

Open
SylviaWhittle opened this issue Sep 24, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@SylviaWhittle
Copy link
Collaborator

Is your feature request related to a problem?

Currently the workflow in tests that need recursive tests of dictionaries containing numpy arrays requires the dict_almost_equal function, but this necessitates the use of large amounts of boilerplate code to overwrite the files when the tests need updating.

Describe the solution you would like.

If we could find a package that is able to:

  • Compare complex & nested dictionaries containing multidimensional numpy arrays
  • Update tests with ease like regtest's --regtest-reset', or syrupy's --snapshot-update`
  • Allow a configurable tolerance for floats
  • Handle np.nan values (ie np.nan == np.nan)

Describe the alternatives you have considered.

  • Syrupy doesn't seem to be able to use tolerances
  • DeepDiff is almost perfect, but flags np.nan != np.nan
  • Regtest doesn't seem to allow tolerances

Additional context

No response

@SylviaWhittle SylviaWhittle added the enhancement New feature or request label Sep 24, 2024
@ns-rse
Copy link
Collaborator

ns-rse commented Sep 24, 2024

Re: DeepDiff

I had a quick search of their issues to see if this had been reported and came across max_diffs doesn't use ignore_nan_inequality flag and the related documentation.

Perhaps we could consider contributing up-stream to fix this and then use DeepDiff. 🤔 🤷

Looking at the code the self.ignore_nan_inequality is passed as the argument to equal_nan in the call to np.equal_nan() but it isn't passed as an argument to np.testing.assert_almost_equal() a couple of lines down....because there is no such option to np.testing.assert_almost_equal().

A quick check though suggests the reason for this is that there is no need to since numpy correctly treats np.nan equality...

import numpy as np

np.testing.assert_almost_equal(np.asarray([np.nan]), np.asarray([np.nan]), verbose=True)

@SylviaWhittle
Copy link
Collaborator Author

SylviaWhittle commented Sep 30, 2024

np.testing.assert_almost_equal doesn't work since it doesn't seem to like handling dictionaries:

np.testing.assert_almost_equal(result_disordered_crop_data, expected_disordered_crop_data, decimal=11)
np.testing.assert_almost_equal(result_all_images, expected_all_images, decimal=11)
FAILED tests/tracing/test_disordered_tracing.py::test_trace_image_disordered[catenane] - TypeError: unsupported operand type(s) for -: 'dict' and 'dict'
FAILED tests/tracing/test_disordered_tracing.py::test_trace_image_disordered[replication intermediate] - TypeError: unsupported operand type(s) for -: 'dict' and 'dict'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants