Startup
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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())
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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())
|
||||
Reference in New Issue
Block a user