File: distcheck-plot.py

package info (click to toggle)
dose3 7.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,276 kB
  • sloc: ml: 25,053; python: 605; perl: 391; sh: 347; makefile: 187
file content (232 lines) | stat: -rwxr-xr-x 8,262 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python
import argparse
import csv
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import dates
import os.path
from itertools import groupby
from operator import itemgetter, attrgetter, methodcaller
import collections

# takes as a input a cvs file and a list of distributions and
# creates a plot of the evolution of broken and outdated packages
# vs time.

# cvs format "date suite packages broken (outdated)"
# ex : 20111125 unstable 35056 337 (73)

# parse a time line file of the form :
# Title: ...
# Date: Mar 25 2013
# <other infos>: ignored
def parse_timeline(fname,timeline) :
    records = []
    for empty, record in groupby(open(fname), key=str.isspace):
      if not empty:
        pairs = map(lambda s : s.split(': '), record)
        pairs = dict(map(lambda (k,v) : (k,v.rstrip()), pairs))
        pairs['Date'] = dt.datetime.strptime(pairs['Date'],"%b %d %Y")
        pairs['Timeline'] = timeline
        records.append(pairs)
    return records

def aggregate_timeline(timelines) :
    t = sorted(timelines,key=lambda t : t['Date'])
    l = []
    i=0
    while i < len(t) - 1 :
        if t[i]['Timeline'] == 'release' :
            if t[i+1]['Timeline'] == 'release' :
                #l.append(t[i])
                i=i+1
            else :
                i=i+1
        elif t[i]['Timeline'] == 'freeze' :
            if t[i+1]['Timeline'] == 'release' :
                l.append((t[i],t[i+1]))
                i=i+2
            else :
                i=i+1
        else :
            i=i+1
    return l

def initdict(dists,distname) :
    dists[distname] = {'date' : [], 'total' : [], 'broken' : [], 'outdated' : [] }
    return dists

def parse(dataset,outdated=False):
    dists = collections.OrderedDict()
    csv_reader = csv.reader(dataset,delimiter=' ')
    for line in sorted(csv_reader,key=lambda k: k[0]):
        distname = line[1]
        if distname not in dists :
            dists = initdict(dists,distname)

        dists[distname]['date'].append(dt.datetime.strptime(line[0],'%Y%m%d'))
        dists[distname]['total'].append(int(line[2]))
        dists[distname]['broken'].append(int(line[3]))
        if outdated : 
            dists[distname]['outdated'].append(int(line[4]))

    return dists

def plot(dists,output,title,intervals,outdated=False) :

    distlist = dists.keys()
    fig = plt.figure()
    fig.suptitle(title)
    plotsidx = [311,312,313] if outdated else [211,212]
    ax1 = fig.add_subplot(plotsidx[0],title='Total Packages vs Time')
    ax2 = fig.add_subplot(plotsidx[1],title='Non-Installable Packages vs Time')
    if outdated : 
        ax3 = fig.add_subplot(plotsidx[2],title='Outdated Packages vs Time')
    for k in distlist :
        ax1.plot(dists[k]['date'],dists[k]['total'],',-',label=k.capitalize())
        ax2.plot(dists[k]['date'],dists[k]['broken'],',-',label=k.capitalize())
        if intervals :
            mdate = min(dists[k]['date'])
            for (f,r) in intervals :
                if f['Date'] > mdate :
                    ax2.axvspan(f['Date'],r['Date'], alpha=0.5, color='red')
        if outdated :
            ax3.plot(dists[k]['date'],dists[k]['outdated'],',-',label=k.capitalize())


    if len(distlist) > 1 :
        ax1.legend(loc='upper left')
        ax2.legend(loc='upper left')
        #ax2.legend()
        if outdated :
            ax3.legend(loc='upper left')

    fig.autofmt_xdate()

    plt.savefig(output)

def multiplot(dists,output,title,timelines,intervals) :

    fig = plt.figure()
    fig.suptitle(title)
    subplotnum = len(dists)
    #fig.set_figheight((subplotnum+1) * 3)
    fig.set_figheight(24)

    ax = {}
    plotsidx = []
    for i in range(1, subplotnum+2) :
        plotsidx.append(int(("%d1%d") % (subplotnum+1,i)))

    print plotsidx

    # First graph puts all dists together in one
    ax[0] = fig.add_subplot(plotsidx[0],title='Total Packages vs Time')
    for k in dists :
        ax[0].plot(dists[k]['date'],dists[k]['total'],',-',label=k.capitalize())
    ax[0].legend(loc='upper left')

    i = 1
    for n in dists.keys() :
        ax[i] = fig.add_subplot(plotsidx[i])
        if i == 1 :
            ax[i].set_title('Non-Installable Packages vs Time')
        ax[i].plot(dists[n]['date'],dists[n]['broken'],',-',label=n.capitalize())
        ax[i].legend(loc='upper left')
        if intervals :
            mdate = min(dists[k]['date'])
            for (f,r) in intervals :
                if f['Date'] > mdate :
                    ax[i].axvspan(f['Date'],r['Date'], alpha=0.5, color='red')
        i += 1

    fig.autofmt_xdate()
    plt.savefig(output)

