forked from amunson27/EyeblinkRig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly_plot.py
More file actions
157 lines (135 loc) · 3.18 KB
/
plotly_plot.py
File metadata and controls
157 lines (135 loc) · 3.18 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
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
151
152
153
154
155
156
157
import plotly
print 'plotly.__version__=', plotly.__version__ # version >1.9.4 required
import plotly.graph_objs as go
import pandas
import numpy as np
import os.path
def myplotlyplot(filePath,output_type):
'''output_type: {filePath, div}'''
print 'myplotlyplot()', filePath
#read data, row 1 is column headers
print '\tpandas.read_csv'
df = pandas.read_csv(filePath, header=1)
print '\tparsing'
#get millis of rows labeled scanimageframe
xTrial = df[(df['event'] == 'startTrial') | (df['event']=='stopTrial')].millis
xFrames = df[df['event']=='scanimageframe'].millis
xMotor = df[(df['event'] == 'motorstart') | (df['event']=='motorstop')].millis
xRotary = df[(df['event'] == 'rotary')].millis
#normalize to startTrial and convert to seconds
startMillis = xTrial.iloc[0]
xTrial -= startMillis
xFrames -= startMillis
xMotor -= startMillis
xRotary -= startMillis
xTrial /= 1000
xFrames /= 1000
xMotor /= 1000
xRotary /= 1000
# number of rows
numTrial = xTrial.count()
numFrames = xFrames.count()
numMotor = xMotor.count()
numRotary = xRotary.count()
#fake out some y values
yTrial = np.zeros(numTrial) + 1
yFrames = np.zeros(numFrames) + 2
yMotor = np.zeros(numMotor) + 3
yRotary = np.zeros(numRotary) + 4
#vertical line for each scanimage frame
xFrames2 = np.empty(numFrames*3)
yFrames2 = np.empty(numFrames*3)
outIdx = 0
for i in xFrames:
xFrames2[outIdx] = i
xFrames2[outIdx+1] = i
xFrames2[outIdx+2] = None
yFrames2[outIdx] = 0
yFrames2[outIdx+1] = 5
yFrames2[outIdx+2] = None
outIdx += 3
'''
layout = go.Layout(
title = "file:" + filePath,
yaxis = dict(range=[0.5,4.5])
)
'''
layout = {
'title': 'file:' + filePath,
'xaxis': {
#'range': [0, 5],
'zeroline': False,
},
'yaxis': {
'range': [0, 5],
'showgrid': False,
},
'width': 500,
'height': 300,
}
layout['shapes'] = []
for i in np.arange(0, numMotor-1, 2):
myrect = {
'type': 'rect',
# x-reference is assigned to the x-values
'xref': 'x',
# y-reference is assigned to the plot paper [0,1]
'yref': 'paper',
'x0': xMotor.iloc[i],
'y0': 0,
'x1': xMotor.iloc[i+1],
'y1': 1,
'fillcolor': '#888888',
'opacity': 0.6,
'line': {
'width': 0,
}
}
#print xMotor.iloc[i]
layout['shapes'].append(myrect)
i += 1
#layout['shapes'] = [myrect,]
trace0 = go.Scatter(
x = xTrial,
y = yTrial,
mode = 'lines+markers',
name = 'this_trial'
)
trace1 = go.Scatter(
x = xFrames2,
y = yFrames2,
mode = 'lines',
name = 'frames',
marker = dict(
size = 10,
color = 'rgba(50, 255, 50, .9)',
line = dict(
width = 2,
)
)
)
trace2 = go.Scatter(
x = xMotor,
y = yMotor,
mode = 'lines+markers',
name = 'motor'
)
trace3 = go.Scatter(
x = xRotary,
y = yRotary,
mode = 'markers',
name = 'rotary'
)
data = [trace0, trace1, trace2, trace3]
#fig = go.Figure(data=data, layout=layout)
fig = {
'data': data,
'layout': layout,
}
#output_type = 'file'
#output_type = 'div'
fileordiv = plotly.offline.plot(fig, filename='templates/xxx.html', output_type=output_type, auto_open=False)
return fileordiv
if __name__ == '__main__':
file = 'data/20160306_211227_t4_d.txt'
myplotlyplot(file)