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