| 1 |
# Copyright 2014 Knowledge Economy Developments Ltd |
| 2 |
# Copyright 2014 David Wells |
| 3 |
# |
| 4 |
# Henry Gomersall |
| 5 |
# [email protected] |
| 6 |
# David Wells |
| 7 |
# drwells <at> vt.edu |
| 8 |
# |
| 9 |
# All rights reserved. |
| 10 |
# |
| 11 |
# Redistribution and use in source and binary forms, with or without |
| 12 |
# modification, are permitted provided that the following conditions are met: |
| 13 |
# |
| 14 |
# * Redistributions of source code must retain the above copyright notice, this |
| 15 |
# list of conditions and the following disclaimer. |
| 16 |
# |
| 17 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 18 |
# this list of conditions and the following disclaimer in the documentation |
| 19 |
# and/or other materials provided with the distribution. |
| 20 |
# |
| 21 |
# * Neither the name of the copyright holder nor the names of its contributors |
| 22 |
# may be used to endorse or promote products derived from this software without |
| 23 |
# specific prior written permission. |
| 24 |
# |
| 25 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 29 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 35 |
# POSSIBILITY OF SUCH DAMAGE. |
| 36 |
# |
| 37 |
|
| 38 |
from pyfftw import (byte_align, is_byte_aligned, ones_aligned, |
| 39 |
empty_aligned, zeros_aligned, simd_alignment,) |
| 40 |
# Test the deprecated functions. |
| 41 |
from pyfftw import n_byte_align, n_byte_align_empty, is_n_byte_aligned |
| 42 |
import numpy |
| 43 |
from timeit import Timer |
| 44 |
|
| 45 |
from .test_pyfftw_base import run_test_suites |
| 46 |
|
| 47 |
import unittest |
| 48 |
import warnings |
| 49 |
|
| 50 |
|
| 51 |
def ignore_deprecation_warning(function): |
| 52 |
def new_function(*args, **kwargs): |
| 53 |
with warnings.catch_warnings(): |
| 54 |
warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 55 |
return function(*args, **kwargs) |
| 56 |
|
| 57 |
return new_function |
| 58 |
|
| 59 |
|
| 60 |
class ByteAlignTest(unittest.TestCase): |
| 61 |
|
| 62 |
def __init__(self, *args, **kwargs): |
| 63 |
|
| 64 |
super(ByteAlignTest, self).__init__(*args, **kwargs) |
| 65 |
|
| 66 |
if not hasattr(self, 'assertRaisesRegex'): |
| 67 |
self.assertRaisesRegex = self.assertRaisesRegexp |
| 68 |
|
| 69 |
def setUp(self): |
| 70 |
|
| 71 |
return |
| 72 |
|
| 73 |
def tearDown(self): |
| 74 |
|
| 75 |
return |
| 76 |
|
| 77 |
def test_ones_aligned(self): |
| 78 |
shape = (10,10) |
| 79 |
# Test a few alignments and dtypes |
| 80 |
for each in [(3, 'float64'), |
| 81 |
(7, 'float64'), |
| 82 |
(9, 'float32'), |
| 83 |
(16, 'int64'), |
| 84 |
(24, 'bool'), |
| 85 |
(23, 'complex64'), |
| 86 |
(63, 'complex128'), |
| 87 |
(64, 'int8')]: |
| 88 |
|
| 89 |
n = each[0] |
| 90 |
a = numpy.ones(shape, dtype=each[1]) |
| 91 |
b = ones_aligned(shape, dtype=each[1], n=n) |
| 92 |
self.assertTrue(b.ctypes.data%n == 0) |
| 93 |
self.assertTrue(b.dtype == each[1]) |
| 94 |
self.assertTrue(numpy.array_equal(a, b)) |
| 95 |
|
| 96 |
def test_zeros_aligned(self): |
| 97 |
shape = (10,10) |
| 98 |
# Test a few alignments and dtypes |
| 99 |
for each in [(3, 'float64'), |
| 100 |
(7, 'float64'), |
| 101 |
(9, 'float32'), |
| 102 |
(16, 'int64'), |
| 103 |
(24, 'bool'), |
| 104 |
(23, 'complex64'), |
| 105 |
(63, 'complex128'), |
| 106 |
(64, 'int8')]: |
| 107 |
|
| 108 |
n = each[0] |
| 109 |
a = numpy.zeros(shape, dtype=each[1]) |
| 110 |
b = zeros_aligned(shape, dtype=each[1], n=n) |
| 111 |
self.assertTrue(b.ctypes.data%n == 0) |
| 112 |
self.assertTrue(b.dtype == each[1]) |
| 113 |
self.assertTrue(numpy.array_equal(a, b)) |
| 114 |
|
| 115 |
def test_empty_aligned(self): |
| 116 |
shape = (10,10) |
| 117 |
# Test a few alignments and dtypes |
| 118 |
for each in [(3, 'float64'), |
| 119 |
(7, 'float64'), |
| 120 |
(9, 'float32'), |
| 121 |
(16, 'int64'), |
| 122 |
(24, 'bool'), |
| 123 |
(23, 'complex64'), |
| 124 |
(63, 'complex128'), |
| 125 |
(64, 'int8')]: |
| 126 |
|
| 127 |
n = each[0] |
| 128 |
b = empty_aligned(shape, dtype=each[1], n=n) |
| 129 |
self.assertTrue(b.ctypes.data%n == 0) |
| 130 |
self.assertTrue(b.dtype == each[1]) |
| 131 |
|
| 132 |
@ignore_deprecation_warning |
| 133 |
def test_n_byte_align_empty(self): |
| 134 |
shape = (10,10) |
| 135 |
# Test a few alignments and dtypes |
| 136 |
for each in [(3, 'float64'), |
| 137 |
(7, 'float64'), |
| 138 |
(9, 'float32'), |
| 139 |
(16, 'int64'), |
| 140 |
(24, 'bool'), |
| 141 |
(23, 'complex64'), |
| 142 |
(63, 'complex128'), |
| 143 |
(64, 'int8')]: |
| 144 |
|
| 145 |
n = each[0] |
| 146 |
b = n_byte_align_empty(shape, n, dtype=each[1]) |
| 147 |
self.assertTrue(b.ctypes.data%n == 0) |
| 148 |
self.assertTrue(b.dtype == each[1]) |
| 149 |
|
| 150 |
def test_byte_align(self): |
| 151 |
shape = (10,10) |
| 152 |
a = numpy.random.randn(*shape) |
| 153 |
# Test a few alignments |
| 154 |
for n in [None, 3, 7, 9, 16, 24, 23, 63, 64]: |
| 155 |
expected_alignment = get_expected_alignment(n) |
| 156 |
b = byte_align(a, n=n) |
| 157 |
self.assertTrue(b.ctypes.data % expected_alignment == 0) |
| 158 |
|
| 159 |
@ignore_deprecation_warning |
| 160 |
def test_n_byte_align(self): |
| 161 |
shape = (10,10) |
| 162 |
a = numpy.random.randn(*shape) |
| 163 |
# Test a few alignments |
| 164 |
for n in [3, 7, 9, 16, 24, 23, 63, 64]: |
| 165 |
b = n_byte_align(a, n) |
| 166 |
self.assertTrue(b.ctypes.data%n == 0) |
| 167 |
|
| 168 |
def test_byte_align_integer_shape(self): |
| 169 |
shape = 100 |
| 170 |
a = numpy.random.randn(shape) |
| 171 |
# Test a few alignments |
| 172 |
for n in [None, 3, 7, 9, 16, 24, 23, 63, 64]: |
| 173 |
expected_alignment = get_expected_alignment(n) |
| 174 |
b = byte_align(a, n=n) |
| 175 |
self.assertTrue(b.ctypes.data % expected_alignment == 0) |
| 176 |
|
| 177 |
@ignore_deprecation_warning |
| 178 |
def test_n_byte_align_integer_shape(self): |
| 179 |
shape = 100 |
| 180 |
a = numpy.random.randn(shape) |
| 181 |
# Test a few alignments |
| 182 |
for n in [3, 7, 9, 16, 24, 23, 63, 64]: |
| 183 |
b = n_byte_align(a, n) |
| 184 |
self.assertTrue(b.ctypes.data%n == 0) |
| 185 |
|
| 186 |
def test_is_byte_aligned(self): |
| 187 |
a = empty_aligned(100) |
| 188 |
self.assertTrue(is_byte_aligned(a, get_expected_alignment(None))) |
| 189 |
|
| 190 |
a = empty_aligned(100, n=16) |
| 191 |
self.assertTrue(is_byte_aligned(a, n=16)) |
| 192 |
|
| 193 |
a = empty_aligned(100, n=5) |
| 194 |
self.assertTrue(is_byte_aligned(a, n=5)) |
| 195 |
|
| 196 |
a = empty_aligned(100, dtype='float32', n=16)[1:] |
| 197 |
self.assertFalse(is_byte_aligned(a, n=16)) |
| 198 |
self.assertTrue(is_byte_aligned(a, n=4)) |
| 199 |
|
| 200 |
@ignore_deprecation_warning |
| 201 |
def test_is_n_byte_aligned(self): |
| 202 |
a = n_byte_align_empty(100, 16) |
| 203 |
self.assertTrue(is_n_byte_aligned(a, 16)) |
| 204 |
|
| 205 |
a = n_byte_align_empty(100, 5) |
| 206 |
self.assertTrue(is_n_byte_aligned(a, 5)) |
| 207 |
|
| 208 |
a = n_byte_align_empty(100, 16, dtype='float32')[1:] |
| 209 |
self.assertFalse(is_n_byte_aligned(a, 16)) |
| 210 |
self.assertTrue(is_n_byte_aligned(a, 4)) |
| 211 |
|
| 212 |
def test_is_byte_aligned_fail_with_non_array(self): |
| 213 |
|
| 214 |
a = [1, 2, 3, 4] |
| 215 |
self.assertRaisesRegex(TypeError, 'Invalid array', |
| 216 |
is_byte_aligned, a, n=16) |
| 217 |
|
| 218 |
@ignore_deprecation_warning |
| 219 |
def test_is_n_byte_aligned_fail_with_non_array(self): |
| 220 |
|
| 221 |
a = [1, 2, 3, 4] |
| 222 |
self.assertRaisesRegex(TypeError, 'Invalid array', |
| 223 |
is_n_byte_aligned, a, 16) |
| 224 |
|
| 225 |
def test_byte_align_fail_with_non_array(self): |
| 226 |
|
| 227 |
a = [1, 2, 3, 4] |
| 228 |
self.assertRaisesRegex(TypeError, 'Invalid array', |
| 229 |
byte_align, a, n=16) |
| 230 |
|
| 231 |
@ignore_deprecation_warning |
| 232 |
def test_n_byte_align_fail_with_non_array(self): |
| 233 |
|
| 234 |
a = [1, 2, 3, 4] |
| 235 |
self.assertRaisesRegex(TypeError, 'Invalid array', |
| 236 |
n_byte_align, a, 16) |
| 237 |
|
| 238 |
def test_byte_align_consistent_data(self): |
| 239 |
shape = (10,10) |
| 240 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 241 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 242 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 243 |
|
| 244 |
# Test a few alignments |
| 245 |
for n in [None, 3, 7, 9, 16, 24, 23, 63, 64]: |
| 246 |
d = byte_align(a, n=n) |
| 247 |
self.assertTrue(numpy.array_equal(a, d)) |
| 248 |
|
| 249 |
d = byte_align(b, n=n) |
| 250 |
self.assertTrue(numpy.array_equal(b, d)) |
| 251 |
|
| 252 |
d = byte_align(c, n=n) |
| 253 |
self.assertTrue(numpy.array_equal(c, d)) |
| 254 |
|
| 255 |
@ignore_deprecation_warning |
| 256 |
def test_n_byte_align_consistent_data(self): |
| 257 |
shape = (10,10) |
| 258 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 259 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 260 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 261 |
|
| 262 |
# Test a few alignments |
| 263 |
for n in [3, 7, 9, 16, 24, 23, 63, 64]: |
| 264 |
d = n_byte_align(a, n) |
| 265 |
self.assertTrue(numpy.array_equal(a, d)) |
| 266 |
|
| 267 |
d = n_byte_align(b, n) |
| 268 |
self.assertTrue(numpy.array_equal(b, d)) |
| 269 |
|
| 270 |
d = n_byte_align(c, n) |
| 271 |
self.assertTrue(numpy.array_equal(c, d)) |
| 272 |
|
| 273 |
def test_byte_align_different_dtypes(self): |
| 274 |
shape = (10,10) |
| 275 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 276 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 277 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 278 |
# Test a few alignments |
| 279 |
for n in [None, 3, 7, 9, 16, 24, 23, 63, 64]: |
| 280 |
expected_alignment = get_expected_alignment(n) |
| 281 |
d = byte_align(a, n=n) |
| 282 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 283 |
self.assertTrue(d.__class__ == a.__class__) |
| 284 |
|
| 285 |
d = byte_align(b, n=n) |
| 286 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 287 |
self.assertTrue(d.__class__ == b.__class__) |
| 288 |
|
| 289 |
d = byte_align(c, n=n) |
| 290 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 291 |
self.assertTrue(d.__class__ == c.__class__) |
| 292 |
|
| 293 |
@ignore_deprecation_warning |
| 294 |
def test_n_byte_align_different_dtypes(self): |
| 295 |
shape = (10,10) |
| 296 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 297 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 298 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 299 |
# Test a few alignments |
| 300 |
for n in [3, 7, 9, 16, 24, 23, 63, 64]: |
| 301 |
d = n_byte_align(a, n) |
| 302 |
self.assertTrue(d.ctypes.data%n == 0) |
| 303 |
self.assertTrue(d.__class__ == a.__class__) |
| 304 |
|
| 305 |
d = n_byte_align(b, n) |
| 306 |
self.assertTrue(d.ctypes.data%n == 0) |
| 307 |
self.assertTrue(d.__class__ == b.__class__) |
| 308 |
|
| 309 |
d = n_byte_align(c, n) |
| 310 |
self.assertTrue(d.ctypes.data%n == 0) |
| 311 |
self.assertTrue(d.__class__ == c.__class__) |
| 312 |
|
| 313 |
def test_byte_align_set_dtype(self): |
| 314 |
shape = (10,10) |
| 315 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 316 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 317 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 318 |
# Test a few alignments |
| 319 |
for n in [None, 3, 7, 9, 16, 24, 23, 63, 64]: |
| 320 |
expected_alignment = get_expected_alignment(n) |
| 321 |
d = byte_align(a, dtype='float32', n=n) |
| 322 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 323 |
self.assertTrue(d.dtype == 'float32') |
| 324 |
|
| 325 |
d = byte_align(b, dtype='float32', n=n) |
| 326 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 327 |
self.assertTrue(d.dtype == 'float32') |
| 328 |
|
| 329 |
d = byte_align(c, dtype='float64', n=n) |
| 330 |
self.assertTrue(d.ctypes.data % expected_alignment == 0) |
| 331 |
self.assertTrue(d.dtype == 'float64') |
| 332 |
|
| 333 |
@ignore_deprecation_warning |
| 334 |
def test_n_byte_align_set_dtype(self): |
| 335 |
shape = (10,10) |
| 336 |
a = numpy.int16(numpy.random.randn(*shape)*16000) |
| 337 |
b = numpy.float64(numpy.random.randn(*shape)) |
| 338 |
c = numpy.int8(numpy.random.randn(*shape)*255) |
| 339 |
# Test a few alignments |
| 340 |
for n in [3, 7, 9, 16, 24, 23, 63, 64]: |
| 341 |
d = n_byte_align(a, n, dtype='float32') |
| 342 |
self.assertTrue(d.ctypes.data%n == 0) |
| 343 |
self.assertTrue(d.dtype == 'float32') |
| 344 |
|
| 345 |
d = n_byte_align(b, n, dtype='float32') |
| 346 |
self.assertTrue(d.ctypes.data%n == 0) |
| 347 |
self.assertTrue(d.dtype == 'float32') |
| 348 |
|
| 349 |
d = n_byte_align(c, n, dtype='float64') |
| 350 |
self.assertTrue(d.ctypes.data%n == 0) |
| 351 |
self.assertTrue(d.dtype == 'float64') |
| 352 |
|
| 353 |
|
| 354 |
def get_expected_alignment(n): |
| 355 |
if n is None: |
| 356 |
return simd_alignment |
| 357 |
else: |
| 358 |
return n |
| 359 |
|
| 360 |
test_cases = ( |
| 361 |
ByteAlignTest,) |
| 362 |
|
| 363 |
test_set = None |
| 364 |
|
| 365 |
if __name__ == '__main__': |
| 366 |
|
| 367 |
run_test_suites(test_cases, test_set) |