-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_tools.py
69 lines (58 loc) · 2.41 KB
/
python_tools.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
#!/usr/bin/env python3
#imports
import uproot
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from math import *
import scipy as sp
import scipy.stats
import scipy.optimize
import glob
import time
import particle
import dask.dataframe as dd
import dask.array as da
import dask.bag as db
try:
import numba
from numba import jit, int32, float32
import numba_scipy
except:
print("Cannot find numba. Don't use numba here!")
#matplotlib options
plt.rc('font', family='serif', size=12)
pltops_hist = { "histtype":'step', "linewidth":1.5}
plt.rc('image', cmap='seismic')
plt.rcParams['figure.figsize'] = (10.0, 8.0)
from cycler import cycler
plt.rcParams['axes.prop_cycle'] = cycler(color=['b','g','r','c','m','y','k','indigo','orange','#137e6d'])
def make_profile_bins(df,lowbin,hibin,nbins,xarg,yarg,option='sem',func='mean'):
xbins = np.linspace(lowbin,hibin,nbins+1)
diff = (xbins[1]-xbins[0])*0.00001
xbins[-1] = xbins[-1]+diff
result = (df[[xarg,yarg]].groupby(np.digitize(df[xarg],bins=xbins,right=False)))[yarg].agg([func,option])
result = result.reindex(range(1,len(xbins),1))
xbins[-1] = xbins[-1]-diff
result["x"] = 0.5*(xbins[:-1]+xbins[1:])
result["xerr"] = 0.5*(xbins[1]-xbins[0])
result.rename(columns={'mean': 'y', option: 'yerr'}, inplace=True)
return result
def make_hists(df,hists,bins,axis,histtype='step',stacked=True):
n,cbins,patches = axis.hist([df.query(hd['cut'])[hd['var']] for hd in hists],
weights=[df.query(hd['cut'])[hd['weight']] for hd in hists],
label=["%s: %.1f"%(hd['label'],df.query(hd['cut'])[hd['weight']].sum()) for hd in hists],
bins=bins,histtype=histtype,stacked=stacked)
for i in range(0,len(patches)):
hd = hists[i]
for p in patches[i]:
if 'color' in hd: p.set_color(hd['color'])
if 'facecolor' in hd: p.set_facecolor(hd['facecolor'])
if 'edgecolor' in hd: p.set_edgecolor(hd['edgecolor'])
if 'fill' in hd: p.set_fill(hd['fill'])
if 'hatch' in hd: p.set_hatch(hd['hatch'])
if 'linewidth' in hd: p.set_linewidth(hd['linewidth'])
def tmp_plot(path="/Users/wketchum/Plots",ftype="pdf"):
return "%s/temp_%d.%s"%(path,np.round(time.time()),ftype)
print("python tools loaded.")