diff options
author | wfspotz@sandia.gov <wfspotz@sandia.gov@localhost> | 2007-04-04 22:41:56 +0000 |
---|---|---|
committer | wfspotz@sandia.gov <wfspotz@sandia.gov@localhost> | 2007-04-04 22:41:56 +0000 |
commit | c070fab445d41ef9ca8ccb7a47619a908aa0dc07 (patch) | |
tree | cce13b01725520a6048a08bd10acb4bdf9142e4d /numpy | |
parent | 13b70a28a42c8eab3eefd18bbfcc2636a094924b (diff) | |
download | numpy-c070fab445d41ef9ca8ccb7a47619a908aa0dc07.tar.gz |
Added tests for 3D typemaps
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/doc/swig/Makefile | 4 | ||||
-rw-r--r-- | numpy/doc/swig/Tensor.cxx | 131 | ||||
-rw-r--r-- | numpy/doc/swig/Tensor.h | 52 | ||||
-rw-r--r-- | numpy/doc/swig/Tensor.i | 49 | ||||
-rwxr-xr-x | numpy/doc/swig/setup.py | 11 | ||||
-rwxr-xr-x | numpy/doc/swig/test3D.py | 795 |
6 files changed, 1038 insertions, 4 deletions
diff --git a/numpy/doc/swig/Makefile b/numpy/doc/swig/Makefile index a4a40040b..757f6421b 100644 --- a/numpy/doc/swig/Makefile +++ b/numpy/doc/swig/Makefile @@ -1,5 +1,5 @@ # SWIG -INTERFACES = Vector.i Matrix.i +INTERFACES = Vector.i Matrix.i Tensor.i WRAPPERS = $(INTERFACES:.i=_wrap.cxx) PROXIES = $(INTERFACES:.i=.py ) @@ -23,7 +23,7 @@ PDF_FILES = numpy_swig.pdf # List all of the subdirectories here for recursive make SUBDIRS = -all: $(WRAPPERS) Vector.cxx Vector.h Matrix.cxx Matrix.h +all: $(WRAPPERS) Vector.cxx Vector.h Matrix.cxx Matrix.h Tensor.cxx Tensor.h ./setup.py build test: all diff --git a/numpy/doc/swig/Tensor.cxx b/numpy/doc/swig/Tensor.cxx new file mode 100644 index 000000000..dce595291 --- /dev/null +++ b/numpy/doc/swig/Tensor.cxx @@ -0,0 +1,131 @@ +#include <stdlib.h> +#include <math.h> +#include <iostream> +#include "Tensor.h" + +// The following macro defines a family of functions that work with 3D +// arrays with the forms +// +// TYPE SNAMENorm( TYPE tensor[2][2][2]); +// TYPE SNAMEMax( TYPE * tensor, int rows, int cols, int num); +// TYPE SNAMEMin( int rows, int cols, int num, TYPE * tensor); +// void SNAMEScale( TYPE tensor[3][3][3]); +// void SNAMEFloor( TYPE * array, int rows, int cols, int num, TYPE floor); +// void SNAMECeil( int rows, int cols, int num, TYPE * array, TYPE ceil); +// void SNAMELUSplit(TYPE in[2][2][2], TYPE lower[2][2][2], TYPE upper[2][2][2]); +// +// for any specified type TYPE (for example: short, unsigned int, long +// long, etc.) with given short name SNAME (for example: short, uint, +// longLong, etc.). The macro is then expanded for the given +// TYPE/SNAME pairs. The resulting functions are for testing numpy +// interfaces, respectively, for: +// +// * 3D input arrays, hard-coded length +// * 3D input arrays +// * 3D input arrays, data last +// * 3D in-place arrays, hard-coded lengths +// * 3D in-place arrays +// * 3D in-place arrays, data last +// * 3D argout arrays, hard-coded length +// +#define TEST_FUNCS(TYPE, SNAME) \ +\ +TYPE SNAME ## Norm(TYPE tensor[2][2][2]) { \ + double result = 0; \ + for (int k=0; k<2; ++k) \ + for (int j=0; j<2; ++j) \ + for (int i=0; i<2; ++i) \ + result += tensor[i][j][k] * tensor[i][j][k]; \ + return (TYPE)sqrt(result/8); \ +} \ +\ +TYPE SNAME ## Max(TYPE * tensor, int rows, int cols, int num) { \ + int i, j, k, index; \ + TYPE result = tensor[0]; \ + for (k=0; k<num; ++k) { \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = k*rows*cols + j*rows + i; \ + if (tensor[index] > result) result = tensor[index]; \ + } \ + } \ + } \ + return result; \ +} \ +\ +TYPE SNAME ## Min(int rows, int cols, int num, TYPE * tensor) { \ + int i, j, k, index; \ + TYPE result = tensor[0]; \ + for (k=0; k<num; ++k) { \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = k*rows*cols + j*rows + i; \ + if (tensor[index] < result) result = tensor[index]; \ + } \ + } \ + } \ + return result; \ +} \ +\ +void SNAME ## Scale(TYPE array[3][3][3], TYPE val) { \ + for (int i=0; i<3; ++i) \ + for (int j=0; j<3; ++j) \ + for (int k=0; k<3; ++k) \ + array[i][j][k] *= val; \ +} \ +\ +void SNAME ## Floor(TYPE * array, int rows, int cols, int num, TYPE floor) { \ + int i, j, k, index; \ + for (k=0; k<num; ++k) { \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = k*cols*rows + j*rows + i; \ + if (array[index] < floor) array[index] = floor; \ + } \ + } \ + } \ +} \ +\ +void SNAME ## Ceil(int rows, int cols, int num, TYPE * array, TYPE ceil) { \ + int i, j, k, index; \ + for (k=0; k<num; ++k) { \ + for (j=0; j<cols; ++j) { \ + for (i=0; i<rows; ++i) { \ + index = j*rows + i; \ + if (array[index] > ceil) array[index] = ceil; \ + } \ + } \ + } \ +} \ +\ +void SNAME ## LUSplit(TYPE tensor[2][2][2], TYPE lower[2][2][2], \ + TYPE upper[2][2][2]) { \ + int sum; \ + for (int k=0; k<2; ++k) { \ + for (int j=0; j<2; ++j) { \ + for (int i=0; i<2; ++i) { \ + sum = i + j + k; \ + if (sum < 2) { \ + lower[i][j][k] = tensor[i][j][k]; \ + upper[i][j][k] = 0; \ + } else { \ + upper[i][j][k] = tensor[i][j][k]; \ + lower[i][j][k] = 0; \ + } \ + } \ + } \ + } \ +} + +TEST_FUNCS(signed char , schar ) +TEST_FUNCS(unsigned char , uchar ) +TEST_FUNCS(short , short ) +TEST_FUNCS(unsigned short , ushort ) +TEST_FUNCS(int , int ) +TEST_FUNCS(unsigned int , uint ) +TEST_FUNCS(long , long ) +TEST_FUNCS(unsigned long , ulong ) +TEST_FUNCS(long long , longLong ) +TEST_FUNCS(unsigned long long, ulongLong) +TEST_FUNCS(float , float ) +TEST_FUNCS(double , double ) diff --git a/numpy/doc/swig/Tensor.h b/numpy/doc/swig/Tensor.h new file mode 100644 index 000000000..d60eb2d2e --- /dev/null +++ b/numpy/doc/swig/Tensor.h @@ -0,0 +1,52 @@ +#ifndef TENSOR_H +#define TENSOR_H + +// The following macro defines the prototypes for a family of +// functions that work with 3D arrays with the forms +// +// TYPE SNAMENorm( TYPE tensor[2][2][2]); +// TYPE SNAMEMax( TYPE * tensor, int rows, int cols, int num); +// TYPE SNAMEMin( int rows, int cols, int num, TYPE * tensor); +// void SNAMEScale( TYPE array[3][3][3]); +// void SNAMEFloor( TYPE * array, int rows, int cols, int num, TYPE floor); +// void SNAMECeil( int rows, int cols, int num, TYPE * array, TYPE ceil ); +// void SNAMELUSplit(TYPE in[3][3][3], TYPE lower[3][3][3], TYPE upper[3][3][3]); +// +// for any specified type TYPE (for example: short, unsigned int, long +// long, etc.) with given short name SNAME (for example: short, uint, +// longLong, etc.). The macro is then expanded for the given +// TYPE/SNAME pairs. The resulting functions are for testing numpy +// interfaces, respectively, for: +// +// * 3D input arrays, hard-coded lengths +// * 3D input arrays +// * 3D input arrays, data last +// * 3D in-place arrays, hard-coded lengths +// * 3D in-place arrays +// * 3D in-place arrays, data last +// * 3D argout arrays, hard-coded length +// +#define TEST_FUNC_PROTOS(TYPE, SNAME) \ +\ +TYPE SNAME ## Norm( TYPE tensor[2][2][2]); \ +TYPE SNAME ## Max( TYPE * tensor, int rows, int cols, int num); \ +TYPE SNAME ## Min( int rows, int cols, int num, TYPE * tensor); \ +void SNAME ## Scale( TYPE array[3][3][3], TYPE val); \ +void SNAME ## Floor( TYPE * array, int rows, int cols, int num, TYPE floor); \ +void SNAME ## Ceil( int rows, int cols, int num, TYPE * array, TYPE ceil ); \ +void SNAME ## LUSplit(TYPE tensor[2][2][2], TYPE lower[2][2][2], TYPE upper[2][2][2]); + +TEST_FUNC_PROTOS(signed char , schar ) +TEST_FUNC_PROTOS(unsigned char , uchar ) +TEST_FUNC_PROTOS(short , short ) +TEST_FUNC_PROTOS(unsigned short , ushort ) +TEST_FUNC_PROTOS(int , int ) +TEST_FUNC_PROTOS(unsigned int , uint ) +TEST_FUNC_PROTOS(long , long ) +TEST_FUNC_PROTOS(unsigned long , ulong ) +TEST_FUNC_PROTOS(long long , longLong ) +TEST_FUNC_PROTOS(unsigned long long, ulongLong) +TEST_FUNC_PROTOS(float , float ) +TEST_FUNC_PROTOS(double , double ) + +#endif diff --git a/numpy/doc/swig/Tensor.i b/numpy/doc/swig/Tensor.i new file mode 100644 index 000000000..24c906d29 --- /dev/null +++ b/numpy/doc/swig/Tensor.i @@ -0,0 +1,49 @@ +// -*- c++ -*- +%module Tensor + +%{ +#define SWIG_FILE_WITH_INIT +#include "Tensor.h" +%} + +// Get the NumPy typemaps +%include "numpy.i" + +%init %{ + import_array(); +%} + +%define %apply_numpy_typemaps(TYPE) + +%apply (TYPE IN_ARRAY3[ANY][ANY][ANY]) {(TYPE tensor[ANY][ANY][ANY])}; +%apply (TYPE* IN_ARRAY3, int DIM1, int DIM2, int DIM3) + {(TYPE* tensor, int rows, int cols, int num)}; +%apply (int DIM1, int DIM2, int DIM3, TYPE* IN_ARRAY3) + {(int rows, int cols, int num, TYPE* tensor)}; + +%apply (TYPE INPLACE_ARRAY3[ANY][ANY][ANY]) {(TYPE array[3][3][3])}; +%apply (TYPE* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) + {(TYPE* array, int rows, int cols, int num)}; +%apply (int DIM1, int DIM2, int DIM3, TYPE* INPLACE_ARRAY3) + {(int rows, int cols, int num, TYPE* array)}; + +%apply (TYPE ARGOUT_ARRAY3[ANY][ANY][ANY]) {(TYPE lower[2][2][2])}; +%apply (TYPE ARGOUT_ARRAY3[ANY][ANY][ANY]) {(TYPE upper[2][2][2])}; + +%enddef /* %apply_numpy_typemaps() macro */ + +%apply_numpy_typemaps(signed char ) +%apply_numpy_typemaps(unsigned char ) +%apply_numpy_typemaps(short ) +%apply_numpy_typemaps(unsigned short ) +%apply_numpy_typemaps(int ) +%apply_numpy_typemaps(unsigned int ) +%apply_numpy_typemaps(long ) +%apply_numpy_typemaps(unsigned long ) +%apply_numpy_typemaps(long long ) +%apply_numpy_typemaps(unsigned long long) +%apply_numpy_typemaps(float ) +%apply_numpy_typemaps(double ) + +// Include the header file to be wrapped +%include "Tensor.h" diff --git a/numpy/doc/swig/setup.py b/numpy/doc/swig/setup.py index adce7672a..13bd7589e 100755 --- a/numpy/doc/swig/setup.py +++ b/numpy/doc/swig/setup.py @@ -27,10 +27,17 @@ _Matrix = Extension("_Matrix", include_dirs = [numpy_include], ) +# _Tensor extension module +_Tensor = Extension("_Tensor", + ["Tensor_wrap.cxx", + "tensor.cxx"], + include_dirs = [numpy_include], + ) + # NumyTypemapTests setup setup(name = "NumpyTypemapTests", description = "Functions that work on arrays", author = "Bill Spotz", - py_modules = ["Vector", "Matrix"], - ext_modules = [_Vector , _Matrix ] + py_modules = ["Vector", "Matrix", "Tensor"], + ext_modules = [_Vector , _Matrix , _Tensor ] ) diff --git a/numpy/doc/swig/test3D.py b/numpy/doc/swig/test3D.py new file mode 100755 index 000000000..ca1b90356 --- /dev/null +++ b/numpy/doc/swig/test3D.py @@ -0,0 +1,795 @@ +#! /usr/bin/env python + +# System imports +from distutils.util import get_platform +import os +import sys +import unittest + +# Import NumPy +import numpy as N +major, minor = [ int(d) for d in N.__version__.split(".")[:2] ] +if major == 0: BadListError = TypeError +else: BadListError = ValueError + +# Add the distutils-generated build directory to the python search path and then +# import the extension module +libDir = "lib.%s-%s" % (get_platform(), sys.version[:3]) +sys.path.insert(0,os.path.join("build", libDir)) +import Tensor + +###################################################################### + +class TensorTestCase(unittest.TestCase): + + #################################################### + ### Test functions that take arrays of type BYTE ### + def testScharNorm(self): + "Test scharNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.scharNorm(tensor), 1) + + def testScharMax(self): + "Test scharMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.scharMax(tensor), 8) + + def testScharMaxNonContainer(self): + "Test scharMax function with None" + self.assertRaises(TypeError, Tensor.scharMax, None) + + def testScharMaxWrongDim(self): + "Test scharMax function with a 1D array" + self.assertRaises(TypeError, Tensor.scharMax, [0, -1, 2, -3]) + + def testScharMin(self): + "Test scharMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.scharMin(tensor), 2) + + def testScharScale(self): + "Test scharScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'b') + Tensor.scharScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testScharFloor(self): + "Test scharFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'b') + Tensor.scharFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testScharCeil(self): + "Test scharCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'b') + Tensor.scharCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testScharLUSplit(self): + "Test scharLUSplit function" + lower, upper = Tensor.scharLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ##################################################### + ### Test functions that take arrays of type UBYTE ### + def testUcharNorm(self): + "Test ucharNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.ucharNorm(tensor), 1) + + def testUcharMax(self): + "Test ucharMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.ucharMax(tensor), 8) + + def testUcharMaxNonContainer(self): + "Test ucharMax function with None" + self.assertRaises(TypeError, Tensor.ucharMax, None) + + def testUcharMaxWrongDim(self): + "Test ucharMax function with a 1D array" + self.assertRaises(TypeError, Tensor.ucharMax, [0, -1, 2, -3]) + + def testUcharMin(self): + "Test ucharMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.ucharMin(tensor), 2) + + def testUcharScale(self): + "Test ucharScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'B') + Tensor.ucharScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testUcharFloor(self): + "Test ucharFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'B') + Tensor.ucharFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testUcharCeil(self): + "Test ucharCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'B') + Tensor.ucharCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testUcharLUSplit(self): + "Test ucharLUSplit function" + lower, upper = Tensor.ucharLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ##################################################### + ### Test functions that take arrays of type SHORT ### + def testShortNorm(self): + "Test shortNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.shortNorm(tensor), 1) + + def testShortMax(self): + "Test shortMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.shortMax(tensor), 8) + + def testShortMaxNonContainer(self): + "Test shortMax function with None" + self.assertRaises(TypeError, Tensor.shortMax, None) + + def testShortMaxWrongDim(self): + "Test shortMax function with a 1D array" + self.assertRaises(TypeError, Tensor.shortMax, [0, -1, 2, -3]) + + def testShortMin(self): + "Test shortMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.shortMin(tensor), 2) + + def testShortScale(self): + "Test shortScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'h') + Tensor.shortScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testShortFloor(self): + "Test shortFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'h') + Tensor.shortFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testShortCeil(self): + "Test shortCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'h') + Tensor.shortCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testShortLUSplit(self): + "Test shortLUSplit function" + lower, upper = Tensor.shortLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ###################################################### + ### Test functions that take arrays of type USHORT ### + def testUshortNorm(self): + "Test ushortNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.ushortNorm(tensor), 1) + + def testUshortMax(self): + "Test ushortMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.ushortMax(tensor), 8) + + def testUshortMaxNonContainer(self): + "Test ushortMax function with None" + self.assertRaises(TypeError, Tensor.ushortMax, None) + + def testUshortMaxWrongDim(self): + "Test ushortMax function with a 1D array" + self.assertRaises(TypeError, Tensor.ushortMax, [0, -1, 2, -3]) + + def testUshortMin(self): + "Test ushortMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.ushortMin(tensor), 2) + + def testUshortScale(self): + "Test ushortScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'H') + Tensor.ushortScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testUshortFloor(self): + "Test ushortFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'H') + Tensor.ushortFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testUshortCeil(self): + "Test ushortCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'H') + Tensor.ushortCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testUshortLUSplit(self): + "Test ushortLUSplit function" + lower, upper = Tensor.ushortLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ################################################### + ### Test functions that take arrays of type INT ### + def testIntNorm(self): + "Test intNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.intNorm(tensor), 1) + + def testIntMax(self): + "Test intMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.intMax(tensor), 8) + + def testIntMaxNonContainer(self): + "Test intMax function with None" + self.assertRaises(TypeError, Tensor.intMax, None) + + def testIntMaxWrongDim(self): + "Test intMax function with a 1D array" + self.assertRaises(TypeError, Tensor.intMax, [0, -1, 2, -3]) + + def testIntMin(self): + "Test intMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.intMin(tensor), 2) + + def testIntScale(self): + "Test intScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'i') + Tensor.intScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testIntFloor(self): + "Test intFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'i') + Tensor.intFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testIntCeil(self): + "Test intCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'i') + Tensor.intCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testIntLUSplit(self): + "Test intLUSplit function" + lower, upper = Tensor.intLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + #################################################### + ### Test functions that take arrays of type UINT ### + def testUintNorm(self): + "Test uintNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.uintNorm(tensor), 1) + + def testUintMax(self): + "Test uintMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.uintMax(tensor), 8) + + def testUintMaxNonContainer(self): + "Test uintMax function with None" + self.assertRaises(TypeError, Tensor.uintMax, None) + + def testUintMaxWrongDim(self): + "Test uintMax function with a 1D array" + self.assertRaises(TypeError, Tensor.uintMax, [0, -1, 2, -3]) + + def testUintMin(self): + "Test uintMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.uintMin(tensor), 2) + + def testUintScale(self): + "Test uintScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'I') + Tensor.uintScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testUintFloor(self): + "Test uintFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'I') + Tensor.uintFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testUintCeil(self): + "Test uintCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'I') + Tensor.uintCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testUintLUSplit(self): + "Test uintLUSplit function" + lower, upper = Tensor.uintLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + #################################################### + ### Test functions that take arrays of type LONG ### + def testLongNorm(self): + "Test longNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.longNorm(tensor), 1) + + def testLongMax(self): + "Test longMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.longMax(tensor), 8) + + def testLongMaxNonContainer(self): + "Test longMax function with None" + self.assertRaises(TypeError, Tensor.longMax, None) + + def testLongMaxWrongDim(self): + "Test longMax function with a 1D array" + self.assertRaises(TypeError, Tensor.longMax, [0, -1, 2, -3]) + + def testLongMin(self): + "Test longMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.longMin(tensor), 2) + + def testLongScale(self): + "Test longScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'l') + Tensor.longScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testLongFloor(self): + "Test longFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'l') + Tensor.longFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testLongCeil(self): + "Test longCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'l') + Tensor.longCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testLongLUSplit(self): + "Test longLUSplit function" + lower, upper = Tensor.longLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ##################################################### + ### Test functions that take arrays of type ULONG ### + def testUlongNorm(self): + "Test ulongNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.ulongNorm(tensor), 1) + + def testUlongMax(self): + "Test ulongMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.ulongMax(tensor), 8) + + def testUlongMaxNonContainer(self): + "Test ulongMax function with None" + self.assertRaises(TypeError, Tensor.ulongMax, None) + + def testUlongMaxWrongDim(self): + "Test ulongMax function with a 1D array" + self.assertRaises(TypeError, Tensor.ulongMax, [0, -1, 2, -3]) + + def testUlongMin(self): + "Test ulongMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.ulongMin(tensor), 2) + + def testUlongScale(self): + "Test ulongScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'L') + Tensor.ulongScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testUlongFloor(self): + "Test ulongFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'L') + Tensor.ulongFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testUlongCeil(self): + "Test ulongCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'L') + Tensor.ulongCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testUlongLUSplit(self): + "Test ulongLUSplit function" + lower, upper = Tensor.ulongLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ######################################################## + ### Test functions that take arrays of type LONGLONG ### + def testLongLongNorm(self): + "Test longLongNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.longLongNorm(tensor), 1) + + def testLongLongMax(self): + "Test longLongMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.longLongMax(tensor), 8) + + def testLongLongMaxNonContainer(self): + "Test longLongMax function with None" + self.assertRaises(TypeError, Tensor.longLongMax, None) + + def testLongLongMaxWrongDim(self): + "Test longLongMax function with a 1D array" + self.assertRaises(TypeError, Tensor.longLongMax, [0, -1, 2, -3]) + + def testLongLongMin(self): + "Test longLongMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.longLongMin(tensor), 2) + + def testLongLongScale(self): + "Test longLongScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'q') + Tensor.longLongScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testLongLongFloor(self): + "Test longLongFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'q') + Tensor.longLongFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testLongLongCeil(self): + "Test longLongCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'q') + Tensor.longLongCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testLongLongLUSplit(self): + "Test longLongLUSplit function" + lower, upper = Tensor.longLongLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ######################################################### + ### Test functions that take arrays of type ULONGLONG ### + def testUlongLongNorm(self): + "Test ulongLongNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertEquals(Tensor.ulongLongNorm(tensor), 1) + + def testUlongLongMax(self): + "Test ulongLongMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.ulongLongMax(tensor), 8) + + def testUlongLongMaxNonContainer(self): + "Test ulongLongMax function with None" + self.assertRaises(TypeError, Tensor.ulongLongMax, None) + + def testUlongLongMaxWrongDim(self): + "Test ulongLongMax function with a 1D array" + self.assertRaises(TypeError, Tensor.ulongLongMax, [0, -1, 2, -3]) + + def testUlongLongMin(self): + "Test ulongLongMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.ulongLongMin(tensor), 2) + + def testUlongLongScale(self): + "Test ulongLongScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'Q') + Tensor.ulongLongScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testUlongLongFloor(self): + "Test ulongLongFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'Q') + Tensor.ulongLongFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testUlongLongCeil(self): + "Test ulongLongCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'Q') + Tensor.ulongLongCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testUlongLongLUSplit(self): + "Test ulongLongLUSplit function" + lower, upper = Tensor.ulongLongLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ##################################################### + ### Test functions that take arrays of type FLOAT ### + def testFloatNorm(self): + "Test floatNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertAlmostEquals(Tensor.floatNorm(tensor), 1.870828, 5) + # 1.8708286933869707 + + def testFloatMax(self): + "Test floatMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.floatMax(tensor), 8) + + def testFloatMaxNonContainer(self): + "Test floatMax function with None" + self.assertRaises(TypeError, Tensor.floatMax, None) + + def testFloatMaxWrongDim(self): + "Test floatMax function with a 1D array" + self.assertRaises(TypeError, Tensor.floatMax, [0, -1, 2, -3]) + + def testFloatMin(self): + "Test floatMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.floatMin(tensor), 2) + + def testFloatScale(self): + "Test floatScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'f') + Tensor.floatScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testFloatFloor(self): + "Test floatFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'f') + Tensor.floatFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testFloatCeil(self): + "Test floatCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'f') + Tensor.floatCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testFloatLUSplit(self): + "Test floatLUSplit function" + lower, upper = Tensor.floatLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + + ###################################################### + ### Test functions that take arrays of type DOUBLE ### + def testDoubleNorm(self): + "Test doubleNorm function" + tensor = [[[0,1], [2,3]], + [[3,2], [1,0]]] + self.assertAlmostEquals(Tensor.doubleNorm(tensor), 1.8708286933869) + + def testDoubleMax(self): + "Test doubleMax function" + tensor = [[[1,2], [3,4]], + [[5,6], [7,8]]] + self.assertEquals(Tensor.doubleMax(tensor), 8) + + def testDoubleMaxNonContainer(self): + "Test doubleMax function with None" + self.assertRaises(TypeError, Tensor.doubleMax, None) + + def testDoubleMaxWrongDim(self): + "Test doubleMax function with a 1D array" + self.assertRaises(TypeError, Tensor.doubleMax, [0, -1, 2, -3]) + + def testDoubleMin(self): + "Test doubleMin function" + tensor = [[[9,8], [7,6]], + [[5,4], [3,2]]] + self.assertEquals(Tensor.doubleMin(tensor), 2) + + def testDoubleScale(self): + "Test doubleScale function" + tensor = N.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'d') + Tensor.doubleScale(tensor,4) + self.assertEquals((tensor == [[[4,0,4], [0,4,0], [4,0,4]], + [[0,4,0], [4,0,4], [0,4,0]], + [[4,0,4], [0,4,0], [4,0,4]]]).all(), True) + + def testDoubleFloor(self): + "Test doubleFloor function" + tensor = N.array([[[1,2], [3,4]], + [[5,6], [7,8]]],'d') + Tensor.doubleFloor(tensor,4) + N.testing.assert_array_equal(tensor, N.array([[[4,4], [4,4]], + [[5,6], [7,8]]])) + + def testDoubleCeil(self): + "Test doubleCeil function" + tensor = N.array([[[9,8], [7,6]], + [[5,4], [3,2]]],'d') + Tensor.doubleCeil(tensor,5) + N.testing.assert_array_equal(tensor, N.array([[[5,5], [5,5]], + [[5,4], [3,2]]])) + + def testDoubleLUSplit(self): + "Test doubleLUSplit function" + lower, upper = Tensor.doubleLUSplit([[[1,1], [1,1]], + [[1,1], [1,1]]]) + self.assertEquals((lower == [[[1,1], [1,0]], + [[1,0], [0,0]]]).all(), True) + self.assertEquals((upper == [[[0,0], [0,1]], + [[0,1], [1,1]]]).all(), True) + +###################################################################### + +if __name__ == "__main__": + + # Build the test suite + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TensorTestCase)) + + # Execute the test suite + print "Testing 3D Functions of Module Tensor" + print "NumPy version", N.__version__ + print + result = unittest.TextTestRunner(verbosity=2).run(suite) + sys.exit(len(result.errors) + len(result.failures)) |