# plot two distribution with different scales
def multiscale(dists,dist1,dist2,output,title,outdated=False) :

    fig = plt.figure()
    fig.suptitle(title)
    fig.autofmt_xdate()

    plotsidx = [311,312,313] if outdated else [211,212]
    ax1 = fig.add_subplot(plotsidx[0],title='Total Packages vs Time')
    ax1.plot(dists[dist1]['date'],dists[dist1]['total'],',-',label=dist1.capitalize())
    ax1.plot(dists[dist2]['date'],dists[dist2]['total'],',-',label=dist2.capitalize())
    ax1.legend(loc='upper left')
    ax1.xaxis.set_visible(False)

    ax2 = fig.add_subplot(plotsidx[1],title='Non-Installable Packages vs Time')
    ax2.plot(dists[dist1]['date'],dists[dist1]['broken'],'o-',label=dist1.capitalize())
    ax2.xaxis.set_visible(False)

    ax22 = ax2.twinx() 
    ax22.plot(dists[dist2]['date'],dists[dist2]['broken'],'s-',label=dist2.capitalize())
    ax22.set_ylim(0, 70)

    if outdated : 
        ax3 = fig.add_subplot(plotsidx[2],title='Outdated Packages vs Time')
        ax3.plot(dists[dist1]['date'],dists[dist1]['outdated'],'o-',label=dist1.capitalize())

        ax33 = ax3.twinx() 
        ax33.plot(dists[dist2]['date'],dists[dist2]['outdated'],'gs-',label=dist2.capitalize())
        ax33.set_ylim(0, 10)
        # ax33.set_yticks([-1,0,1])

        plt.setp(ax3.xaxis.get_majorticklabels(), rotation=30)

    plt.savefig(output)

def main():
    parser = argparse.ArgumentParser(description='plot outdated/broken')
    parser.add_argument('-v', '--verbose')
    parser.add_argument('-d', '--debug', action='store_true', default=False)
    parser.add_argument('-o', '--output', action='store')
    parser.add_argument('-t', '--title', action='store')
    parser.add_argument('-s', '--split', action='store_true', default=False)
    parser.add_argument('--ms', action='store_true', default=False, help="multi plot")
    parser.add_argument('--mp', action='store_true', default=False, help="multi scale")
    parser.add_argument('--releases', type=str, nargs=1, help="release timeline")
    parser.add_argument('--freezes', type=str, nargs=1, help="freeze timeline")
    parser.add_argument('--outdated', action='store_true', default=False)
    parser.add_argument('dataset', type=str, nargs=1, help="dataset")
    args = parser.parse_args()
 
    fname = args.dataset[0]

    dataset = open(fname)
    dists = parse(dataset)
    dataset.close()

    title = args.title[0] if args.title else ""

    timelines = {}
    intervals = []
    if args.releases :
        timelines['releases'] = parse_timeline(args.releases[0],'release')
    if args.freezes :
        timelines['freezes'] = parse_timeline(args.freezes[0],'freeze')
    if args.releases and args.freezes :
        intervals = aggregate_timeline(timelines['releases'] + timelines['freezes'])

    if args.ms :
        output = "aggregate-%s-ms.png" % os.path.splitext(os.path.basename(fname))[0]
        multiscale(dists, output, title, args.outdated)
    elif args.mp :
        output = "aggregate-%s-mp.png" % os.path.splitext(os.path.basename(fname))[0]
        multiplot(dists, output, title, timelines, intervals)
    else :
        if args.split :
            # different graphs, one for each suite
            for d in dists.keys() :
                output = "%s-%s.png" % (d,os.path.splitext(os.path.basename(fname))[0])
                plot(dists, [d] , output, title, intervals, args.outdated)
        else :
            # all in one graph
            output = "aggregate-%s.png" % os.path.splitext(os.path.basename(fname))[0]
            plot(dists, output, title, intervals, args.outdated)

if __name__ == '__main__':
    main()