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

Added additional validation for connectivity checks. #168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

david-sh-csiro
Copy link
Collaborator

@david-sh-csiro david-sh-csiro commented Dec 16, 2024

Starting with just edge face connectivity to make sure were on the right track.

@david-sh-csiro david-sh-csiro linked an issue Dec 16, 2024 that may be closed by this pull request
Comment on lines +751 to +754
try:
fill_value = data_array.encoding['_FillValue']
except KeyError:
return True
Copy link
Contributor

@mx-moth mx-moth Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the nature of check functions like this, an early return of True can lead to bugs in the future. If another developer comes along and adds another check below this check - just as you've added a check below the existing checks - then that new check will be skipped if there is a missing _FillValue. An early return of False is always valid as any failed check renders the whole connectivity array invalid. Consider instead:

if '_FillValue' in data_array.encoding:
    fill_value = data_array.encoding['_FillValue']
    ...
    if lower_bound < fill_value < upper_bound:
        warnings.warn(...)
        return False

return True

New checks can be safely added later by appending them below the existing set of checks without risk of the check being skipped.

Comment on lines +768 to +774
if lower_bound < fill_value < theoretical_upper_bound:
warnings.warn(
f"Got a face_edge_connectivity variable {data_array.name!r} with "
f"a _FillValue inside the theoretical index range",
ConventionViolationWarning,
)
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be checked. The theoretical upper bound is a conservative over estimate, useful when computing our own _FillValue as a value that is guaranteed never to collide. However in practice lower values can safely be used as long as the actual _FillValue is lower than actual upper bound. This test will give superfluous warnings on valid data sets.


lower_bound = _get_start_index(data_array)
theoretical_upper_bound = self.face_count * self.max_node_count
actual_upper_bound = numpy.nanmax(data_array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum edge ID can be found more precisely with

upper_bound = self.edge_count + lower_bound

Because of the exact issue of variables being incorrectly masked and therefore missing valid data, any information gleaned by introspecting the data is potentially suspect. This check as written would fail if _FillValue was exactly the maximum edge ID. numpy.nanmax() would not find this value as it has been masked, returning the second-to-last value which would then incorrectly cause the checks below to pass.

theoretical_upper_bound = self.face_count * self.max_node_count
actual_upper_bound = numpy.nanmax(data_array)

if lower_bound < fill_value < actual_upper_bound:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be less than or equal checks. A fill value of 0 and a lower bound of 0 is invalid as it will mask out the first edge ID:

if lower_bound <= fill_value <= upper_bound:

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.

Connectivity Fill value Validation
2 participants