-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d_plot.py
More file actions
105 lines (84 loc) · 2.57 KB
/
Copy path3d_plot.py
File metadata and controls
105 lines (84 loc) · 2.57 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
#imports
import numpy as np
import matplotlib as mpl
#mpl.use('Agg')
import matplotlib.pyplot as plt
# This import registers the 3D projection, but is otherwise unused.
from mpl_toolkits.mplot3d import Axes3D # noqa: F401 unused import
import matplotlib.cm as cmx
import matplotlib.colors as colors
#more imports
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PIL import Image
import os
import sys
# call with categorize_drugs.py drug_labels.csv categorize_labels.csv user.csv
#parameters?
if len(sys.argv) < 3:
print("Missing parameters.")
exit(-1)
#input data
filename = sys.argv[1] #"prediction_results.csv"
#read file
try:
pred_file = open(filename, "r")
pred_lines = pred_file.readlines()
except:
print("Missing input file.")
exit(-1)
#size
drug_size = int(pred_lines[0].split(',')[0].strip()) #10
#drugs
drugs = [] #['Paracetamol Starch', 'Penicillin Procaine', 'Starch', 'Lactose', 'Amoxicillin', 'Cellulose', 'Vitamin C', 'Quinine', 'Benzyl Penicillin', 'Paracetamol' ]
drug_list = pred_lines[3].split(',')
#loop over drugs and add
for drug in drug_list:
drugs.append(drug.strip())
#grab output from training
#matrix
m = np.empty([drug_size, drug_size])
#loop over rows
for i in range(0, drug_size):
#split row
m_line = pred_lines[4 + i].split(',')
#set elements
for j in range(0, drug_size):
try:
fl = float(m_line[j].strip())
m[i,j] = fl
except:
continue
# setup the figure and axes
fig = plt.figure(figsize=(8, 8))
# A canvas must be manually attached to the figure
canvas = FigureCanvasAgg(fig)
ax1 = fig.add_subplot(111, projection='3d')
#ax2 = fig.add_subplot(122, projection='3d')
# generate colors
cm = plt.get_cmap('jet')
vv = range(drug_size * drug_size)
cNorm = colors.Normalize(vmin=0, vmax=vv[-1])
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
colorVals = [scalarMap.to_rgba(i) for i in range(drug_size * drug_size)]
#normalize
m = ((m.T * 100.0)/m.sum(axis=1)).T
#flatten for plot
mf = m.flatten()
# axis data
_x = np.arange(drug_size)
_y = np.arange(drug_size)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
top = mf
bottom = np.zeros_like(top)
width = depth = .9
ax1.bar3d(x, y, bottom, width, depth, top, shade=True, color=colorVals)
ax1.set_title('Drug/Distractor accuracy')
ax1.xaxis.set_ticks(_x)
ax1.yaxis.set_ticks(_y)
ax1.set_xticklabels(drugs, fontsize=7, ha='right', va='center', ma='right')
ax1.set_yticklabels(drugs, fontsize=7, ha='left', va='bottom', ma='right')
ax1.set_zlabel('%')
#plt.show()
#save image
fig.savefig(sys.argv[2])