From 35fe5f3d9f8269e75b0380a189d99348278afcbf Mon Sep 17 00:00:00 2001 From: Alexandre Gobbo Date: Wed, 11 Dec 2019 18:14:43 +0100 Subject: [PATCH] Startup --- script/scitest/dataset_test.py | 1056 ------------------------------- script/scitest/external_test.py | 184 ------ script/scitest/io_test.py | 176 ------ script/scitest/linalg_test.py | 89 --- script/scitest/random_test.py | 73 --- script/scitest/scisoft_test.py | 593 ----------------- 6 files changed, 2171 deletions(-) delete mode 100644 script/scitest/dataset_test.py delete mode 100644 script/scitest/external_test.py delete mode 100644 script/scitest/io_test.py delete mode 100644 script/scitest/linalg_test.py delete mode 100644 script/scitest/random_test.py delete mode 100644 script/scitest/scisoft_test.py diff --git a/script/scitest/dataset_test.py b/script/scitest/dataset_test.py deleted file mode 100644 index ab6b758..0000000 --- a/script/scitest/dataset_test.py +++ /dev/null @@ -1,1056 +0,0 @@ -### -# Copyright 2011 Diamond Light Source Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -### - -# @PydevCodeAnalysisIgnore -""" -Test dataset methods - -import unittest -unittest.TestProgram(argv=["dataset_test"]) -""" -import unittest - -import scisoftpy as np - -import os - -isjava = os.name == "java" - - -def toInt(o): - return int(o) - - -def toAny(o): - return o - - -class Test(unittest.TestCase): - def setUp(self): - self.a = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] - self.mm = [[[0.0, 2.0], [6.0, 12.0]], [[20.0, 30.0], [42.0, 56.0]]] - self.q = [[[0.0, 0.5], [0.6666666666666666, 0.75]], [[0.8, 0.8333333333333334], [0.8571428571428571, 0.875]]] - self.r = [[[1.0, 3.0], [5.0, 7.0]], [[9.0, 11.0], [13.0, 15.0]]] - self.s = [0, 1, 2, 3, 4, 5, 6, 7] - self.t = [[[5.0]]] - self.u = [[[0.0, 1.0], [7.0, 3.0]], [[4.0, 5.0], [7.0, 7.0]]] - self.dda = np.array(self.a, np.float) - - self.dz = 1.0 - self.da = [0.1, 1.5] - self.db = [[0, 1.0], [2.5, 3]] - self.db2 = [[0, 2.0], [4.0, 6]] - self.dc = [[[0, 1.0], [2.5, 3]], [[4.2, 5.1], [6.6, 7.9]]] - if isjava: - import Jama.Matrix as mat - - self.m = mat(self.db) - else: - self.m = self.db - self.zz = 1.0 + 0.5j - self.za = [0.1, 1.5] - self.zb = [[0, 1.0], [2.5, 3]] - self.zc = [[[0, 1.0], [2.5, 3]], [[4.2, 5.1], [6.6, 7.9]]] - - def checkitems(self, la, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i]), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals(convert(la[i][j]), ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k]), ds[i, j, k]) - - def testZeros(self): # make new datasets with zeros - print "test zeros" - dds = np.zeros(3, dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(1, dds.ndim) - self.assertEquals(3, dds.shape[0]) - self.assertEquals(0, dds[0]) - dds = np.zeros((2, 3), dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(0, dds[0, 0]) - dds = np.zeros_like(dds) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(0, dds[0, 0]) - - def testOnes(self): # make new datasets with ones - print "test ones" - dds = np.ones(3, dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(1, dds.ndim) - self.assertEquals(3, dds.shape[0]) - self.assertEquals(1, dds[0]) - dds = np.ones((2, 3), np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(1, dds[0, 0]) - dds = np.ones_like(dds) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(1, dds[0, 0]) - - def testTwos(self): # make new datasets with twos - print "test twos" - dds = np.full(3, 2.0) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(1, dds.ndim) - self.assertEquals(3, dds.shape[0]) - self.assertEquals(2.0, dds[0]) - dds = np.full((2, 3), 2.0) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(2.0, dds[0, 0]) - dds = np.full_like(dds, 2.0) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(2.0, dds[0, 0]) - - dds = np.full(3, 2, dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(1, dds.ndim) - self.assertEquals(3, dds.shape[0]) - self.assertEquals(2.0, dds[0]) - dds = np.full((2, 3), 2, dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(2.0, dds[0, 0]) - dds = np.full_like(dds, 2, dtype=np.float) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(np.float64, dds.dtype) - self.assertEquals(2, dds.ndim) - self.assertEquals(2, dds.shape[0]) - self.assertEquals(3, dds.shape[1]) - self.assertEquals(2.0, dds[0, 0]) - - def testCompound(self): # make new datasets with ones - print "test compound" - # dds = np.ndarrayCB(3, (3,4)) - # self.assertEquals(3, dds.itemsize, msg="itemsize incorrect") - dds = np.zeros((3, 5), np.rgb) - print type(dds), dds.dtype, dds.shape - self.assertEquals(6, dds.itemsize, msg="itemsize incorrect") - dds = dds.red - print type(dds), dds.dtype, dds.shape - self.assertEquals(2, dds.itemsize, msg="itemsize incorrect") - dds = np.zeros((3, 4), np.cint32, elements=2) - print type(dds), dds.dtype, dds.shape - self.assertEquals(8, dds.itemsize, msg="itemsize incorrect") - dds = np.zeros((3, 4), np.cint32(2)) - print type(dds), dds.dtype, dds.shape - self.assertEquals(8, dds.itemsize, msg="itemsize incorrect") - - def testArray(self): # make new datasets - print "test array" - dds = np.array(self.dz, np.float) - self.assertEquals(self.dz, dds) - self.assertEquals(dds, self.dz) - self.assertEquals(dds.item(), self.dz) - if isjava: - self.assertEquals(1, dds.dtype.elements) - self.assertEquals(8, dds.itemsize) - dds = np.array(self.da, np.float) - self.checkitems(self.da, dds) - dds = np.array(self.db, np.float) - self.checkitems(self.db, dds) - dds = np.array(self.dc, np.float) - self.checkitems(self.dc, dds) - mds = np.array(self.m) - self.checkitems(self.db, mds) - zds = np.array(self.zz, np.complex) - self.assertEquals(self.zz, zds) - self.assertEquals(zds, self.zz) - self.assertEquals(zds.item(), self.zz) - self.assertEquals(zds.real.item(), 1.0) - self.assertEquals(zds.imag.item(), 0.5) - self.assertEquals(np.real(zds), 1.0) - self.assertEquals(np.imag(zds), 0.5) - - zds = np.array([self.zz, self.zz], np.complex) - self.assertEquals(self.zz, zds[0]) - self.assertRaises(ValueError, zds.item) - if isjava: - self.assertEquals(1, zds.dtype.elements) - self.assertEquals(16, zds.itemsize) - self.checkitems([1.0, 1.0], zds.real) - self.checkitems([0.5, 0.5], zds.imag) - self.checkitems([1.0, 1.0], np.real(zds)) - self.checkitems([0.5, 0.5], np.imag(zds)) - a = np.array([1 + 2j, 3 + 4j, 5 + 6j]) - a.real = np.array([2, 4, 6]) - self.checkitems([2, 4, 6], np.real(a)) - a.imag = np.array([8, 10, 12]) - self.checkitems([8, 10, 12], np.imag(a)) - zds = np.array(self.za, np.complex) - self.checkitems(self.za, zds) - zds = np.array(self.zb, np.complex) - self.checkitems(self.zb, zds) - zds = np.array(self.zc, np.complex) - self.checkitems(self.zc, zds) - ids = np.array(self.s, np.int) - self.checkitems(self.s, ids) - ids = np.array(self.a, np.int) - self.checkitems(self.a, ids) - ids = np.array(self.s, np.int8) - self.checkitems(self.s, ids) - ids = np.array(self.a, np.int8) - self.checkitems(self.a, ids) - ids = np.array(self.s, np.int16) - self.checkitems(self.s, ids) - ids = np.array(self.a, np.int16) - self.checkitems(self.a, ids) - ids = np.array(self.s, np.int64) - self.checkitems(self.s, ids) - ids = np.array(self.a, np.int64) - self.checkitems(self.a, ids) - - tds = np.array(self.a) - self.assertEquals(tds.dtype, np.int_) - tds = np.array(self.m) - self.assertEquals(tds.dtype, np.float64) - tds = np.array(self.q) - self.assertEquals(tds.dtype, np.float64) - tds = np.array(self.r) - self.assertEquals(tds.dtype, np.float64) - tds = np.array(self.s) - self.assertEquals(tds.dtype, np.int_) - tds = np.array(self.t) - self.assertEquals(tds.dtype, np.float64) - tds = np.array(self.u) - self.assertEquals(tds.dtype, np.float64) - - tds = np.array([np.arange(5), np.arange(5) + 5]) - self.checkitems(np.arange(10).reshape(2, 5), tds) - - def testAsArray(self): - ta = np.array([1, 2]) - ata = np.asarray(ta) - self.assertEquals(ata.dtype, np.int_) - self.checkitems([1, 2], ata) - ata = np.asarray(ta, np.float) - self.assertEquals(ata.dtype, np.float_) - self.checkitems([1, 2], ata) - - def testSlicing(self): - a = np.array([], dtype=np.float_) - self.assertEquals(len(a), 0) - a = np.zeros((2,)) - self.assertEquals(len(a), 2) - self.checkitems([0, 0], a[:]) - self.assertEquals(a[1], 0) - a[:] = 0.5 - self.checkitems([0.5, 0.5], a[:]) - - a = np.zeros((2, 3)) - self.assertEquals(len(a), 2) - self.checkitems([0, 0, 0], a[0]) - a[1] = 0.2 - self.checkitems([0.2, 0.2, 0.2], a[1]) - a = np.zeros((2, 3, 4)) - self.assertEquals(len(a), 2) - a = np.arange(10).reshape((5, 2)) - a[3, :] = np.array([0, 0]) - self.checkitems([0, 0], a[3]) - a[3, :] = np.array([1, 1]).reshape(1, 2) - self.checkitems([1, 1], a[3]) - a[:2, 1] = np.array([2, 2]) - self.checkitems([2, 2], a[:2, 1]) - - a = np.arange(7) - c = range(7) - self.checkitems(c[::2], a[::2]) - self.checkitems(c[0::2], a[0::2]) - self.checkitems(c[3::2], a[3::2]) - self.checkitems(c[6::2], a[6::2]) - self.checkitems(c[6:6:2], a[6:6:2]) - self.checkitems(c[7::2], a[7::2]) - self.checkitems(c[-1::2], a[-1::2]) - self.checkitems(c[-2::2], a[-2::2]) - self.checkitems(c[::-2], a[::-2]) - self.checkitems(c[0::-2], a[0::-2]) - self.checkitems(c[3::-2], a[3::-2]) - self.checkitems(c[6::-2], a[6::-2]) - self.checkitems(c[6:6:-2], a[6:6:-2]) - self.checkitems(c[7::-2], a[7::-2]) - self.checkitems(c[-1::-2], a[-1::-2]) - self.checkitems(c[-2::-2], a[-2::-2]) - self.checkitems(a[::-2], a[:-8:-2]) - - a = np.arange(24).reshape(6, 4) - self.assertEqual((6, 4), a[:].shape) - self.assertEqual((6, 4), a[:,].shape) - self.assertEqual((6, 3), a[:, :3].shape) - self.assertEqual((0, 3), a[4:2, :3].shape) - self.assertEqual((6,), a[:, 3].shape) - self.assertEqual((0,), a[4:2, 3].shape) - - def testArange(self): - print "test arange" - dds = np.arange(12, dtype=np.float) - self.checkitems(range(12), dds) - dds = np.arange(2, 12, dtype=np.int) - self.checkitems(range(2, 12), dds) - dds = np.arange(2, 12, 3) - self.checkitems(range(2, 12, 3), dds) - dds = np.arange(12, 2, -3) - self.checkitems(range(12, 2, -3), dds) - - def testSpace(self): - print "test space" - dds = np.linspace(0, 11, 12) - self.checkitems(range(12), dds) - dds = np.linspace(0, 11, 12, dtype=np.int32) - self.checkitems(range(12), dds, toInt) - dds = np.linspace(0, 5, 11) - self.checkitems([0.5 * i for i in range(11)], dds) - dds = np.linspace(0, 5, 11, dtype=np.int32) - self.checkitems([i / 2 for i in range(11)], dds, toInt) - - import math - - dds = np.logspace(0, 2, 5) - self.checkitems([math.pow(10.0, 0.5 * i) for i in range(5)], dds) - dds = np.logspace(0, 2, 5, dtype=np.int32) - self.checkitems([1, 3, 10, 31, 100], dds, toInt) - - def checkitemsadd(self, la, lb, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i]) + convert(lb[i]), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals(convert(la[i][j]) + convert(lb[i][j]), ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k]) + convert(lb[i][j][k]), ds[i, j, k]) - - def checkitemsaddconst(self, la, c, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i]) + c, ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals(convert(la[i][j]) + c, ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k]) + c, ds[i, j, k]) - - def testAdd(self): - print "test add" - da = np.array(self.db, np.int) - dr = da + da - self.checkitemsadd(self.db, self.db, dr, convert=toInt) - dr = da.copy() - dr += da - self.checkitemsadd(self.db, self.db, dr, convert=toInt) - dr = da + 1.2 - self.checkitemsaddconst(self.db, 1.2, dr, convert=toInt) - dr = da.copy() - dr += 1 - self.checkitemsaddconst(self.db, toInt(1.2), dr, convert=toInt) - dr = da + da + 1.2 - self.checkitemsaddconst(self.db2, 1.2, dr, convert=toInt) - - da = np.array(self.db, np.float) - dr = da + da - self.checkitemsadd(self.db, self.db, dr) - dr = da.copy() - dr += da - self.checkitemsadd(self.db, self.db, dr) - dr = da + 1.2 - self.checkitemsaddconst(self.db, 1.2, dr) - dr = da.copy() - dr += 1.2 - self.checkitemsaddconst(self.db, 1.2, dr) - - za = np.array(self.zb, np.complex) - zr = za + za - self.checkitemsadd(self.zb, self.zb, zr) - zr = za.copy() - zr += za - self.checkitemsadd(self.zb, self.zb, zr) - zr = za + (1.3 + 0.2j) - self.checkitemsaddconst(self.zb, (1.3 + 0.2j), zr) - zr = za.copy() - zr += 1.3 + 0.2j - self.checkitemsaddconst(self.zb, (1.3 + 0.2j), zr) - - def checkitemssub(self, la, lb, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i]) - convert(lb[i]), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals(convert(la[i][j]) - convert(lb[i][j]), ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k]) - convert(lb[i][j][k]), ds[i, j, k]) - - def checkitemssubconst(self, la, c, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i]) - c, ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals(convert(la[i][j]) - c, ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k]) - c, ds[i, j, k]) - - def checkitemssubconst2(self, la, c, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertEquals(convert(la[i] - c), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertEquals( - convert(convert(la[i][j]) - c), - ds[i, j], - msg="%d, %d : %r %r" % (i, j, convert(convert(la[i][j]) - c), ds[i, j]), - ) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertEquals(convert(la[i][j][k] - c), ds[i, j, k]) - - def testSub(self): - print "test sub" - da = np.array(self.db, np.int) - dr = da - da - self.checkitemssub(self.db, self.db, dr, convert=toInt) - dr = da.copy() - dr -= da - self.checkitemssub(self.db, self.db, dr, convert=toInt) - dr = da - 1.2 - self.checkitemssubconst(self.db, 1.2, dr, convert=toInt) - # dr = da.copy() - # print 'Before', dr[1,0] - # dr -= 1.2 - # print 'After', dr[1,0] - # self.checkitemssubconst2(self.db, 1.2, dr, convert=toInt) - - da = np.array(self.db, np.float) - dr = da - da - self.checkitemssub(self.db, self.db, dr) - dr = da.copy() - dr -= da - self.checkitemssub(self.db, self.db, dr) - dr = da - 1.2 - self.checkitemssubconst(self.db, 1.2, dr) - dr = 1.2 - da - self.checkitemssubconst(self.db, 1.2, 0 - dr) - dr = da.copy() - dr -= 1.2 - self.checkitemssubconst2(self.db, 1.2, dr) - - za = np.array(self.zb, np.complex) - zr = za - za - self.checkitemssub(self.zb, self.zb, zr) - zr = za.copy() - zr -= za - self.checkitemssub(self.zb, self.zb, zr) - zr = za - (1.3 + 0.2j) - self.checkitemssubconst(self.zb, (1.3 + 0.2j), zr) - zr = za.copy() - zr -= 1.3 + 0.2j - self.checkitemssubconst2(self.zb, (1.3 + 0.2j), zr) - - def checkitemsdiv(self, la, lb, ds, convert=toAny, iscomplex=False): - if ds.ndim == 1: - for i in range(ds.shape[0]): - x = convert(la[i]) / convert(lb[i] + 1.0) - if iscomplex: - x = complex(x) - self.assertAlmostEquals(x, ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - x = convert(la[i][j]) / convert(lb[i][j] + 1.0) - z = ds[i, j] - if iscomplex: - x = complex(x) - self.assertAlmostEquals(x.real, z.real) - self.assertAlmostEquals(x.imag, z.imag) - else: - self.assertAlmostEquals(x, z) - - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertAlmostEquals(convert(la[i][j][k]) / convert(lb[i][j][k] + 1.0), ds[i, j, k]) - - def checkitemsdivconst(self, la, c, ds, convert=toAny, iscomplex=False): - if ds.ndim == 1: - for i in range(ds.shape[0]): - try: - iter(la) - d = convert(la[i]) - except: - d = la - try: - iter(c) - n = convert(c[i] + 1.0) - except: - n = c - self.assertAlmostEquals(d / n, ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - try: - iter(la) - d = convert(la[i][j]) - except: - d = la - try: - iter(c) - n = convert(c[i][j] + 1.0) - except: - n = c - x = d / n - z = ds[i, j] - if iscomplex: - x = complex(x) - self.assertAlmostEquals(x.real, z.real) - self.assertAlmostEquals(x.imag, z.imag) - else: - self.assertAlmostEquals(x, z, msg="%d, %d : %r %r" % (i, j, x, z)) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - try: - iter(la) - d = convert(la[i][j][k]) - except: - d = la - try: - iter(c) - n = convert(c[i][j][k] + 1.0) - except: - n = c - self.assertAlmostEquals(d / n, ds[i, j, k]) - - def checkitemsdivconst2(self, la, c, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - try: - iter(la) - d = convert(la[i]) - except: - d = la - try: - iter(c) - n = convert(c[i] + 1.0) - except: - n = c - self.assertEquals(d / n, ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - try: - iter(la) - d = convert(la[i][j]) - except: - d = la - try: - iter(c) - n = c[i][j] + 1.0 - except: - n = c - self.assertEquals(convert(d / n), ds[i, j], msg="%d, %d : %r %r" % (i, j, convert(d / n), ds[i, j])) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - try: - iter(la) - d = convert(la[i][j][k]) - except: - d = la - try: - iter(c) - n = convert(c[i][j][k] + 1.0) - except: - n = c - self.assertEquals(d / n, ds[i, j, k]) - - def testDiv(self): - print "test div" - da = np.array(self.db, np.int) - dr = da / (da + 1) - self.checkitemsdiv(self.db, self.db, dr, convert=toInt) - dr = da.copy() - dr /= da + 1 - self.checkitemsdiv(self.db, self.db, dr, convert=toInt) - dr = da / 1.2 - self.checkitemsdivconst(self.db, 1.2, dr, convert=toInt) - # dr = da.copy() - # dr /= 1.2 - # self.checkitemsdivconst2(self.db, 1.2, dr, convert=toInt) - - da = np.array(self.db, np.float) - dr = da / (da + 1) - self.checkitemsdiv(self.db, self.db, dr) - dr = da.copy() - dr /= da + 1 - self.checkitemsdiv(self.db, self.db, dr) - dr = da / 1.2 - self.checkitemsdivconst(self.db, 1.2, dr) - dr = 1.2 / (da + 1) - self.checkitemsdivconst(1.2, self.db, dr) - dr = da.copy() - dr /= 1.2 - self.checkitemsdivconst(self.db, 1.2, dr) - - za = np.array(self.zb, np.complex) - zr = za / (za + 1) - self.checkitemsdiv(self.zb, self.zb, zr, iscomplex=True) - zr = za.copy() - zr /= za + 1 - self.checkitemsdiv(self.zb, self.zb, zr, iscomplex=True) - zr = za / (1.3 + 0.2j) - self.checkitemsdivconst(self.zb, (1.3 + 0.2j), zr, iscomplex=True) - zr = za.copy() - zr /= 1.3 + 0.2j - self.checkitemsdivconst(self.zb, (1.3 + 0.2j), zr, iscomplex=True) - - a = np.arange(-4, 4, dtype=np.int8) - self.checkitems([-2, -2, -1, -1, 0, 0, 1, 1], a / 2) - self.checkitems([-2, -2, -1, -1, 0, 0, 1, 1], a // 2) - self.checkitems([0, 1, 0, 1, 0, 1, 0, 1], a % 2) - self.checkitems([2, 1, 1, 0, 0, -1, -1, -2], a / -2) - self.checkitems([2, 1, 1, 0, 0, -1, -1, -2], a // -2) - self.checkitems([0, -1, 0, -1, 0, -1, 0, -1], a % -2) - self.checkitems([-1.6, -1.2, -0.8, -0.4, 0.0, 0.4, 0.8, 1.2], a / 2.5) - self.checkitems([-2.0, -2.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0], a // 2.5) - self.checkitems([1.0, 2.0, 0.5, 1.5, 0.0, 1.0, 2.0, 0.5], a % 2.5) - self.checkitems([1.6, 1.2, 0.8, 0.4, -0.0, -0.4, -0.8, -1.2], a / -2.5) - self.checkitems([1.0, 1.0, 0.0, 0.0, -0.0, -1.0, -1.0, -2.0], a // -2.5) - self.checkitems([-1.5, -0.5, -2.0, -1.0, 0.0, -1.5, -0.5, -2.0], a % -2.5) - - def testCopyItems(self): - if isjava: - import jarray - - print "test copy items" - da = np.array(self.db, np.float) - jda = da._jdataset() - ta = np.zeros(da.shape) - jda.copyItemsFromAxes(None, None, ta._jdataset()) - print ta.data - ta = np.array(self.m) - jda.setItemsOnAxes(None, None, ta.data) - print da.data - - print "2" - da = np.array(self.db, np.float) - jda = da._jdataset() - ta = np.zeros((2,)) - axes = [True, False] - jda.copyItemsFromAxes(None, axes, ta._jdataset()) - print ta.data - ta = jarray.array([-2, -3.4], "d") - jda.setItemsOnAxes(None, axes, ta) - print da.data - - print "3" - da = np.array(self.db, np.float) - jda = da._jdataset() - ta = np.zeros((2,)) - axes = [False, True] - jda.copyItemsFromAxes(None, axes, ta._jdataset()) - print ta.data - ta = jarray.array([-2.5, -3.4], "d") - jda.setItemsOnAxes(None, axes, ta) - print da.data - - def testDatasetSlice(self): - print "test slicing with start and stop" - ds = np.arange(16) - dr = ds[2:10] - self.checkitems(range(2, 10), dr) - - print "test slicing with steps" - dr = ds[::2] - self.checkitems(range(0, 16, 2), dr) - - print "test slicing 3D" - dr = self.dda[1:, ::2, 1::2] - self.checkitems(self.t, dr) - - print "test putting in a 3D slice" - dr = self.dda.copy() - dr[:, 1::2, ::2] = 7.0 - self.checkitems(self.u, dr) - - print "test putting a list in a 3D slice" - dr = self.dda.copy() - print dr[:, 1::2, ::2].shape - dr[:, 1::2, ::2] = np.array([7.0, 7]).reshape(2, 1, 1) - self.checkitems(self.u, dr) - - print "test slicing with ellipse" - dr = ds[...] - self.checkitems(range(16), dr) - - print "test slicing 3D with ellipse" - dr = self.dda[..., 1::2] - self.checkitems(self.t, dr[1:, ::2]) - - print "test putting in a 3D slice with ellipsis" - dr = self.dda.copy() - dr[..., 1::2, ::2] = 7.0 - self.checkitems(self.u, dr) - - print "test putting a list in a 3D slice with ellipsis" - dr = self.dda.copy() - print dr[..., 1::2, ::2].shape - dr[..., 1::2, ::2] = np.array([7.0, 7]).reshape(2, 1, 1) - self.checkitems(self.u, dr) - - print "test slice shape reduction" - dr = np.arange(120).reshape(4, 3, 5, 2) - s = dr[:2, ..., 1:].shape - print s - self.assertEquals(s, (2, 3, 5, 1)) - s = dr[:2, ..., 1].shape - print s - self.assertEquals(s, (2, 3, 5)) - "" - s = dr[:2, :, :, 1].shape - print s - self.assertEquals(s, (2, 3, 5)) - - print "test newaxis" - dr = np.arange(15).reshape(3, 5) - s = dr[np.newaxis, :, 1:].shape - print s - self.assertEquals(s, (1, 3, 4)) - s = dr[np.newaxis, ...].shape - print s - self.assertEquals(s, (1, 3, 5)) - s = dr[np.newaxis, ..., np.newaxis].shape - print s - self.assertEquals(s, (1, 3, 5, 1)) - s = dr[..., np.newaxis].shape - print s - self.assertEquals(s, (3, 5, 1)) - s = dr[:, np.newaxis, ..., np.newaxis].shape - print s - self.assertEquals(s, (3, 1, 5, 1)) - - def testTake(self): - print "test take" - ds = np.arange(16) - self.checkitems([2, 5], ds.take([2, 5])) - self.checkitems([2, 5], ds.take(np.array([2, 5]))) - ds = np.arange(16).reshape(4, 4) - self.checkitems([2, 5], ds.take([2, 5])) - self.checkitems([[4, 5, 6, 7], [12, 13, 14, 15]], ds.take([1, 3], 0)) - self.checkitems([[1, 3], [5, 7], [9, 11], [13, 15]], ds.take([1, 3], 1)) - - def testPut(self): - print "test put" - ds = np.arange(6.0) - ds.put([2, 5], [-2, -5.5]) - self.checkitems([0, 1, -2, 3, 4, -5.5], ds) - ds.put(np.array([0, 4]), [-2, -5.5]) - self.checkitems([-2, 1, -2, 3, -5.5, -5.5], ds) - ds = np.arange(6.0).reshape(2, 3) - ds.put([2, 5], [-2, -5.5]) - self.checkitems([[0, 1, -2], [3, 4, -5.5]], ds) - ds.put(np.array([0, 4]), [-2, -5.5]) - self.checkitems([[-2, 1, -2], [3, -5.5, -5.5]], ds) - - def testArgs(self): - print "test arg maxs" - ds = np.array([[[1.0, 0.0, 3.0], [0.5, 2.5, 2.0]], [[0.0, 3.0, 1.0], [1.5, 2.5, 2.0]]]) - self.assertEquals(ds.argmax(), 2) - self.checkitems([[0, 1, 0], [1, 0, 0]], ds.argmax(0)) - self.checkitems([[0, 1, 0], [1, 0, 1]], ds.argmax(1)) - self.checkitems([[2, 1], [1, 1]], ds.argmax(2)) - - print "test arg mins" - self.assertEquals(ds.argmin(), 1) - self.checkitems([[1, 0, 1], [0, 0, 0]], ds.argmin(0)) - self.checkitems([[1, 0, 1], [0, 1, 0]], ds.argmin(1)) - self.checkitems([[1, 0], [0, 0]], ds.argmin(2)) - - def testProds(self): - print "test prod" - ds = np.arange(12).reshape(3, 4) - self.assertEquals(np.prod(ds), 0) - self.checkitems([0, 45, 120, 231], np.prod(ds, 0)) - self.checkitems([0, 840, 7920], np.prod(ds, 1)) - - print "test cumprod" - self.checkitems([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], np.cumprod(ds)) - self.checkitems([[0, 1, 2, 3], [0, 5, 12, 21], [0, 45, 120, 231]], np.cumprod(ds, 0)) - self.checkitems([[0, 0, 0, 0], [4, 20, 120, 840], [8, 72, 720, 7920]], np.cumprod(ds, 1)) - - def testSums(self): - print "test sum" - ds = np.arange(12).reshape((3, 4)) - self.assertEquals(np.sum(ds), 66) - self.checkitems([12, 15, 18, 21], np.sum(ds, 0)) - self.checkitems([6, 22, 38], np.sum(ds, 1)) - lds = np.arange(1024 * 1024, dtype=np.int32) - self.assertEquals(np.sum(lds, dtype=np.int32), -524288) - self.assertEquals(np.sum(lds, dtype=np.int64), 549755289600) - - print "test cumsum" - self.checkitems([0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66], np.cumsum(ds)) - self.checkitems([[0, 1, 2, 3], [4, 6, 8, 10], [12, 15, 18, 21]], np.cumsum(ds, 0)) - self.checkitems([[0, 1, 3, 6], [4, 9, 15, 22], [8, 17, 27, 38]], np.cumsum(ds, 1)) - - def testGDA2270(self): - print "test negative indices" - ds = np.arange(10.0) - self.assertEquals(8, ds[-2]) - ds[-2] = 0 - self.assertEquals(0, ds[8]) - ds = np.arange(10.0).reshape(2, 5) - du = np.arange(5.0, 10) - dt = ds[-1] - for i in range(du.shape[0]): - self.assertEquals(dt[i], du[i]) - ds[-1] = -2 - for i in range(ds.shape[1]): - self.assertEquals(ds[-1, i], -2) - - def testGDA2271(self): - print "test out of bound indices" - ds = np.arange(10.0) - - def getf(ind): - return ds[ind] - - def setf(ind, value): - ds[ind] = value - - self.assertRaises(IndexError, getf, 10) - self.assertRaises(IndexError, getf, -11) - - if isjava: - # ds.extendible = False - self.assertRaises(IndexError, setf, 11, 0.0) - self.assertRaises(IndexError, setf, -11, 0.0) - - # ds.extendible = True - # ds[10] = 0. - - def testBoolean(self): - print "test boolean get and set" - tm = np.array(self.mm) - c = tm > 11.6 - lc = [[[False, False], [False, True]], [[True, True], [True, True]]] - self.checkitems(lc, c) - - d = tm[c] - self.checkitems([12.0, 20.0, 30.0, 42.0, 56.0], d) - - d = tm[tm > 11.6] - self.checkitems([12.0, 20.0, 30.0, 42.0, 56.0], d) - - d = tm[np.logical_not(c)] - self.checkitems([0, 2.0, 6], d) - - ta = tm.copy() - ta[c] = -2.3 - self.checkitems([[[0.0, 2.0], [6.0, -2.3]], [[-2.3, -2.3], [-2.3, -2.3]]], ta) - - tm[tm > 11.6] = -2.3 - self.checkitems([[[0.0, 2.0], [6.0, -2.3]], [[-2.3, -2.3], [-2.3, -2.3]]], tm) - - tm[np.logical_not(c)] = -3.3 - self.checkitems([[[-3.3, -3.3], [-3.3, -2.3]], [[-2.3, -2.3], [-2.3, -2.3]]], tm) - - def testInteger(self): - print "test integer get and set" - tm = np.array(self.mm).flatten() - - d = tm[np.array([2, 4, 7])] - self.checkitems([6.0, 20.0, 56.0], d) - - tm[np.array([3, 4, 5, 6, 7])] = -2.3 - self.checkitems([0.0, 2.0, 6.0, -2.3, -2.3, -2.3, -2.3, -2.3], tm) - - def testIntegers(self): - print "test integers get and set" - a = np.array([8, 9, 10, 11, 12, 13]) - d = a[np.where(a > 10)] - self.checkitems([11, 12, 13], d) - - a.shape = 3, 2 - d = a[np.where(a > 10)] - self.checkitems([11, 12, 13], d) - - a[np.where(a > 10)] = -1 - self.checkitems([[8, 9], [10, -1], [-1, -1]], a) - - # fancy indexing - a = np.array([8, 9, 10, 11, 12, 13]) - a.shape = 3, 2 - self.assertEquals(13, a[(2, 1)]) - d = a[(2, 1),] - self.checkitems([[12, 13], [10, 11]], d) - - tm = np.array(self.mm) - d = tm[[1]] - self.checkitems([[[20.0, 30.0], [42.0, 56.0]]], d) - - d = tm[np.array([0, 1]), np.array([1, 1])] - self.checkitems([[6.0, 12.0], [42.0, 56.0]], d) - - d = tm[np.array([0, 1]), np.array([1, 1]), :1] - self.checkitems([[6.0], [42.0]], d) - - d = tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] - self.checkitems([[[6.0], [42.0]], [[42.0], [20.0]]], d) - - d = tm[[[0, 1], [1, 1]], [[1, 1], [1, 0]], :1] - self.checkitems([[[6.0], [42.0]], [[42.0], [20.0]]], d) - - tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] = -2.3 - self.checkitems([[[0.0, 2.0], [-2.3, 12.0]], [[-2.3, 30.0], [-2.3, 56.0]]], tm) - - # fancy with booleans too - tm = np.array(self.mm) - d = tm[np.array([0, 1]), np.array([False, True]), :1] - self.checkitems([[6.0], [42.0]], d) - - def testSelect(self): - print "test select" - tm = np.select( - [np.array([[[False, True], [True, False]], [[True, True], [False, False]]])], [np.array(self.mm)], -2.3 - ) - self.checkitems([[[-2.3, 2.0], [6.0, -2.3]], [[20.0, 30.0], [-2.3, -2.3]]], tm) - - def testWhere(self): - print "test where" - t = np.array([[[-0.5, 0.1], [2, 0]], [[1.3, 1.4], [-10, -20]]]) - tm = np.where(t > 0, np.array(self.mm), -2.3) - self.checkitems([[[-2.3, 2.0], [6.0, -2.3]], [[20.0, 30.0], [-2.3, -2.3]]], tm) - - def testPrint(self): - print "test print" - a = np.arange(10) - print type(a) - print a - - def testMean(self): - print "test mean" - a = np.arange(10).reshape(2, 5) - self.assertEqual(4.5, a.mean()) - self.checkitems([2.5, 3.5, 4.5, 5.5, 6.5], a.mean(0)) - self.checkitems([2.0, 7.0], a.mean(1)) - - def testMaxMin(self): - print "test max/min" - a = np.arange(10, dtype=np.float).reshape(2, 5) - self.assertEqual(0.0, a.min()) - self.checkitems([0.0, 1.0, 2.0, 3.0, 4.0], a.min(0)) - self.checkitems([0.0, 5.0], a.min(1)) - self.assertEqual(0, a.argmin()) - self.checkitems([0, 0, 0, 0, 0], a.argmin(0)) - self.checkitems([0, 0], a.argmin(1)) - if isjava: - self.checkitems([0.0, 1.0, 2.0, 3.0, 4.0], a.min(0, True)) - self.checkitems([0.0, 5.0], a.min(1, True)) - self.checkitems([0, 0, 0, 0, 0], a.argmin(0, True)) - self.checkitems([0, 0], a.argmin(1, True)) - - self.assertEqual(9.0, a.max()) - self.checkitems([5.0, 6.0, 7.0, 8.0, 9.0], a.max(0)) - self.checkitems([4.0, 9.0], a.max(1)) - self.assertEqual(9, a.argmax()) - self.checkitems([1, 1, 1, 1, 1], a.argmax(0)) - self.checkitems([4, 4], a.argmax(1)) - if isjava: - self.checkitems([5.0, 6.0, 7.0, 8.0, 9.0], a.max(0, True)) - self.checkitems([4.0, 9.0], a.max(1, True)) - self.checkitems([1, 1, 1, 1, 1], a.argmax(0, True)) - self.checkitems([4, 4], a.argmax(1, True)) - - def testIterate(self): - print "test iterate" - l = [v for v in np.arange(3)] - self.assertEqual([0, 1, 2], l) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test)) - return suite - - -if __name__ == "__main__": - __import__("__main__").Test = Test - unittest.TextTestRunner(verbosity=2).run(suite()) \ No newline at end of file diff --git a/script/scitest/external_test.py b/script/scitest/external_test.py deleted file mode 100644 index 8ec4687..0000000 --- a/script/scitest/external_test.py +++ /dev/null @@ -1,184 +0,0 @@ -### -# Copyright 2013 Diamond Light Source Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -### - -""" -Test random class -import unittest -unittest.TestProgram(argv=["external_test"]) -""" -import unittest - -from scisoftpy.external import save_args, load_args, pyenv -import scisoftpy as dnp - -import shutil - - -class Test(unittest.TestCase): - from os import path - - epath_list = [get_context().setup.expand("{script}/scitest")] ###[path.dirname(__file__)] - - def checkArgs(self, arg): - d = save_args(arg) - a = load_args(d) - shutil.rmtree(d) - self.assertTrue(self.equals(arg, a), "%s != %s" % (arg, a)) - - def equals(self, a, b): - if type(a) != type(b): - return False - if isinstance(a, dict): - if a.keys() != b.keys(): - return False - for k in a: - if not self.equals(a[k], b[k]): - return False - elif isinstance(a, (list, tuple)): - if len(a) != len(b): - return False - for ia, ib in zip(a, b): - if not self.equals(ia, ib): - return False - elif isinstance(a, dnp.ndarray): - return dnp.all(a == b) - else: - return a == b - return True - - def testSaveArgs(self): - self.checkArgs(None) - self.checkArgs(([None, None], [None])) - self.checkArgs(([None, 1, 1.5], [None])) - self.checkArgs(([None, 1, 1.5], (None,))) - self.checkArgs(([None, 1, 1.5], dnp.arange(12))) - self.checkArgs(([None, 1, 1.5], {"blah": dnp.arange(12), "foo": dnp.ones((3, 4)), "boo": -2.345})) - - def testSubprocess(self): - import subprocess as sub - - pyexe, pypath, _pyldpath = pyenv() - import os - - env = dict(os.environ) - env["PYTHONPATH"] = ":".join(pypath) - from pprint import pprint - - pprint(pypath) - # p = sub.Popen('echo $PYTHONPATH', shell=True, env=_env, stdin=sub.PIPE, stdout=sub.PIPE, stderr=sub.PIPE) - # print p.communicate() - p = sub.Popen([pyexe], env=env, shell=False, stdin=sub.PIPE, stdout=sub.PIPE, stderr=sub.PIPE) - p.stdin.write('print "Hello World"\n') - p.stdin.write('print "Hello World2"\n') - p.stdin.close() - l = p.stdout.read() - print l - - def testDLSmodule(self): - print dnp.external.get_dls_module() - - def testPythonInstallation(self): - print dnp.external.get_python() - - def testExternal(self): - from external_functions import fun, funadd, fundec # , funexception - - a = fun() - efun = dnp.external.create_function(fun, dls_module=True) - print a, self.equals(efun(), a) - efun = dnp.external.create_function("fun", "external_functions", dls_module=True) - print a, self.equals(efun(), a) - - a = funadd(dnp.arange(3.0), 1.5) - efun = dnp.external.create_function(funadd, dls_module=True) - print a, self.equals(efun(dnp.arange(3.0), 1.5), a) - efun = dnp.external.create_function("funadd", "external_functions", dls_module=True) - print a, self.equals(efun(dnp.arange(3.0), 1.5), a) - - a = fundec(dnp.arange(3.0)) - efun = dnp.external.create_function(fundec, dls_module=True) - print a, self.equals(efun(dnp.arange(3.0)), a) - efun = dnp.external.create_function("fundec", "external_functions", dls_module=True) - print a, self.equals(efun(dnp.arange(3.0)), a) - - a = fundec(dnp.arange(3.0), b=2.5) - efun = dnp.external.create_function(fundec, dls_module=True) - print a, self.equals(efun(dnp.arange(3.0), b=2.5), a) - efun = dnp.external.create_function("fundec", "external_functions", dls_module=True) - print a, self.equals(efun(dnp.arange(3.0), b=2.5), a) - - def testException(self): - # from external_functions import funexception - # funexception() - efunexception = dnp.external.create_function( - "funexception", "external_functions", extra_path=Test.epath_list, dls_module=True - ) - self.assertRaises(ValueError, efunexception) - - # efunexception() - - def testSciPy(self): - efun = dnp.external.create_function( - "funscipy", "external_functions", extra_path=Test.epath_list, dls_module="scipy/0.10.0" - ) - print "0.10.0", - self.assertEquals(efun(), "0.10.0") - print "passed" - - def testArrayScalar(self): - efun = dnp.external.create_function( - "funarrayscalar", "external_functions", extra_path=Test.epath_list, dls_module=True - ) - a = 2 + 3j, 1.0, 123, True - print a, - self.assertEquals(efun(), a) - print "passed" - - def testSpeed(self): - efun = dnp.external.create_function( - "fun", "external_functions", extra_path=Test.epath_list, dls_module=True, keep=False - ) - import time - - t = time.clock() - for _i in xrange(10): - efun() - print time.clock() - t - efun = dnp.external.create_function( - "fun", "external_functions", extra_path=Test.epath_list, dls_module=True, keep=True - ) - t = time.clock() - for _i in xrange(10): - efun() - print time.clock() - t - - def testHello(self): - py = dnp.external.PythonSubProcess("python", None) - print py.communicate('print "Hello World!"\n') - print py.communicate('print "Hello World2!"\n') - print py.communicate("for i in range(4): print i\n") - py.stop() - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test)) - return suite - - -if __name__ == "__main__": - __import__("__main__").Test = Test - unittest.TextTestRunner(verbosity=2).run(suite()) \ No newline at end of file diff --git a/script/scitest/io_test.py b/script/scitest/io_test.py deleted file mode 100644 index 6ad1c81..0000000 --- a/script/scitest/io_test.py +++ /dev/null @@ -1,176 +0,0 @@ -### -# Copyright 2011 Diamond Light Source Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -### - -""" -Test random class -import unittest -unittest.TestProgram(argv=["io_test"]) -""" -import unittest - -import scisoftpy as dnp - -TestFolder = "../../../uk.ac.diamond.scisoft.analysis/testfiles/images/" -IOTestFolder = TestFolder + "../gda/analysis/io/" -OutTestFolder = TestFolder + "../../test-scratch/" - -import os - -isjava = os.name == "java" - - -class Test(unittest.TestCase): - def load(self, name, testfolder=TestFolder): - path = testfolder + name - im = dnp.io.load(path) - print type(im[0]), im[0].shape - return im - - def colourload(self, name, testfolder=TestFolder): - path = testfolder + name - im = dnp.io.load(path, ascolour=True) - print type(im[0]), im[0].shape - return im[0] - - def testLoading(self): - import os - - print os.getcwd() - self.load("test.png") - self.load("testrgb.png") - - self.colourload("test.png") - im = self.colourload("testrgb.png") - print "slicing RGB: ", type(im[:5, 2]) - - self.load("test.jpg") - self.load("testlossy85.jpg") - self.load("testrgb.jpg") - self.load("testrgblossy85.jpg") - - self.colourload("test.jpg") - self.colourload("testrgb.jpg") - self.colourload("testrgblossy85.jpg") - - self.load("test.tiff") - self.load("test-df.tiff") - self.load("test-pb.tiff") - self.load("testrgb.tiff") - self.load("testrgb-df.tiff") - self.load("testrgb-lzw.tiff") - self.load("testrgb-pb.tiff") - try: - self.load("test-trunc.tiff") - except IOError, e: - print "Expected IO error caught:", e - except: - import sys - - print "Unexpected exception caught", sys.exc_info() - - self.colourload("testrgb.tiff") - self.colourload("testrgb-df.tiff") - self.colourload("testrgb-lzw.tiff") - self.colourload("testrgb-pb.tiff") - return True - - def testLoadingSRS(self): - dh = self.load("96356.dat", IOTestFolder + "SRSLoaderTest/") - print dh.eta - - def testLoadingNXS(self): - if isjava: - f = IOTestFolder + "NexusLoaderTest/" - nm = dnp.io.loadnexus(f + "FeKedge_1_15.nxs") - print 'There are %d datasets called "Energy"' % len(nm["Energy"]) - - def testLoadingHDF(self): - f = IOTestFolder + "NexusLoaderTest/" - t = dnp.io.load(f + "FeKedge_1_15.nxs", formats=["hdf5"]) - self.checkTree(t) - t = dnp.io.load(f + "FeKedge_1_15.nxs") - self.checkTree(t) - - def checkTree(self, t): - print t - g = t["entry1/instrument/FFI0"] - h = g["/entry1/instrument/FFI0"] - self.assertEquals(g, h, "relative and absolute do not match!") - ga = g[".."] - assert ga is t["entry1/instrument"], "parent is wrong!" - assert g["."] is g, "self is wrong!" - print t["entry1/instrument/FFI0/../../"] - print t["entry1/counterTimer01"].keys() - l = t.getnodes("Energy") - print "List of energy datasets is:", len(l) - assert len(l) is 1, "Number of energy datasets should be 1" - d = l[0] - print type(d) - assert d.shape == (489,), "Wrong shape" - dd = d[...] - assert dd.shape == (489,), "Wrong shape" - print type(d), type(dd) - - def save(self, name, data, testfolder=OutTestFolder): - path = testfolder + name - dnp.io.save(path, data) - - def testSaving(self): - d = dnp.arange(100).reshape(10, 10) % 3 - self.save("chequered.png", d) - im = self.load("chequered.png", testfolder=OutTestFolder) - im = self.load("test.png") - self.save("grey.png", im) - im2 = self.load("grey.png", testfolder=OutTestFolder) - - def testSavingOthers(self): - im = self.colourload("testrgb.png") - self.save("colour.png", im) - im2 = self.colourload("colour.png", testfolder=OutTestFolder) - - def testSavingBits(self): - d = dnp.arange(12 * 32).reshape((12, 32)) - b = dnp.abs(dnp.array(d, dnp.int8)) - b[b < 0] = 0 - print b.min(), b.max() - dnp.io.save(OutTestFolder + "uint.tiff", d, bits=32, signed=False) - dnp.io.save(OutTestFolder + "ushort.tiff", d, bits=16, signed=False) - dnp.io.save(OutTestFolder + "ubyte.tiff", b, bits=8, signed=False) - dnp.io.save(OutTestFolder + "int.tiff", d, bits=32) - dnp.io.save(OutTestFolder + "short.tiff", d, bits=16) - dnp.io.save(OutTestFolder + "byte.tiff", dnp.array(d, dnp.int8), bits=8) - dnp.io.save(OutTestFolder + "double.tiff", d, bits=33) - dnp.io.save(OutTestFolder + "float.tiff", d, bits=33) - dnp.io.save(OutTestFolder + "short.png", d, bits=16) - dnp.io.save(OutTestFolder + "byte.png", b, bits=8) - - def testB16data(self): - d = dnp.io.load(IOTestFolder + "SRSLoaderTest/34146.dat", formats=["srs"]) - print d.keys() - print d.metadata.keys() - print d.metadata.values() - - def testNulldata(self): - try: - d = self.load("null.dat") - except Exception, e: - print "Expected exception caught:", e - - -if __name__ == "__main__": - # import sys;sys.argv = ['', 'Test.testName'] - __import__("__main__").Test = Test - unittest.main() \ No newline at end of file diff --git a/script/scitest/linalg_test.py b/script/scitest/linalg_test.py deleted file mode 100644 index 5b86e13..0000000 --- a/script/scitest/linalg_test.py +++ /dev/null @@ -1,89 +0,0 @@ -import unittest - -import scisoftpy as np -import scisoftpy.linalg as la - - -def toInt(o): - return int(o) - - -def toAny(o): - return o - - -class Test(unittest.TestCase): - def checkitems(self, la, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertAlmostEquals(convert(la[i]), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertAlmostEquals(convert(la[i][j]), ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertAlmostEquals(convert(la[i][j][k]), ds[i, j, k]) - - def testDot(self): - a = np.arange(1, 9).reshape(2, 2, 2) - b = np.array([-2, -3, 5, 7]).reshape(2, 2) - self.checkitems([35, 63], np.tensordot(a, b)) - self.checkitems([63, 70], np.tensordot(b, a)) - a = np.arange(60.0).reshape(3, 4, 5) - b = np.arange(24.0).reshape(4, 3, 2) - self.checkitems( - [[4400.0, 4730.0], [4532.0, 4874.0], [4664.0, 5018.0], [4796.0, 5162.0], [4928.0, 5306.0]], - np.tensordot(a, b, axes=([1, 0], [0, 1])), - ) - - def testPower(self): - a = np.array([[0, 1], [-1, 0]]) # matrix equiv. of the imaginary unit - self.checkitems([[0, -1], [1, 0]], np.linalg.matrix_power(a, 3)) - self.checkitems([[1, 0], [0, 1]], np.linalg.matrix_power(a, 0)) - self.checkitems([[0, 1], [-1, 0]], np.linalg.matrix_power(a, -3)) - - def testNorm(self): - a = np.arange(9) - 4 - b = a.reshape((3, 3)) - self.assertAlmostEqual(7.745966692414834, la.norm(a)) - self.assertAlmostEqual(7.745966692414834, la.norm(b)) - self.assertAlmostEqual(7.745966692414834, la.norm(b, "fro")) - self.assertAlmostEqual(4, la.norm(a, np.inf)) - self.assertAlmostEqual(9, la.norm(b, np.inf)) - self.assertAlmostEqual(0, la.norm(a, -np.inf)) - self.assertAlmostEqual(2, la.norm(b, -np.inf)) - self.assertAlmostEqual(20, la.norm(a, 1)) - self.assertAlmostEqual(7, la.norm(b, 1)) - self.assertAlmostEqual(-4.6566128774142013e-010, la.norm(a, -1)) - self.assertAlmostEqual(6, la.norm(b, -1)) - self.assertAlmostEqual(7.745966692414834, la.norm(a, 2)) - self.assertAlmostEqual(7.3484692283495345, la.norm(b, 2)) - # self.assertTrue(np.isnan(la.norm(a, -2))) - self.assertAlmostEqual(1.8570331885190563e-016, la.norm(b, -2)) - self.assertAlmostEqual(5.8480354764257312, la.norm(a, 3)) - - # self.assertTrue(np.isnan(la.norm(a, -3))) - - def testCond(self): - a = np.array([[1, 0, -1], [0, 1, 0], [1, 0, 1]]) - self.assertAlmostEqual(1.4142135623730951, np.linalg.cond(a)) - self.assertAlmostEqual(3.1622776601683795, np.linalg.cond(a, "fro")) - self.assertAlmostEqual(2.0, np.linalg.cond(a, np.inf)) - self.assertAlmostEqual(1.0, np.linalg.cond(a, -np.inf)) - self.assertAlmostEqual(2.0, np.linalg.cond(a, 1)) - self.assertAlmostEqual(1.0, np.linalg.cond(a, -1)) - self.assertAlmostEqual(1.4142135623730951, np.linalg.cond(a, 2)) - self.assertAlmostEqual(0.70710678118654746, np.linalg.cond(a, -2)) - - def testDet(self): - a = np.array([[1, 2], [3, 4]]) - self.assertAlmostEqual(-2.0, np.linalg.det(a)) - - -if __name__ == "__main__": - # import sys;sys.argv = ['', 'Test.testName'] - __import__("__main__").Test = Test - unittest.main() \ No newline at end of file diff --git a/script/scitest/random_test.py b/script/scitest/random_test.py deleted file mode 100644 index 378feaa..0000000 --- a/script/scitest/random_test.py +++ /dev/null @@ -1,73 +0,0 @@ -### -# Copyright 2011 Diamond Light Source Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -### - -""" -Test random class -import unittest -unittest.TestProgram(argv=["random_test"]) -""" -import unittest - -import scisoftpy as np -import scisoftpy.random as rnd - - -class Test(unittest.TestCase): - def testRandom(self): - import os - - if os.name == "java": - import jarray - - ja = jarray.array([1, 2], "i") - else: - ja = np.array([1, 2]) - print np.asIterable(ja) - - print rnd.rand() - print rnd.rand(1) - print rnd.rand(2, 4) - print rnd.randn() - print rnd.randn(1) - print rnd.randn(2, 4) - for i in range(10): - print i, rnd.randint(1) - print rnd.randint(2) - print rnd.randint(5, size=[2, 4]) - print rnd.random_integers(1) - print rnd.random_integers(5, size=[2, 4]) - print rnd.exponential(1.1) - print rnd.exponential(1.1, [2, 4]) - print rnd.poisson(1.1) - print rnd.poisson(1.1, [2, 4]) - a = np.ones([2, 3]) - print rnd.poisson(1.2, a.shape) - rnd.seed() - print rnd.rand(2, 3) - rnd.seed() - print rnd.rand(2, 3) - rnd.seed(12343) - print rnd.rand(2, 3) - rnd.seed(12343) - print rnd.rand(2, 3) - a = rnd.rand(200, 300) - print a.mean(), a.std() - - -if __name__ == "__main__": - # import sys;sys.argv = ['', 'Test.testName'] - __import__("__main__").Test = Test - unittest.main() \ No newline at end of file diff --git a/script/scitest/scisoft_test.py b/script/scitest/scisoft_test.py deleted file mode 100644 index c92995b..0000000 --- a/script/scitest/scisoft_test.py +++ /dev/null @@ -1,593 +0,0 @@ -### -# Copyright 2011 Diamond Light Source Ltd. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -### - -""" -Test rudimentary aspects of scisoft package - -import unittest -unittest.TestProgram(argv=["scisoft_test"]) -""" -import unittest -import scisoftpy as np - -import os - -isjava = os.name == "java" - - -def toInt(o): - return int(o) - - -def toAny(o): - return o - - -class Test(unittest.TestCase): - def setUp(self): - pass - - # import pydevd - # pydevd.settrace(stdoutToServer=True, stderrToServer=True) - - def checkitems(self, la, ds, convert=toAny): - if ds.ndim == 1: - for i in range(ds.shape[0]): - self.assertAlmostEquals(convert(la[i]), ds[i]) - elif ds.ndim == 2: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - self.assertAlmostEquals(convert(la[i][j]), ds[i, j]) - elif ds.ndim == 3: - for i in range(ds.shape[0]): - for j in range(ds.shape[1]): - for k in range(ds.shape[2]): - self.assertAlmostEquals(convert(la[i][j][k]), ds[i, j, k]) - - def testStrAndRepr(self): - print "String and repr testing" - a = np.arange(6, dtype=np.int32) - print str(a), repr(a) - a = np.arange(6, dtype=np.float) - print str(a), repr(a) - a = np.array([4, 3.0]) - print str(a), repr(a) - - def testMethods(self): - print "Methods testing" - print np.arange(6, dtype=np.int32) - print np.arange(6, dtype=np.float) - a = np.array([4, 3.0]) - print type(a) - print np.sort(a, None) - print a.sort() - a = np.arange(6, dtype=np.float) - print a.take([0, 2, 4]) - print a.take([0, 2, 4], 0) - d = np.take(a, [0, 2, 4], 0) - print type(d), d - d = np.diag([0, 2, 3]) - print type(d), d - a.shape = 2, 3 - self.checkitems([1, 2], a.take([1, 2])) - self.checkitems([[1, 2], [4, 5]], a.take([1, 2], 1)) - self.checkitems([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], a.repeat(2)) - self.checkitems([[0, 1, 2], [0, 1, 2], [3, 4, 5], [3, 4, 5]], a.repeat(2, axis=0)) - self.checkitems([[0, 0, 1, 1, 2, 2], [3, 3, 4, 4, 5, 5]], a.repeat(2, axis=1)) - - # print a.sort() - - def testScisoft(self): - a = np.ones([3, 4]) - print a.shape - a.shape = [2, 6] - print a.shape - a.shape = 12 - print a.shape - a.shape = (2, 6) - print a.shape - print a - print a * 2 - b = np.arange(12) - print b - print b[0] - b[2] = 2.3 - print b[1:8:3] - b[6:2:-1] = -2.1 - b.shape = [2, 6] - print b + 2 - print 2 + b - b += 2 - print b[1, 3] - b[0, 5] = -2.3 - print b[0, 2:5] - b[:, 1] = -7.1 - print b - try: - c = np.add(a, b) - print c - except: - print "Failed with an IAE as expected" - - def testReshape(self): - print "Reshape testing" - a = np.arange(10.0) - a.reshape(2, 5) - a.reshape((2, 5)) - - def testResize(self): - print "Resize testing" - a = np.arange(10.0) - a.resize(12, refcheck=False) - self.checkitems([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0], a) - print a - a = np.arange(10.0) - a.resize((2, 6), refcheck=False) - self.checkitems([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 0, 0]], a) - print a - a = np.arange(10.0) - a.resize(2, 6, refcheck=False) - self.checkitems([[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 0, 0]], a) - print a - a = np.arange(6.0) - self.checkitems([0, 1, 2, 3, 4], np.resize(a, 5)) - self.checkitems([0, 1, 2, 3, 4, 5, 0, 1, 2], np.resize(a, 9)) - self.checkitems([[0, 1, 2], [3, 4, 5]], np.resize(a, (2, 3))) - self.checkitems([[0, 1, 2], [3, 4, 5], [0, 1, 2]], np.resize(a, (3, 3))) - - def testCompounds(self): - a = np.arange(24).reshape(3, 4, 2) - oa = np.compoundarray(a) - ca = oa.copy() - self.assertEquals(ca.shape[0], 3) - self.assertEquals(ca.shape[1], 4) - la = ca[1, 2] - print la - self.assertEquals(la[0], 12) - self.assertEquals(la[1], 13) - ca[2, 2] = (0, 0) - self.assertEquals(ca[2, 2][0], 0) - self.assertEquals(ca[2, 2][1], 0) - ca[2, 2] = (-2, 1) - self.assertEquals(ca[2, 2][0], -2) - self.assertEquals(ca[2, 2][1], 1) - ca[1:, 3:] = (-1, -1) - self.assertEquals(ca[2, 3][0], -1) - self.assertEquals(ca[2, 3][1], -1) - ca[1:, 3:] = (3, -4) - self.assertEquals(ca[2, 3][0], 3) - self.assertEquals(ca[2, 3][1], -4) - if isjava: - pass - """ ### - ia = np.array([2, -7]) - print "Integer index testing" - ca = oa.copy() - cb = ca[ia] - print cb - - self.assertEquals(cb.shape[0], 2) - self.assertEquals(cb[0][0], 4) - self.assertEquals(cb[0][1], 5) - self.assertEquals(cb[1][0], 10) - self.assertEquals(cb[1][1], 11) - ca[ia] = [1, 2] - self.assertEquals(ca[0, 2][0], 1) - self.assertEquals(ca[0, 2][1], 2) - self.assertEquals(ca[1, 1][0], 1) - self.assertEquals(ca[1, 1][1], 2) - """ - - print "Boolean index testing" - ba = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 1]], dtype=np.bool) - ca = oa.copy() - cc = ca[ba] - # index dataset does not work - # test boolean too - print cc - self.assertEquals(cc.shape[0], 4) - self.assertEquals(cc[0][0], 4) - self.assertEquals(cc[0][1], 5) - self.assertEquals(cc[1][0], 8) - self.assertEquals(cc[1][1], 9) - self.assertEquals(cc[2][0], 18) - self.assertEquals(cc[2][1], 19) - self.assertEquals(cc[3][0], 22) - self.assertEquals(cc[3][1], 23) - ca[ba] = (1, 2) - self.assertEquals(ca[0, 2][0], 1) - self.assertEquals(ca[0, 2][1], 2) - self.assertEquals(ca[1, 0][0], 1) - self.assertEquals(ca[1, 0][1], 2) - self.assertEquals(ca[2, 1][0], 1) - self.assertEquals(ca[2, 1][1], 2) - self.assertEquals(ca[2, 3][0], 1) - self.assertEquals(ca[2, 3][1], 2) - - def testBools(self): - b = np.array([False, True], dtype=np.bool) - self.assertEquals(b[0], False) - self.assertEquals(b[1], True) - - def testHelp(self): - import sys - - if sys.executable is None: - if len(sys.path) > 0: - sys.executable = sys.path[0] - else: - sys.executable = sys.__file__ # @UndefinedVariable - - help(np) - - def testCentroid(self): - print "Centroid testing" - a = np.arange(12.0) - x = np.arange(12.0) + 2 - ca = np.centroid(a) - self.assertEquals(ca[0], 539.0 / 66) - ca = np.centroid(a, x) - self.assertEquals(ca[0], 539.0 / 66 + 1.5) - a.shape = (3, 4) - ca = np.centroid(a) - self.assertEquals(ca[0], 131.0 / 66) - self.assertEquals(ca[1], 147.0 / 66) - x = np.arange(3.0) + 2 - y = np.arange(4.0) + 3 - ca = np.centroid(a, [x, y]) - self.assertEquals(ca[0], 131.0 / 66 + 1.5) - self.assertEquals(ca[1], 312.0 / 66) # 147./66 + 2.5) - - def testQuantile(self): - print "Quantile testing" - a = np.array([6.0, 47.0, 49.0, 15.0, 42.0, 41.0, 7.0, 39.0, 43.0, 40.0, 36.0, 21.0]) - ans = [19.5, 39.5, 42.25] - self.assertEquals(np.median(a), ans[1]) - iqr = np.iqr(a) - self.assertEquals(iqr, ans[2] - ans[0]) - qs = np.quantile(a, [0.25, 0.5, 0.75]) - self.checkitems(ans, np.array(qs)) - a.shape = (3, 4) - qs = np.quantile(a, [0.25, 0.5, 0.75], axis=1) - self.checkitems([12.75, 31.0, 32.25], qs[0]) - self.checkitems([31.0, 40.0, 38.0], qs[1]) - self.checkitems([47.5, 41.25, 40.75], qs[2]) - iqr = np.iqr(a, axis=1) - print type(iqr) - self.assertEquals(-12.75 + 47.5, iqr[0]) - self.assertEquals(-31.0 + 41.25, iqr[1]) - self.assertEquals(-32.25 + 40.75, iqr[2]) - - def testGradient(self): - print "Gradient testing" - z = np.arange(200.0) - dz = np.gradient(z) - self.assertEquals(1, len(dz.shape)) - self.assertEquals(200, dz.size) - self.assertEquals(1, dz[0]) - - x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float) - g = np.gradient(x) - self.checkitems([1.0, 1.5, 2.5, 3.5, 4.5, 5.0], g) - g = np.gradient(x, 2) - self.checkitems([0.5, 0.75, 1.25, 1.75, 2.25, 2.5], g) - a = np.arange(6, dtype=np.float) * 2 - g = np.gradient(x, a) - self.checkitems([0.5, 0.75, 1.25, 1.75, 2.25, 2.5], g) - - g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float)) - self.checkitems([[2.0, 2.0, -1.0], [2.0, 2.0, -1.0]], g[0]) - self.checkitems([[1.0, 2.5, 4.0], [1.0, 1.0, 1.0]], g[1]) - - g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 2) - self.checkitems([[1.0, 1.0, -0.5], [1.0, 1.0, -0.5]], g[0]) - self.checkitems([[0.5, 1.25, 2.0], [0.5, 0.5, 0.5]], g[1]) - - g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 2, 1) - self.checkitems([[1.0, 1.0, -0.5], [1.0, 1.0, -0.5]], g[0]) - self.checkitems([[1.0, 2.5, 4.0], [1.0, 1.0, 1.0]], g[1]) - - g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 1, np.array([1.0, 2.0, 5.0])) - self.checkitems([[2.0, 2.0, -1.0], [2.0, 2.0, -1.0]], g[0]) - self.checkitems([[1.0, 1.25, 4.0 / 3], [1.0, 0.5, 1.0 / 3]], g[1]) - - # test slice views - x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float) - g = np.gradient(x[2:]) - self.checkitems([3, 3.5, 4.5, 5.0], g) - - def testAsfarray(self): - print "Float array testing" - self.assertEquals(np.float64, np.asfarray([1.0]).dtype, "") - self.assertEquals(np.float64, np.asfarray([1.0], dtype=np.int).dtype, "") - self.assertEquals(np.float64, np.asfarray([1]).dtype, "") - self.failUnlessRaises(TypeError, np.asfarray, [1 + 12j]) - - def testRoll(self): - print "Roll testing" - x = np.arange(10) - r = np.roll(x, 2) - self.checkitems([8, 9, 0, 1, 2, 3, 4, 5, 6, 7], r) - x.shape = (2, 5) - r = np.roll(x, 1) - self.checkitems([[9, 0, 1, 2, 3], [4, 5, 6, 7, 8]], r) - r = np.roll(x, 1, 0) - self.checkitems([[5, 6, 7, 8, 9], [0, 1, 2, 3, 4]], r) - r = np.roll(x, 1, 1) - self.checkitems([[4, 0, 1, 2, 3], [9, 5, 6, 7, 8]], r) - - def testItem(self): - print "Item testing" - a = np.array(10) - self.assertEquals(10, a.item()) - self.assertEquals(10, a.item(0)) - self.assertRaises(IndexError, a.item, 1) - self.assertRaises(ValueError, a.item, 1, 1) - a = np.array([10.0]) - self.assertEquals(10, a.item()) - self.assertEquals(10, a.item(0)) - self.assertRaises(IndexError, a.item, 1) - self.assertRaises(ValueError, a.item, 1, 1) - - a = np.arange(10.0) - self.assertEquals(4, a.item(4)) - self.assertRaises(IndexError, a.item, 11) - self.assertRaises(ValueError, a.item, 1, 1) - a.shape = (2, 5) - self.assertEquals(4, a.item(4)) - self.assertEquals(4, a.item(0, 4)) - self.assertRaises(IndexError, a.item, 11) - self.assertRaises(IndexError, a.item, 2, 1) - - def testZeroRank(self): - print "Zero rank arrays testing" - zi = np.array(1) - print zi - self.assertEquals(0, len(zi.shape)) - self.assertEquals(1, zi[()]) - self.assertEquals(np.array(1), zi[...]) - zi[()] = -3 - self.assertEquals(-3, zi[()]) - zf = np.array(1.0) - self.assertEquals(0, len(zf.shape)) - self.assertEquals(1.0, zf[()]) - self.assertEquals(np.array(1.0), zf[...]) - zf[()] = -3 - self.assertEquals(-3, zf[()]) - - def testUnpack(self): - print "Unpacking testing" - print tuple(np.arange(6)) - print tuple(np.arange(6).reshape(2, 3)) - print tuple(np.arange(6).reshape(3, 2)) - - def testIndices(self): - print "Indices testing" - x, y = np.indices((516, 516)) - - def testSlicing(self): - print "Slicing testing" - a = np.arange(60).reshape(2, 5, 3, 2) - self.assertEquals((5, 3, 2), a[-1].shape) - self.assertEquals((5, 3, 2), a[-1, :, :].shape) - self.assertEquals((5, 3, 2), a[-1, :, :, :].shape) - self.assertEquals((5, 2, 2), a[-1, :, 1:, :].shape) - self.assertEquals((5, 3, 2), a[-1, ...].shape) - self.assertEquals((2, 5, 3), a[..., -1].shape) - self.assertEquals((5, 3), a[1, ..., -1].shape) - self.assertEquals((1, 5, 3, 2), a[-1, np.newaxis].shape) - self.assertEquals((2, 1, 5, 3, 2), a[:, np.newaxis].shape) - self.assertEquals((2, 1, 3, 2), a[:, np.newaxis, -1].shape) - self.assertEquals((2, 5, 3, 1), a[..., -1, np.newaxis].shape) - self.assertEquals((2, 1, 5, 3), a[:, np.newaxis, ..., -1].shape) - self.assertEquals((2, 1, 5, 3, 1), a[:, np.newaxis, ..., np.newaxis, -1].shape) - self.assertEquals((2, 1, 5, 3, 1), a[:, np.newaxis, ..., -1, np.newaxis].shape) - - def testSlicedViews(self): - print "Sliced view testing" - a = np.arange(9).reshape(3, 3) - a[1][1] = -3 - self.assertEquals(-3, a[1, 1]) - self.assertEquals(-3, a[1][1]) - b = a[::2, 1:] - b[...] = 0 - self.assertEquals(0, a[0, 1]) - - a = np.arange(28).reshape(4, 7)[1:4, ::2] - try: - a.shape = 12 - self.fail("This should fail") - except: - pass - a.shape = 3, 2, 2 - - # broadcasted set slice - a[...] = np.array([-1, -2]) - self.checkitems([[-1, -2], [-1, -2]], a[1]) - - def testAppend(self): - print "Append testing" - a = np.array([]) - x = 1 - print np.append(a, x) - - def testTranspose(self): - print "Transpose testing" - a = np.arange(20).reshape(4, 5) - print a.T - - def testEquals(self): - print "Equality testing" - self.checkitems([False, True], np.array([2.0, 3]) == 3) - self.checkitems([True], np.array([2.0]) == 2) - self.checkitems([False], np.array([3.0]) == 2) - self.assertTrue(np.array(-2.0) == -2) - self.assertFalse(np.array(-2.0) == 2) - self.checkitems([False, True], np.array([2.0 - 3.5j, 3]) == 3) - self.assertTrue(np.array(-2.0 + 3.5j) == -2 + 3.5j) - self.assertFalse(np.array(-2.0) == -2 + 3.5j) - - def testIndexesAndPositions(self): - print "Indexes testing" - self.assertTrue(np.unravel_index(1621, (6, 7, 8, 9)) == (3, 1, 4, 1)) - l = np.unravel_index([22, 41, 37], (7, 6)) - self.checkitems([3, 6, 6], l[0]) - self.checkitems([4, 5, 1], l[1]) - - print "Positions testing" - self.assertTrue(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)) == 1621) - arr = np.array([[3, 6, 6], [4, 5, 1]]) - self.checkitems([22, 41, 37], np.ravel_multi_index(arr, (7, 6))) - self.checkitems([22, 23, 19], np.ravel_multi_index(arr, (4, 6), mode="clip")) - self.checkitems([12, 13, 13], np.ravel_multi_index(arr, (4, 4), mode=("clip", "wrap"))) - - def testRoots(self): - print "Roots testing" - rts = np.roots([1, 0, -1]) - rts.real.sort() - self.checkitems([-1, 1], rts.real) - self.checkitems([0, 0], rts.imag) - rts = np.roots([1, 2, 1]) - self.checkitems([-1, -1], rts.real) - self.checkitems([0, 0], rts.imag) - rts = np.roots([1, 0, 1]) - rts.real.sort() - self.checkitems([0, 0], rts.real) - self.checkitems([1, -1], rts.imag) - rts = np.roots([3.2, 2, 1]) - self.checkitems([-0.3125, -0.3125], rts.real) - self.checkitems([0.46351240544347894, -0.46351240544347894], rts.imag) - - def testBitwise(self): - print "Bitwise testing" - a = np.arange(-4, 4, dtype=np.int8) - b = np.arange(8, dtype=np.int8) - self.checkitems([0, 1, 2, 3, 0, 1, 2, 3], np.bitwise_and(a, b)) - self.checkitems([-4, -3, -2, -1, 4, 5, 6, 7], np.bitwise_or(a, b)) - self.checkitems([-4, -4, -4, -4, 4, 4, 4, 4], np.bitwise_xor(a, b)) - self.checkitems([3, 2, 1, 0, -1, -2, -3, -4], np.invert(a)) - self.checkitems([-1, -2, -3, -4, -5, -6, -7, -8], np.invert(b)) - self.checkitems([-4, -6, -8, -8, 0, 32, -128, -128], np.left_shift(a, b)) - self.checkitems([0, 0, 0, 0, 4, 10, 24, 56], np.left_shift(b, a)) - self.checkitems([0, 0, 0, 0, 0, 2, 8, 24], np.left_shift(a, a)) - self.checkitems([-4, -2, -1, -1, 0, 0, 0, 0], np.right_shift(a, b)) - self.checkitems([0, 0, 0, 0, 4, 2, 1, 0], np.right_shift(b, a)) - self.checkitems([-1, -1, -1, -1, 0, 0, 0, 0], np.right_shift(a, a)) - - def testDivmod(self): - print "Divmod testing" - a = np.arange(-4, 4, dtype=np.int8) - c = divmod(a, 2) - self.checkitems([-2, -2, -1, -1, 0, 0, 1, 1], c[0]) - self.checkitems([0, 1, 0, 1, 0, 1, 0, 1], c[1]) - c = divmod(a, 2.5) - self.checkitems([-2.0, -2.0, -1.0, -1.0, 0.0, 0.0, 0.0, 1.0], c[0]) - self.checkitems([1.0, 2.0, 0.5, 1.5, 0.0, 1.0, 2.0, 0.5], c[1]) - - def testModf(self): - print "Modf testing" - a = np.modf(2.5) - self.assertAlmostEqual(0.5, a[0], 5) - self.assertAlmostEqual(2.0, a[1], 5) - a = np.modf(-0.4) - self.assertAlmostEqual(-0.4, a[0], 5) - self.assertAlmostEqual(0.0, a[1], 5) - - def testRemainder(self): - print "Remainder testing" - a = np.remainder([4, 7], [2, 3]) - self.checkitems([0, 1], a) - self.checkitems([0, -2, 5, -1], np.remainder([4, 7, -3, -7], [2, -3, 8, -3])) - self.checkitems([0, 1, -3, -1], np.fmod([4, 7, -3, -7], [2, -3, 8, -3])) - self.checkitems([-1, 0, -1, 1, 0, 1], np.fmod([-3, -2, -1, 1, 2, 3], 2)) - self.checkitems([1, 0, 1, 1, 0, 1], np.mod([-3, -2, -1, 1, 2, 3], 2)) - - def testInterpolate(self): - print "Interpolate testing" - xp = [1, 2, 3] - fp = [3, 2, 0] - self.assertAlmostEqual(1.0, np.interp(2.5, xp, fp), 5) - self.checkitems([3.0, 3.0, 2.5, 0.56, 0.0], np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)) - UNDEF = -99.0 - self.assertAlmostEqual(UNDEF, np.interp(3.14, xp, fp, right=UNDEF), 5) - - def testAtleast(self): - print "Atleast testing" - self.checkitems([1.0], np.atleast_1d(1.0)) - self.checkitems([[1.0]], np.atleast_2d(1.0)) - self.checkitems([[[1.0]]], np.atleast_3d(1.0)) - - a = np.atleast_1d(1.0, [2, 3]) - self.checkitems([1.0], a[0]) - self.checkitems([2, 3], a[1]) - a = np.atleast_2d(np.arange(2)) - self.checkitems([[0, 1]], a) - a = np.atleast_3d(np.arange(2)) - self.checkitems([[[0], [1]]], a) - a = np.atleast_3d(np.arange(6).reshape(3, 2)) - self.checkitems([[[0], [1]], [[2], [3]], [[4], [5]]], a) - - def testStack(self): - print "Stack testing" - self.checkitems([1, 1, 1], np.hstack(np.ones(3))) - self.checkitems([[1], [1], [1]], np.vstack(np.ones(3))) - self.checkitems([[[1, 1, 1]]], np.dstack(np.ones(3))) - - a = np.array([1, 2, 3]) - b = np.array([2, 3, 4]) - self.checkitems([1, 2, 3, 2, 3, 4], np.hstack((a, b))) - self.checkitems([[1, 2], [2, 3], [3, 4]], np.hstack((a.reshape(3, 1), b.reshape(3, 1)))) - - self.checkitems([[1, 2, 3], [2, 3, 4]], np.vstack((a, b))) - self.checkitems([[1], [2], [3], [2], [3], [4]], np.vstack((a.reshape(3, 1), b.reshape(3, 1)))) - - self.checkitems([[[1, 2], [2, 3], [3, 4]]], np.dstack((a, b))) - self.checkitems([[[1, 2]], [[2, 3]], [[3, 4]]], np.dstack((a.reshape(3, 1), b.reshape(3, 1)))) - - def testMeshGrid(self): - print "Meshgrid testing" - x = np.arange(0, 6, 1) - y = np.arange(0, 4, 1) - xy = np.meshgrid(x, y) - self.checkitems([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]], xy[0]) - self.checkitems([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]], xy[1]) - - xy = np.meshgrid(x, y, indexing="ij") - self.checkitems([[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]], xy[1]) - self.checkitems([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5]], xy[0]) - - def testHistogram(self): - print "Histogram testing" - h, v = np.histogram([1, 2, 1], bins=[0, 1, 2, 3]) - self.checkitems([0, 2, 1], h) - self.checkitems([0, 1, 2, 3], v) - h, v = np.histogram([0, 1, 2, 1], bins=2) - self.checkitems([1, 3], h) - self.checkitems([0, 1, 2], v) - h, v = np.histogram(np.arange(4), bins=np.arange(5)) - self.checkitems([1, 1, 1, 1], h) - self.checkitems([0, 1, 2, 3, 4, 5], v) - - -def suite(): - suite = unittest.TestSuite() - suite.addTest(unittest.TestLoader().loadTestsFromTestCase(Test)) - return suite - - -if __name__ == "__main__": - __import__("__main__").Test = Test - unittest.TextTestRunner(verbosity=2).run(suite()) \ No newline at end of file