Skip to content

Commit

Permalink
math/fast_cov.py: add conversion from masked array to regular array a…
Browse files Browse the repository at this point in the history
…t end of calculation b/c it is what most users are expecting - e.g. numpy.isnan and pandas.isnull doesn't detect a masked element in a masked array
  • Loading branch information
Dave Lahr committed Jul 11, 2019
1 parent a839c6a commit 908d943
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmapPy/math/fast_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,17 @@ def nan_fast_cov(x, y=None, destination=None):
else:
y_masked = numpy.ma.array(y, mask=numpy.isnan(y))

dest_was_None = False
if destination is None:
destination = numpy.ma.zeros((x_masked.shape[1], y_masked.shape[1]))
dest_was_None = True

r = _fast_cov(numpy.nanmean, _nan_dot_divide, x_masked, y_masked, destination)

r[numpy.isinf(r)] = numpy.nan

r = numpy.ma.filled(r, fill_value=numpy.nan) if dest_was_None else r

return r


Expand Down
12 changes: 12 additions & 0 deletions cmapPy/math/tests/test_fast_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ def test_nan_fast_cov_x_and_y_different_shapes(self):
self.assertTrue(numpy.isnan(r[1,0]), """expect this entry to be nan b/c for the intersection of x[:,1] and y[:,0]
there is only one entry in common, therefore covariance is undefined""")

def test_nan_fast_cov_all_nan(self):
x = numpy.zeros(3)
x[:] = numpy.nan
x = x[:, numpy.newaxis]
logger.debug("x:\n{}".format(x))

r = fast_cov.nan_fast_cov(x)
logger.debug("r:\n{}".format(r))

self.assertEqual(1, numpy.sum(numpy.isnan(r)))


if __name__ == "__main__":
setup_logger.setup(verbose=True)

Expand Down

0 comments on commit 908d943

Please sign in to comment.