def test_unary_operations(self): print("Test unary operations") X, Y, Z = 1024, 512, 512 X, Y, Z = 256, 512, 512 steps = [timer()] a = -numpy.ones((X, Y, Z), dtype='float32') steps.append(timer()) t0 = dt(steps) print(t0) #print("a refcount " , sys.getrefcount(a)) ds = DataContainer(a, False, ['X', 'Y', 'Z']) ds.sign(out=ds) steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], -1.) ds.abs(out=ds) steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 1.) ds.__imul__(2) ds.sqrt(out=ds) steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], numpy.sqrt(2., dtype='float32'))
def testGb_creation_nocopy(self): X, Y, Z = 512, 512, 512 X, Y, Z = 256, 512, 512 steps = [timer()] a = numpy.ones((X, Y, Z), dtype='float32') steps.append(timer()) t0 = dt(steps) print("test clone") #print("a refcount " , sys.getrefcount(a)) ds = DataContainer(a, False, ['X', 'Y', 'Z']) #print("a refcount " , sys.getrefcount(a)) self.assertEqual(sys.getrefcount(a), 3) ds1 = ds.copy() self.assertNotEqual(aid(ds.as_array()), aid(ds1.as_array())) ds1 = ds.clone() self.assertNotEqual(aid(ds.as_array()), aid(ds1.as_array()))
def testInlineAlgebra(self): print("Test Inline Algebra") X, Y, Z = 1024, 512, 512 X, Y, Z = 256, 512, 512 steps = [timer()] a = numpy.ones((X, Y, Z), dtype='float32') steps.append(timer()) t0 = dt(steps) print(t0) #print("a refcount " , sys.getrefcount(a)) ds = DataContainer(a, False, ['X', 'Y', 'Z']) #ds.__iadd__( 2 ) ds += 2 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 3.) #ds.__isub__( 2 ) ds -= 2 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 1.) #ds.__imul__( 2 ) ds *= 2 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 2.) #ds.__idiv__( 2 ) ds /= 2 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 1.) ds1 = ds.copy() #ds1.__iadd__( 1 ) ds1 += 1 #ds.__iadd__( ds1 ) ds += ds1 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 3.) #ds.__isub__( ds1 ) ds -= ds1 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 1.) #ds.__imul__( ds1 ) ds *= ds1 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 2.) #ds.__idiv__( ds1 ) ds /= ds1 steps.append(timer()) print(dt(steps)) self.assertEqual(ds.as_array()[0][0][0], 1.)
def binary_divide(self): print("Test binary divide") X, Y, Z = 1024, 512, 512 X, Y, Z = 256, 512, 512 steps = [timer()] a = numpy.ones((X, Y, Z), dtype='float32') steps.append(timer()) t0 = dt(steps) #print("a refcount " , sys.getrefcount(a)) ds = DataContainer(a, False, ['X', 'Y', 'Z']) ds1 = ds.copy() t1 = 0 t2 = 0 for i in range(10): steps.append(timer()) ds.divide(ds1, out=ds) steps.append(timer()) t1 += dt(steps)/10. print("ds.divide(ds1, out=ds)", dt(steps)) steps.append(timer()) ds2 = ds.divide(ds1) steps.append(timer()) t2 += dt(steps)/10. print("ds2 = ds.divide(ds1)", dt(steps)) #self.assertLess(t1, t2) self.assertEqual(ds.as_array()[0][0][0], 1.) ds0 = ds ds0.divide(2, out=ds0) steps.append(timer()) print("ds0.divide(2,out=ds0)", dt(steps), 0.5, ds0.as_array()[0][0][0]) self.assertEqual(0.5, ds0.as_array()[0][0][0]) dt1 = dt(steps) ds3 = ds0.divide(2) steps.append(timer()) print("ds3 = ds0.divide(2)", dt(steps), 0.25, ds3.as_array()[0][0][0]) dt2 = dt(steps) #self.assertLess(dt1, dt2) self.assertEqual(.25, ds3.as_array()[0][0][0]) self.assertEqual(.5, ds.as_array()[0][0][0])
def test_exp_log(self): a0 = numpy.asarray([1 for i in range(2*3*4)]) ds0 = DataContainer(numpy.reshape(a0,(2,3,4)), suppress_warning=True) # ds1 = DataContainer(numpy.reshape(a1,(2,3,4)), suppress_warning=True) b = ds0.exp().log() self.assertNumpyArrayEqual(ds0.as_array(), b.as_array()) self.assertEqual(ds0.exp().as_array()[0][0][0], numpy.exp(1)) self.assertEqual(ds0.log().as_array()[0][0][0], 0.)
def test_dot(self): a0 = numpy.asarray([i for i in range(2 * 3 * 4)]) a1 = numpy.asarray([2 * i for i in range(2 * 3 * 4)]) ds0 = DataContainer(numpy.reshape(a0, (2, 3, 4))) ds1 = DataContainer(numpy.reshape(a1, (2, 3, 4))) numpy.testing.assert_equal(ds0.dot(ds1), a0.dot(a1)) a2 = numpy.asarray([2 * i for i in range(2 * 3 * 5)]) ds2 = DataContainer(numpy.reshape(a2, (2, 3, 5))) # it should fail if the shape is wrong try: ds2.dot(ds0) self.assertTrue(False) except ValueError as ve: self.assertTrue(True) print("test dot numpy") n0 = (ds0 * ds1).sum() n1 = ds0.as_array().ravel().dot(ds1.as_array().ravel()) self.assertEqual(n0, n1)
def binary_multiply(self): print("Test binary multiply") X, Y, Z = 1024, 512, 512 X, Y, Z = 256, 512, 512 steps = [timer()] a = numpy.ones((X, Y, Z), dtype='float32') steps.append(timer()) t0 = dt(steps) #print("a refcount " , sys.getrefcount(a)) ds = DataContainer(a, False, ['X', 'Y', 'Z']) ds1 = ds.copy() steps.append(timer()) ds.multiply(ds1, out=ds) steps.append(timer()) t1 = dt(steps) print("ds.multiply(ds1, out=ds)", dt(steps)) steps.append(timer()) ds2 = ds.multiply(ds1) steps.append(timer()) t2 = dt(steps) print("ds2 = ds.multiply(ds1)", dt(steps)) #self.assertLess(t1, t2) ds0 = ds ds0.multiply(2, out=ds0) steps.append(timer()) print("ds0.multiply(2,out=ds0)", dt( steps), 2., ds0.as_array()[0][0][0]) self.assertEqual(2., ds0.as_array()[0][0][0]) dt1 = dt(steps) ds3 = ds0.multiply(2) steps.append(timer()) print("ds3 = ds0.multiply(2)", dt(steps), 4., ds3.as_array()[0][0][0]) dt2 = dt(steps) #self.assertLess(dt1, dt2) self.assertEqual(4., ds3.as_array()[0][0][0]) self.assertEqual(2., ds.as_array()[0][0][0]) ds.multiply(2.5, out=ds0) self.assertEqual(2.5*2., ds0.as_array()[0][0][0])