1
- import numpy
1
+ import sys
2
+
3
+ import numpy
2
4
import matplotlib .pyplot as plt
3
5
6
+ if sys .version_info .major == 2 :
7
+ import mock
8
+ else :
9
+ from unittest import mock
4
10
import pytest
5
11
import numpy .testing as nptest
6
12
13
19
from probscale .probscale import _minimal_norm
14
20
15
21
22
+ BASELINE_DIR = 'baseline_images/test_viz'
23
+
24
+
16
25
class Test__fit_line (object ):
17
26
def setup (self ):
18
27
self .data = numpy .array ([
@@ -367,180 +376,139 @@ def plot_data():
367
376
return data
368
377
369
378
370
- @pytest .mark .mpl_image_compare (
371
- baseline_dir = 'baseline_images/test_viz' ,
372
- tolerance = 10
373
- )
379
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
374
380
def test_probplot_prob (plot_data ):
375
381
fig , ax = plt .subplots ()
376
- fig = viz .probplot (plot_data , ax = ax , xlabel = 'Test xlabel' , datascale = 'log' )
382
+ fig = viz .probplot (plot_data , ax = ax , problabel = 'Test xlabel' , datascale = 'log' )
377
383
assert isinstance (fig , plt .Figure )
378
384
return fig
379
385
380
386
381
- @pytest .mark .mpl_image_compare (
382
- baseline_dir = 'baseline_images/test_viz' ,
383
- tolerance = 10
384
- )
387
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
385
388
def test_probplot_qq (plot_data ):
386
389
fig , ax = plt .subplots ()
387
- fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , ylabel = 'Test label' ,
390
+ fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , datalabel = 'Test label' ,
388
391
datascale = 'log' , scatter_kws = dict (color = 'r' ))
389
392
return fig
390
393
391
394
392
- @pytest .mark .mpl_image_compare (
393
- baseline_dir = 'baseline_images/test_viz' ,
394
- tolerance = 10
395
- )
395
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
396
396
def test_probplot_pp (plot_data ):
397
397
fig , ax = plt .subplots ()
398
398
scatter_kws = dict (color = 'b' , linestyle = '--' , markeredgecolor = 'g' , markerfacecolor = 'none' )
399
399
fig = viz .probplot (plot_data , ax = ax , plottype = 'pp' , datascale = 'linear' ,
400
- xlabel = 'test x' , ylabel = 'test y' , scatter_kws = scatter_kws )
400
+ datalabel = 'test x' , problabel = 'test y' ,
401
+ scatter_kws = scatter_kws )
401
402
return fig
402
403
403
404
404
- @pytest .mark .mpl_image_compare (
405
- baseline_dir = 'baseline_images/test_viz' ,
406
- tolerance = 10
407
- )
405
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
408
406
def test_probplot_prob_bestfit (plot_data ):
409
407
fig , ax = plt .subplots ()
410
- fig = viz .probplot (plot_data , ax = ax , xlabel = 'Test xlabel' , bestfit = True , datascale = 'log' )
408
+ fig = viz .probplot (plot_data , ax = ax , datalabel = 'Test xlabel' , bestfit = True , datascale = 'log' )
411
409
assert isinstance (fig , plt .Figure )
412
410
return fig
413
411
414
412
415
- @pytest .mark .mpl_image_compare (
416
- baseline_dir = 'baseline_images/test_viz' ,
417
- tolerance = 10
418
- )
413
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
419
414
def test_probplot_qq_bestfit (plot_data ):
420
415
fig , ax = plt .subplots ()
421
416
fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , bestfit = True ,
422
- ylabel = 'Test label' , datascale = 'log' )
417
+ problabel = 'Test label' , datascale = 'log' )
423
418
return fig
424
419
425
420
426
- @pytest .mark .mpl_image_compare (
427
- baseline_dir = 'baseline_images/test_viz' ,
428
- tolerance = 10
429
- )
421
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
430
422
def test_probplot_pp_bestfit (plot_data ):
431
423
fig , ax = plt .subplots ()
432
424
scatter_kws = {'marker' : 's' , 'color' : 'red' }
433
425
line_kws = {'linestyle' : '--' , 'linewidth' : 3 }
434
426
fig = viz .probplot (plot_data , ax = ax , plottype = 'pp' , datascale = 'linear' ,
435
- xlabel = 'test x' , bestfit = True , ylabel = 'test y' ,
427
+ datalabel = 'test x' , bestfit = True , problabel = 'test y' ,
436
428
scatter_kws = scatter_kws , line_kws = line_kws )
437
429
return fig
438
430
439
431
440
- @pytest .mark .mpl_image_compare (
441
- baseline_dir = 'baseline_images/test_viz' ,
442
- tolerance = 10
443
- )
432
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
444
433
def test_probplot_prob_probax_y (plot_data ):
445
434
fig , ax = plt .subplots ()
446
- fig = viz .probplot (plot_data , ax = ax , xlabel = 'Test xlabel' , datascale = 'log' , probax = 'y' )
435
+ fig = viz .probplot (plot_data , ax = ax , datalabel = 'Test xlabel' , datascale = 'log' , probax = 'y' )
447
436
assert isinstance (fig , plt .Figure )
448
437
return fig
449
438
450
439
451
- @pytest .mark .mpl_image_compare (
452
- baseline_dir = 'baseline_images/test_viz' ,
453
- tolerance = 10
454
- )
440
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
455
441
def test_probplot_qq_probax_y (plot_data ):
456
442
fig , ax = plt .subplots ()
457
- fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , ylabel = 'Test label' , probax = 'y' ,
443
+ fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , problabel = 'Test label' , probax = 'y' ,
458
444
datascale = 'log' , scatter_kws = dict (color = 'r' ))
459
445
return fig
460
446
461
447
462
- @pytest .mark .mpl_image_compare (
463
- baseline_dir = 'baseline_images/test_viz' ,
464
- tolerance = 10
465
- )
448
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
466
449
def test_probplot_pp_probax_y (plot_data ):
467
450
fig , ax = plt .subplots ()
468
451
scatter_kws = dict (color = 'b' , linestyle = '--' , markeredgecolor = 'g' , markerfacecolor = 'none' )
469
452
fig = viz .probplot (plot_data , ax = ax , plottype = 'pp' , datascale = 'linear' , probax = 'y' ,
470
- xlabel = 'test x' , ylabel = 'test y' , scatter_kws = scatter_kws )
453
+ datalabel = 'test x' , problabel = 'test y' , scatter_kws = scatter_kws )
471
454
return fig
472
455
473
456
474
- @pytest .mark .mpl_image_compare (
475
- baseline_dir = 'baseline_images/test_viz' ,
476
- tolerance = 10
477
- )
457
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
478
458
def test_probplot_prob_bestfit_probax_y (plot_data ):
479
459
fig , ax = plt .subplots ()
480
- fig = viz .probplot (plot_data , ax = ax , xlabel = 'Test xlabel' , bestfit = True ,
460
+ fig = viz .probplot (plot_data , ax = ax , datalabel = 'Test xlabel' , bestfit = True ,
481
461
datascale = 'log' , probax = 'y' )
482
462
assert isinstance (fig , plt .Figure )
483
463
return fig
484
464
485
465
486
- @pytest .mark .mpl_image_compare (
487
- baseline_dir = 'baseline_images/test_viz' ,
488
- tolerance = 10
489
- )
466
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
490
467
def test_probplot_qq_bestfit_probax_y (plot_data ):
491
468
fig , ax = plt .subplots ()
492
- fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , bestfit = True , ylabel = 'Test label' ,
469
+ fig = viz .probplot (plot_data , ax = ax , plottype = 'qq' , bestfit = True , problabel = 'Test label' ,
493
470
datascale = 'log' , probax = 'y' )
494
471
return fig
495
472
496
473
497
- @pytest .mark .mpl_image_compare (
498
- baseline_dir = 'baseline_images/test_viz' ,
499
- tolerance = 10
500
- )
474
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
501
475
def test_probplot_pp_bestfit_probax_y (plot_data ):
502
476
fig , ax = plt .subplots ()
503
477
scatter_kws = {'marker' : 's' , 'color' : 'red' }
504
478
line_kws = {'linestyle' : '--' , 'linewidth' : 3 }
505
479
fig = viz .probplot (plot_data , ax = ax , plottype = 'pp' , datascale = 'linear' , probax = 'y' ,
506
- xlabel = 'test x' , bestfit = True , ylabel = 'test y' ,
480
+ datalabel = 'test x' , bestfit = True , problabel = 'test y' ,
507
481
scatter_kws = scatter_kws , line_kws = line_kws )
508
482
return fig
509
483
510
484
511
- @pytest .mark .mpl_image_compare (
512
- baseline_dir = 'baseline_images/test_viz' ,
513
- tolerance = 15
514
- )
485
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 15 )
515
486
@pytest .mark .skipif (stats is None , reason = "no scipy" )
516
487
def test_probplot_beta_dist_best_fit_y (plot_data ):
517
488
fig , (ax1 , ax2 ) = plt .subplots (ncols = 2 )
518
489
dist = stats .beta (3 , 3 )
519
- fig = viz .probplot (plot_data , dist = dist , ax = ax1 , ylabel = 'Beta scale' ,
490
+ fig = viz .probplot (plot_data , dist = dist , ax = ax1 , problabel = 'Beta scale' ,
520
491
bestfit = True , datascale = 'log' , probax = 'y' )
521
492
ax1 .set_ylim (bottom = 0.5 , top = 98 )
522
493
523
- fig = viz .probplot (plot_data , ax = ax2 , xlabel = 'Default (norm)' ,
494
+ fig = viz .probplot (plot_data , ax = ax2 , datalabel = 'Default (norm)' ,
524
495
bestfit = True , datascale = 'log' , probax = 'y' )
525
496
ax2 .set_ylim (bottom = 0.5 , top = 98 )
526
497
527
498
assert isinstance (fig , plt .Figure )
528
499
return fig
529
500
530
501
531
- @pytest .mark .mpl_image_compare (
532
- baseline_dir = 'baseline_images/test_viz' ,
533
- tolerance = 10
534
- )
502
+ @pytest .mark .mpl_image_compare (baseline_dir = BASELINE_DIR , tolerance = 10 )
535
503
@pytest .mark .skipif (stats is None , reason = "no scipy" )
536
504
def test_probplot_beta_dist_best_fit_x (plot_data ):
537
505
fig , (ax1 , ax2 ) = plt .subplots (nrows = 2 )
538
506
dist = stats .beta (3 , 3 )
539
- fig = viz .probplot (plot_data , dist = dist , ax = ax1 , xlabel = 'Beta scale' ,
507
+ fig = viz .probplot (plot_data , dist = dist , ax = ax1 , problabel = 'Beta scale' ,
540
508
bestfit = True , datascale = 'log' , probax = 'x' )
541
509
ax1 .set_xlim (left = 0.5 , right = 98 )
542
510
543
- fig = viz .probplot (plot_data , ax = ax2 , xlabel = 'Default (norm)' ,
511
+ fig = viz .probplot (plot_data , ax = ax2 , problabel = 'Default (norm)' ,
544
512
bestfit = True , datascale = 'log' , probax = 'x' )
545
513
ax2 .set_xlim (left = 0.5 , right = 98 )
546
514
@@ -550,9 +518,29 @@ def test_probplot_beta_dist_best_fit_x(plot_data):
550
518
551
519
def test_probplot_test_results (plot_data ):
552
520
fig , ax = plt .subplots ()
553
- fig , results = viz .probplot (plot_data , return_results = True )
521
+ fig , results = viz .probplot (plot_data , return_best_fit_results = True )
554
522
555
523
assert isinstance (results , dict )
556
524
known_keys = sorted (['q' , 'x' , 'y' , 'xhat' , 'yhat' , 'res' ])
557
525
assert sorted (list (results .keys ())) == known_keys
558
526
return fig
527
+
528
+
529
+
530
+
531
+ @pytest .mark .parametrize ('probax' , ['x' , 'y' ])
532
+ @pytest .mark .parametrize (('N' , 'minval' , 'maxval' ), [
533
+ (8 , 10 , 90 ),
534
+ (37 , 1 , 99 ),
535
+ (101 , 0.1 , 99.9 ),
536
+ (10001 , 0.001 , 99.999 )
537
+ ])
538
+ def test__set_prob_limits_x (probax , N , minval , maxval ):
539
+ from probscale import validate
540
+ ax = mock .Mock ()
541
+ with mock .patch .object (validate , 'axes_object' , return_value = [None , ax ]):
542
+ viz ._set_prob_limits (ax , probax , N )
543
+ if probax == 'x' :
544
+ ax .set_xlim .assert_called_once_with (left = minval , right = maxval )
545
+ elif probax == 'y' :
546
+ ax .set_ylim .assert_called_once_with (bottom = minval , top = maxval )
0 commit comments