-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplotting
executable file
·230 lines (137 loc) · 4.56 KB
/
plotting
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 26 00:24:36 2019
@author: jha
"""
#%%
import csv
import mysql.connector as mariadb
from tabulate import tabulate
import pandas as pd
import numpy as np
amrdb= mariadb.connect(
host="localhost",
user="root",
passwd="Sukhoi@90",
database ="myamr"
)
cursor = amrdb.cursor(buffered=True) # else it fetches one row for everytime it is executed
print("We are at line 31 we have connection, lets begin")
#%%
#%%
dfplot=pd.DataFrame(columns=["antibiotics","SS","SnS","ECS","ECnS","CS","CnS","LS","LnS"])
list_antibiotics=[]
lst=[]
#%%
#%%
numrows= cursor.execute("SELECT antibiotics FROM ECnS ")
print("Selected %s rows" %numrows)
print("Selected %s rows " %cursor.rowcount)
lst=list(cursor.fetchall())#fetch all rows at once
for i in lst:
if i not in list_antibiotics:
#print("yo",i[0])
list_antibiotics.append(i)
#%%
#%%
list_anti=[]
for tup in list_antibiotics:
for val in tup:
list_anti.append(val)
se=pd.Series(list_anti)
dfplot["antibiotics"]= se.values
#
#%%
#%%
numrows= cursor.execute("SELECT antibiotics,percent_nonS FROM SNS ")
print("Selected %s rows" %numrows)
print("Selected %s rows " %cursor.rowcount)
lst=list(cursor.fetchall())#fetch all rows at once
lstd=dict(lst)
dfplot['SnS'] = dfplot['antibiotics'].map(lstd)
#%%
#%%
from sqlalchemy import create_engine
cnx = create_engine('mysql+pymysql://root:Sukhoi@90@localhost/myamr')
dfplot.to_sql(name='astall', con=cnx, if_exists = 'replace', index=False)
print("we are at line 356 in code")
#%%
#susceptible
#%%
# libraries
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
# set height of bar
bars1 = dfplot["SS"]#.sort_values('SS',ascending=False)
bars2 = dfplot["CS"]#.sort_values('CS',ascending=False)
bars3 = dfplot["ECS"]#.sort_values('ECS',ascending=False)
bars4 = dfplot["LS"]#.sort_values('LS',ascending=False)
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + 4*barWidth for x in r1]
r3 = [x + 3*barWidth for x in r2]
r4 = [x+ 2*barWidth for x in r3]
# Make the plot
plt.bar(r1, bars1, color='cadetblue', width=barWidth, edgecolor='white', label='Salmonella')
plt.bar(r2, bars2, color='cornflowerblue', width=barWidth, edgecolor='white', label='Campylobacter')
plt.bar(r3, bars3, color='navy', width=barWidth, edgecolor='white', label='Ecoli')
plt.bar(r4, bars4, color='slateblue', width=barWidth, edgecolor='white', label='Listeria')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], dfplot["antibiotics"], rotation=90)
plt.title("percentage susceptibility of 4 microbes for 50 antibiotics" )
plt.xlabel("antibiotics")
plt.ylabel("percentage susceptibility")
# Create legend & Show graphic
plt.legend()
plt.show()
#%%
#NON susceptible
#%%
# libraries
import numpy as np
import matplotlib.pyplot as plt
# set width of bar
barWidth = 0.25
dfsns=dfplot.sort_values("SnS",ascending=False)
dfcns=dfplot.sort_values("CnS",ascending=False)
dfecns=dfplot.sort_values("ECnS",ascending=False)
dflns=dfplot.sort_values("LnS",ascending=False)
# set height of bar
bars1 = dfsns["SnS"]#.sort_values('SS',ascending=False)
bars2 = dfcns["CnS"]#.sort_values('CS',ascending=False)
bars3 = dfecns["ECnS"]#.sort_values('ECS',ascending=False)
bars4 = dflns["LnS"]#.sort_values('LS',ascending=False)
# Set position of bar on X axis
r1 = np.arange(len(bars1))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
r4 = [x + barWidth for x in r3]
# Make the plot
plt.bar(r1, bars1, color='red', width=barWidth, edgecolor='white', label='Salmonella')
plt.bar(r2, bars2, color='salmon', width=barWidth, edgecolor='white', label='Campylobacter')
plt.bar(r3, bars3, color='brown', width=barWidth, edgecolor='white', label='Ecoli')
plt.bar(r4, bars4, color='chocolate', width=barWidth, edgecolor='white', label='Listeria')
# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(bars1))], dfplot["antibiotics"], rotation=90)
plt.title("percentage of non-susceptibility of 4 microbes for 50 antibiotics" )
plt.xlabel("antibiotics")
plt.ylabel("percentage non-susceptible")
plt.tight_layout()
plt.legend(loc='upper right')
# Create legend & Show graphic
plt.legend()
plt.show()
#%%
#%%
dfplotT=dfplot.T
dfplotT=dfplotT.reset_index()
dfplotT.columns=dfplotT.iloc[0]
dfplotT=dfplotT.drop([0])
dfplotT=dfplotT.reset_index()
dfplotT=dfplotT.drop(["index"])
#%%