|
| 1 | +from __future__ import division |
| 2 | +import sys |
| 3 | +from Numeric import arange, asarray |
| 4 | +from matplotlib.transforms import Bound1D, Bound2D, Transform |
| 5 | +from matplotlib.transforms import identity, log10, pow10 |
| 6 | +from matplotlib.transforms import bound2d_all |
| 7 | +from matplotlib.transforms import transform_bound1d |
| 8 | +from matplotlib.transforms import TransformSize, Points, Dots, \ |
| 9 | + Centimeter, Inches, RefVal |
| 10 | +from matplotlib.artist import DPI |
| 11 | + |
| 12 | +def closeto(x,y): |
| 13 | + try: assert(abs(asarray(x)-asarray(y))<1e-10) |
| 14 | + except AssertionError: |
| 15 | + raise AssertionError('Failed: %s and %s not close' %(x,y)) |
| 16 | + |
| 17 | +# testing bounding boxes |
| 18 | +bbox = Bound2D( 1,1,5,5) |
| 19 | +bbox.x.update([1,2,3,4,5,6]) |
| 20 | +bbox.y.update([-1,2,3,4,5,6,12]) |
| 21 | + |
| 22 | +left, bottom, width, height = bbox.get_bounds() |
| 23 | +top = bottom + height |
| 24 | +right = left + width |
| 25 | +assert(left==1) |
| 26 | +assert(right==6) |
| 27 | +assert(width==5) |
| 28 | +assert(bottom==-1) |
| 29 | +assert(top==12) |
| 30 | +assert(height==13) |
| 31 | + |
| 32 | +b = Bound1D(-1, 1) |
| 33 | +closeto(b.scale(1.9).bounds(), (-1.9, 1.9) ) |
| 34 | +b = Bound1D(-1, 1) |
| 35 | +closeto(b.scale(0.9).bounds(), (-0.9, 0.9) ) |
| 36 | +print 'passed bounding box tests ...' |
| 37 | + |
| 38 | +# test positive bounding box |
| 39 | +x = arange(-1.0001, 1.0, 0.01) |
| 40 | +bpos = Bound1D(.1, 1, isPos=True) |
| 41 | +bpos.update(x) |
| 42 | +assert(bpos.min()>0) |
| 43 | +bpos.is_positive(False) |
| 44 | +bpos.update(x) |
| 45 | +assert(bpos.min()==-1.0001) |
| 46 | + |
| 47 | +print 'passed positive bounding box tests ...' |
| 48 | + |
| 49 | +# testing transforms |
| 50 | +i1 = Bound1D(0,1) |
| 51 | +i2 = Bound1D(-6,6) |
| 52 | +identityTrans = Transform() |
| 53 | +linearTrans = Transform(i1,i2) |
| 54 | +logTrans = Transform(Bound1D(0.1,1), i2, funcs=(log10, pow10)) |
| 55 | + |
| 56 | +scalar = 1 |
| 57 | +tup = 1,2,3 |
| 58 | +a = arange(0.0, 2.0, 0.1) |
| 59 | + |
| 60 | +assert( identityTrans.positions( scalar ) == scalar ) |
| 61 | +assert( identityTrans.positions( tup )== tup ) |
| 62 | +assert( identityTrans.positions( a ) == a ) |
| 63 | +assert( identityTrans.scale( scalar ) == scalar ) |
| 64 | +assert( identityTrans.scale( tup ) == tup ) |
| 65 | +assert( identityTrans.scale( a ) == a ) |
| 66 | + |
| 67 | +assert( linearTrans.positions(1) == 6 ) |
| 68 | +assert( linearTrans.scale(1) == 12 ) |
| 69 | +print 'passed transform tests ... ' |
| 70 | + |
| 71 | +bound = Bound1D(0.25, 0.75) |
| 72 | +trans = Transform( Bound1D(0,1), Bound1D(-6,6)) |
| 73 | +bout = transform_bound1d(bound, trans) |
| 74 | +assert( bout.bounds() == (-3.0, 3.0) ) |
| 75 | +print 'passed bbox transform tests ... ' |
| 76 | + |
| 77 | +# testing bound2d_all |
| 78 | +b1 = Bound2D(1, 1, 1, 1) |
| 79 | +b2 = Bound2D(1.1, 1.1, 1, 1) |
| 80 | +b3 = Bound2D(1.2, 1.2, 1, 1) |
| 81 | +b4 = Bound2D(1.3, 1.3, 4, 1) |
| 82 | + |
| 83 | +bbox = bound2d_all((b1,b2,b3,b4)) |
| 84 | + |
| 85 | +closeto( bbox.x.min(), 1) |
| 86 | +closeto( bbox.x.max(), 5.3) |
| 87 | +closeto( bbox.y.min(), 1) |
| 88 | +closeto( bbox.y.max(), 2.3) |
| 89 | +print 'passed bound2d_all tests ... ' |
| 90 | + |
| 91 | +# testing inverses |
| 92 | +bpos = Bound1D(.1, 1, isPos=True) |
| 93 | +trans = Transform( bpos, Bound1D(-6,6), funcs=(log10, pow10)) |
| 94 | +x = 0.2 |
| 95 | +closeto( trans.inverse_positions(trans.positions(x)), x ) |
| 96 | +closeto( trans.inverse_scale(trans.scale(x)), x ) |
| 97 | + |
| 98 | +trans.set_funcs( (identity, identity) ) |
| 99 | +closeto( trans.inverse_positions(trans.positions(x)), x ) |
| 100 | +closeto( trans.inverse_scale(trans.scale(x)), x ) |
| 101 | +print 'passed inverse transform tests ... ' |
| 102 | + |
| 103 | + |
| 104 | +dpi = DPI(100) |
| 105 | +dots = Dots(dpi) |
| 106 | +pts = Points(dpi) |
| 107 | + |
| 108 | +trans = TransformSize(pts, dots, RefVal(10)) |
| 109 | +closeto(trans.positions(0), 10) |
| 110 | +closeto(trans.positions(72), 110) |
| 111 | + |
| 112 | +dpi.set(200) |
| 113 | +closeto(trans.positions(0), 10) |
| 114 | +closeto(trans.positions(72), 210) |
| 115 | +closeto(trans.positions(36), 110) |
| 116 | + |
| 117 | +cm = Centimeter(dpi) |
| 118 | +inch = Inches(dpi) |
| 119 | + |
| 120 | +trans = TransformSize(cm, inch, RefVal(0)) |
| 121 | +closeto(trans.positions(2), 2/2.54) |
| 122 | +closeto(trans.inverse_positions(trans.positions(2)), 2) |
| 123 | +#closeto(trans.positions(36), 110) |
| 124 | + |
| 125 | +dpi.set(100) |
| 126 | +pts = Points(dpi) |
| 127 | +dots = Dots(dpi) |
| 128 | + |
| 129 | +trans = TransformSize(pts, dots, RefVal(62.5)) |
| 130 | +closeto( trans.positions(12), 12/72*100 + 62.5) |
| 131 | + |
| 132 | +trans = TransformSize(pts, dots, RefVal(10)) |
| 133 | +#print trans.positions(-12) |
| 134 | + |
| 135 | +print 'passed size transform tests ... ' |
| 136 | + |
| 137 | + |
0 commit comments