-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_utils.py
217 lines (150 loc) · 5.52 KB
/
plot_utils.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
#
# Utilities for plotting
#
#
#
import logging
import itertools
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
FORMAT = '%(asctime)-15s- %(levelname)s - %(name)s -%(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger = logging.getLogger(__name__)
def plot_corr(data,thevars=None):
"""
Plot correlation matrix
"""
df = data.fillna(value=0) # this returns a copy of the original dataframe
if thevars==None:
thevars=df.columns
corr = np.abs(df[thevars].corr())
mask = np.zeros_like(corr, dtype=bool)
mask[np.triu_indices_from(corr,0)] = True
corr[mask]=np.nan
fig, ax = plt.subplots()
ax.matshow(corr)
ax.set_xticks(range(len(thevars)))
ax.set_yticks(range(len(thevars)))
ax.set_xticklabels(thevars)
ax.set_yticklabels(thevars)
for (x, y), value in np.ndenumerate(corr):
if (y<x):
ax.text(y, x, f"{value:.2f}", va="center", ha="center")
corr = corr.stack().reset_index()
logger.info(corr)
return plt
def plot_pairs(data, thevars=None) :
df = data.fillna(value=0) # this returns a copy of the original dataframe
if thevars==None:
thevars=df.columns
pairs = list(itertools.combinations(thevars,2))
nvars = len(thevars)
if (nvars%2):
n = nvars-1
m = nvars
else:
n = nvars
m = nvars-1
n=int(n/2)
fig, axs = plt.subplots(nrows=n, ncols=m, squeeze=False)
for ax, ind in zip(axs.flat, pairs):
ax.set_title('')
ax.hist2d(x=df[ind[0]], y=df[ind[1]], bins=(50,50))
ax.set_xlabel(ind[0])
ax.set_ylabel(ind[1])
return plt
def plot_diff(data, targetvar, sbvar=None, varstoplot=None, nrows=3, ncols=3, bins=100) :
"""
plot dfs (signal) - dfsb (sideband) vs dfbkg (background)
"""
df = data.fillna(value=0) # this returns a copy of the original dataframe
if (sbvar):
dfs = df.loc[(df[targetvar]==1) & (df[sbvar]==1)]
dfsb = df.loc[(df[targetvar]==0) & (df[sbvar]==1)]
dfbkg = df.loc[(df[targetvar]==0)]
else:
dfs = df.loc[(df[targetvar]==1)]
dfsb = df.loc[(df[targetvar]==0)]
dfbkg = df.loc[(df[targetvar]==0)]
if varstoplot==None:
varstoplot=[col for col in df.columns]
nvars = len(varstoplot)
fig, axs = plt.subplots(nrows=nrows, ncols=ncols, squeeze=False)
for ax, var in zip(axs.flat, varstoplot):
_hs,_edges = np.histogram(dfs[var].to_numpy(), bins=bins)
_hsb,_edges = np.histogram(dfsb[var].to_numpy(), bins=_edges)
_hb,_edges = np.histogram(dfbkg[var].to_numpy(), bins=_edges)
width = 1.0 * (_edges[1] - _edges[0])
center = (_edges[:-1] + _edges[1:]) / 2
with np.errstate(divide='ignore', invalid='ignore'):
hists = (_hs-_hsb)/np.sum(_hs-_hsb)
histb = _hb/np.sum(_hb)
np.nan_to_num(hists, copy=False, nan=0.0, posinf=0.0, neginf=0.0)
np.nan_to_num(histb, copy=False, nan=0.0, posinf=0.0, neginf=0.0)
ax.bar(center, hists, align='center', width=width)
ax.bar(center, histb, align='center', width=width, alpha = 0.6)
ax.set_xlabel(var)
return plt
def plot_roc(y, out, sb=None) :
"""
ToDo: replace ns0 etc. with a formula related to a matrix of the probabilities in each bag
"""
data = np.column_stack([y,out])
df = pd.DataFrame(data,columns=['y','out'])
if sb is not None:
df['sb'] = sb
else:
df['sb'] = 1
minout = df['out'].min()
maxout = df['out'].max()
seff=np.array([])
beff=np.array([])
yts =np.array([])
ns0=len(df.loc[ (df.y==1) & (df.sb==1) ]) - len(df.loc[ (df.y==0) & (df.sb==1) ])
nb0=len(df.loc[ (df.y==0) ] )
for yt in np.linspace(minout,maxout,100):
ns=len(df.loc[ (df.y==1) & (df.sb==1) & (df.out>yt)]) - len(df.loc[ (df.y==0) & (df.sb==1) & (df.out>yt)])
nb=len(df.loc[ (df.y==0) & (df.out>yt) ] )
yts = np.append(yts, yt)
seff = np.append(seff,ns/ns0)
beff = np.append(beff,nb/nb0)
brej = 1.-beff
plt.plot(brej,seff,'o-')
return plt
def plot_rate(y, out, rate, sb=None) :
"""
ToDo: replace ns0 etc. with a formula related to a matrix of the probabilities in each bag
"""
data = np.column_stack([y,out])
df = pd.DataFrame(data,columns=['y','out'])
if sb is not None:
df['sb'] = sb
else:
df['sb'] = 1
minout = df['out'].min()
maxout = df['out'].max()
seff=np.array([])
beff=np.array([])
yts =np.array([])
ns0=len(df.loc[ (df.y==1) & (df.sb==1) ]) - len(df.loc[ (df.y==0) & (df.sb==1) ])
nb0=len(df.loc[ (df.y==0) ] )
for yt in np.linspace(minout,maxout,1000):
ns=len(df.loc[ (df.y==1) & (df.sb==1) & (df.out>yt)]) - len(df.loc[ (df.y==0) & (df.sb==1) & (df.out>yt)])
nb=len(df.loc[ (df.y==0) & (df.out>yt) ] )
yts = np.append(yts, yt)
seff = np.append(seff,ns/ns0)
beff = np.append(beff,nb/nb0)
brej = beff * rate
plt.plot(seff,brej,'o-')
plt.yscale('log')
plt.xticks(ticks=list(np.linspace(0,1,11)))
plt.xlabel("signal efficiency")
plt.ylabel("background rate (Hz)")
plt.grid()
return plt
'''
#dfpbar[selection].plot(x='dt0',y='z0',kind='hexbin',gridsize=50) #,vmax=10)
#plt.hist(dfpbar['dt0'],bins=100) #,vmax=10)
#plt.show()
'''