-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathrfutils.py
95 lines (78 loc) · 2.94 KB
/
rfutils.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
#!/usr/bin/env python
# Copyright 2016-2018 The RamFuzz contributors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""RamFuzz-related utilities. Most depend on ../pymod being installed."""
import numpy as np
import ramfuzz
def logparse(f):
"""Parses a RamFuzz run log and yields each entry (a value/location pair) in
turn."""
fd = f.fileno()
while True:
entry = ramfuzz.load(fd)
if entry is None:
break
yield entry
def loc2val(f):
return {loc: val for (val, loc) in logparse(f)}
class indexes:
"""Assigns unique indexes to input values.
An index is generated for each distinct value given to make_index(). In
the object's lifetime, the same value always gets the same index.
"""
def __init__(self):
self.d = dict()
self.watermark = 1
def get_index(self, x):
"""Returns x's index, if it exists, otherwise None."""
if x in self.d:
return self.d[x]
else:
return None
def make_index(self, x):
"""Like get_index, but makes a new index if it doesn't exist."""
if x not in self.d:
self.d[x] = self.watermark
self.watermark += 1
return self.d[x]
def count_locpos(files):
"""Counts distinct positions and locations in a list of files.
Returns a pair (position count, location indexes object).
"""
posmax = 0
locidx = indexes()
for fname in files:
with open(fname) as f:
for (pos, (val, loc)) in enumerate(logparse(f)):
locidx.make_index(loc)
posmax = max(posmax, pos)
return posmax + 1, locidx
def read_data(files, poscount, locidx):
"""Builds input data from a files list."""
locs = [] # One element per file; each is a list of location indexes.
vals = [] # One element per file; each is a parallel list of values.
labels = [] # One element per file: true for '.s', false for '.f'.
for fname in files:
flocs = np.zeros(poscount, np.uint64)
fvals = np.zeros((poscount, 1), np.float64)
with open(fname) as f:
for (p, (v, l)) in enumerate(logparse(f)):
idx = locidx.get_index(l)
if idx:
flocs[p] = idx
fvals[p] = v
locs.append(flocs)
vals.append(fvals)
labels.append(fname.endswith('.s'))
return np.array(locs), np.array(vals), np.array(labels)