NCAS-CMS / NCAS-CMS/cf-python

Tests: fixed-tolerance over strict NumPy array comparison

Open
#128 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

testing
Dominant language
Python
Stars
150
Forks
23
Avg merge
1d 11h
Merged PRs (30d)
2

Description

In the test suite, for direct comparisons of arrays where some operation has been applied to either or both arrays that results in floating points and therefore is subject to floating point precision, it would be more robust to check the arrays are equal or otherwise within a given tolerance, over comparing for strict element-wise equality e.g. of some arrays cf_data.array and numpy_array as applied often throughout the suite via some statement ultimately similar to:

assertTrue((cf_data.array == numpy_array).all())

This came to my attention because a specific test test_Data_exp failed on one of the CI jobs, whilst passing locally and on other jobs for a different OSs, where changing to a fixed-tolerance comparison namely to a numpy test method:

numpy.testing.assert_allclose(a.array, b)

meant it passed consistently.

I am aware cf-python has its own tolerance handling for equality testing but here I am referring to cases where we are testing the values of numpy arrays underlying the field constructs or data, such that cf-pytohn methods are unavailable. And even if they were, I think it is preferable to not use that in testing as the behaviour of the tolerance handling would add another dependent variable to the 'experiment' of each test.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating test_Data_exp and the other test cases that directly compare NumPy arrays with strict element-wise equality. Review the existing fixed-tolerance comparison using numpy.testing.assert_allclose, then update applicable floating-point comparisons and run the affected tests; done means they remain consistent across CI environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
testing-qa
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.