-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_play.py
More file actions
executable file
·141 lines (108 loc) · 4.17 KB
/
nn_play.py
File metadata and controls
executable file
·141 lines (108 loc) · 4.17 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
#!/usr/bin/python
from urllib.parse import DefragResultBytes
import MySQLdb
from PIL import Image, ImageEnhance, ImageStat
import json
import MySQLdb
import numpy as np
import tensorflow as tf
from tensorflow import keras
import PIL
import urllib.request
from zipfile import ZipFile
import regionRoutine
import cv2 as cv
import csv
import pad_analysis
# grab image other defines
url = 'http://pad.crc.nd.edu'
dest = './temp.png'
output_file = 'accuracy_conc_1b.csv'
# drug labels
cat_labels = ['Albendazole','Amoxicillin','Ampicillin','Azithromycin','Benzyl','Penicillin','Ceftriaxone','Chloroquine','Ciprofloxacin','Doxycycline','Epinephrine','Ethambutol','Ferrous,Sulfate','Hydroxychloroquine','Isoniazid','Promethazine','Hydrochloride','Pyrazinamide','Rifampicin','RIPE','Sulfamethoxazole','Tetracycline','Distractor']
# set lite model
cat_model_file = '/var/www/html/joomla/neuralnetworks/tf_lite/fhi360_small_lite/1.0/fhi360_small_1_21.tflite'
conc_model_file = '/var/www/html/joomla/neuralnetworks/tf_lite/fhi360_conc_large_lite/1.0/fhi360_conc_large_1_21.tflite'
# pls
coefficients_file = "/var/www/html/joomla/neuralnetworks/pls/fhi360_concentration/1.0/pls_fhi360_conc_coefficients.csv"
# create cat nn
cat_nn = pad_analysis.pad_neural_network(cat_model_file)
# create conc nn
conc_nn = pad_analysis.pad_neural_network(conc_model_file)
# creat pls
pls_conc = pad_analysis.pls(coefficients_file)
# setup query
QUERY1 = 'SELECT `sample_name`,`processed_file_location`,`quantity`,`id`,`sample_id` FROM `card` WHERE `category`="FHI2022"' # AND `notes` LIKE "%Predicted drug = %" AND `notes` NOT LIKE "%Notes version%"'
# AND `notes` LIKE "%(pls%"
#get database credentials
with open('credentials.txt') as f:
line = f.readline()
split = line.split(",")
f.close()
#open database
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user=split[0], # your username
passwd=split[1], # your password
db="pad") # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cur = db.cursor()
# get list of samples
cur.execute(QUERY1)
# setup for loop
drug_correct = {}
drug_total = {}
for drug in cat_labels:
drug_correct[drug.lower()] = 0
drug_total[drug.lower()] = 0
# drug_correct['distractor'] = 0
# drug_total['distractor'] = 0
count = 0
doBreak = True
# loop through CSV
with open(output_file,'w') as myFile:
# loop
for row in cur.fetchall() :
#print(row[0])
count += 1
# increment Drugs
if row[0].lower() in drug_correct:
drug_total[row[0].lower()] += 1
else:
continue
#drug_total['distractor'] += 1
# grab image from server
urllib.request.urlretrieve(url + row[1], dest)
# do analysis
pred_drug, conf = cat_nn.catagorize(dest)
#print("NN", row[0], pred_drug, conf,)
if row[0].lower() == pred_drug.lower():
if row[0].lower() in drug_correct:
drug_correct[row[0].lower()] += 1
# else:
# drug_correct['distractor'] += 1
nn_concentration, conc_conf = conc_nn.catagorize(dest)
pls_concentration = pls_conc.quantity(dest, pred_drug)
print(count,"NN cat", row[0], pred_drug, conf,"PLS", pls_concentration, "NN conc", nn_concentration, "Db", row[2])
# save info
myFile.write(str(row[4]) + ',' + str(row[3]) + ',' + row[0] + ',' + pred_drug + ',' + str(conf) + ',' + str(row[2]) + ',' + str(nn_concentration) + ',' + str(pls_concentration) + '\n')
# if count > 10:
# break
# calculate ratios
drug_ratio = {}
for drug in cat_labels:
if drug_total[drug.lower()] != 0:
drug_ratio[drug.lower()] = float(drug_correct[drug.lower()]) / float(drug_total[drug.lower()])
else:
drug_ratio[drug.lower()] = 0.
# if drug_total['distractor'] != 0:
# drug_ratio['distractor'] = float(drug_correct['distractor']) / float(drug_total['distractor'])
# else:
# drug_ratio['distractor'] = 0.
print("Count",count, drug_ratio)
print(drug_correct, drug_total)
myFile.close()
# Close all cursors
cur.close()
# Close all databases
db.close()