Example #1
0
    def testNanReduction(self):
        raw = np.random.choice(a=[0, 1, np.nan],
                               size=(10, 10),
                               p=[0.3, 0.4, 0.3])

        arr = tensor(raw, chunk_size=3)

        self.assertEqual(np.nansum(raw),
                         self.executor.execute_tensor(nansum(arr))[0])
        self.assertEqual(np.nanprod(raw),
                         self.executor.execute_tensor(nanprod(arr))[0])
        self.assertEqual(np.nanmax(raw),
                         self.executor.execute_tensor(nanmax(arr))[0])
        self.assertEqual(np.nanmin(raw),
                         self.executor.execute_tensor(nanmin(arr))[0])
        self.assertEqual(np.nanmean(raw),
                         self.executor.execute_tensor(nanmean(arr))[0])
        self.assertAlmostEqual(np.nanvar(raw),
                               self.executor.execute_tensor(nanvar(arr))[0])
        self.assertAlmostEqual(
            np.nanvar(raw, ddof=1),
            self.executor.execute_tensor(nanvar(arr, ddof=1))[0])
        self.assertAlmostEqual(np.nanstd(raw),
                               self.executor.execute_tensor(nanstd(arr))[0])
        self.assertAlmostEqual(
            np.nanstd(raw, ddof=1),
            self.executor.execute_tensor(nanstd(arr, ddof=1))[0])

        arr = tensor(raw, chunk_size=10)

        self.assertEqual(np.nansum(raw),
                         self.executor.execute_tensor(nansum(arr))[0])
        self.assertEqual(np.nanprod(raw),
                         self.executor.execute_tensor(nanprod(arr))[0])
        self.assertEqual(np.nanmax(raw),
                         self.executor.execute_tensor(nanmax(arr))[0])
        self.assertEqual(np.nanmin(raw),
                         self.executor.execute_tensor(nanmin(arr))[0])
        self.assertEqual(np.nanmean(raw),
                         self.executor.execute_tensor(nanmean(arr))[0])
        self.assertAlmostEqual(np.nanvar(raw),
                               self.executor.execute_tensor(nanvar(arr))[0])
        self.assertAlmostEqual(
            np.nanvar(raw, ddof=1),
            self.executor.execute_tensor(nanvar(arr, ddof=1))[0])
        self.assertAlmostEqual(np.nanstd(raw),
                               self.executor.execute_tensor(nanstd(arr))[0])
        self.assertAlmostEqual(
            np.nanstd(raw, ddof=1),
            self.executor.execute_tensor(nanstd(arr, ddof=1))[0])

        raw = np.random.random((10, 10))
        raw[:3, :3] = np.nan
        arr = tensor(raw, chunk_size=3)
        self.assertEqual(np.nanargmin(raw),
                         self.executor.execute_tensor(nanargmin(arr))[0])
        self.assertEqual(np.nanargmax(raw),
                         self.executor.execute_tensor(nanargmax(arr))[0])

        raw = np.full((10, 10), np.nan)
        arr = tensor(raw, chunk_size=3)

        self.assertEqual(0, self.executor.execute_tensor(nansum(arr))[0])
        self.assertEqual(1, self.executor.execute_tensor(nanprod(arr))[0])
        self.assertTrue(np.isnan(self.executor.execute_tensor(nanmax(arr))[0]))
        self.assertTrue(np.isnan(self.executor.execute_tensor(nanmin(arr))[0]))
        self.assertTrue(np.isnan(
            self.executor.execute_tensor(nanmean(arr))[0]))
        with self.assertRaises(ValueError):
            _ = self.executor.execute_tensor(nanargmin(arr))[0]  # noqa: F841
        with self.assertRaises(ValueError):
            _ = self.executor.execute_tensor(nanargmax(arr))[0]  # noqa: F841

        raw = sps.random(10, 10, density=.1, format='csr')
        raw[:3, :3] = np.nan
        arr = tensor(raw, chunk_size=3)

        self.assertAlmostEqual(np.nansum(raw.A),
                               self.executor.execute_tensor(nansum(arr))[0])
        self.assertAlmostEqual(np.nanprod(raw.A),
                               self.executor.execute_tensor(nanprod(arr))[0])
        self.assertAlmostEqual(np.nanmax(raw.A),
                               self.executor.execute_tensor(nanmax(arr))[0])
        self.assertAlmostEqual(np.nanmin(raw.A),
                               self.executor.execute_tensor(nanmin(arr))[0])
        self.assertAlmostEqual(np.nanmean(raw.A),
                               self.executor.execute_tensor(nanmean(arr))[0])
        self.assertAlmostEqual(np.nanvar(raw.A),
                               self.executor.execute_tensor(nanvar(arr))[0])
        self.assertAlmostEqual(
            np.nanvar(raw.A, ddof=1),
            self.executor.execute_tensor(nanvar(arr, ddof=1))[0])
        self.assertAlmostEqual(np.nanstd(raw.A),
                               self.executor.execute_tensor(nanstd(arr))[0])
        self.assertAlmostEqual(
            np.nanstd(raw.A, ddof=1),
            self.executor.execute_tensor(nanstd(arr, ddof=1))[0])

        arr = nansum(1)
        self.assertEqual(self.executor.execute_tensor(arr)[0], 1)
