forked from cms-gem-daq-project/gem-plotting-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anautilities.py
129 lines (105 loc) · 4.36 KB
/
anautilities.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
#!/bin/env python
"""
Utilities for vfatqc scans
By: Brian Dorney ([email protected])
"""
# Imports
import sys, os
import numpy as np
import ROOT as r
#import root_numpy as rp
def filePathExists(searchPath, subPath):
import glob
dirs = glob.glob(searchPath)
foundDir = False
for path in dirs:
if path.rfind(subPath) > 0:
foundDir = True
pass
pass
if not foundDir:
print "Unable to find %s in location: %s"%(subPath, searchPath)
return False
else:
print "Found %s"%s(subPath)
return True
def initVFATArray(array_dtype, nstrips=128):
list_dtypeTuple = []
for idx in range(0,len(array_dtype)):
if array_dtype.names[idx] == 'vfatN': continue
if array_dtype.names[idx] == 'vfatCh': continue
if array_dtype.names[idx] == 'panPin': continue
if array_dtype.names[idx] == 'ROBstr': continue
list_dtypeTuple.append((array_dtype.names[idx],array_dtype[idx]))
pass
return np.zeros(nstrips, dtype=list_dtypeTuple)
def make3x8Canvas(name, initialContent = None, drawOption = ''):
"""Creates a 3x8 canvas for summary plots.
initialContent should be None or an array of 24 (one per VFAT) TObject that
will be drawn on the canvas. drawOption will be passed to the Draw
function."""
canv = r.TCanvas(name,name,500*8,500*3)
canv.Divide(8,3)
if initialContent != None:
for vfat in range(24):
canv.cd(vfat+1)
initialContent[vfat].Draw(drawOption)
canv.Update()
return canv
#Use Median absolute deviation (MAD) to reject outliers
#See: http://stackoverflow.com/questions/22354094/pythonic-way-of-detecting-outliers-in-one-dimensional-observation-data
#And also: http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h.htm
def rejectOutliersMAD(arrayData, thresh=3.5):
arrayOutliers = isOutlierMAD(arrayData, thresh)
return arrayData[arrayOutliers != True]
#Use MAD to reject outliers, but consider only high or low tail
def rejectOutliersMADOneSided(arrayData, thresh=3.5, rejectHighTail=True):
arrayOutliers = isOutlierMADOneSided(arrayData, thresh, rejectHighTail)
return arrayData[arrayOutliers != True]
#Use inter-quartile range (IQR) to reject outliers
#Returns a boolean array with True if points are outliers and False otherwise.
def isOutlierIQR(arrayData):
dMin = np.min(arrayData, axis=0)
dMax = np.max(arrayData, axis=0)
median = np.median(arrayData, axis=0)
q1,q3 = np.percentile(arrayData, [25,75], axis=0)
IQR = q3 - q1
return (arrayData < (q1 - 1.5 * IQR)) | (arrayData > (q3 + 1.5 * IQR))
#Use inter-quartile range (IQR) to reject outliers, but consider only high or low tail
#Returns a boolean array with True if points are outliers and False otherwise.
def isOutlierIQROneSided(arrayData, rejectHighTail=True):
dMin = np.min(arrayData, axis=0)
dMax = np.max(arrayData, axis=0)
median = np.median(arrayData, axis=0)
q1,q3 = np.percentile(arrayData, [25,75], axis=0)
IQR = q3 - q1
if rejectHighTail:
return arrayData > (q3 + 1.5 * IQR)
else:
return arrayData < (q1 - 1.5 * IQR)
#Use Median absolute deviation (MAD) to reject outliers
#See: https://github.com/joferkington/oost_paper_code/blob/master/utilities.py
#Returns a boolean array with True if points are outliers and False otherwise.
def isOutlierMAD(arrayData, thresh=3.5):
median = np.median(arrayData, axis=0)
diff = np.abs(arrayData - median)
med_abs_deviation = np.median(diff)
if med_abs_deviation == 0:
return isOutlierIQR(arrayData)
else:
modified_z_score = 0.6745 * diff / med_abs_deviation
return modified_z_score > thresh
#Use MAD to reject outliers, but consider only high or low tail
#Returns a boolean array with True if points are outliers and False otherwise.
def isOutlierMADOneSided(arrayData, thresh=3.5, rejectHighTail=True):
median = np.median(arrayData, axis=0)
diff = arrayData - median
med_abs_deviation = np.median(np.abs(diff))
if med_abs_deviation == 0:
return isOutlierIQROneSided(arrayData, rejectHighTail)
else:
modified_z_score = 0.6745 * diff / med_abs_deviation
if rejectHighTail:
return modified_z_score > thresh
else:
return modified_z_score < -1.0 * thresh