-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmse_distance.py
231 lines (194 loc) · 7.32 KB
/
mse_distance.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
226
227
228
229
230
231
import numpy as np
import argparse
from scipy.io import wavfile
import pandas as pd
from scipy.spatial.distance import euclidean
from numpy.fft import fft
from numpy.linalg import norm
import pdb
from scipy.signal import resample
import librosa
import matplotlib.pyplot as plt
import os
import mir_eval
import csv
from sklearn.metrics import f1_score
# from utils import read_data
def my_f1_score(y_true, y_pred, ths=2):
# Initialize counters for true positives, false positives, and false negatives
TP = 0
FP = 0
FN = 0
# Iterate over the true and predicted arrays
for i in range(len(y_true)):
true = y_true[i]
pred = y_pred[i]
if true == 1 and np.sum(y_pred[i-ths: i+ths]) >= 1:
TP += 1
elif true == 0 and pred == 1:
FP += 1
elif true == 1 and np.sum(y_pred[i-ths: i+ths]) == 0:
FN += 1
# Calculate precision and recall
precision = TP / (TP + FP) if (TP + FP) > 0 else 0
recall = TP / (TP + FN) if (TP + FN) > 0 else 0
if precision == 0 or recall == 0:
return 0
return 2 * precision * recall / (precision + recall)
def load_data(file_path):
sr = None
if file_path.endswith('.npy'):
array = np.load(file_path, allow_pickle=True)
if len(array.shape) == 0:
array = np.array([array])
return sr, array
elif file_path.endswith('.wav'):
# For wav files, we ignore the sample rate
data, sr = librosa.load(file_path, sr=None)
return sr, data
else:
raise ValueError("Unsupported file type. Please provide .npy or .wav file.")
def compute_mse_from_target(args, input_array):
if 'change_point_detect' in args.target_file \
or 'outlier_detect' in args.target_file:
# do F1 score instead
array1 = np.int64(input_array)
array2 = np.load(args.target_file, allow_pickle=True)
max_n = max(array1.max(), array2.max())
_array_1, _array_2 = np.zeros(max_n), np.zeros(max_n)
for n in array1:
_array_1[n-1] = 1
for n in array2:
_array_2[n-1] = 1
return my_f1_score(_array_2, _array_1)
if args.file in ('ppg', 'general'):
# array2_max = max(np.load(args.input_file[0], allow_pickle=True))
array2_max = max(load_data(args.input_file[0])[1])
# convert back to the original scale
input_array = input_array / 100 * array2_max
elif args.file == 'ecg_data' and 'heartrate' not in args.target_file:
input_array = input_array / 1000
if len(input_array) == 0:
return np.nan
array1 = input_array
_, array2 = load_data(args.target_file)
if len(array1) > len(array2):
array1 = array1[:len(array2)]
elif len(array2) > len(array1):
array1 = np.concatenate([array1, np.zeros(len(array2) - len(array1))])
if 'speech' in args.input_file[0]:
mse, _, _, _ = mir_eval.separation.bss_eval_sources(
array2, array1
)
mse = mse[0]
else:
mse = ((array1 - array2)**2).mean()
return mse
def compute_mse_from_str(type, string1, string2):
array1 = string1.split(', ')
array1 = np.array([float(a) for a in array1 if a.isnumeric()])
array2 = string2.split(', ')
array2 = np.array([float(a) for a in array2 if a.isnumeric()])
if len(array1) > len(array2):
array1 = array1[:len(array2)]
elif len(array2) > len(array1):
array1 = np.concatenate([array1, np.zeros(len(array2) - len(array1))])
if type == 'ecg':
array1 /= 1000
array2 /= 1000
return ((array1 - array2)**2).mean()
else:
raise NotImplementedError("Other modalities haven't been implemeted")
def compute_mse(file_path1, file_path2, args=None):
# file_path1: calculated signal
# file_path2: GT signal
# Load the arrays from the .npy files
if not os.path.exists(file_path1):
if 'wav' in file_path2:
return -np.inf
else:
return np.inf
sr1, array1 = load_data(file_path1)
sr2, array2 = load_data(file_path2)
if np.sum(array1**2) == 0 or np.isnan(np.sum(array1)):
# this is an empty signal
if 'wav' in file_path2:
return np.nan
# else:
# return np.nan
if array1.size == 0 or array1 is None:
return np.nan
length = min(array1.shape[0], array2.shape[0])
if 'wav' in file_path2:
# use sdr
if sr2 != sr1:
num_samples1 = int(len(array1) * (sr2 / sr1))
resampled_array1 = resample(array1, num_samples1)
resampled_array1 = resampled_array1.astype(array1.dtype)
array1 = resampled_array1
length = min(array1.shape[0], array2.shape[0])
# resampled_file1_path = ''.join(file_path1.split('.wav')[:-1])+'rs.wav'
mse, _, _, _ = mir_eval.separation.bss_eval_sources(
array2[:length], array1[:length]
)
mse = mse[0]
elif 'change_point_detect' in file_path2 \
or 'outlier_detect' in file_path2:
if array1.dtype.kind in 'fc':
return np.inf
max_n = max(array1.max(), array2.max())
_array_1, _array_2 = np.zeros(max_n), np.zeros(max_n)
for n in array1:
_array_1[n-1] = 1
for n in array2:
_array_2[n-1] = 1
# mse = f1_score(_array_2, _array_1)
mse = my_f1_score(_array_2, _array_1)
# pdb.set_trace()
elif 'recover_respiration_trace' in file_path2:
_, org_data = load_data(args.input_file[0])
identified_trace = org_data[:, array1].squeeze()
array1 = identified_trace
array1 = (array1 - array1.min())/(array1.max() - identified_trace.min())
array2 = (array2 - array2.min()) / (array2.max() - array2.min())
# mse = np.sqrt(np.mean((identified_trace-array2)**2))
correlation_matrix = np.corrcoef(array1, array2)
mse = correlation_matrix[0, 1]
mse = abs(mse)
elif 'recover_respiration' in file_path2:
mse = np.abs((array1[:length]))[0]
else:
# Compute the Mean Squared Error (MSE)
mse = np.mean((array1[:length] - array2[:length]) ** 2)
# if 'ppg-imputation' in file_path1 or \
# 'ppg-extrapolation' in file_path1:
# mse /= 100*2
return mse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--input", type=str, default=None, help="input file"
)
parser.add_argument(
"--gt", type=str, default=None, help="ground truth"
)
parser.add_argument(
"--output", type=str, default=None, help="output file"
)
args = parser.parse_args()
# Example usage
file1 = args.input
mse1 = compute_mse(file1, args.gt)
# pdb.set_trace()
print(mse1)
# # df = pd.DataFrame({'{}'.format(file1.split('/')[-1]): [mse1], '{}'.format(file2.split('/')[-1]): [mse2]})
# dirs = file1.split('/')[:-1]
# dirs = '/'.join(dirs)
# result_file = args.gt.split('/')[-1].split('.')[0]
# # df.to_csv(dirs+'/{}.csv'.format(result_file), index=False)
# rows = ['mse', mse1]
# with open(dirs+'/result.csv', 'a', newline='') as csvfile:
# # Creating a csv writer object
# csvwriter = csv.writer(csvfile)
# # Appending the data
# csvwriter.writerow(rows)