-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcircuiterror.py
More file actions
92 lines (73 loc) · 2.89 KB
/
circuiterror.py
File metadata and controls
92 lines (73 loc) · 2.89 KB
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
import numpy as np
def extract_numbers(filename):
'''
Reads a txt file and returns the list of numbers inside the file
Parameters
----------
filename : string
name of the text file to read
Returns
-------
array
list of numbers of each row of the file
'''
result = []
file = open(filename, 'r')
rows = file.read().split('\n')
for row in rows:
row = row.replace(' ','')
if row.isdigit():
result.append(int(row))
file.close()
return result
def compute_error(metric, original, approximate):
'''
Computes the error between two different testbench output files
Parameters
----------
metric : string
equation to measure the error
options med, wce, wcre,mred, msed
original : string
path to the original results text file
approximate : string
path to the approximate results text file
'''
# Read original output content
original_output = extract_numbers(original)
# Read modified output content
approximate_output = extract_numbers(approximate)
original_len = len(original_output)
approx_len = len(approximate_output)
assert original_len == approx_len, f"The output of the original and the approximate simulations doesn't match: {original_len}!={approx_len}. Make sure both outputs are being generated correctly."
# compute the error distance ED := |a - a'|
error_distance = [abs(original_output[x] - approximate_output[x])
for x in range(0,len(original_output))]
square_error_distance = [ error_distance[x]**2 for x in range(0,len(error_distance))]
# compute the relative error distance RED := ED / a
relative_error_distance = [
0 if original_output[x] == 0 else error_distance[x]/original_output[x]
for x in range(0,len(original_output))]
# Error Rate:
if (metric == "er"):
return round(sum((error>0 for error in error_distance))/total,3)
# Mean Hamming Distance see: https://stackoverflow.com/questions/40875282/fastest-way-to-get-hamming-distance-for-integer-array
if (metric == "hd"):
hamming_distance=np.bitwise_xor(original_output,approximate_output)
hamming_distance=[f'{hd:b}'.count('1') for hd in hamming_distance]
return round(np.mean(hamming_distance),3)
# Mean Error Distance MED := sum { ED(bj,b) * pj }
if (metric == "med"):
mean_error = sum(error_distance) / len(error_distance)
return round(mean_error,3)
# Worst Case Error
elif (metric == "wce"):
return max(error_distance)
# Mean Relative Error Distance
elif (metric == "mred"):
mred = sum(relative_error_distance)/len(relative_error_distance)
return round(mred,3)
# Mean Square Error Distance
elif (metric == "msed"):
msed = sum(square_error_distance)/len(square_error_distance)
return round(msed,3)