Skip to content

Instantly share code, notes, and snippets.

@amckinley
Created March 22, 2015 06:40
Show Gist options
  • Save amckinley/4d126566f285b8143a68 to your computer and use it in GitHub Desktop.
Save amckinley/4d126566f285b8143a68 to your computer and use it in GitHub Desktop.
from __future__ import print_function
from os.path import basename
import math
import sys
import numpy as np
# FIXME: look into decimal type
def main():
if len(sys.argv) != 2:
sys.exit("%s: <infile>" % basename(sys.argv[0]))
# Country codes conform to format defined by ISO 3166-1 alpha-2
ccode = basename(sys.argv[1]).split('_')[0]
# Analaysis
p = { key: None for key in [.50, .75, .85, .90, .95, .99] }
with open(sys.argv[1], 'rU') as f:
lines = map(float, f)
arr = np.array(lines)
count = arr.size
maximum = np.amax(arr)
# Presentation
#print("Country\tp50\tp75\tp85\tp90\tp95\tp99\tMean\tMax\tSigma\tCount")
# Country
print("%s\t" % ccode, end='')
# Percentiles
for k in sorted(p.keys()):
res = np.percentile(arr, k * 100)
print("%d\t" % res, end='')
# Misc stats
print("%d\t%d\t%d\t%d" % (0, maximum, 0, count))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment