-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError_functions and plots.py
117 lines (101 loc) · 4.24 KB
/
Error_functions and plots.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
import numpy as np
import math
import matplotlib.pyplot as plt
def alt_sigmoid(x):
transformed = []
for i in x:
transformed.append(1/(1+math.exp(4.5-(0.9*i))))
return transformed
def linear_error(x):
transformed = []
for i in x:
transformed.append(i* (1/12))
return transformed
def hearing_prediction(sim_data):
"""returns the indexes of the three highest-valued notes in the decision ensemble for simulated inputs.
Takes as input the probe data from decision ensemble at 0.15s intervals
E.g., hearing_prediction(sim.data[probe2]) """
output_indexes = []
for i in range(len(sim_data)):
temp = sim_data[i,:]
output_index = np.where((temp) == (max(temp)))[0]
temp = np.delete((temp),(output_index))
output_index2 = np.where((temp) == (max(temp)))[0]
temp = np.delete((temp),(output_index2))
output_index3 = np.where((temp) == (max(temp)))[0]
np.delete((temp),(output_index3))
all_indexes = [output_index[0], output_index2[0], output_index3[0]]
output_indexes.append(all_indexes)
return(output_indexes)
def correct_prediction(array):
"""This function outputs the indexes (number of the note) of all correct notes for all notes in the labels matrix.
Used for evaluating accuracy when compared against hearing_prediction function.
Input is the output_array"""
indexes = []
for i in range(len(array)):
index = np.where((array[i,:])>2)[0]
indexes.append((index.tolist())) #this array (indexes) contains the index of every note in the chord
return indexes
def detection_error_loss(prediction, target):
""" Both predition and target are assumed to be matrices where each row has length 3"""
bools= []
for array in range(len(prediction)):
for index in range(3):
if prediction[array][index] == target[array][0]:
bools.append(0)
elif prediction[array][index] == target[array][1]:
bools.append(0)
elif prediction[array][index] == target[array][2]:
bools.append(0)
else:
bools.append(1)
return bools
def distance_loss(prediction, target):
differences = []
for array in range(len(prediction)):
for index in range(3):
diff = np.abs(prediction[array][index] - target[array][0])
diff= np.minimum(np.abs(prediction[array][index] - target[array][1]), diff)
diff = np.minimum(np.abs(prediction[array][index] - target[array][2]), diff)
differences.append(np.mod(diff,12))
output = alt_sigmoid(differences)
output2 = linear_error(differences)
return output,output2
#find the closest value in prediction to any of the targets and calculate the difference
# define some sort of progressive error such that bigger distances have higher error
def sethares(prediction, target):
differences = []
consonance_rank = [(1,2), (2,3), (3,5), (3,4), (4,5), (5,8), (5,6), (5,7), (5,9), (8,9), (8,15), (15,16)]
ranking = {0:0, 1:12, 2:10, 3:7, 4:5, 5:4, 6:8, 7:2, 8:6, 9:3, 10:11 ,11:9, 12:1}
for array in range(len(prediction)):
for index in range(3):
diff = np.abs(prediction[array][index] - target[array][0])
diff = ranking[np.mod(diff, 12)]
diff2 = np.abs(prediction[array][index] - target[array][1])
diff2= ranking[np.mod(diff2, 12)]
diff3 = np.abs(prediction[array][index] - target[array][2])
diff3 = ranking[np.mod(diff3, 12)]
smallest = min(min(diff, diff2),diff3)
differences.append(smallest)
output = alt_sigmoid(differences)
output2 = linear_error(differences)
return output, output2
#based on calculating the difference (as in the above function),
# use the distances to set the error with the sethares method.
axis= [x / 10.0 for x in range(0, 122)]
plt.rcParams.update({'font.size': 15})
plt.figure()
plt.plot(axis, alt_sigmoid(axis) , label="c")
plt.xlabel("Loss")
plt.ylabel("Error")
plt.grid()
plt.title("Scaled Sigmoid Error")
plt.xlim(0,12)
plt.figure()
plt.plot(axis, linear_error(axis) , label="c")
plt.title("Linear Error")
plt.xlabel("Loss")
plt.ylabel("Error")
plt.xlim(0,12)
plt.grid()
plt.show()