Replace Owl with focusanimal variable
[swc-testing-nose.git] / python / mean_sightings-full.py
1 #!/usr/bin/env python
2
3 '''
4 Module/script to calculate mean number of sightings of a given animal in a 
5 given sightings csv file.
6 '''
7
8 import sys
9 import matplotlib.mlab as ml
10 import numpy as np
11
12
13 def get_sightings(filename, focusanimal):
14
15     # Load table
16     tab = ml.csv2rec(filename)
17
18     # Standardize capitalization of focusanimal
19     focusanimal = focusanimal.capitalize()
20
21     # Find number of records and total count of animals seen
22     isfocus = (tab['animal'] == focusanimal)
23     totalrecs = np.sum(isfocus)
24
25     if totalrecs == 0:
26         meancount = 0
27     else:
28         meancount = np.mean(tab['count'][isfocus])
29
30     # Return num of records and animals seen
31     return totalrecs, meancount
32
33
34 def get_sightings_loop(filename, focusanimal):
35
36     # Load table
37     tab = ml.csv2rec(filename)
38
39     # Loop through all records, countings recs and animals
40     totalrecs = 0
41     totalcount = 0
42     for rec in tab:
43         if rec['animal'] == focusanimal:
44             totalrecs += 1
45             totalcount += rec['count']
46
47     meancount = totalcount/totalrecs
48
49     # Return num of records and animals seen
50     return totalrecs, meancount
51
52 if __name__ == '__main__':
53     #print sys.argv
54     filename = sys.argv[1]
55     focusanimal = sys.argv[2]
56     print get_sightings(filename, focusanimal)