-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviz_functions.py
193 lines (163 loc) · 7.09 KB
/
viz_functions.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 8 16:17:23 2020
@author: rfuchs
"""
import re
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from matplotlib.patches import Polygon
def plot_2Dcyto(X, y, q1, q2, colors = None, title = None):
''' Plot a 2D cytogram of dimension q1 vs dimension q2
X (n x nb_curves ndarray / pandas DataFrame): The curves representing the particles (Total FWS, SWS...)
y (numpy array or pandas Series): The label of the particles
q1 (str): The name of the column in X to plot on the first axis
q2 (str): The name of the column in X to plot on the second axis
colors (array): An array with the color codes of each PFG
title (str): The title to print on the graph
---------------------------------------------------------------------
returns None: A matplolib graph
'''
classes = list(set(y))
classes.sort()
if colors == None:
colors_codes = ['#96ceb4', 'gold', 'lawngreen', 'black', 'green', 'red',\
'purple', 'blue', 'brown', 'grey']
colors = dict(zip(classes, colors_codes[:len(classes)]))
fig, ax1 = plt.subplots(1,1, figsize=(11, 11))
for id_, label in enumerate(classes):
obs = X[y == label]
formatted_label = label.capitalize()
ax1.scatter(obs[q1], obs[q2], c = colors[label], label= formatted_label, s = 1.5)
ax1.legend(loc= 'upper left', shadow=True, fancybox=True,\
prop={'size': 14}, ncol=2, markerscale = 4.0)
axes_labels = ['$10^0$', '$10^1$', '$10^2$', '$10^3$', '$10^4$', '$10^5$',\
'$10^6$']
ax1.set_xticklabels(axes_labels, fontsize = 'xx-large')
ax1.set_yticklabels(axes_labels, fontsize = 'xx-large')
ax1.set_title('True: ' + q1 + ' vs ' + q2, fontsize = 'xx-large')
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xlabel(q1, fontsize = 'xx-large')
ax1.set_ylabel(q2, fontsize = 'xx-large')
ax1.set_xlim(1, 1*10**6)
ax1.set_ylim(1, 10**6)
plt.show()
def true_vs_pred_2Dcyto(preds, tn, q1, q2, loc = 'upper left', colors = None, title = None):
''' Plot two 2D cytograms to compare manual and automatic classifications
preds: The prediction data coming from the .parq file
tn (pandas DataFrame): The nomenclature used by the CNN
q1 (str): The name of the column in X to plot on the first axis
q2 (str): The name of the column in X to plot on the second axis
loc (str): The localisation of the title of the graph
colors (array): An array with the color codes of each PFG
title (str): The title to print on the graph
---------------------------------------------------------------------
returns None: A matplolib graph with two subplots
'''
if colors == None:
colors = ['#96ceb4', 'gold', 'lawngreen', 'black', 'green', 'red',\
'purple', 'blue', 'brown', 'grey']
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(16,4))
for id_, label in enumerate(list(tn['Particle_class'])):
obs = preds[preds['True PFG name'] == label]
ax1.scatter(obs[q1], obs[q2], c = colors[id_], label= label, s=1)
ax1.legend(loc= loc, shadow=True, fancybox=True, prop={'size':8})
ax1.set_title('True :' + q1 + ' vs ' + q2)
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xlabel(q1)
ax1.set_ylabel(q2)
ax1.set_xlim(1, 10**6)
ax1.set_ylim(1, 10**6)
for id_, label in enumerate(list(tn['Particle_class'])):
obs = preds[preds['Pred PFG name'] == label]
ax2.scatter(obs[q1], obs[q2], c = colors[id_], label= label, alpha=1, s=1)
ax2.legend(loc= loc, shadow=True, fancybox=True, prop={'size':8})
ax2.set_title('Pred :' + q1 + ' vs ' + q2)
ax2.set_xscale('log')
ax2.set_yscale('log')
ax2.set_xlabel(q1)
ax2.set_ylabel(q2)
ax2.set_xlim(1, 10**6)
ax2.set_ylim(1, 10**6)
plt.show()
def plot_decision_boundaries(preds, tn, q1, q2, loc = 'upper left', colors = None, title = None):
'''
Infer the decision boundaries using a convex hull around the predicted
points.
preds: The prediction data coming from the .parq file
tn (pandas DataFrame): The nomenclature used by the CNN
q1 (str): The name of the column in X to plot on the first axis
q2 (str): The name of the column in X to plot on the second axis
loc (str): The localisation of the title of the graph
colors (array): An array with the color codes of each PFG
title (str): The title to print on the graph
'''
if colors == None:
colors = ['#96ceb4', 'gold', 'lawngreen', 'black', 'green', 'red', 'purple', 'blue', 'grey']
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(16,4))
for id_, label in enumerate(list(tn['Particle_class'])):
obs = preds[preds['True FFT Label'] == label]
if len(obs) == 0:
continue
points = obs[[q1, q2]].values
hull = ConvexHull(points)
cent = np.mean(points, 0)
pts = []
for pt in points[hull.simplices]:
pts.append(pt[0].tolist())
pts.append(pt[1].tolist())
pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
p[0] - cent[0]))
pts = pts[0::2] # Deleting duplicates
pts.insert(len(pts), pts[0])
k = 1.1
#color = 'green'
poly = Polygon(k*(np.array(pts)- cent) + cent,
facecolor=colors[id_], alpha=0.2,\
label = label)
poly.set_capstyle('round')
ax1.add_patch(poly)
#ax1.fill(obs[q1], obs[q2], c = colors[id_], label = label)
ax1.legend(loc = loc, shadow=True, fancybox=True, prop={'size':8})
ax1.set_title('True :' + q1 + ' vs ' + q2)
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xlabel(q1)
ax1.set_ylabel(q2)
ax1.set_xlim(1, 10**6)
ax1.set_ylim(1, 10**6)
for id_, label in enumerate(list(tn['Particle_class'])):
obs = preds[preds['Pred FFT Label'] == label]
if len(obs) == 0:
continue
points = obs[[q1, q2]].values
hull = ConvexHull(points)
cent = np.mean(points, 0)
pts = []
for pt in points[hull.simplices]:
pts.append(pt[0].tolist())
pts.append(pt[1].tolist())
pts.sort(key=lambda p: np.arctan2(p[1] - cent[1],
p[0] - cent[0]))
pts = pts[0::2] # Deleting duplicates
pts.insert(len(pts), pts[0])
k = 1.1
#color = 'green'
poly = Polygon(k*(np.array(pts)- cent) + cent,
facecolor=colors[id_], alpha=0.2,\
label = label)
poly.set_capstyle('round')
ax2.add_patch(poly)
#ax1.fill(obs[q1], obs[q2], c = colors[id_], label = label)
ax2.legend(loc = loc, shadow=True, fancybox=True, prop={'size':8})
ax2.set_title('Pred :' + q1 + ' vs ' + q2)
ax2.set_xscale('log')
ax2.set_yscale('log')
ax2.set_xlabel(q1)
ax2.set_ylabel(q2)
ax2.set_xlim(1, 10**6)
ax2.set_ylim(1, 10**6)
plt.show()