Repository navigation
Expand file tree
/
Copy pathihistogram.py
More file actions
119 lines (89 loc) · 3.05 KB
/
Copy pathihistogram.py
File metadata and controls
119 lines (89 loc) · 3.05 KB
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
#!/usr/bin/python
"""Draw a quick histogram of the timestamps on the hard drive"""
import matplotlib
matplotlib.use('agg.pdf')
import dfxml.fiwalk as fiwalk
import datetime
from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter
from matplotlib.dates import MONDAY,SATURDAY
import time
from pylab import *
def get_dates_and_counts(times):
from datetime import date
data = {}
for t in times:
gm = time.gmtime(t)
d = date(gm[0],gm[1],gm[2])
data[d] = data.get(d,0)+1
# Create a list of key,val items so you can sort by date
dates_and_counts = [ (date,count) for date,count in data.items()]
dates_and_counts = sorted(dates_and_counts)
return dates_and_counts
def version1(times):
import pylab
pylab.grid()
pylab.hist(times,100)
pylab.show()
def version2(times):
# see http://mail.python.org/pipermail/python-list/2003-November/236559.html
# http://www.gossamer-threads.com/lists/python/python/665014
from matplotlib.pylab import plot, show, title, xlabel, ylabel, gca, bar, savefig, plot_date
dates_and_counts = get_dates_and_counts(times)
dates, counts = zip(*dates_and_counts)
# bar(dates,counts)
plot_date(dates,counts)
xlabel("Date")
ylabel("count")
show()
def version3(times):
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as pyplot
import matplotlib.dates as mdates
import matplotlib.mlab as mlab
dates_and_counts = get_dates_and_counts(times)
dates, counts = zip(*dates_and_counts)
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
yearsFmt = mdates.DateFormatter('%Y')
fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.bar(dates,counts)
ax.set_ylabel('file count')
ax.set_xlabel('file modification time (mtime)')
#ax.set_yscale('log')
# Format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yearsFmt)
#ax.xaxis.set_minor_locator(months)
datemin = datetime.date(min(dates).year, 1, 1)
datemax = datetime.date(max(dates).year, 1, 1)
ax.set_xlim(datemin, datemax)
ax.set_ylim(0,max(counts))
# format the coords message box
def price(x): return '$%1.2f'%x
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = price
ax.grid(True)
# rotates and right aligns the x labels, and moves the bottom of the
# axes up to make room for them
fig.autofmt_xdate()
plt.savefig("hist.pdf",format='pdf')
print("dates:",dates)
print("num dates:",len(dates))
if __name__=="__main__":
import sys
from optparse import OptionParser
from sys import stdout
parser = OptionParser()
parser.usage = '%prog [options] xmlfile '
(options,args) = parser.parse_args()
import time
times = []
for fi in fiwalk.fileobjects_using_sax(xmlfile=open(args[0])):
try:
times.append(fi.mtime())
except KeyError:
pass
version3(times)