Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import re
- import sys
- from collections import Counter
- def get_times_map(filename):
- times_map = {}
- with open(filename, 'r') as f:
- lines = f.readlines()
- for line in lines:
- if line:
- split_line = re.split('\s+|,\s+', line.strip())
- name = split_line[0]
- times = []
- for time in split_line[1:]:
- if time:
- mins, seconds = re.split(':', time)
- times.append(60 * int(mins) + int(seconds))
- times_map[name] = times
- return times_map
- def decimal_round(x):
- return "{0}".format(str(round(x, 1) if x % 1 else int(x)))
- def seconds_to_time(seconds):
- mins = str(int(seconds) / 60)
- remaining_seconds = decimal_round(seconds % 60)
- if ('.' not in remaining_seconds and len(remaining_seconds) < 2) or \
- ('.' in remaining_seconds and remaining_seconds.index('.') == 1):
- remaining_seconds = '0' + remaining_seconds
- return '%s:%s' % (mins, remaining_seconds)
- def print_stats(category, times, num_common=5):
- min_time = seconds_to_time(min(times))
- max_time = seconds_to_time(max(times))
- avg_time = seconds_to_time(np.mean(times))
- med_time = seconds_to_time(np.median(times))
- variance = seconds_to_time(np.var(times))
- print '%s: avg %s, median %s, min %s, max %s, variance %s [data points: %s]' % (
- category, avg_time, med_time, min_time, max_time, variance, len(times))
- if num_common:
- counter = Counter(times)
- sorted_times = sorted(counter, key=counter.get)
- print 'Most common times:'
- for time in reversed(sorted_times[-num_common:]):
- print '\t%2s: %s' % (counter[time], seconds_to_time(time))
- print 'Least common times:'
- for time in sorted_times[:num_common]:
- print '\t%2s: %s' % (counter[time], seconds_to_time(time))
- def print_time_stats(filename):
- times_map = get_times_map(filename)
- for category in times_map:
- print_stats(category, times_map[category])
- if __name__=='__main__':
- if len(sys.argv) != 2:
- print 'Usage: time_stats.py [input file]'
- else:
- print_time_stats(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment