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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#!/var/www/html/compbio/BNW_1.3/bnw-env/bin/python3
import os
import sys
import plotly
import plotly.graph_objs as go
import csv
import string
netID = sys.argv[-1]
outfile = netID+"ts_plotly.html"
filename=netID+"ts_output.txt"
f=open(filename,"r")
lines=f.readlines()
#Read the last line to get the variable name
line=lines.pop()
#line = map(string.strip,line.strip().split(" "))
line = line.strip().split(" ")
varName = line[4][:-1]
plot_title=varName+" Test Set Predictions"
#print varName
header=lines.pop(0)
#Read type file to determine if it is continuous or discrete
typefile = netID+"type.txt"
tf=open(typefile,"r")
line=tf.readline()
#varnames = map(string.strip,line.strip().split("\t"))
varnames = line.strip().split("\t")
line=tf.readline()
#vartypes = map(string.strip,line.strip().split("\t"))
vartypes = line.strip().split("\t")
varindex = varnames.index(varName)
cd_type = int(vartypes[varindex])
if cd_type == 1:
#Make scatterplot for continuous_data
#Read introductory lines from file
# for i in range(6):
# line = f.readline()
#Read the data
x = []
y = []
# line = f.readline()
# while line:
for line in lines:
#line = map(string.strip,line.strip().split("\t"))
line = line.strip().split("\t")
if line[1] != 'NA':
x.append(float(line[1]))
y.append(float(line[2]))
# line=f.readline()
data = [go.Scatter(x=x,y=y,mode='markers')]
layout = go.Layout(
title=plot_title,
titlefont=dict(
family='Arial, sans-serif',
size=24,
color='black'
),
title_xref="paper",
title_x=0.5,
title_xanchor="center",
title_yanchor="middle",
xaxis=dict(
autorange=True,
title='Actual values',
titlefont=dict(
family='Arial, sans-serif',
size=18,
color='black'
),
),
yaxis=dict(
autorange=True,
title='Predicted values',
titlefont=dict(
family='Arial, sans-serif',
size=18,
color='black'
),
),
margin=dict(t=30,l=50,b=40)
)
fig = go.Figure(data=data, layout = layout)
plotly.offline.plot(fig,filename=outfile)
else:
#Make bar chart for discrete data
#Read introductory lines from file
# for i in range(5):
# line = f.readline()
#Get names of states
#header = map(string.strip,header.strip().split("\t"))
header = header.strip().split("\t")
states = header[2:]
#Read the data
actual = []
predicted = []
# line = f.readline()
# while line:
for line in lines:
#line = map(string.strip,line.strip().split("\t"))
line = line.strip().split("\t")
if line[1] != 'NA':
actual.append(line[1])
predict_x = line[2:]
predict_x = [float(x) for x in predict_x]
max_value = max(predict_x)
max_index = predict_x.index(max_value)
predicted.append(states[max_index])
#check if multiple states are equally likely to be predicted states
#I am not going to count these as being predicted here
max_items = [x for x in predict_x if (abs(x-max_value) < 0.000001)]
if len(max_items) > 1:
predicted.pop()
actual.pop()
# line=f.readline()
#Go through states and get number of true positives, false positives, and false negatives
tp_all = []
fp_all = []
fn_all = []
for state in states:
tp = 0
fp = 0
fn = 0
for i in range(len(actual)):
actual_i = actual[i]
predicted_i = predicted[i]
if actual_i == state:
if predicted_i == state:
tp = tp + 1
else:
fn = fn + 1
elif predicted_i == state:
fp = fp + 1
tp_all.append(tp)
fn_all.append(fn)
fp_all.append(fp)
trace1 = go.Bar(x=states,y=tp_all,name="True Positives")
trace2 = go.Bar(x=states,y=fn_all,name="False Negatives")
trace3 = go.Bar(x=states,y=fp_all,name="False Positives")
data = [trace1,trace2,trace3]
layout = go.Layout(
barmode='group',
title=plot_title,
titlefont=dict(
family='Arial, sans-serif',
size=24,
color='black'
),
title_xref="paper",
title_x=0.5,
title_xanchor="center",
title_yanchor="middle",
xaxis=dict(
autorange=True,
title='State',
titlefont=dict(
family='Arial, sans-serif',
size=18,
color='black'
),
),
yaxis=dict(
autorange=True,
title='Number of cases',
titlefont=dict(
family='Arial, sans-serif',
size=18,
color='black'
),
),
margin=dict(t=30,l=50,b=40)
)
fig = go.Figure(data=data, layout = layout)
plotly.offline.plot(fig,filename=outfile)
|