def testCast(self): N = 100 arrays = { ak.int64: ak.randint(-(2**48), 2**48, N), ak.float64: ak.randint(0, 1, N, dtype=ak.float64), ak.bool: ak.randint(0, 2, N, dtype=ak.bool) } roundtripable = set( ((ak.bool, ak.bool), (ak.int64, ak.int64), (ak.int64, ak.float64), (ak.int64, npstr), (ak.float64, ak.float64), (ak.float64, npstr), (ak.uint8, ak.int64), (ak.uint8, ak.float64), (ak.uint8, npstr))) for t1, orig in arrays.items(): for t2 in ak.DTypes: t2 = ak.dtype(t2) other = ak.cast(orig, t2) self.assertEqual(orig.size, other.size) if (t1, t2) in roundtripable: roundtrip = ak.cast(other, t1) self.assertTrue((orig == roundtrip).all(), f"{t1}: {orig[:5]}, {t2}: {roundtrip[:5]}") self.assertTrue((ak.array([1, 2, 3, 4, 5]) == ak.cast(ak.linspace(1, 5, 5), dt=ak.int64)).all()) self.assertEqual( ak.cast(ak.arange(0, 5), dt=ak.float64).dtype, ak.float64) self.assertTrue((ak.array([False, True, True, True, True]) == ak.cast(ak.linspace(0, 4, 5), dt=ak.bool)).all())
def test_flatten(self): orig = ak.array(['one|two', 'three|four|five', 'six']) flat, mapping = orig.flatten('|', return_segments=True) ans = ak.array(['one', 'two', 'three', 'four', 'five', 'six']) ans2 = ak.array([0, 2, 5]) self.assertTrue((flat == ans).all()) self.assertTrue((mapping == ans2).all()) thirds = [ak.cast(ak.arange(i, 99, 3), 'str') for i in range(3)] thickrange = thirds[0].stick(thirds[1], delimiter=', ').stick(thirds[2], delimiter=', ') flatrange = thickrange.flatten(', ') self.assertTrue((ak.cast(flatrange, 'int64') == ak.arange(99)).all())
def testCast(self): N = 100 arrays = { ak.int64: ak.randint(-(2**48), 2**48, N), ak.float64: ak.randint(0, 1, N, dtype=ak.float64), ak.bool: ak.randint(0, 2, N, dtype=ak.bool) } roundtripable = set( ((ak.bool, ak.bool), (ak.int64, ak.int64), (ak.int64, ak.float64), (ak.int64, akstr), (ak.float64, ak.float64), (ak.float64, akstr), (ak.uint8, ak.int64), (ak.uint8, ak.float64), (ak.uint8, akstr))) for t1, orig in arrays.items(): for t2 in ak.DTypes: t2 = ak.dtype(t2) other = ak.cast(orig, t2) self.assertEqual(orig.size, other.size) if (t1, t2) in roundtripable: roundtrip = ak.cast(other, t1) self.assertTrue((orig == roundtrip).all(), f"{t1}: {orig[:5]}, {t2}: {roundtrip[:5]}")
def testPrecision(self): # See https://github.com/Bears-R-Us/arkouda/issues/964 # Grouped sum was exacerbating floating point errors # This test verifies the fix N = 10**6 G = N // 10 ub = 2**63 // N groupnum = ak.randint(0, G, N, seed=1) intval = ak.randint(0, ub, N, seed=2) floatval = ak.cast(intval, ak.float64) g = ak.GroupBy(groupnum) _, intmean = g.mean(intval) _, floatmean = g.mean(floatval) ak_mse = ak.mean((intmean - floatmean)**2) self.assertTrue(np.isclose(ak_mse, 0.0))