forked from kad-ecoli/python_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPvalue.py
More file actions
executable file
·69 lines (60 loc) · 2.16 KB
/
getPvalue.py
File metadata and controls
executable file
·69 lines (60 loc) · 2.16 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
#!/usr/bin/env python
docstring='''
getPvalue.py data1.csv data2.csv
Perform Student T-test on two datasets using two-sided Student T test
by scipy.stats.ttest_rel or scipy.stats.ttest_ind
input files:
data1.csv data2.csv - two lists of data points, one data point per line
options:
-paired={true,false}
whether to the two datasets are paired
-equal_var={true,false}
whether to assume same variance. only two unpaired dataset can have
inequal variance.
-two_tailed={true,false}
whether to perform two tailed test. if false, null hypothesis is
data1<=data2, alternative is data1>data2.
output:
t-statistics p-value
'''
import numpy as np
import scipy.stats
import sys
def getPvalue(a=[],b=[],paired=True,equal_var=True,two_tailed=True):
'''perform student T-test on array a and b,
paired - whether to the two datasets are paired
equal_var - whether to assume same variance
two_tailed - whether to perform two tailed test
'''
if paired:
tstatistic,pvalue=scipy.stats.ttest_rel(a,b)
else:
tstatistic,pvalue=scipy.stats.ttest_ind(a,b,equal_var=equal_var)
if not two_tailed:
pvalue=pvalue*0.5
if tstatistic<0:
pvalue=1-pvalue
return tstatistic,pvalue
if __name__=="__main__":
paired=True # related dataset
equal_var=True # equal variance
two_tailed=True # two tailed test
argv=[]
for arg in sys.argv[1:]:
if arg.startswith("-paired="):
paired=(arg[len("-paired="):].lower()=="true")
elif arg.startswith("-equal_var="):
equal_var=(arg[len("-equal_var="):].lower()=="true")
elif arg.startswith("-two_tailed="):
two_tailed=(arg[len("-two_tailed="):].lower()=="true")
elif arg.startswith('-'):
sys.stderr.write("ERROR! Unknown option %s\n"%arg)
exit()
else:
argv.append(arg)
if len(argv)!=2:
sys.stderr.write(docstring)
exit()
tstatistic,pvalue=getPvalue(np.loadtxt(argv[0]),np.loadtxt(argv[1]),
paired,equal_var,two_tailed)
sys.stderr.write(str(tstatistic)+'\t'+str(pvalue)+'\n')