@@ -134,25 +134,6 @@ def test_label_shift():
134
134
assert ax .yaxis .get_label ().get_horizontalalignment ()== "center"
135
135
136
136
137
- @check_figures_equal (extensions = ["png" ])
138
- def test_acorr (fig_test ,fig_ref ):
139
- np .random .seed (19680801 )
140
- Nx = 512
141
- x = np .random .normal (0 ,1 ,Nx ).cumsum ()
142
- maxlags = Nx - 1
143
-
144
- ax_test = fig_test .subplots ()
145
- ax_test .acorr (x ,maxlags = maxlags )
146
-
147
- ax_ref = fig_ref .subplots ()
148
- # Normalized autocorrelation
149
- norm_auto_corr = np .correlate (x ,x ,mode = "full" )/ np .dot (x ,x )
150
- lags = np .arange (- maxlags ,maxlags + 1 )
151
- norm_auto_corr = norm_auto_corr [Nx - 1 - maxlags :Nx + maxlags ]
152
- ax_ref .vlines (lags , [0 ],norm_auto_corr )
153
- ax_ref .axhline (y = 0 ,xmin = 0 ,xmax = 1 )
154
-
155
-
156
137
@check_figures_equal (extensions = ["png" ])
157
138
def test_spy (fig_test ,fig_ref ):
158
139
np .random .seed (19680801 )
@@ -4623,237 +4604,6 @@ def test_subplot_key_hash():
4623
4604
assert ax .get_subplotspec ().get_geometry ()== (5 ,1 ,0 ,0 )
4624
4605
4625
4606
4626
- @image_comparison (
4627
- ["specgram_freqs.png" ,"specgram_freqs_linear.png" ,
4628
- "specgram_noise.png" ,"specgram_noise_linear.png" ],
4629
- remove_text = True ,tol = 0.07 ,style = "default" )
4630
- def test_specgram ():
4631
- """Test axes.specgram in default (psd) mode."""
4632
-
4633
- # use former defaults to match existing baseline image
4634
- matplotlib .rcParams ['image.interpolation' ]= 'nearest'
4635
-
4636
- n = 1000
4637
- Fs = 10.
4638
-
4639
- fstims = [[Fs / 4 ,Fs / 5 ,Fs / 11 ], [Fs / 4.7 ,Fs / 5.6 ,Fs / 11.9 ]]
4640
- NFFT_freqs = int (10 * Fs / np .min (fstims ))
4641
- x = np .arange (0 ,n ,1 / Fs )
4642
- y_freqs = np .concatenate (
4643
- np .sin (2 * np .pi * np .multiply .outer (fstims ,x )).sum (axis = 1 ))
4644
-
4645
- NFFT_noise = int (10 * Fs / 11 )
4646
- np .random .seed (0 )
4647
- y_noise = np .concatenate ([np .random .standard_normal (n ),np .random .rand (n )])
4648
-
4649
- all_sides = ["default" ,"onesided" ,"twosided" ]
4650
- for y ,NFFT in [(y_freqs ,NFFT_freqs ), (y_noise ,NFFT_noise )]:
4651
- noverlap = NFFT // 2
4652
- pad_to = int (2 ** np .ceil (np .log2 (NFFT )))
4653
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4654
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4655
- pad_to = pad_to ,sides = sides )
4656
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4657
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4658
- pad_to = pad_to ,sides = sides ,
4659
- scale = "linear" ,norm = matplotlib .colors .LogNorm ())
4660
-
4661
-
4662
- @image_comparison (
4663
- ["specgram_magnitude_freqs.png" ,"specgram_magnitude_freqs_linear.png" ,
4664
- "specgram_magnitude_noise.png" ,"specgram_magnitude_noise_linear.png" ],
4665
- remove_text = True ,tol = 0.07 ,style = "default" )
4666
- def test_specgram_magnitude ():
4667
- """Test axes.specgram in magnitude mode."""
4668
-
4669
- # use former defaults to match existing baseline image
4670
- matplotlib .rcParams ['image.interpolation' ]= 'nearest'
4671
-
4672
- n = 1000
4673
- Fs = 10.
4674
-
4675
- fstims = [[Fs / 4 ,Fs / 5 ,Fs / 11 ], [Fs / 4.7 ,Fs / 5.6 ,Fs / 11.9 ]]
4676
- NFFT_freqs = int (100 * Fs / np .min (fstims ))
4677
- x = np .arange (0 ,n ,1 / Fs )
4678
- y = np .sin (2 * np .pi * np .multiply .outer (fstims ,x )).sum (axis = 1 )
4679
- y [:,- 1 ]= 1
4680
- y_freqs = np .hstack (y )
4681
-
4682
- NFFT_noise = int (10 * Fs / 11 )
4683
- np .random .seed (0 )
4684
- y_noise = np .concatenate ([np .random .standard_normal (n ),np .random .rand (n )])
4685
-
4686
- all_sides = ["default" ,"onesided" ,"twosided" ]
4687
- for y ,NFFT in [(y_freqs ,NFFT_freqs ), (y_noise ,NFFT_noise )]:
4688
- noverlap = NFFT // 2
4689
- pad_to = int (2 ** np .ceil (np .log2 (NFFT )))
4690
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4691
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4692
- pad_to = pad_to ,sides = sides ,mode = "magnitude" )
4693
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4694
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4695
- pad_to = pad_to ,sides = sides ,mode = "magnitude" ,
4696
- scale = "linear" ,norm = matplotlib .colors .LogNorm ())
4697
-
4698
-
4699
- @image_comparison (
4700
- ["specgram_angle_freqs.png" ,"specgram_phase_freqs.png" ,
4701
- "specgram_angle_noise.png" ,"specgram_phase_noise.png" ],
4702
- remove_text = True ,tol = 0.07 ,style = "default" )
4703
- def test_specgram_angle ():
4704
- """Test axes.specgram in angle and phase modes."""
4705
-
4706
- # use former defaults to match existing baseline image
4707
- matplotlib .rcParams ['image.interpolation' ]= 'nearest'
4708
-
4709
- n = 1000
4710
- Fs = 10.
4711
-
4712
- fstims = [[Fs / 4 ,Fs / 5 ,Fs / 11 ], [Fs / 4.7 ,Fs / 5.6 ,Fs / 11.9 ]]
4713
- NFFT_freqs = int (10 * Fs / np .min (fstims ))
4714
- x = np .arange (0 ,n ,1 / Fs )
4715
- y = np .sin (2 * np .pi * np .multiply .outer (fstims ,x )).sum (axis = 1 )
4716
- y [:,- 1 ]= 1
4717
- y_freqs = np .hstack (y )
4718
-
4719
- NFFT_noise = int (10 * Fs / 11 )
4720
- np .random .seed (0 )
4721
- y_noise = np .concatenate ([np .random .standard_normal (n ),np .random .rand (n )])
4722
-
4723
- all_sides = ["default" ,"onesided" ,"twosided" ]
4724
- for y ,NFFT in [(y_freqs ,NFFT_freqs ), (y_noise ,NFFT_noise )]:
4725
- noverlap = NFFT // 2
4726
- pad_to = int (2 ** np .ceil (np .log2 (NFFT )))
4727
- for mode in ["angle" ,"phase" ]:
4728
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4729
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4730
- pad_to = pad_to ,sides = sides ,mode = mode )
4731
- with pytest .raises (ValueError ):
4732
- ax .specgram (y ,NFFT = NFFT ,Fs = Fs ,noverlap = noverlap ,
4733
- pad_to = pad_to ,sides = sides ,mode = mode ,
4734
- scale = "dB" )
4735
-
4736
-
4737
- def test_specgram_fs_none ():
4738
- """Test axes.specgram when Fs is None, should not throw error."""
4739
- spec ,freqs ,t ,im = plt .specgram (np .ones (300 ),Fs = None ,scale = 'linear' )
4740
- xmin ,xmax ,freq0 ,freq1 = im .get_extent ()
4741
- assert xmin == 32 and xmax == 96
4742
-
4743
-
4744
- @check_figures_equal (extensions = ["png" ])
4745
- def test_specgram_origin_rcparam (fig_test ,fig_ref ):
4746
- """Test specgram ignores image.origin rcParam and uses origin 'upper'."""
4747
- t = np .arange (500 )
4748
- signal = np .sin (t )
4749
-
4750
- plt .rcParams ["image.origin" ]= 'upper'
4751
-
4752
- # Reference: First graph using default origin in imshow (upper),
4753
- fig_ref .subplots ().specgram (signal )
4754
-
4755
- # Try to overwrite the setting trying to flip the specgram
4756
- plt .rcParams ["image.origin" ]= 'lower'
4757
-
4758
- # Test: origin='lower' should be ignored
4759
- fig_test .subplots ().specgram (signal )
4760
-
4761
-
4762
- def test_specgram_origin_kwarg ():
4763
- """Ensure passing origin as a kwarg raises a TypeError."""
4764
- t = np .arange (500 )
4765
- signal = np .sin (t )
4766
-
4767
- with pytest .raises (TypeError ):
4768
- plt .specgram (signal ,origin = 'lower' )
4769
-
4770
-
4771
- @image_comparison (
4772
- ["psd_freqs.png" ,"csd_freqs.png" ,"psd_noise.png" ,"csd_noise.png" ],
4773
- remove_text = True ,tol = 0.002 )
4774
- def test_psd_csd ():
4775
- n = 10000
4776
- Fs = 100.
4777
-
4778
- fstims = [[Fs / 4 ,Fs / 5 ,Fs / 11 ], [Fs / 4.7 ,Fs / 5.6 ,Fs / 11.9 ]]
4779
- NFFT_freqs = int (1000 * Fs / np .min (fstims ))
4780
- x = np .arange (0 ,n ,1 / Fs )
4781
- ys_freqs = np .sin (2 * np .pi * np .multiply .outer (fstims ,x )).sum (axis = 1 )
4782
-
4783
- NFFT_noise = int (1000 * Fs / 11 )
4784
- np .random .seed (0 )
4785
- ys_noise = [np .random .standard_normal (n ),np .random .rand (n )]
4786
-
4787
- all_kwargs = [{"sides" :"default" },
4788
- {"sides" :"onesided" ,"return_line" :False },
4789
- {"sides" :"twosided" ,"return_line" :True }]
4790
- for ys ,NFFT in [(ys_freqs ,NFFT_freqs ), (ys_noise ,NFFT_noise )]:
4791
- noverlap = NFFT // 2
4792
- pad_to = int (2 ** np .ceil (np .log2 (NFFT )))
4793
- for ax ,kwargs in zip (plt .figure ().subplots (3 ),all_kwargs ):
4794
- ret = ax .psd (np .concatenate (ys ),NFFT = NFFT ,Fs = Fs ,
4795
- noverlap = noverlap ,pad_to = pad_to ,** kwargs )
4796
- assert len (ret )== 2 + kwargs .get ("return_line" ,False )
4797
- ax .set (xlabel = "" ,ylabel = "" )
4798
- for ax ,kwargs in zip (plt .figure ().subplots (3 ),all_kwargs ):
4799
- ret = ax .csd (* ys ,NFFT = NFFT ,Fs = Fs ,
4800
- noverlap = noverlap ,pad_to = pad_to ,** kwargs )
4801
- assert len (ret )== 2 + kwargs .get ("return_line" ,False )
4802
- ax .set (xlabel = "" ,ylabel = "" )
4803
-
4804
-
4805
- @image_comparison (
4806
- ["magnitude_spectrum_freqs_linear.png" ,
4807
- "magnitude_spectrum_freqs_dB.png" ,
4808
- "angle_spectrum_freqs.png" ,
4809
- "phase_spectrum_freqs.png" ,
4810
- "magnitude_spectrum_noise_linear.png" ,
4811
- "magnitude_spectrum_noise_dB.png" ,
4812
- "angle_spectrum_noise.png" ,
4813
- "phase_spectrum_noise.png" ],
4814
- remove_text = True )
4815
- def test_spectrum ():
4816
- n = 10000
4817
- Fs = 100.
4818
-
4819
- fstims1 = [Fs / 4 ,Fs / 5 ,Fs / 11 ]
4820
- NFFT = int (1000 * Fs / min (fstims1 ))
4821
- pad_to = int (2 ** np .ceil (np .log2 (NFFT )))
4822
-
4823
- x = np .arange (0 ,n ,1 / Fs )
4824
- y_freqs = ((np .sin (2 * np .pi * np .outer (x ,fstims1 ))* 10 ** np .arange (3 ))
4825
- .sum (axis = 1 ))
4826
- np .random .seed (0 )
4827
- y_noise = np .hstack ([np .random .standard_normal (n ),np .random .rand (n )])- .5
4828
-
4829
- all_sides = ["default" ,"onesided" ,"twosided" ]
4830
- kwargs = {"Fs" :Fs ,"pad_to" :pad_to }
4831
- for y in [y_freqs ,y_noise ]:
4832
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4833
- spec ,freqs ,line = ax .magnitude_spectrum (y ,sides = sides ,** kwargs )
4834
- ax .set (xlabel = "" ,ylabel = "" )
4835
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4836
- spec ,freqs ,line = ax .magnitude_spectrum (y ,sides = sides ,** kwargs ,
4837
- scale = "dB" )
4838
- ax .set (xlabel = "" ,ylabel = "" )
4839
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4840
- spec ,freqs ,line = ax .angle_spectrum (y ,sides = sides ,** kwargs )
4841
- ax .set (xlabel = "" ,ylabel = "" )
4842
- for ax ,sides in zip (plt .figure ().subplots (3 ),all_sides ):
4843
- spec ,freqs ,line = ax .phase_spectrum (y ,sides = sides ,** kwargs )
4844
- ax .set (xlabel = "" ,ylabel = "" )
4845
-
4846
-
4847
- def test_psd_csd_edge_cases ():
4848
- # Inverted yaxis or fully zero inputs used to throw exceptions.
4849
- axs = plt .figure ().subplots (2 )
4850
- for ax in axs :
4851
- ax .yaxis .set (inverted = True )
4852
- with np .errstate (divide = "ignore" ):
4853
- axs [0 ].psd (np .zeros (5 ))
4854
- axs [1 ].csd (np .zeros (5 ),np .zeros (5 ))
4855
-
4856
-
4857
4607
@check_figures_equal (extensions = ['png' ])
4858
4608
def test_twin_remove (fig_test ,fig_ref ):
4859
4609
ax_test = fig_test .add_subplot ()