-
Notifications
You must be signed in to change notification settings - Fork 9
/
flatten.py
executable file
·227 lines (159 loc) · 5.06 KB
/
flatten.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
#!/usr/bin/env python3
"""
Remove large spikes from data.
This is useful when a very large spike causes the rest of the chart to appear flat.
Don't use this data exclusively, as you will still want to see those spikes.
This just allows seeing the rest of the data in chart that is easier to read.
"""
import sys
from sys import stdin ,stderr
import os
import numpy as np
import csv
dataDir='diskgroup-breakout'
def detectSpikes(dataset):
# dataset is a dict of lists
spikes={}
threshold=5 # number of std deviations
for colNum in dataset:
arrayData = dataset[colNum]
mean_1 = np.mean(arrayData)
std_1 =np.std(arrayData)
if std_1 < 0.1:
std_1 = 0.1
for y in arrayData:
#z_score= (y - mean_1)/std_1
z_score=0
try:
z_score = y / std_1
except:
z_score=0
print('double_scalars:', file=sys.stderr)
print('y: {}'.format(y), file=sys.stderr)
print('mean_1: {}'.format(mean_1), file=sys.stderr)
print('std_1: {}'.format(std_1), file=sys.stderr)
if np.abs(z_score) > threshold:
if not colNum in spikes.keys():
spikes[colNum] = []
spikes[colNum].append(y)
return spikes
def getLines():
lines=[]
for line in stdin:
lines.append(line.strip())
return lines
def validateWorkingColumns(hdr,workingColumns):
for colName in workingColumns:
if colName not in hdr:
print('{} is in invalid column name'.format(colName),file=sys.stderr)
sys.exit(1)
def getHdr(lines):
hdrLine=lines.pop(0)
#return hdrLine.strip().upper().split(',')
return hdrLine.strip().split(',')
def getColRefs(hdrLine):
i = 0
colRefs={}
for col in hdrLine:
colRefs[col] = i
i += 1
return colRefs
def getColSets(lines):
pass
def cleanData(lines):
cleanLines=[]
for line in lines:
a = line.split(',')
#print('cleanData: {}'.format(a))
if ('LINUX-RESTART' in a) \
or ('# hostname' in a) \
or ('timestamp' in a) \
or ('hostname' in a):
#print('a: {}'.format(a))
#print('skipping line: {}'.format(line))
continue
cleanLines.append(line)
return cleanLines
# get a dict of arrays for the data
# used as source to detect spikes per column
def getDataSet(columnRef,workingColumns,lines):
colVals={}
#print('column#: {}'.format(columnRef))
for line in lines:
#print('line: {}'.format(line))
a = line.split(',')
for colName in workingColumns:
#print('colName: {}'.format(colName))
colNum = columnRef[colName]
#print('colNum: {}'.format(colNum))
#print('{} val {}'.format(colName,a[colNum]))
if not colNum in colVals.keys():
colVals[colNum] = []
colVals[colNum].append(float(a[colNum]))
return colVals
def removeSpikes(spikes, lines, removedLines):
# spikes is a dict of lists
# the key is the column number in the line
# remove any line where any of the references values is an outlier
for line in lines:
#print('line: {}'.format(line))
a = line.split(',')
for colNum in spikes:
if float(a[colNum]) in spikes[colNum]:
removedLines.append(line)
lines.remove(line)
break
return lines
def spikeRpt(spikes,workingColumns, colRefs):
csvWriter = csv.writer(sys.stderr,delimiter=',')
#for colNum in spikes:
for i in range(0,len(spikes)):
colName = workingColumns[i]
print('colName: {}'.format(colName), file=sys.stderr)
csvWriter.writerow(spikes[colRefs[colName]])
def removedRpt(removedLines):
for line in removedLines:
print('removed: {}'.format(line), file=sys.stderr)
def main():
#print('args: {}'.format(sys.argv))
if len(sys.argv) < 2:
print('at least 2 parameters needed')
print('fa2.py col1 col2 ... < STDIN')
sys.exit(1)
lines = getLines()
hdr = getHdr(lines) # array
lines = cleanData(lines)
if sys.argv[1] == 'hdrs':
hdrList='\n'.join(hdr)
print(hdrList)
sys.exit(0)
colRefs = getColRefs(hdr)
#print('colRefs: {}'.format(colRefs), file=sys.stderr)
workingColumns = []
for column in sys.argv[1:]:
workingColumns.append(column)
validateWorkingColumns(hdr,workingColumns)
#print('working columns: {}'.format(' - '.join(workingColumns)), file=sys.stderr)
#print("file: {} \n hdr: {}".format(filename,hdr))
#print('first line: {}'.format(lines[0]))
# get the position in the data array for each column to check
colNums = [ colRefs[i] for i in workingColumns ]
#print('colNums: {}'.format(colNums), file=sys.stderr)
#sys.exit(0)
# this is a dict of lists, where each list is all values for the column
#colValues = getDataSet(colRefs[workingColumns[0]],lines)
colValues = getDataSet(colRefs,workingColumns,lines)
#print('colValues {}'.format(colValues))
spikes = detectSpikes(colValues)
removedLines=[]
normalized = removeSpikes(spikes, lines, removedLines)
#print('\nnormalized:')
hdrList=','.join(hdr)
print(hdrList)
for line in normalized:
print(line)
# reports to stderr
#spikeRpt(spikes,workingColumns, colRefs)
#removedRpt(removedLines)
if __name__ == '__main__':
main()