Debian Codegraph @

tests/plot_lengths_histogram_delitem.py

raw
1 """Plot histogram of SortedList sublist lengths while items are deleted.
2
3 """
4
5 from __future__ import division, print_function
6
7 import argparse
8 import itertools as it
9 import numpy as np
10 import scipy
11 import scipy.stats
12 import sortedcontainers as sc
13 from matplotlib import pyplot as plt
14 from matplotlib import animation
15 import random
16
17
18 def background():
19 "Plot the background of each animated frame."
20 hist_line.set_data([], [])
21 norm_line.set_data([], [])
22 return hist_line, norm_line
23
24
25 def frame(num):
26 "Draw frame."
27 for value in xrange(LOAD):
28 del values[random.randrange(len(values))]
29 data = np.array([len(sublist) for sublist in values._lists])
30 hist, bins = np.histogram(data, bins=BINS) # , normed=True)
31 hist_line.set_data(bins[:-1], hist)
32
33 return hist_line, norm_line
34
35
36 if __name__ == '__main__':
37 parser = argparse.ArgumentParser()
38 parser.add_argument('--seed', type=int, default=0)
39 parser.add_argument('--load', type=float, default=1e3)
40 parser.add_argument('--show', action='store_true')
41 parser.add_argument('--filename', default='sublist-lengths-delitem.mp4')
42
43 args = parser.parse_args()
44
45 random.seed(args.seed)
46
47 LOAD = int(args.load)
48 BINS = np.array(range(LOAD // 2, LOAD * 2 + 1, LOAD // 20))
49
50 values = sc.SortedList(xrange(LOAD * LOAD), load=LOAD)
51
52 fig = plt.figure()
53 bounds = (LOAD // 2 - 5 * LOAD // 100, LOAD * 2 - 5 * LOAD // 100)
54 ax = plt.axes(xlim=bounds, ylim=(0, 1000))
55 hist_line, = ax.plot([], [])
56 norm_line, = ax.plot([], [])
57
58 animator = animation.FuncAnimation(
59 fig, frame, init_func=background, frames=1000, interval=10, blit=True,
60 )
61
62 if args.show:
63 plt.show()
64 else:
65 animator.save(
66 args.filename, fps=20, extra_args=['-vcodec', 'libx264']
67 )