@@ -1259,25 +1259,11 @@ def check_maxfreq(self, spec, fsp, fstims):
1259
1259
del fstimst [- 1 ]
1260
1260
spect [maxind - 5 :maxind + 5 ] = 0
1261
1261
1262
- def test_spectral_helper_raises_complex_same_data (self ):
1263
- # test that mode 'complex' cannot be used if x is not y
1262
+ @pytest .mark .parametrize ( # Modes that require `x is y`.
1263
+ 'mode' , ['complex' , 'magnitude' , 'angle' , 'phase' ])
1264
+ def test_spectral_helper_mode_requires_x_is_y (self , mode ):
1264
1265
with pytest .raises (ValueError ):
1265
- mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = 'complex' )
1266
-
1267
- def test_spectral_helper_raises_magnitude_same_data (self ):
1268
- # test that mode 'magnitude' cannot be used if x is not y
1269
- with pytest .raises (ValueError ):
1270
- mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = 'magnitude' )
1271
-
1272
- def test_spectral_helper_raises_angle_same_data (self ):
1273
- # test that mode 'angle' cannot be used if x is not y
1274
- with pytest .raises (ValueError ):
1275
- mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = 'angle' )
1276
-
1277
- def test_spectral_helper_raises_phase_same_data (self ):
1278
- # test that mode 'phase' cannot be used if x is not y
1279
- with pytest .raises (ValueError ):
1280
- mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = 'phase' )
1266
+ mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = mode )
1281
1267
1282
1268
def test_spectral_helper_raises_unknown_mode (self ):
1283
1269
# test that unknown value for mode cannot be used
@@ -1305,15 +1291,10 @@ def test_spectral_helper_raises_winlen_ne_NFFT(self):
1305
1291
mlab ._spectral_helper (x = self .y , y = self .y , NFFT = 10 ,
1306
1292
window = np .ones (9 ))
1307
1293
1308
- def test_single_spectrum_helper_raises_mode_default ( self ):
1309
- # test that mode 'default' cannot be used with _single_spectrum_helper
1294
+ @ pytest . mark . parametrize ( 'mode' , [ 'default' , 'psd' ])
1295
+ def test_single_spectrum_helper_unsupported_modes ( self , mode ):
1310
1296
with pytest .raises (ValueError ):
1311
- mlab ._single_spectrum_helper (x = self .y , mode = 'default' )
1312
-
1313
- def test_single_spectrum_helper_raises_mode_psd (self ):
1314
- # test that mode 'psd' cannot be used with _single_spectrum_helper
1315
- with pytest .raises (ValueError ):
1316
- mlab ._single_spectrum_helper (x = self .y , mode = 'psd' )
1297
+ mlab ._single_spectrum_helper (x = self .y , mode = mode )
1317
1298
1318
1299
def test_spectral_helper_psd (self ):
1319
1300
freqs = self .freqs_density
@@ -1397,10 +1378,14 @@ def test_psd(self):
1397
1378
assert spec .shape == freqs .shape
1398
1379
self .check_freqs (spec , freqs , fsp , self .fstims )
1399
1380
1400
- def test_psd_detrend_mean_func_offset (self ):
1381
+ @pytest .mark .parametrize (
1382
+ 'make_data, detrend' ,
1383
+ [(np .zeros , mlab .detrend_mean ), (np .zeros , 'mean' ),
1384
+ (np .arange , mlab .detrend_linear ), (np .arange , 'linear' )])
1385
+ def test_psd_detrend (self , make_data , detrend ):
1401
1386
if self .NFFT_density is None :
1402
1387
return
1403
- ydata = np . zeros (self .NFFT_density )
1388
+ ydata = make_data (self .NFFT_density )
1404
1389
ydata1 = ydata + 5
1405
1390
ydata2 = ydata + 3.3
1406
1391
ydata = np .vstack ([ydata1 , ydata2 ])
@@ -1413,118 +1398,13 @@ def test_psd_detrend_mean_func_offset(self):
1413
1398
Fs = self .Fs ,
1414
1399
noverlap = 0 ,
1415
1400
sides = self .sides ,
1416
- detrend = mlab . detrend_mean )
1401
+ detrend = detrend )
1417
1402
spec_b , fsp_b = mlab .psd (x = ydatab ,
1418
1403
NFFT = self .NFFT_density ,
1419
1404
Fs = self .Fs ,
1420
1405
noverlap = 0 ,
1421
1406
sides = self .sides ,
1422
- detrend = mlab .detrend_mean )
1423
- spec_c , fsp_c = mlab .psd (x = ycontrol ,
1424
- NFFT = self .NFFT_density ,
1425
- Fs = self .Fs ,
1426
- noverlap = 0 ,
1427
- sides = self .sides )
1428
- assert_array_equal (fsp_g , fsp_c )
1429
- assert_array_equal (fsp_b , fsp_c )
1430
- assert_allclose (spec_g , spec_c , atol = 1e-08 )
1431
- # these should not be almost equal
1432
- with pytest .raises (AssertionError ):
1433
- assert_allclose (spec_b , spec_c , atol = 1e-08 )
1434
-
1435
- def test_psd_detrend_mean_str_offset (self ):
1436
- if self .NFFT_density is None :
1437
- return
1438
- ydata = np .zeros (self .NFFT_density )
1439
- ydata1 = ydata + 5
1440
- ydata2 = ydata + 3.3
1441
- ydata = np .vstack ([ydata1 , ydata2 ])
1442
- ydata = np .tile (ydata , (20 , 1 ))
1443
- ydatab = ydata .T .flatten ()
1444
- ydata = ydata .flatten ()
1445
- ycontrol = np .zeros_like (ydata )
1446
- spec_g , fsp_g = mlab .psd (x = ydata ,
1447
- NFFT = self .NFFT_density ,
1448
- Fs = self .Fs ,
1449
- noverlap = 0 ,
1450
- sides = self .sides ,
1451
- detrend = 'mean' )
1452
- spec_b , fsp_b = mlab .psd (x = ydatab ,
1453
- NFFT = self .NFFT_density ,
1454
- Fs = self .Fs ,
1455
- noverlap = 0 ,
1456
- sides = self .sides ,
1457
- detrend = 'mean' )
1458
- spec_c , fsp_c = mlab .psd (x = ycontrol ,
1459
- NFFT = self .NFFT_density ,
1460
- Fs = self .Fs ,
1461
- noverlap = 0 ,
1462
- sides = self .sides )
1463
- assert_array_equal (fsp_g , fsp_c )
1464
- assert_array_equal (fsp_b , fsp_c )
1465
- assert_allclose (spec_g , spec_c , atol = 1e-08 )
1466
- # these should not be almost equal
1467
- with pytest .raises (AssertionError ):
1468
- assert_allclose (spec_b , spec_c , atol = 1e-08 )
1469
-
1470
- def test_psd_detrend_linear_func_trend (self ):
1471
- if self .NFFT_density is None :
1472
- return
1473
- ydata = np .arange (self .NFFT_density )
1474
- ydata1 = ydata + 5
1475
- ydata2 = ydata + 3.3
1476
- ydata = np .vstack ([ydata1 , ydata2 ])
1477
- ydata = np .tile (ydata , (20 , 1 ))
1478
- ydatab = ydata .T .flatten ()
1479
- ydata = ydata .flatten ()
1480
- ycontrol = np .zeros_like (ydata )
1481
- spec_g , fsp_g = mlab .psd (x = ydata ,
1482
- NFFT = self .NFFT_density ,
1483
- Fs = self .Fs ,
1484
- noverlap = 0 ,
1485
- sides = self .sides ,
1486
- detrend = mlab .detrend_linear )
1487
- spec_b , fsp_b = mlab .psd (x = ydatab ,
1488
- NFFT = self .NFFT_density ,
1489
- Fs = self .Fs ,
1490
- noverlap = 0 ,
1491
- sides = self .sides ,
1492
- detrend = mlab .detrend_linear )
1493
- spec_c , fsp_c = mlab .psd (x = ycontrol ,
1494
- NFFT = self .NFFT_density ,
1495
- Fs = self .Fs ,
1496
- noverlap = 0 ,
1497
- sides = self .sides )
1498
- assert_array_equal (fsp_g , fsp_c )
1499
- assert_array_equal (fsp_b , fsp_c )
1500
- assert_allclose (spec_g , spec_c , atol = 1e-08 )
1501
- # these should not be almost equal
1502
- with pytest .raises (AssertionError ):
1503
- assert_allclose (spec_b , spec_c , atol = 1e-08 )
1504
-
1505
- def test_psd_detrend_linear_str_trend (self ):
1506
- if self .NFFT_density is None :
1507
- return
1508
- ydata = np .arange (self .NFFT_density )
1509
- ydata1 = ydata + 5
1510
- ydata2 = ydata + 3.3
1511
- ydata = np .vstack ([ydata1 , ydata2 ])
1512
- ydata = np .tile (ydata , (20 , 1 ))
1513
- ydatab = ydata .T .flatten ()
1514
- ydata = ydata .flatten ()
1515
- ycontrol = np .zeros_like (ydata )
1516
- spec_g , fsp_g = mlab .psd (x = ydata ,
1517
- NFFT = self .NFFT_density ,
1518
- Fs = self .Fs ,
1519
- noverlap = 0 ,
1520
- sides = self .sides ,
1521
- detrend = 'linear' )
1522
- spec_b , fsp_b = mlab .psd (x = ydatab ,
1523
- NFFT = self .NFFT_density ,
1524
- Fs = self .Fs ,
1525
- noverlap = 0 ,
1526
- sides = self .sides ,
1527
- detrend = 'linear' )
1407
+ detrend = detrend )
1528
1408
spec_c , fsp_c = mlab .psd (x = ycontrol ,
1529
1409
NFFT = self .NFFT_density ,
1530
1410
Fs = self .Fs ,
@@ -1710,145 +1590,37 @@ def test_phase_spectrum(self):
1710
1590
assert_allclose (fsp , freqs , atol = 1e-06 )
1711
1591
assert spec .shape == freqs .shape
1712
1592
1713
- def test_specgram_auto (self ):
1714
- freqs = self .freqs_specgram
1715
- spec , fsp , t = mlab .specgram (x = self .y ,
1716
- NFFT = self .NFFT_specgram ,
1717
- Fs = self .Fs ,
1718
- noverlap = self .nover_specgram ,
1719
- pad_to = self .pad_to_specgram ,
1720
- sides = self .sides )
1721
- specm = np .mean (spec , axis = 1 )
1722
-
1723
- assert_allclose (fsp , freqs , atol = 1e-06 )
1724
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1725
-
1726
- assert spec .shape [0 ] == freqs .shape [0 ]
1727
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1728
-
1729
- # since we are using a single freq, all time slices
1730
- # should be about the same
1731
- if np .abs (spec .max ()) != 0 :
1732
- assert_allclose (np .diff (spec , axis = 1 ).max ()/ np .abs (spec .max ()), 0 ,
1733
- atol = 1e-02 )
1734
- self .check_freqs (specm , freqs , fsp , self .fstims )
1735
-
1736
- def test_specgram_default (self ):
1737
- freqs = self .freqs_specgram
1738
- spec , fsp , t = mlab .specgram (x = self .y ,
1739
- NFFT = self .NFFT_specgram ,
1740
- Fs = self .Fs ,
1741
- noverlap = self .nover_specgram ,
1742
- pad_to = self .pad_to_specgram ,
1743
- sides = self .sides ,
1744
- mode = 'default' )
1745
- specm = np .mean (spec , axis = 1 )
1746
-
1747
- assert_allclose (fsp , freqs , atol = 1e-06 )
1748
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1749
-
1750
- assert spec .shape [0 ] == freqs .shape [0 ]
1751
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1752
-
1753
- # since we are using a single freq, all time slices
1754
- # should be about the same
1755
- if np .abs (spec .max ()) != 0 :
1756
- assert_allclose (np .diff (spec , axis = 1 ).max ()/ np .abs (spec .max ()), 0 ,
1757
- atol = 1e-02 )
1758
- self .check_freqs (specm , freqs , fsp , self .fstims )
1759
-
1760
- def test_specgram_psd (self ):
1761
- freqs = self .freqs_specgram
1762
- spec , fsp , t = mlab .specgram (x = self .y ,
1763
- NFFT = self .NFFT_specgram ,
1764
- Fs = self .Fs ,
1765
- noverlap = self .nover_specgram ,
1766
- pad_to = self .pad_to_specgram ,
1767
- sides = self .sides ,
1768
- mode = 'psd' )
1769
- specm = np .mean (spec , axis = 1 )
1770
-
1771
- assert_allclose (fsp , freqs , atol = 1e-06 )
1772
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1773
-
1774
- assert spec .shape [0 ] == freqs .shape [0 ]
1775
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1776
- # since we are using a single freq, all time slices
1777
- # should be about the same
1778
- if np .abs (spec .max ()) != 0 :
1779
- assert_allclose (np .diff (spec , axis = 1 ).max ()/ np .abs (spec .max ()), 0 ,
1780
- atol = 1e-02 )
1781
- self .check_freqs (specm , freqs , fsp , self .fstims )
1782
-
1783
- def test_specgram_complex (self ):
1784
- freqs = self .freqs_specgram
1785
- spec , fsp , t = mlab .specgram (x = self .y ,
1786
- NFFT = self .NFFT_specgram ,
1787
- Fs = self .Fs ,
1788
- noverlap = self .nover_specgram ,
1789
- pad_to = self .pad_to_specgram ,
1790
- sides = self .sides ,
1791
- mode = 'complex' )
1792
- specm = np .mean (np .abs (spec ), axis = 1 )
1793
- assert_allclose (fsp , freqs , atol = 1e-06 )
1794
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1795
-
1796
- assert spec .shape [0 ] == freqs .shape [0 ]
1797
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1798
-
1799
- self .check_freqs (specm , freqs , fsp , self .fstims )
1800
-
1801
- def test_specgram_magnitude (self ):
1593
+ @pytest .mark .parametrize (
1594
+ 'kwargs' ,
1595
+ [{}, {'mode' : 'default' }, {'mode' : 'psd' }, {'mode' : 'magnitude' },
1596
+ {'mode' : 'complex' }, {'mode' : 'angle' }, {'mode' : 'phase' }])
1597
+ def test_specgram (self , kwargs ):
1802
1598
freqs = self .freqs_specgram
1803
1599
spec , fsp , t = mlab .specgram (x = self .y ,
1804
1600
NFFT = self .NFFT_specgram ,
1805
1601
Fs = self .Fs ,
1806
1602
noverlap = self .nover_specgram ,
1807
1603
pad_to = self .pad_to_specgram ,
1808
1604
sides = self .sides ,
1809
- mode = 'magnitude' )
1605
+ ** kwargs )
1606
+ if kwargs .get ('mode' ) == 'complex' :
1607
+ spec = np .abs (spec )
1810
1608
specm = np .mean (spec , axis = 1 )
1811
- assert_allclose (fsp , freqs , atol = 1e-06 )
1812
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1813
1609
1814
- assert spec .shape [0 ] == freqs .shape [0 ]
1815
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1816
- # since we are using a single freq, all time slices
1817
- # should be about the same
1818
- if np .abs (spec .max ()) != 0 :
1819
- assert_allclose (np .diff (spec , axis = 1 ).max ()/ np .abs (spec .max ()), 0 ,
1820
- atol = 1e-02 )
1821
- self .check_freqs (specm , freqs , fsp , self .fstims )
1822
-
1823
- def test_specgram_angle (self ):
1824
- freqs = self .freqs_specgram
1825
- spec , fsp , t = mlab .specgram (x = self .y ,
1826
- NFFT = self .NFFT_specgram ,
1827
- Fs = self .Fs ,
1828
- noverlap = self .nover_specgram ,
1829
- pad_to = self .pad_to_specgram ,
1830
- sides = self .sides ,
1831
- mode = 'angle' )
1832
1610
assert_allclose (fsp , freqs , atol = 1e-06 )
1833
1611
assert_allclose (t , self .t_specgram , atol = 1e-06 )
1834
1612
1835
1613
assert spec .shape [0 ] == freqs .shape [0 ]
1836
1614
assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1837
1615
1838
- def test_specgram_phase (self ):
1839
- freqs = self .freqs_specgram
1840
- spec , fsp , t = mlab .specgram (x = self .y ,
1841
- NFFT = self .NFFT_specgram ,
1842
- Fs = self .Fs ,
1843
- noverlap = self .nover_specgram ,
1844
- pad_to = self .pad_to_specgram ,
1845
- sides = self .sides ,
1846
- mode = 'phase' )
1847
- assert_allclose (fsp , freqs , atol = 1e-06 )
1848
- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1849
-
1850
- assert spec .shape [0 ] == freqs .shape [0 ]
1851
- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1616
+ if kwargs .get ('mode' ) not in ['complex' , 'angle' , 'phase' ]:
1617
+ # using a single freq, so all time slices should be about the same
1618
+ if np .abs (spec .max ()) != 0 :
1619
+ assert_allclose (
1620
+ np .diff (spec , axis = 1 ).max () / np .abs (spec .max ()), 0 ,
1621
+ atol = 1e-02 )
1622
+ if kwargs .get ('mode' ) not in ['angle' , 'phase' ]:
1623
+ self .check_freqs (specm , freqs , fsp , self .fstims )
1852
1624
1853
1625
def test_specgram_warn_only1seg (self ):
1854
1626
"""Warning should be raised if len(x) <= NFFT."""
0 commit comments