@@ -1214,90 +1214,48 @@ def check_maxfreq(self, spec, fsp, fstims):
12141214 del fstimst [- 1 ]
12151215 spect [maxind - 5 :maxind + 5 ] = 0
12161216
1217- @pytest .mark .parametrize ( # Modes that require `x is y`.
1218- 'mode' , ['complex' , 'magnitude' , 'angle' , 'phase' ])
1219- def test_spectral_helper_mode_requires_x_is_y (self , mode ):
1220- with pytest .raises (ValueError ):
1221- mlab ._spectral_helper (x = self .y , y = self .y + 1 , mode = mode )
1222-
1223- def test_spectral_helper_raises_unknown_mode (self ):
1224- # test that unknown value for mode cannot be used
1225- with pytest .raises (ValueError ):
1226- mlab ._spectral_helper (x = self .y , mode = 'spam' )
1227-
1228- def test_spectral_helper_raises_unknown_sides (self ):
1229- # test that unknown value for sides cannot be used
1230- with pytest .raises (ValueError ):
1231- mlab ._spectral_helper (x = self .y , y = self .y , sides = 'eggs' )
1232-
1233- def test_spectral_helper_raises_noverlap_gt_NFFT (self ):
1234- # test that noverlap cannot be larger than NFFT
1235- with pytest .raises (ValueError ):
1236- mlab ._spectral_helper (x = self .y , y = self .y , NFFT = 10 , noverlap = 20 )
1237-
1238- def test_spectral_helper_raises_noverlap_eq_NFFT (self ):
1239- # test that noverlap cannot be equal to NFFT
1240- with pytest .raises (ValueError ):
1241- mlab ._spectral_helper (x = self .y , NFFT = 10 , noverlap = 10 )
1242-
1243- def test_spectral_helper_raises_winlen_ne_NFFT (self ):
1244- # test that the window length cannot be different from NFFT
1245- with pytest .raises (ValueError ):
1246- mlab ._spectral_helper (x = self .y , y = self .y , NFFT = 10 ,
1247- window = np .ones (9 ))
1217+ def test_spectral_helper_raises (self ):
1218+ # We don't use parametrize here to handle ``y = self.y``.
1219+ for kwargs in [ # Various error conditions:
1220+ {"y" : self .y + 1 , "mode" : "complex" }, # Modes requiring ``x is y``.
1221+ {"y" : self .y + 1 , "mode" : "magnitude" },
1222+ {"y" : self .y + 1 , "mode" : "angle" },
1223+ {"y" : self .y + 1 , "mode" : "phase" },
1224+ {"mode" : "spam" }, # Bad mode.
1225+ {"y" : self .y , "sides" : "eggs" }, # Bad sides.
1226+ {"NFFT" : 10 , "noverlap" : 20 }, # noverlap > NFFT.
1227+ {"y" : self .y , "NFFT" : 10 , "noverlap" : 20 }, # noverlap == NFFT.
1228+ {"y" : self .y , "NFFT" : 10 ,
1229+ "window" : np .ones (9 )}, # len(win) != NFFT.
1230+ ]:
1231+ with pytest .raises (ValueError ):
1232+ mlab ._spectral_helper (x = self .y , ** kwargs )
12481233
12491234 @pytest .mark .parametrize ('mode' , ['default' , 'psd' ])
12501235 def test_single_spectrum_helper_unsupported_modes (self , mode ):
12511236 with pytest .raises (ValueError ):
12521237 mlab ._single_spectrum_helper (x = self .y , mode = mode )
12531238
1254- def test_spectral_helper_psd (self ):
1255- freqs = self .freqs_density
1256- spec , fsp , t = mlab ._spectral_helper (x = self .y , y = self .y ,
1257- NFFT = self .NFFT_density ,
1258- Fs = self .Fs ,
1259- noverlap = self .nover_density ,
1260- pad_to = self .pad_to_density ,
1261- sides = self .sides ,
1262- mode = 'psd' )
1239+ @pytest .mark .parametrize ("mode, case" , [
1240+ ("psd" , "density" ),
1241+ ("magnitude" , "specgram" ),
1242+ ("magnitude" , "spectrum" ),
1243+ ])
1244+ def test_spectral_helper_psd (self , mode , case ):
1245+ freqs = getattr (self , f"freqs_{ case } " )
1246+ spec , fsp , t = mlab ._spectral_helper (
1247+ x = self .y , y = self .y ,
1248+ NFFT = getattr (self , f"NFFT_{ case } " ),
1249+ Fs = self .Fs ,
1250+ noverlap = getattr (self , f"nover_{ case } " ),
1251+ pad_to = getattr (self , f"pad_to_{ case } " ),
1252+ sides = self .sides ,
1253+ mode = mode )
12631254
12641255 assert_allclose (fsp , freqs , atol = 1e-06 )
1265- assert_allclose (t , self .t_density , atol = 1e-06 )
1266-
1256+ assert_allclose (t , getattr (self , f"t_{ case } " ), atol = 1e-06 )
12671257 assert spec .shape [0 ] == freqs .shape [0 ]
1268- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1269-
1270- def test_spectral_helper_magnitude_specgram (self ):
1271- freqs = self .freqs_specgram
1272- spec , fsp , t = mlab ._spectral_helper (x = self .y , y = self .y ,
1273- NFFT = self .NFFT_specgram ,
1274- Fs = self .Fs ,
1275- noverlap = self .nover_specgram ,
1276- pad_to = self .pad_to_specgram ,
1277- sides = self .sides ,
1278- mode = 'magnitude' )
1279-
1280- assert_allclose (fsp , freqs , atol = 1e-06 )
1281- assert_allclose (t , self .t_specgram , atol = 1e-06 )
1282-
1283- assert spec .shape [0 ] == freqs .shape [0 ]
1284- assert spec .shape [1 ] == self .t_specgram .shape [0 ]
1285-
1286- def test_spectral_helper_magnitude_magnitude_spectrum (self ):
1287- freqs = self .freqs_spectrum
1288- spec , fsp , t = mlab ._spectral_helper (x = self .y , y = self .y ,
1289- NFFT = self .NFFT_spectrum ,
1290- Fs = self .Fs ,
1291- noverlap = self .nover_spectrum ,
1292- pad_to = self .pad_to_spectrum ,
1293- sides = self .sides ,
1294- mode = 'magnitude' )
1295-
1296- assert_allclose (fsp , freqs , atol = 1e-06 )
1297- assert_allclose (t , self .t_spectrum , atol = 1e-06 )
1298-
1299- assert spec .shape [0 ] == freqs .shape [0 ]
1300- assert spec .shape [1 ] == 1
1258+ assert spec .shape [1 ] == getattr (self , f"t_{ case } " ).shape [0 ]
13011259
13021260 def test_csd (self ):
13031261 freqs = self .freqs_density
@@ -1508,42 +1466,17 @@ def test_psd_windowarray_scale_by_freq(self):
15081466 spec_n / self .Fs * win .sum ()** 2 ,
15091467 atol = 1e-08 )
15101468
1511- def test_complex_spectrum (self ):
1512- freqs = self .freqs_spectrum
1513- spec , fsp = mlab .complex_spectrum (x = self .y ,
1514- Fs = self .Fs ,
1515- sides = self .sides ,
1516- pad_to = self .pad_to_spectrum )
1517- assert_allclose (fsp , freqs , atol = 1e-06 )
1518- assert spec .shape == freqs .shape
1519-
1520- def test_magnitude_spectrum (self ):
1521- freqs = self .freqs_spectrum
1522- spec , fsp = mlab .magnitude_spectrum (x = self .y ,
1523- Fs = self .Fs ,
1524- sides = self .sides ,
1525- pad_to = self .pad_to_spectrum )
1526- assert spec .shape == freqs .shape
1527- self .check_maxfreq (spec , fsp , self .fstims )
1528- self .check_freqs (spec , freqs , fsp , self .fstims )
1529-
1530- def test_angle_spectrum (self ):
1531- freqs = self .freqs_spectrum
1532- spec , fsp = mlab .angle_spectrum (x = self .y ,
1533- Fs = self .Fs ,
1534- sides = self .sides ,
1535- pad_to = self .pad_to_spectrum )
1536- assert_allclose (fsp , freqs , atol = 1e-06 )
1537- assert spec .shape == freqs .shape
1538-
1539- def test_phase_spectrum (self ):
1469+ @pytest .mark .parametrize (
1470+ "kind" , ["complex" , "magnitude" , "angle" , "phase" ])
1471+ def test_spectrum (self , kind ):
15401472 freqs = self .freqs_spectrum
1541- spec , fsp = mlab .phase_spectrum (x = self .y ,
1542- Fs = self .Fs ,
1543- sides = self .sides ,
1544- pad_to = self .pad_to_spectrum )
1473+ spec , fsp = getattr (mlab , f"{ kind } _spectrum" )(
1474+ x = self .y ,
1475+ Fs = self .Fs , sides = self .sides , pad_to = self .pad_to_spectrum )
15451476 assert_allclose (fsp , freqs , atol = 1e-06 )
15461477 assert spec .shape == freqs .shape
1478+ if kind == "magnitude" :
1479+ self .check_maxfreq (spec , fsp , self .fstims )
15471480
15481481 @pytest .mark .parametrize (
15491482 'kwargs' ,
@@ -1598,7 +1531,8 @@ def test_psd_csd_equal(self):
15981531 assert_array_almost_equal_nulp (Pxx , Pxy )
15991532 assert_array_equal (freqsxx , freqsxy )
16001533
1601- def test_specgram_auto_default_equal (self ):
1534+ @pytest .mark .parametrize ("mode" , ["default" , "psd" ])
1535+ def test_specgram_auto_default_psd_equal (self , mode ):
16021536 """
16031537 Test that mlab.specgram without mode and with mode 'default' and 'psd'
16041538 are all the same.
@@ -1615,34 +1549,18 @@ def test_specgram_auto_default_equal(self):
16151549 noverlap = self .nover_specgram ,
16161550 pad_to = self .pad_to_specgram ,
16171551 sides = self .sides ,
1618- mode = 'default' )
1552+ mode = mode )
16191553 assert_array_equal (speca , specb )
16201554 assert_array_equal (freqspeca , freqspecb )
16211555 assert_array_equal (ta , tb )
16221556
1623- def test_specgram_auto_psd_equal (self ):
1624- """
1625- Test that mlab.specgram without mode and with mode 'default' and 'psd'
1626- are all the same.
1627- """
1628- speca , freqspeca , ta = mlab .specgram (x = self .y ,
1629- NFFT = self .NFFT_specgram ,
1630- Fs = self .Fs ,
1631- noverlap = self .nover_specgram ,
1632- pad_to = self .pad_to_specgram ,
1633- sides = self .sides )
1634- specc , freqspecc , tc = mlab .specgram (x = self .y ,
1635- NFFT = self .NFFT_specgram ,
1636- Fs = self .Fs ,
1637- noverlap = self .nover_specgram ,
1638- pad_to = self .pad_to_specgram ,
1639- sides = self .sides ,
1640- mode = 'psd' )
1641- assert_array_equal (speca , specc )
1642- assert_array_equal (freqspeca , freqspecc )
1643- assert_array_equal (ta , tc )
1644-
1645- def test_specgram_complex_mag_equivalent (self ):
1557+ @pytest .mark .parametrize (
1558+ "mode, conv" , [
1559+ ("magnitude" , np .abs ),
1560+ ("angle" , np .angle ),
1561+ ("phase" , lambda x : np .unwrap (np .angle (x ), axis = 0 ))
1562+ ])
1563+ def test_specgram_complex_equivalent (self , mode , conv ):
16461564 specc , freqspecc , tc = mlab .specgram (x = self .y ,
16471565 NFFT = self .NFFT_specgram ,
16481566 Fs = self .Fs ,
@@ -1656,73 +1574,11 @@ def test_specgram_complex_mag_equivalent(self):
16561574 noverlap = self .nover_specgram ,
16571575 pad_to = self .pad_to_specgram ,
16581576 sides = self .sides ,
1659- mode = 'magnitude' )
1577+ mode = mode )
16601578
16611579 assert_array_equal (freqspecc , freqspecm )
16621580 assert_array_equal (tc , tm )
1663- assert_allclose (np .abs (specc ), specm , atol = 1e-06 )
1664-
1665- def test_specgram_complex_angle_equivalent (self ):
1666- specc , freqspecc , tc = mlab .specgram (x = self .y ,
1667- NFFT = self .NFFT_specgram ,
1668- Fs = self .Fs ,
1669- noverlap = self .nover_specgram ,
1670- pad_to = self .pad_to_specgram ,
1671- sides = self .sides ,
1672- mode = 'complex' )
1673- speca , freqspeca , ta = mlab .specgram (x = self .y ,
1674- NFFT = self .NFFT_specgram ,
1675- Fs = self .Fs ,
1676- noverlap = self .nover_specgram ,
1677- pad_to = self .pad_to_specgram ,
1678- sides = self .sides ,
1679- mode = 'angle' )
1680-
1681- assert_array_equal (freqspecc , freqspeca )
1682- assert_array_equal (tc , ta )
1683- assert_allclose (np .angle (specc ), speca , atol = 1e-06 )
1684-
1685- def test_specgram_complex_phase_equivalent (self ):
1686- specc , freqspecc , tc = mlab .specgram (x = self .y ,
1687- NFFT = self .NFFT_specgram ,
1688- Fs = self .Fs ,
1689- noverlap = self .nover_specgram ,
1690- pad_to = self .pad_to_specgram ,
1691- sides = self .sides ,
1692- mode = 'complex' )
1693- specp , freqspecp , tp = mlab .specgram (x = self .y ,
1694- NFFT = self .NFFT_specgram ,
1695- Fs = self .Fs ,
1696- noverlap = self .nover_specgram ,
1697- pad_to = self .pad_to_specgram ,
1698- sides = self .sides ,
1699- mode = 'phase' )
1700-
1701- assert_array_equal (freqspecc , freqspecp )
1702- assert_array_equal (tc , tp )
1703- assert_allclose (np .unwrap (np .angle (specc ), axis = 0 ), specp ,
1704- atol = 1e-06 )
1705-
1706- def test_specgram_angle_phase_equivalent (self ):
1707- speca , freqspeca , ta = mlab .specgram (x = self .y ,
1708- NFFT = self .NFFT_specgram ,
1709- Fs = self .Fs ,
1710- noverlap = self .nover_specgram ,
1711- pad_to = self .pad_to_specgram ,
1712- sides = self .sides ,
1713- mode = 'angle' )
1714- specp , freqspecp , tp = mlab .specgram (x = self .y ,
1715- NFFT = self .NFFT_specgram ,
1716- Fs = self .Fs ,
1717- noverlap = self .nover_specgram ,
1718- pad_to = self .pad_to_specgram ,
1719- sides = self .sides ,
1720- mode = 'phase' )
1721-
1722- assert_array_equal (freqspeca , freqspecp )
1723- assert_array_equal (ta , tp )
1724- assert_allclose (np .unwrap (speca , axis = 0 ), specp ,
1725- atol = 1e-06 )
1581+ assert_allclose (conv (specc ), specm , atol = 1e-06 )
17261582
17271583 def test_psd_windowarray_equal (self ):
17281584 win = mlab .window_hanning (np .ones (self .NFFT_density_real ))
0 commit comments