@@ -1259,25 +1259,11 @@ def check_maxfreq(self, spec, fsp, fstims):
12591259 del fstimst [- 1 ]
12601260 spect [maxind - 5 :maxind + 5 ] = 0
12611261
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 ):
12641265 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 )
12811267
12821268 def test_spectral_helper_raises_unknown_mode (self ):
12831269 # test that unknown value for mode cannot be used
@@ -1305,15 +1291,10 @@ def test_spectral_helper_raises_winlen_ne_NFFT(self):
13051291 mlab ._spectral_helper (x = self .y , y = self .y , NFFT = 10 ,
13061292 window = np .ones (9 ))
13071293
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 ):
13101296 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 )
13171298
13181299 def test_spectral_helper_psd (self ):
13191300 freqs = self .freqs_density
@@ -1397,10 +1378,14 @@ def test_psd(self):
13971378 assert spec .shape == freqs .shape
13981379 self .check_freqs (spec , freqs , fsp , self .fstims )
13991380
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 ):
14011386 if self .NFFT_density is None :
14021387 return
1403- ydata = np . zeros (self .NFFT_density )
1388+ ydata = make_data (self .NFFT_density )
14041389 ydata1 = ydata + 5
14051390 ydata2 = ydata + 3.3
14061391 ydata = np .vstack ([ydata1 , ydata2 ])
@@ -1413,118 +1398,13 @@ def test_psd_detrend_mean_func_offset(self):
14131398 Fs = self .Fs ,
14141399 noverlap = 0 ,
14151400 sides = self .sides ,
1416- detrend = mlab . detrend_mean )
1401+ detrend = detrend )
14171402 spec_b , fsp_b = mlab .psd (x = ydatab ,
14181403 NFFT = self .NFFT_density ,
14191404 Fs = self .Fs ,
14201405 noverlap = 0 ,
14211406 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 )
15281408 spec_c , fsp_c = mlab .psd (x = ycontrol ,
15291409 NFFT = self .NFFT_density ,
15301410 Fs = self .Fs ,
@@ -1710,145 +1590,37 @@ def test_phase_spectrum(self):
17101590 assert_allclose (fsp , freqs , atol = 1e-06 )
17111591 assert spec .shape == freqs .shape
17121592
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 ):
18021598 freqs = self .freqs_specgram
18031599 spec , fsp , t = mlab .specgram (x = self .y ,
18041600 NFFT = self .NFFT_specgram ,
18051601 Fs = self .Fs ,
18061602 noverlap = self .nover_specgram ,
18071603 pad_to = self .pad_to_specgram ,
18081604 sides = self .sides ,
1809- mode = 'magnitude' )
1605+ ** kwargs )
1606+ if kwargs .get ('mode' ) == 'complex' :
1607+ spec = np .abs (spec )
18101608 specm = np .mean (spec , axis = 1 )
1811- assert_allclose (fsp , freqs , atol = 1e-06 )
1812- assert_allclose (t , self .t_specgram , atol = 1e-06 )
18131609
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' )
18321610 assert_allclose (fsp , freqs , atol = 1e-06 )
18331611 assert_allclose (t , self .t_specgram , atol = 1e-06 )
18341612
18351613 assert spec .shape [0 ] == freqs .shape [0 ]
18361614 assert spec .shape [1 ] == self .t_specgram .shape [0 ]
18371615
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 )
18521624
18531625 def test_specgram_warn_only1seg (self ):
18541626 """Warning should be raised if len(x) <= NFFT."""
0 commit comments