-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_statistics.py
241 lines (206 loc) · 8.12 KB
/
audio_statistics.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
232
233
234
235
236
237
238
239
240
241
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
import librosa
def get_wav_duration(file_path):
try:
duration = librosa.get_duration(filename=file_path)
failed = None
except Exception as e:
print(f"Error in {file_path}: {e}")
# Remove the file
os.remove(file_path)
failed = file_path
duration = 0.0
return duration, failed
def calculate_total_audio_duration(directory):
total_duration = 0.0
n_failed = 0
total_audio_files = 0
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".wav"):
file_path = os.path.join(root, file)
duration, failed = get_wav_duration(file_path)
total_audio_files += 1
if failed:
n_failed += 1
total_duration += duration
print("Total number of failed audios:", n_failed)
print("Total number of audio files:", total_audio_files)
return total_duration
def main():
directory = input("Enter the directory path: ")
total_duration_seconds = calculate_total_audio_duration(directory)
total_duration_hours = total_duration_seconds / 3600
print(f"Total audio duration: {total_duration_hours:.2f} hours")
if __name__ == "__main__":
main()
def load_data():
"""
Load the dataset from the CSV file.
"""
df_hc = pd.read_csv("data/version_to_zenodo/metadata/metadata_hc.csv")
df_pd = pd.read_csv("data/version_to_zenodo/metadata/metadata_pd.csv")
return df_hc, df_pd
def general_statistic():
"""
Print the general statistic of the dataset.
"""
df_hc, df_pd = load_data()
# Read all audios, the path to them is in the column "Audio"
audios_hc = df_hc["Audio"].tolist()
audios_pd = df_pd["Audio"].tolist()
# Get total hours of audio registered
import librosa
total_hours_hc = 0
for audio in audios_hc:
y, sr = librosa.load(audio)
total_hours_hc += librosa.get_duration(y=y, sr=sr)
total_hours_pd = 0
for audio in audios_pd:
y, sr = librosa.load(audio)
total_hours_pd += librosa.get_duration(y=y, sr=sr)
print(f"Total hours of audio in healthy controls: {total_hours_hc / 3600}")
print(f"Total hours of audio in patients: {total_hours_pd / 3600}")
print(f"Total hours of audio: {(total_hours_hc + total_hours_pd) / 3600}")
# The filename of the audio is composed by condition_audiotype_patientId.wav
# We can split the filename by "_" and get the condition, audiotype and patientId
# Select all vowels. They have an audiotype of len 2, which is the vowel and the recording number
vowels_hc = [audio for audio in audios_hc if len(audio.split("_")[1]) == 2]
vowels_pd = [audio for audio in audios_pd if len(audio.split("_")[1]) == 2]
# Use try and catch to avoid errors in the audios
durations_hc = []
failed_audios = []
for audio in vowels_hc:
try:
y, sr = librosa.load(audio)
durations_hc.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of vowels in healthy controls: {np.mean(durations_hc)}")
print(f"Std duration of vowels in healthy controls: {np.std(durations_hc)}")
print(f"Failed audios in healthy controls: {failed_audios}")
durations_pd = []
failed_audios = []
for audio in vowels_pd:
try:
y, sr = librosa.load(audio)
durations_pd.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of vowels in patients: {np.mean(durations_pd)}")
print(f"Std duration of vowels in patients: {np.std(durations_pd)}")
print(f"Failed audios in patients: {failed_audios}")
# Select all monologues. Their audiotype is "ESPONTANEA".
monologues_hc = [
audio for audio in audios_hc if audio.split("_")[1] == "ESPONTANEA"
]
monologues_pd = [
audio for audio in audios_pd if audio.split("_")[1] == "ESPONTANEA"
]
# Check the mean and std duration of the monologues using librosa
durations_hc = []
failed_audios = []
for audio in monologues_hc:
try:
y, sr = librosa.load(audio)
durations_hc.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of monologues in healthy controls: {np.mean(durations_hc)}")
print(f"Std duration of monologues in healthy controls: {np.std(durations_hc)}")
print(f"Failed audios in healthy controls: {failed_audios}")
durations_pd = []
failed_audios = []
for audio in monologues_pd:
try:
y, sr = librosa.load(audio)
durations_pd.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of monologues in patients: {np.mean(durations_pd)}")
print(f"Std duration of monologues in patients: {np.std(durations_pd)}")
print(f"Failed audios in patients: {failed_audios}")
# Now select all the sentences. That is, all that has not been selected yet
sentences_hc = [
audio
for audio in audios_hc
if audio not in vowels_hc and audio not in monologues_hc
]
sentences_pd = [
audio
for audio in audios_pd
if audio not in vowels_pd and audio not in monologues_pd
]
# Check the mean and std duration of the sentences using librosa
durations_hc = []
failed_audios = []
for audio in sentences_hc:
try:
y, sr = librosa.load(audio)
durations_hc.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of sentences in healthy controls: {np.mean(durations_hc)}")
print(f"Std duration of sentences in healthy controls: {np.std(durations_hc)}")
print(f"Failed audios in healthy controls: {failed_audios}")
durations_pd = []
failed_audios = []
for audio in sentences_pd:
try:
y, sr = librosa.load(audio)
durations_pd.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of sentences in patients: {np.mean(durations_pd)}")
print(f"Std duration of sentences in patients: {np.std(durations_pd)}")
print(f"Failed audios in patients: {failed_audios}")
# Check the mean and std duration of the "patakas" using librosa
patakas_hc = vowels_pd = [
audio for audio in audios_hc if audio.split("_")[1] == "PATAKA"
]
patakas_pd = vowels_pd = [
audio for audio in audios_pd if audio.split("_")[1] == "PATAKA"
]
durations_hc = []
failed_audios = []
for audio in patakas_hc:
try:
y, sr = librosa.load(audio)
durations_hc.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of patakas in healthy controls: {np.mean(durations_hc)}")
print(f"Std duration of patakas in healthy controls: {np.std(durations_hc)}")
print(f"Failed audios in healthy controls: {failed_audios}")
durations_pd = []
failed_audios = []
for audio in patakas_pd:
try:
y, sr = librosa.load(audio)
durations_pd.append(librosa.get_duration(y=y, sr=sr))
except:
failed_audios.append(audio)
pass
print(f"Mean duration of patakas in patients: {np.mean(durations_pd)}")
print(f"Std duration of patakas in patients: {np.std(durations_pd)}")
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
sns.histplot(df_pd.groupby("ID").first()["UPDRS scale"], kde=True, color="skyblue")
plt.title("Distribution of UPDRS Scale")
# Plotting the distribution of H-Y stadium
plt.subplot(1, 2, 2)
sns.countplot(x="H-Y Stadium", data=df_pd.groupby("ID").first(), palette="Set2")
plt.title("Distribution of H-Y Stadium")
plt.tight_layout()
plt.show()