Skip to content

Commit

Permalink
Add NaN/Inf checks into check_numpy_array_features
Browse files Browse the repository at this point in the history
For normal scenario (and provide a killer switch to turn off the check), all numpy arrays should not have NaN or Inf.
  • Loading branch information
yslan authored and inducer committed Jan 11, 2025
1 parent 2547d52 commit 733317e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion course/page/code_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def check_numpy_array_sanity(self, name, num_axes, data):
0, f"'{name}' does not consist of floating point numbers--"
f"got: '{data.dtype}'")

def check_numpy_array_features(self, name, ref, data, report_failure=True):
def check_numpy_array_features(self, name, ref, data, report_failure=True, check_finite=True):
import numpy as np
assert isinstance(ref, np.ndarray)

Expand All @@ -91,6 +91,12 @@ def bad(msg):
f"'{name}' does not have correct data type--"
f"got: '{data.dtype}', expected: '{ref.dtype}'")

if check_finite:
if np.any(np.isnan(data)):
return bad(f"'{name}' contains NaN")
if np.any(np.isinf(data)):
return bad(f"'{name}' contains Inf")

return True

def check_numpy_array_allclose(self, name, ref, data, accuracy_critical=True,
Expand Down

0 comments on commit 733317e

Please sign in to comment.