~ubuntu-branches/ubuntu/oneiric/python-scipy/oneiric-proposed

« back to all changes in this revision

Viewing changes to scipy/fftpack/tests/test_basic.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 21:26:25 UTC
  • mfrom: (9.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110406212625-3izdplobqe6fzeql
Tags: 0.9.0+dfsg1-1
* New upstream release (Closes: #614407, #579041, #569008)
* Convert to dh_python2 (Closes: #617028)

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
  python tests/test_basic.py
12
12
"""
13
13
 
14
 
from numpy.testing import *
 
14
from numpy.testing import assert_, assert_equal, assert_array_almost_equal, \
 
15
        assert_array_almost_equal_nulp, assert_raises, run_module_suite, \
 
16
        TestCase, dec
15
17
from scipy.fftpack import ifft,fft,fftn,ifftn,rfft,irfft, fft2
16
18
from scipy.fftpack import _fftpack as fftpack
17
19
 
112
114
    def setUp(self):
113
115
        self.cdt = None
114
116
        self.rdt = None
 
117
        np.random.seed(1234)
115
118
 
116
119
    def test_definition(self):
117
120
        x = np.array([1,2,3,4+1j,1,2,3,4+2j], dtype = self.cdt)
118
121
        y = fft(x)
119
 
        self.failUnless(y.dtype == self.cdt,
 
122
        self.assertTrue(y.dtype == self.cdt,
120
123
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
121
124
        y1 = direct_dft(x)
122
125
        assert_array_almost_equal(y,y1)
127
130
        x1 = np.array([1,2,3,4], dtype=self.rdt)
128
131
        x2 = np.array([1,2,3,4], dtype=self.rdt)
129
132
        y = fft([x1,x2],n=4)
130
 
        self.failUnless(y.dtype == self.cdt,
 
133
        self.assertTrue(y.dtype == self.cdt,
131
134
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
132
135
        assert_equal(y.shape,(2,4))
133
136
        assert_array_almost_equal(y[0],direct_dft(x1))
137
140
        x1 = np.array([1,2,3,4+1j], dtype=self.cdt)
138
141
        x2 = np.array([1,2,3,4+1j], dtype=self.cdt)
139
142
        y = fft([x1,x2],n=4)
140
 
        self.failUnless(y.dtype == self.cdt,
 
143
        self.assertTrue(y.dtype == self.cdt,
141
144
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
142
145
        assert_equal(y.shape,(2,4))
143
146
        assert_array_almost_equal(y[0],direct_dft(x1))
168
171
        pass
169
172
 
170
173
class _TestIFFTBase(TestCase):
 
174
    def setUp(self):
 
175
        np.random.seed(1234)
 
176
 
171
177
    def test_definition(self):
172
178
        x = np.array([1,2,3,4+1j,1,2,3,4+2j], self.cdt)
173
179
        y = ifft(x)
174
180
        y1 = direct_idft(x)
175
 
        self.failUnless(y.dtype == self.cdt,
 
181
        self.assertTrue(y.dtype == self.cdt,
176
182
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
177
183
        assert_array_almost_equal(y,y1)
178
184
 
182
188
    def test_definition_real(self):
183
189
        x = np.array([1,2,3,4,1,2,3,4], self.rdt)
184
190
        y = ifft(x)
185
 
        self.failUnless(y.dtype == self.cdt,
 
191
        self.assertTrue(y.dtype == self.cdt,
186
192
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
187
193
        y1 = direct_idft(x)
188
194
        assert_array_almost_equal(y,y1)
189
195
 
190
196
        x = np.array([1,2,3,4,5], dtype=self.rdt)
191
 
        self.failUnless(y.dtype == self.cdt,
 
197
        self.assertTrue(y.dtype == self.cdt,
192
198
                "Output dtype is %s, expected %s" % (y.dtype, self.cdt))
193
199
        assert_array_almost_equal(ifft(x),direct_idft(x))
194
200
 
208
214
            x = random([size]).astype(self.cdt) +1j*x
209
215
            y1 = ifft(fft(x))
210
216
            y2 = fft(ifft(x))
211
 
            self.failUnless(y1.dtype == self.cdt,
 
217
            self.assertTrue(y1.dtype == self.cdt,
212
218
                    "Output dtype is %s, expected %s" % (y1.dtype, self.cdt))
213
 
            self.failUnless(y2.dtype == self.cdt,
 
219
            self.assertTrue(y2.dtype == self.cdt,
214
220
                    "Output dtype is %s, expected %s" % (y2.dtype, self.cdt))
215
221
            assert_array_almost_equal (y1, x)
216
222
            assert_array_almost_equal (y2, x)
220
226
            x = random([size]).astype(self.rdt)
221
227
            y1 = ifft(fft(x))
222
228
            y2 = fft(ifft(x))
223
 
            self.failUnless(y1.dtype == self.cdt,
 
229
            self.assertTrue(y1.dtype == self.cdt,
224
230
                    "Output dtype is %s, expected %s" % (y1.dtype, self.cdt))
225
 
            self.failUnless(y2.dtype == self.cdt,
 
231
            self.assertTrue(y2.dtype == self.cdt,
226
232
                    "Output dtype is %s, expected %s" % (y2.dtype, self.cdt))
227
233
            assert_array_almost_equal (y1, x)
228
234
            assert_array_almost_equal (y2, x)
263
269
        self.rdt = np.float32
264
270
 
265
271
class _TestRFFTBase(TestCase):
 
272
    def setUp(self):
 
273
        np.random.seed(1234)
266
274
 
267
275
    def test_definition(self):
268
276
        for t in [[1, 2, 3, 4, 1, 2, 3, 4], [1, 2, 3, 4, 1, 2, 3, 4, 5]]:
270
278
            y = rfft(x)
271
279
            y1 = direct_rdft(x)
272
280
            assert_array_almost_equal(y,y1)
273
 
            self.failUnless(y.dtype == self.rdt,
 
281
            self.assertTrue(y.dtype == self.rdt,
274
282
                    "Output dtype is %s, expected %s" % (y.dtype, self.rdt))
275
283
 
276
284
    def test_djbfft(self):
299
307
        self.rdt = np.float32
300
308
 
301
309
class _TestIRFFTBase(TestCase):
 
310
    def setUp(self):
 
311
        np.random.seed(1234)
302
312
 
303
313
    def test_definition(self):
304
314
        x1 = [1,2,3,4,1,2,3,4]
309
319
        def _test(x, xr):
310
320
            y = irfft(np.array(x, dtype=self.rdt))
311
321
            y1 = direct_irdft(x)
312
 
            self.failUnless(y.dtype == self.rdt,
 
322
            self.assertTrue(y.dtype == self.rdt,
313
323
                    "Output dtype is %s, expected %s" % (y.dtype, self.rdt))
314
324
            assert_array_almost_equal(y,y1, decimal=self.ndec)
315
325
            assert_array_almost_equal(y,ifft(xr), decimal=self.ndec)
337
347
            x = random([size]).astype(self.rdt)
338
348
            y1 = irfft(rfft(x))
339
349
            y2 = rfft(irfft(x))
340
 
            self.failUnless(y1.dtype == self.rdt,
 
350
            self.assertTrue(y1.dtype == self.rdt,
341
351
                    "Output dtype is %s, expected %s" % (y1.dtype, self.rdt))
342
 
            self.failUnless(y2.dtype == self.rdt,
 
352
            self.assertTrue(y2.dtype == self.rdt,
343
353
                    "Output dtype is %s, expected %s" % (y2.dtype, self.rdt))
344
354
            assert_array_almost_equal (y1, x, decimal=self.ndec,
345
355
                                       err_msg="size=%d" % size)
378
388
        self.ndec = 5
379
389
 
380
390
class Testfft2(TestCase):
 
391
    def setUp(self):
 
392
        np.random.seed(1234)
 
393
 
381
394
    def test_regression_244(self):
382
395
        """fft returns wrong result with axes parameter."""
383
396
        # fftn (and hence fft2) used to break when both axes and shape were
388
401
        assert_array_almost_equal(y, y_r)
389
402
 
390
403
class TestFftnSingle(TestCase):
 
404
    def setUp(self):
 
405
        np.random.seed(1234)
 
406
 
391
407
    def test_definition(self):
392
408
        x = [[1,2,3],[4,5,6],[7,8,9]]
393
409
        y = fftn(np.array(x, np.float32))
401
417
        for size in SMALL_COMPOSITE_SIZES + SMALL_PRIME_SIZES:
402
418
            np.random.seed(1234)
403
419
            x = np.random.rand(size, size) + 1j*np.random.rand(size, size)
404
 
            y1 = fftn(x.astype(np.float32))
405
 
            y2 = fftn(x.astype(np.float64)).astype(np.complex64)
 
420
            y1 = fftn(x.real.astype(np.float32))
 
421
            y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
406
422
 
407
423
            self.failUnless(y1.dtype == np.complex64)
408
424
            assert_array_almost_equal_nulp(y1, y2, 2000)
410
426
        for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES:
411
427
            np.random.seed(1234)
412
428
            x = np.random.rand(size, 3) + 1j*np.random.rand(size, 3)
413
 
            y1 = fftn(x.astype(np.float32))
414
 
            y2 = fftn(x.astype(np.float64)).astype(np.complex64)
 
429
            y1 = fftn(x.real.astype(np.float32))
 
430
            y2 = fftn(x.real.astype(np.float64)).astype(np.complex64)
415
431
 
416
432
            self.failUnless(y1.dtype == np.complex64)
417
433
            assert_array_almost_equal_nulp(y1, y2, 2000)
418
434
 
419
435
class TestFftn(TestCase):
 
436
    def setUp(self):
 
437
        np.random.seed(1234)
420
438
 
421
439
    def test_definition(self):
422
440
        x = [[1,2,3],[4,5,6],[7,8,9]]
561
579
        assert_array_almost_equal(y, numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
562
580
 
563
581
    def test_shape_argument_more(self):
564
 
        # Test that fftn raise a value error exception when s.shape is longer
565
 
        # than x.shape
 
582
        """Test that fftn raises ValueError when s.shape is longer than x.shape"""
566
583
        x = zeros((4, 4, 2))
567
 
        try:
568
 
            fx = fftn(x, shape = (8, 8, 2, 1))
569
 
            raise AssertionError("s.shape longer than x.shape succeded, "\
570
 
                                 "but should not have.")
571
 
        except ValueError:
572
 
            pass
 
584
        assert_raises(ValueError, fftn, x, shape=(8, 8, 2, 1))
 
585
 
573
586
 
574
587
class _TestIfftn(TestCase):
575
588
    dtype = None
576
589
    cdtype = None
 
590
 
 
591
    def setUp(self):
 
592
        np.random.seed(1234)
 
593
 
577
594
    def test_definition(self):
578
595
        x = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype=self.dtype)
579
596
        y = ifftn(x)
580
 
        assert y.dtype == self.cdtype
 
597
        assert_(y.dtype == self.cdtype)
581
598
        assert_array_almost_equal_nulp(y,direct_idftn(x),self.maxnlp)
582
599
        x = random((20,26))
583
600
        assert_array_almost_equal_nulp(ifftn(x),direct_idftn(x),self.maxnlp)
601
618
    maxnlp = 3500
602
619
 
603
620
class TestLongDoubleFailure(TestCase):
 
621
    def setUp(self):
 
622
        np.random.seed(1234)
604
623
 
605
624
    def test_complex(self):
606
625
        if np.dtype(np.longcomplex).itemsize == np.dtype(np.complex).itemsize:
633
652
            except ValueError:
634
653
                pass
635
654
 
 
655
 
 
656
 
 
657
class TestOverwrite(object):
 
658
    """
 
659
    Check input overwrite behavior of the FFT functions
 
660
    """
 
661
 
 
662
    real_dtypes = [np.float32, np.float64]
 
663
    dtypes = real_dtypes + [np.complex64, np.complex128]
 
664
 
 
665
    def _check(self, x, routine, fftsize, axis):
 
666
        x2 = x.copy()
 
667
        y = routine(x2, fftsize, axis)
 
668
 
 
669
        sig = "%s(%s%r, %r, axis=%r)" % (routine.__name__, x.dtype, x.shape,
 
670
                                         fftsize, axis)
 
671
        assert_equal(x2, x, err_msg="spurious overwrite in %s" % sig)
 
672
 
 
673
    def _check_1d(self, routine, dtype, shape, axis):
 
674
        np.random.seed(1234)
 
675
        if np.issubdtype(dtype, np.complexfloating):
 
676
            data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
 
677
        else:
 
678
            data = np.random.randn(*shape)
 
679
        data = data.astype(dtype)
 
680
 
 
681
        for fftsize in [8, 16, 32]:
 
682
            self._check(data, routine, fftsize, axis)
 
683
 
 
684
    def test_fft(self):
 
685
        for dtype in self.dtypes:
 
686
            self._check_1d(fft, dtype, (16,), -1)
 
687
            self._check_1d(fft, dtype, (16, 2), 0)
 
688
            self._check_1d(fft, dtype, (2, 16), 1)
 
689
 
 
690
    def test_ifft(self):
 
691
        for dtype in self.dtypes:
 
692
            self._check_1d(ifft, dtype, (16,), -1)
 
693
            self._check_1d(ifft, dtype, (16, 2), 0)
 
694
            self._check_1d(ifft, dtype, (2, 16), 1)
 
695
 
 
696
    def test_rfft(self):
 
697
        for dtype in self.real_dtypes:
 
698
            self._check_1d(rfft, dtype, (16,), -1)
 
699
            self._check_1d(rfft, dtype, (16, 2), 0)
 
700
            self._check_1d(rfft, dtype, (2, 16), 1)
 
701
 
 
702
    def test_irfft(self):
 
703
        for dtype in self.real_dtypes:
 
704
            self._check_1d(irfft, dtype, (16,), -1)
 
705
            self._check_1d(irfft, dtype, (16, 2), 0)
 
706
            self._check_1d(irfft, dtype, (2, 16), 1)
 
707
 
 
708
    def _check_nd_one(self, routine, dtype, shape, axes):
 
709
        np.random.seed(1234)
 
710
        if np.issubdtype(dtype, np.complexfloating):
 
711
            data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
 
712
        else:
 
713
            data = np.random.randn(*shape)
 
714
        data = data.astype(dtype)
 
715
 
 
716
        def fftshape_iter(shp):
 
717
            if len(shp) <= 0:
 
718
                yield ()
 
719
            else:
 
720
                for j in (shp[0]//2, shp[0], shp[0]*2):
 
721
                    for rest in fftshape_iter(shp[1:]):
 
722
                        yield (j,) + rest
 
723
 
 
724
        if axes is None:
 
725
            part_shape = shape
 
726
        else:
 
727
            part_shape = tuple(np.take(shape, axes))
 
728
 
 
729
        for fftshape in fftshape_iter(part_shape):
 
730
            self._check(data, routine, fftshape, axes)
 
731
            if data.ndim > 1:
 
732
                # check fortran order: it never overwrites
 
733
                self._check(data.T, routine, fftshape, axes)
 
734
 
 
735
    def _check_nd(self, routine, dtype):
 
736
        self._check_nd_one(routine, dtype, (16,), None)
 
737
        self._check_nd_one(routine, dtype, (16,), (0,))
 
738
        self._check_nd_one(routine, dtype, (16, 2), (0,))
 
739
        self._check_nd_one(routine, dtype, (2, 16), (1,))
 
740
        self._check_nd_one(routine, dtype, (8, 16), None)
 
741
        self._check_nd_one(routine, dtype, (8, 16), (0, 1))
 
742
        self._check_nd_one(routine, dtype, (8, 16, 2), (0, 1))
 
743
        self._check_nd_one(routine, dtype, (8, 16, 2), (1, 2))
 
744
        self._check_nd_one(routine, dtype, (8, 16, 2), (0,))
 
745
        self._check_nd_one(routine, dtype, (8, 16, 2), (1,))
 
746
        self._check_nd_one(routine, dtype, (8, 16, 2), (2,))
 
747
        self._check_nd_one(routine, dtype, (8, 16, 2), None)
 
748
        self._check_nd_one(routine, dtype, (8, 16, 2), (0,1,2))
 
749
 
 
750
    def test_fftn(self):
 
751
        for dtype in self.dtypes:
 
752
            self._check_nd(fftn, dtype)
 
753
 
 
754
    def test_ifftn(self):
 
755
        for dtype in self.dtypes:
 
756
            self._check_nd(ifftn, dtype)
 
757
 
 
758
 
636
759
if __name__ == "__main__":
637
760
    run_module_suite()