-
Notifications
You must be signed in to change notification settings - Fork 0
/
defmod.py
150 lines (108 loc) · 5.08 KB
/
defmod.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
from __future__ import division
import hddm
import pandas as pd
import numpy as np
from patsy import dmatrix
import os
from mydata.munge import find_path
pth=find_path()
data=pd.read_csv(pth+"/beh_hddm/allsx_feat.csv")
def z_link_func(x, data=data):
stim = (np.asarray(dmatrix('0 + C(s,[[1],[-1]])', {'s':data.stimulus.ix[x.index]})))
return 1 / (1 + np.exp(-(x * stim)))
def v_link_func(x, data=data):
stim = (np.asarray(dmatrix('0 + C(s,[[1],[-1]])', {'s':data.stimulus.ix[x.index]})))
return x * stim
def define_model(mname, project='imaging', regress=False):
check_model(mname)
data=find_data(mname, project)
if project=='imaging':
intercept="b50N"
else:
intercept="c50N"
if regress:
if mname=='msm':
msm_vreg = {'model': 'v ~ 1 + C(cue, Treatment('+intercept+'))', 'link_func': v_link_func}
m=hddm.HDDMRegressor(data, msm_vreg, depends_on={'v':'stim', 'z':'cue'}, bias=True, informative=False, include=['v', 'z', 't', 'a'])
elif mname=='pbm':
m=hddm.HDDM(data, depends_on={'v':'stim', 'z':'cue'}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
elif mname=='dbm':
dbm_vreg = {'model': 'v ~ 1 + C(cue, Treatment('+intercept+'))', 'link_func': v_link_func(data=data)}
m = hddm.HDDMRegressor(data, dbm_vreg, depends_on={'v':'stim'}, bias=False, informative=False, include=['v', 't', 'a'])
elif mname=='dbmz':
dbmz_vreg = {'model': 'v ~ 1 + C(cue, Treatment('+intercept+'))', 'link_func': v_link_func}
m=hddm.HDDMRegressor(data, dbmz_vreg, depends_on={'v':'stim'}, bias=True, informative=False, include=['v', 'z', 't', 'a'])
else:
if mname=='msmt':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue'], 'z':'cue', 't':['stim', 'cue']}, bias=True, informative=False, include=['v', 'z', 't', 'a', 'sv', 'sz', 'st'])
elif mname=='msm':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue'], 'z':'cue'}, bias=True, informative=False, include=['v', 'z', 't', 'a', 'sv', 'sz', 'st'])
elif mname=='pbm':
m=hddm.HDDM(data, depends_on={'v':'stim', 'z':'cue'}, bias=True, informative=False, include=['v', 'z', 't', 'a', 'sv', 'sz', 'st'])
elif mname=='dbm':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue']}, bias=False, informative=False, include=['v', 'z', 't', 'a', 'sv', 'sz', 'st'])
elif mname=='dbmz':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue']}, bias=True, informative=False, include=['v', 'z', 't', 'a', 'sv', 'sz', 'st'])
return m
def define_sxbayes(mname, data, project='imaging', regress=False):
m=define_single(mname, data, project='imaging', regress=False)
return m
def define_single(mname, data, project='imaging', regress=False):
check_model(mname)
if project=='imaging':
vreg = {'model': 'v ~ 1 + C(cue, Treatment("b50N"))', 'link_func': v_link_func}
else:
vreg = {'model': 'v ~ 1 + C(cue, Treatment("c50N"))', 'link_func': v_link_func}
if regress:
if mname=='msm':
m=hddm.HDDMRegressor(data, vreg, depends_on={'v':'stim', 'z':'cue'}, bias=True, informative=False, include=['v', 'z', 't', 'a'])
elif mname=='pbm':
m=hddm.HDDM(data, depends_on={'v':'stim', 'z':'cue'}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
elif mname=='dbm':
dbm = hddm.HDDMRegressor(data, vreg, depends_on={'v':'stim'}, bias=False, informative=False, include=['v', 't', 'a'])
elif mname=='dbmz':
m=hddm.HDDMRegressor(data, vreg, depends_on={'v':'stim'}, bias=True, informative=False, include=['v', 'z', 't', 'a'])
else:
if mname=='msmt':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue'], 'z':'cue', 't':['stim', 'cue']}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
elif mname=='msm':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue'], 'z':'cue'}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
elif mname=='pbm':
m=hddm.HDDM(data, depends_on={'v':'stim', 'z':'cue'}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
elif mname=='dbm':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue']}, informative=False, bias=False, include=['v', 't', 'a'])
elif mname=='dbmz':
m=hddm.HDDM(data, depends_on={'v':['stim', 'cue']}, informative=False, bias=True, include=['v', 'z', 't', 'a'])
return m
def build_model(mname, project='imaging'):
m=define_model(mname, project)
m=load_traces(m, mname, project)
return m
def check_model(mname):
mname_list=['msmt', 'msm', 'pbm', 'dbm', 'dbmz']
if mname not in mname_list:
print "mname not recognized: must be 'msmt', 'msm', 'pbm', or 'dbm'"
exit()
else:
print "building ", mname
def find_data(mname, project='imaging'):
pth=find_path()
if project=='behav':
data=pd.read_csv(pth+"beh_hddm/allsx_feat.csv")
else:
data=pd.read_csv(pth+"img_hddm/allsx_ewma.csv")
return data
def load_traces(m, mname, project='imaging'):
pth=find_path()
if project=='behav':
m.load_db(pth+"beh_hddm/"+mname+"/"+mname+"_traces.db", db='pickle')
else:
m.load_db(pth+"img_hddm/"+mname+"/"+mname+"_traces.db", db='pickle')
return m
def build_avgm(m, project='imaging'):
avgm=m.get_average_mname()
avgm=load_traces(avgm, project)
return avgm
if __name__=="__main__":
main()