Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3898437

Browse files
committed
added some unit tests
svn path=/trunk/matplotlib/; revision=337
1 parent e5e7aea commit 3898437

7 files changed

Lines changed: 213 additions & 1 deletion

File tree

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
New entries should be added at the top
22

33
2004-06-08 Rewrote ft2font using CXX as part of general memory leak
4-
fixes - JDH
4+
fixes; also fixed transform memory leaks - JDH
55

66
2004-06-07 Fixed several problems with log ticks and scaling - JDH
77

examples/cursor_demo.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from matplotlib.matlab import *
2+
3+
4+
class Cursor:
5+
def __init__(self, canvas, ax):
6+
self.canvas = canvas
7+
self.ax = ax
8+
self.lx, = ax.plot( (0,0), (0,0), 'k-' ) # the horiz line
9+
self.ly, = ax.plot( (0,0), (0,0), 'k-' ) # the vert line
10+
11+
# text location in axes coords
12+
self.txt = ax.text( 0.7, 0.9, '', transform=ax.transAxes)
13+
14+
def mouse_move(self, widget, event):
15+
height = self.ax.figure.bbox.height()
16+
x, y = event.x, height-event.y
17+
18+
if self.ax.in_axes(x, y):
19+
# transData transforms data coords to display coords. Use
20+
# the inverse method to transform back to data coords then
21+
# update the line
22+
23+
# the cursor position
24+
x, y = ax.transData.inverse_xy_tup( (x,y) )
25+
# the view limits
26+
minx, maxx = ax.viewLim.intervalx().get_bounds()
27+
miny, maxy = ax.viewLim.intervaly().get_bounds()
28+
29+
# update the line positions
30+
self.lx.set_data( (minx, maxx), (y, y) )
31+
self.ly.set_data( (x, x), (miny, maxy) )
32+
33+
self.txt.set_text( 'x=%1.2f, y=%1.2f'%(x,y) )
34+
self.canvas.draw()
35+
36+
37+
class SnaptoCursor:
38+
"""
39+
Like Cursor but the crosshair snaps to the nearest x,y point
40+
For simplicity, I'm assuming x is sorted
41+
"""
42+
def __init__(self, canvas, ax, x, y):
43+
self.canvas = canvas
44+
self.ax = ax
45+
self.lx, = ax.plot( (0,0), (0,0), 'k-' ) # the horiz line
46+
self.ly, = ax.plot( (0,0), (0,0), 'k-' ) # the vert line
47+
self.x = x
48+
self.y = y
49+
# text location in axes coords
50+
self.txt = ax.text( 0.7, 0.9, '', transform=ax.transAxes)
51+
52+
def mouse_move(self, widget, event):
53+
height = self.ax.figure.bbox.height()
54+
x, y = event.x, height-event.y
55+
56+
if self.ax.in_axes(x, y):
57+
# transData transforms data coords to display coords. Use
58+
# the inverse method to transform back to data coords then
59+
# update the line
60+
61+
# the cursor position
62+
x, y = ax.transData.inverse_xy_tup( (x,y) )
63+
# the view limits
64+
minx, maxx = ax.viewLim.intervalx().get_bounds()
65+
miny, maxy = ax.viewLim.intervaly().get_bounds()
66+
67+
indx = searchsorted(self.x, [x])[0]
68+
x = self.x[indx]
69+
y = self.y[indx]
70+
# update the line positions
71+
self.lx.set_data( (minx, maxx), (y, y) )
72+
self.ly.set_data( (x, x), (miny, maxy) )
73+
74+
self.txt.set_text( 'x=%1.2f, y=%1.2f'%(x,y) )
75+
print 'x=%1.2f, y=%1.2f'%(x,y)
76+
self.canvas.draw()
77+
78+
t = arange(0.0, 1.0, 0.01)
79+
s = sin(2*2*pi*t)
80+
ax = subplot(111)
81+
82+
canvas = get_current_fig_manager().canvas
83+
#cursor = Cursor(canvas, ax)
84+
cursor = SnaptoCursor(canvas, ax, t, s)
85+
canvas.connect('motion_notify_event', cursor.mouse_move)
86+
87+
ax.plot(t, s, 'o')
88+
axis([0,1,-1,1])
89+
show()

unit/__init__.py

Whitespace-only changes.

