-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn_analysis.py
More file actions
executable file
·134 lines (98 loc) · 3.82 KB
/
nn_analysis.py
File metadata and controls
executable file
·134 lines (98 loc) · 3.82 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
#!/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
# NN
methods = ['fhi360_small_lite','fhi360_conc_large_lite','pls_fhi360_conc']
# 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)
# create pls
pls_conc = pad_analysis.pls(coefficients_file)
# setup query
QUERY1 = 'SELECT `id`,`date_of_creation`,`notes`,`processed_file_location` FROM `card` WHERE `category`="FHI2022"'
#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)
count = 0
doBreak = True
# loop
for row in cur.fetchall() :
#print(row[0])
count += 1
# grab image from server
urllib.request.urlretrieve(url + row[3], dest)
# do analysis
pred_drug, conf = cat_nn.catagorize(dest)
nn_concentration, conc_conf = conc_nn.catagorize(dest)
pls_concentration = pls_conc.quantity(dest, pred_drug)
# arrays
results = [pred_drug, nn_concentration, '{0:.2f}'.format(pls_concentration)]
confs = [conf, conc_conf, 1.0]
# get notes text
notes_json = json.loads(row[2])
# get data
app_device_type = notes_json["App type"]
#print(count,"Device", app_device_type, "NN cat", row[0], pred_drug, conf,"PLS", pls_concentration, "NN conc", nn_concentration, "Db", row[2])
# cal_nn = notes_json["Neural net"]
# cal_result = notes_json["Predicted drug"]
# cal_confidence = notes_json["Prediction score"]
# pls_result = notes_json["Quantity PLS"]
# conc_result = notes_json["Quantity NN"]
# print(device_type, cal_nn, cal_result, cal_confidence, pls_result, conc_result)
# create query
for i in range(3):
QUERY2 = 'INSERT INTO analysis(`id`, `device_type`, `method`, `version`, `result`, `confidence`) VALUES (%d,%s,%s,%s,%s,%.3f)' % \
(row[0], "\""+app_device_type+"\"", "\""+methods[i]+"\"", "\"1.0\"", "\""+results[i]+"\"", confs[i])
print(QUERY2)
try:
# execute query
cur.execute(QUERY2)
# commit your changes
db.commit()
print("Inserted", QUERY2)
except MySQLdb.IntegrityError:
print("Could not insert", QUERY2)
# if count > 1:
# break
#
# Close all cursors
cur.close()
# Close all databases
db.close()