-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
62 lines (42 loc) · 1.3 KB
/
helpers.py
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
import numpy as np
import xlrd
#functions take in a standard python list and
#internally converts to numpy arrays when needed
#calculates the x axis range for the histogram
#and the optimal bin width using the Freedman-Diaconis rule
def setBins(data):
x = np.asarray(data)
iqr = np.subtract(*np.percentile(x, [75, 25])) #interquartile range
h = 2.*iqr.item()*x.size**(-1./3)
xmax = max(data)
xmin = min(data)
nbins = int((xmax-xmin)/h)
return {'xmin': xmin,
'xmax:': xmax,
'nbins': nbins
}
#calculates and returns the standard deviation of the dataset
def calcSD(data):
x = np.asarray(data)
r = np.std(x)
return r.item()
#calculates and returns the mean of the dataset
def calcMean(data):
x = np.asarray(data)
r = np.mean(x)
return r.item()
#calculates and returns the mode of the dataset
def calcMode(data):
#http://stackoverflow.com/a/28129716
return max(set(data), key=data.count)
#calculates and returns the median of the dataset
def calcMed(data):
x = np.asarray(data)
r = np.median(x)
return r.item()
def getSheets(fname):
xls = xlrd.open_workbook(fname, on_demand=True)
sheets = []
for name in xls.sheet_names():
sheets.append(name)
return sheets