unit/agg_memleak.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys, time, os
2+
from matplotlib.ft2font import FT2Font
3+
from matplotlib.numerix import rand
4+
from matplotlib.backend_bases import GraphicsContextBase
5+
from matplotlib.backends._backend_agg import RendererAgg
6+
7+
def report_memory(i):
8+
pid = os.getpid()
9+
a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
10+
print i, ' ', a2[1],
11+
return int(a2[1].split()[0])
12+
13+
fname = '/usr/local/share/matplotlib/Vera.ttf'
14+
15+
N = 200
16+
for i in range(N):
17+
gc = GraphicsContextBase()
18+
gc.set_clip_rectangle( [20,20,20,20] )
19+
o = RendererAgg(400,400, 72)
20+
21+
for j in range(50):
22+
xs = [400*int(rand()) for k in range(8)]
23+
ys = [400*int(rand()) for k in range(8)]
24+
rgb = (1,0,0)
25+
pnts = zip(xs, ys)
26+
o.draw_polygon(gc, rgb, pnts)
27+
o.draw_polygon(gc, None, pnts)
28+
29+
for j in range(50):
30+
x = [400*int(rand()) for k in range(4)]
31+
y = [400*int(rand()) for k in range(4)]
32+
o.draw_lines( gc, x, y)
33+
34+
for j in range(50):
35+
args = [400*int(rand()) for k in range(4)]
36+
rgb = (1,0,0)
37+
o.draw_rectangle(gc, rgb, *args)
38+
39+
if 1: # add text
40+
font = FT2Font(fname)
41+
font.clear()
42+
font.set_text('hi mom', 60)
43+
font.set_size(12, 72)
44+
o.draw_text( font, 30, 40, gc)
45+
46+
o.write_png('aggtest%d.png'%i)
47+
val = report_memory(i)
48+
if i==1: start = val
49+
50+
end = val
51+
print 'Average memory consumed per loop: %1.4f\n' % ((end-start)/float(N))
52+
53+
# w/o text and w/o write_png: Average memory consumed per loop: 0.02
54+
# w/o text and w/ write_png : Average memory consumed per loop: 0.3400
55+
# w/ text and w/ write_png : Average memory consumed per loop: 0.32

unit/ft2font_memleak.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys, time, os
2+
from matplotlib.numerix import rand
3+
from matplotlib.ft2font import FT2Font
4+
5+
fname = '/usr/local/share/matplotlib/Vera.ttf'
6+
7+
def report_memory(i):
8+
pid = os.getpid()
9+
a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
10+
print i, ' ', a2[1],
11+
return int(a2[1].split()[0])
12+
13+
fname = '/usr/local/share/matplotlib/Vera.ttf'
14+
N = 400
15+
for i in range(N):
16+
font = FT2Font(fname)
17+
font.clear()
18+
font.set_text('hi mom', 60)
19+
font.set_size(12, 72)
20+
#glyph = font.load_char(int(140*rand()))
21+
font.draw_glyphs_to_bitmap()
22+
#font.draw_glyph_to_bitmap(0, 0, glyph)
23+
24+
val = report_memory(i)
25+
if i==1: start = val
26+
27+
end = val
28+
print 'Average memory consumed per loop: %1.4f\n' % ((end-start)/float(N))
29+
30+
# Average memory consumed per loop: 0.09

unit/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys, time, os
2+
from matplotlib.numerix import rand
3+
from matplotlib.transforms import identity_transform, unit_bbox, Func, IDENTITY
4+
from matplotlib.transforms import one, Point, Value, Bbox, get_bbox_transform
5+
6+
7+
def rand_point():
8+
xy = rand(2)
9+
return Point( Value(xy[0]), Value(xy[1]) )
10+
11+
def rand_bbox():
12+
ll = rand_point()
13+
ur = rand_point()
14+
return Bbox(ll, ur)
15+
16+
def rand_transform():
17+
b1 = rand_bbox()
18+
b2 = rand_bbox()
19+
return get_bbox_transform(b1, b2)
20+

unit/transform_memleak.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import sys, time, os
2+
from helpers import rand_transform
3+
4+
def report_memory(i):
5+
pid = os.getpid()
6+
a2 = os.popen('ps -p %d -o rss,sz' % pid).readlines()
7+
print i, ' ', a2[1],
8+
return int(a2[1].split()[0])
9+
10+
N = 200
11+
for i in range(N):
12+
t = rand_transform()
13+
val = report_memory(i)
14+
if i==1: start = val
15+
16+
end = val
17+
print 'Average memory consumed per loop: %1.4f\n' % ((end-start)/float(N))
18+

0 commit comments

Comments
 (0)