Example #2
0
def test_nan_reduction(setup):
    raw = np.random.choice(a=[0, 1, np.nan], size=(10, 10), p=[0.3, 0.4, 0.3])

    arr = tensor(raw, chunk_size=6)

    assert np.nansum(raw) == nansum(arr).execute().fetch()
    assert np.nanprod(raw) == nanprod(arr).execute().fetch()
    assert np.nanmax(raw) == nanmax(arr).execute().fetch()
    assert np.nanmin(raw) == nanmin(arr).execute().fetch()
    assert np.nanmean(raw) == nanmean(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw)) == nanvar(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw, ddof=1)) == nanvar(
        arr, ddof=1).execute().fetch()
    assert pytest.approx(np.nanstd(raw)) == nanstd(arr).execute().fetch()
    assert pytest.approx(np.nanstd(raw, ddof=1)) == nanstd(
        arr, ddof=1).execute().fetch()

    arr = tensor(raw, chunk_size=10)

    assert np.nansum(raw) == nansum(arr).execute().fetch()
    assert np.nanprod(raw) == nanprod(arr).execute().fetch()
    assert np.nanmax(raw) == nanmax(arr).execute().fetch()
    assert np.nanmin(raw) == nanmin(arr).execute().fetch()
    assert np.nanmean(raw) == nanmean(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw)) == nanvar(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw, ddof=1)) == nanvar(
        arr, ddof=1).execute().fetch()
    assert pytest.approx(np.nanstd(raw)) == nanstd(arr).execute().fetch()
    assert pytest.approx(np.nanstd(raw, ddof=1)) == nanstd(
        arr, ddof=1).execute().fetch()

    raw = np.random.random((10, 10))
    raw[:3, :3] = np.nan
    arr = tensor(raw, chunk_size=6)
    assert np.nanargmin(raw) == nanargmin(arr).execute().fetch()
    assert np.nanargmax(raw) == nanargmax(arr).execute().fetch()

    raw = np.full((10, 10), np.nan)
    arr = tensor(raw, chunk_size=6)

    assert 0 == nansum(arr).execute().fetch()
    assert 1 == nanprod(arr).execute().fetch()
    assert np.isnan(nanmax(arr).execute().fetch())
    assert np.isnan(nanmin(arr).execute().fetch())
    assert np.isnan(nanmean(arr).execute().fetch())
    with pytest.raises(ValueError):
        _ = nanargmin(arr).execute()  # noqa: F841
    with pytest.raises(ValueError):
        _ = nanargmax(arr).execute()  # noqa: F841

    raw = sps.random(10, 10, density=.1, format='csr')
    raw[:3, :3] = np.nan
    arr = tensor(raw, chunk_size=6)

    assert pytest.approx(np.nansum(raw.A)) == nansum(arr).execute().fetch()
    assert pytest.approx(np.nanprod(raw.A)) == nanprod(arr).execute().fetch()
    assert pytest.approx(np.nanmax(raw.A)) == nanmax(arr).execute().fetch()
    assert pytest.approx(np.nanmin(raw.A)) == nanmin(arr).execute().fetch()
    assert pytest.approx(np.nanmean(raw.A)) == nanmean(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw.A)) == nanvar(arr).execute().fetch()
    assert pytest.approx(np.nanvar(raw.A, ddof=1)) == nanvar(
        arr, ddof=1).execute().fetch()
    assert pytest.approx(np.nanstd(raw.A)) == nanstd(arr).execute().fetch()
    assert pytest.approx(np.nanstd(raw.A, ddof=1)) == nanstd(
        arr, ddof=1).execute().fetch()

    arr = nansum(1)
    assert arr.execute().fetch() == 1