-
Notifications
You must be signed in to change notification settings - Fork 6
/
view_correlations.py
272 lines (237 loc) · 9.92 KB
/
view_correlations.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Exploring correlations between two variables
"""
__author__ = 'Diego'
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
import matplotlib
from vcorr.qt_models import VarListModel
matplotlib.use("Qt4Agg")
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from vcorr.gui.correlations_gui import Ui_correlation_app
import numpy as np
import seaborn as sns
import scipy.stats
import pandas as pd
class CorrelationMatrixFigure(FigureCanvas):
SquareSelected = QtCore.pyqtSignal(pd.DataFrame)
def __init__(self,large_df):
self.f, self.ax = plt.subplots(figsize=(9, 9))
plt.tight_layout()
super(CorrelationMatrixFigure, self).__init__(self.f)
palette = self.palette()
self.f.set_facecolor(palette.background().color().getRgbF()[0:3])
self.df = None
self.corr = None
self.cmap = sns.blend_palette(["#00008B", "#6A5ACD", "#F0F8FF",
"#FFE6F8", "#C71585", "#8B0000"], as_cmap=True)
self.mpl_connect("motion_notify_event", self.get_tooltip_message)
self.mpl_connect("button_press_event",self.square_clicked)
self.large_df = large_df
self.on_draw()
def on_draw(self):
plt.sca(self.ax)
plt.clf()
self.ax = plt.axes()
if self.df is None:
message = "Select two or more variables from list"
self.ax.text(0.5, 0.5, message, horizontalalignment='center',
verticalalignment='center', fontsize=16)
else:
plt.sca(self.ax)
sns.corrplot(self.df, annot=False, sig_stars=True, cmap_range="full",
diag_names=False, sig_corr=False, cmap=self.cmap, ax=self.ax, cbar=True)
plt.tight_layout()
self.draw()
def set_variables(self, vars_list):
#print vars_list
if len(vars_list) < 2:
self.df = None
self.corr = None
else:
self.df = self.large_df[vars_list].copy()
self.corr = self.df.corr()
self.on_draw()
def get_tooltip_message(self, event):
QtGui.QToolTip.hideText()
if event.inaxes == self.ax and self.df is not None:
x_int, y_int = int(round(event.xdata)), int(round(event.ydata))
if y_int <= x_int:
return
x_name, y_name = self.df.columns[x_int], self.df.columns[y_int]
r = self.corr.loc[x_name, y_name]
message = "%s v.s. %s: r = %.2f" % (x_name, y_name, r)
_, height = self.get_width_height()
point = QtCore.QPoint(event.x, height - event.y)
g_point = self.mapToGlobal(point)
QtGui.QToolTip.showText(g_point, message)
def square_clicked(self,event):
if event.inaxes == self.ax and self.df is not None:
x_int , y_int = int(round(event.xdata)) , int(round(event.ydata))
if y_int<=x_int:
return
x_name,y_name = self.df.columns[x_int],self.df.columns[y_int]
df2 = self.df[[x_name,y_name]]
self.SquareSelected.emit(df2)
class RegFigure(FigureCanvas):
def __init__(self):
self.f, self.ax = plt.subplots(figsize=(9, 9))
super(RegFigure, self).__init__(self.f)
palette = self.palette()
self.f.set_facecolor(palette.background().color().getRgbF()[0:3])
self.draw_initial_message()
self.mpl_connect("motion_notify_event",self.motion_to_pick)
self.mpl_connect("pick_event",self.draw_tooltip)
self.hidden_subjs = set()
self.df = None
self.df2 = None
self.dfh = None
self.scatter_h_artist=None
self.limits = None
def draw_initial_message(self):
self.ax.clear()
message = "Click in the correlation matrix"
self.ax.text(0.5, 0.5, message, horizontalalignment='center',
verticalalignment='center', fontsize=16)
plt.sca(self.ax)
plt.tight_layout()
self.draw()
def draw_reg(self,df):
assert df.shape[1] == 2
#print df
self.ax.clear()
plt.sca(self.ax)
plt.sca(self.ax)
df = df.dropna()
self.df = df.copy()
plt.tight_layout()
self.limits = None
self.ax.set_xlim(auto=True)
self.ax.set_ylim(auto=True)
self.re_draw_reg()
def re_draw_reg(self):
self.ax.clear()
i2 = [i for i in self.df.index if i not in self.hidden_subjs]
df2 = self.df.loc[i2]
self.df2 = df2
y_name,x_name = df2.columns
x_vals=df2[x_name].get_values()
y_vals=df2[y_name].get_values()
sns.regplot(x_name,y_name,df2,ax=self.ax,scatter_kws={"picker":5,})
mat = np.column_stack((x_vals,y_vals))
mat = mat[np.all(np.isfinite(mat),1),]
m,b,r,p,e = scipy.stats.linregress(mat)
plot_title = "r=%.2f p=%.5g"%(r,p)
self.ax.set_title(plot_title)
#print e
self.ax.set_title(plot_title)
if self.limits is not None:
xl,yl = self.limits
self.ax.set_xlim(xl[0],xl[1],auto=False)
self.ax.set_ylim(yl[0],yl[1],auto=False)
else:
self.limits = (self.ax.get_xlim(),self.ax.get_ylim())
ih = [i for i in self.df.index if i in self.hidden_subjs]
dfh = self.df.loc[ih]
self.dfh = dfh
current_color = matplotlib.rcParams["axes.color_cycle"][0]
self.scatter_h_artist=self.ax.scatter(dfh[x_name].get_values(),dfh[y_name].get_values(),
edgecolors=current_color,facecolors="None",urls=ih,picker=2)
self.draw()
def motion_to_pick(self,event):
self.ax.pick(event)
def draw_tooltip(self,event):
QtGui.QToolTip.hideText()
mouse_event = event.mouseevent
if isinstance(event.artist,matplotlib.collections.PathCollection):
index = event.ind
message_pieces=[]
#if the pick involves different subjects
if event.artist == self.scatter_h_artist:
dfp = self.dfh
else:
dfp = self.df2
for i in index:
datum = dfp.iloc[[i]]
message = "Subject %s\n%s : %g\n%s : %g"%\
(datum.index[0],
datum.columns[0],datum.iloc[0,0],
datum.columns[1],datum.iloc[0,1],)
message_pieces.append(message)
big_message="\n\n".join(message_pieces)
_,height = self.get_width_height()
point = QtCore.QPoint(event.mouseevent.x,height-event.mouseevent.y)
g_point = self.mapToGlobal(point)
QtGui.QToolTip.showText(g_point,big_message)
if mouse_event.button == 1:
if len(index) == 1:
name = datum.index[0]
if event.artist == self.scatter_h_artist:
print "recovering %s"%name
self.hidden_subjs.remove(name)
else:
print "hidding %s"%name
self.hidden_subjs.add(name)
self.re_draw_reg()
def selection_changed(self,selection):
if self.df is None:
return
sel_set = set(selection)
current_vars = set(self.df.columns)
if not current_vars <= sel_set:
#current vars are not contained in current selection
self.df = None
self.draw_initial_message()
def handle_clicks(self,event):
pass
class CorrelationsApp(QtGui.QMainWindow):
def __init__(self,data_frame):
super(CorrelationsApp, self).__init__()
self.ui = None
self.cor_mat = CorrelationMatrixFigure(data_frame)
self.reg_plot = RegFigure()
self.vars_model = VarListModel(checkeable=True)
self.vars_model.set_variables(data_frame.columns)
self.setup_ui()
def setup_ui(self):
self.ui = Ui_correlation_app()
self.ui.setupUi(self)
self.ui.variables_list.setModel(self.vars_model)
self.ui.cor_layout = QtGui.QHBoxLayout()
self.ui.cor_mat_frame.setLayout(self.ui.cor_layout)
self.ui.cor_layout.addWidget(self.cor_mat)
self.vars_model.CheckedChanged.connect(self.cor_mat.set_variables)
self.vars_model.CheckedChanged.connect(self.reg_plot.selection_changed)
self.ui.reg_layout = QtGui.QHBoxLayout()
self.ui.reg_frame.setLayout(self.ui.reg_layout)
self.ui.reg_layout.addWidget(self.reg_plot)
self.cor_mat.SquareSelected.connect(self.reg_plot.draw_reg)
self.ui.actionSave_Matrix.triggered.connect(self.save_matrix)
self.ui.actionSave_Scatter.triggered.connect(self.save_reg)
def save_matrix(self):
filename = unicode(QtGui.QFileDialog.getSaveFileName(self,
"Save Matrix",".","PDF (*.pdf);;PNG (*.png);;svg (*.svg)"))
self.cor_mat.f.savefig(filename)
def save_reg(self):
filename = unicode(QtGui.QFileDialog.getSaveFileName(self,
"Save Scatter",".","PDF (*.pdf);;PNG (*.png);;svg (*.svg)"))
self.reg_plot.f.savefig(filename)
def remove_non_ascii(s):
return str("".join(i for i in s if ord(i)<128))
if __name__ == "__main__":
app = QtGui.QApplication([])
qt_name = QtGui.QFileDialog.getOpenFileName(None,"Select Data",".","csv (*.csv)")
file_name = str(qt_name.toAscii())
try:
with open(file_name) as in_file:
df = pd.read_csv(in_file,na_values="#NULL!",index_col=0)
except Exception:
df = None
if df is None or len(df.columns) == 0:
#try again with "french" excel defaults
df = pd.read_csv(file_name,index_col=0,sep=";",decimal=",")
df.columns = map(remove_non_ascii,df.columns)
main_window = CorrelationsApp(df)
main_window.show()
app.exec_()