Skip to content
Snippets Groups Projects
Commit 06ddd893 authored by Loic-Lenof's avatar Loic-Lenof
Browse files

Sorting audio names

+ added sorting of names at importation to improve stability
parent 1624c55d
Branches
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ import pandas as pd ...@@ -13,7 +13,7 @@ import pandas as pd
from scipy.signal import butter, filtfilt from scipy.signal import butter, filtfilt
#%% Functions process #%% Functions process
def import_csv(csv_name, folder, separator, useless=True): def import_csv(csv_name, folder, useless=True, slash="/"):
""" """
Parameters Parameters
---------- ----------
...@@ -35,7 +35,7 @@ def import_csv(csv_name, folder, separator, useless=True): ...@@ -35,7 +35,7 @@ def import_csv(csv_name, folder, separator, useless=True):
data = [] data = []
# read data csv # read data csv
with open(folder + separator + csv_name, newline="") as csv_file: with open(folder + slash + csv_name, newline="") as csv_file:
lines = csv.reader(csv_file, delimiter=',') lines = csv.reader(csv_file, delimiter=',')
for row in lines: for row in lines:
data = data + [row] data = data + [row]
...@@ -72,7 +72,7 @@ def get_csv(csv_folder, slash="\\"): ...@@ -72,7 +72,7 @@ def get_csv(csv_folder, slash="\\"):
------- -------
data_20_21 : DATAFRAME data_20_21 : DATAFRAME
Contains the inforamtion inside the .CSVs. Contains the inforamtion inside the .CSVs.
audio_paths : LIST sorted_names : LIST
Names corresponding to audio files in the first column of the .CSVs. Names corresponding to audio files in the first column of the .CSVs.
""" """
csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')] csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')]
...@@ -93,7 +93,18 @@ def get_csv(csv_folder, slash="\\"): ...@@ -93,7 +93,18 @@ def get_csv(csv_folder, slash="\\"):
if filename.endswith(".wav"): if filename.endswith(".wav"):
audio_names = np.append(audio_names, filename.replace('/', slash)) audio_names = np.append(audio_names, filename.replace('/', slash))
return data_frame, audio_names # sort audio files names
# We want to sort it by year
years = np.unique([path[4:8] for path in audio_names])
sorted_names = np.array([], dtype='<U49')
# and inside each year, sort it by date.
for year in years:
idx_to_sort = np.where(np.array([path[4:8] for path in audio_names]) == year)[0]
temp_path = np.copy(audio_names[idx_to_sort])
temp_path.sort()
sorted_names = np.append(temp_path, sorted_names)
return data_frame, sorted_names
def butter_pass_filter(data, cutoff, fs, order=1, mode='high'): def butter_pass_filter(data, cutoff, fs, order=1, mode='high'):
""" """
......
...@@ -76,7 +76,7 @@ def get_csv(csv_folder, slash="\\"): ...@@ -76,7 +76,7 @@ def get_csv(csv_folder, slash="\\"):
------- -------
data_20_21 : DATAFRAME data_20_21 : DATAFRAME
Contains the inforamtion inside the .CSVs. Contains the inforamtion inside the .CSVs.
audio_paths : LIST sorted_names : LIST
Names corresponding to audio files in the first column of the .CSVs. Names corresponding to audio files in the first column of the .CSVs.
""" """
csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')] csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')]
...@@ -97,7 +97,18 @@ def get_csv(csv_folder, slash="\\"): ...@@ -97,7 +97,18 @@ def get_csv(csv_folder, slash="\\"):
if filename.endswith(".wav"): if filename.endswith(".wav"):
audio_names = np.append(audio_names, filename.replace('/', slash)) audio_names = np.append(audio_names, filename.replace('/', slash))
return data_frame, audio_names # sort audio files names
# We want to sort it by year
years = np.unique([path[4:8] for path in audio_names])
sorted_names = np.array([], dtype='<U49')
# and inside each year, sort it by date.
for year in years:
idx_to_sort = np.where(np.array([path[4:8] for path in audio_names]) == year)[0]
temp_path = np.copy(audio_names[idx_to_sort])
temp_path.sort()
sorted_names = np.append(temp_path, sorted_names)
return data_frame, sorted_names
def butter_pass_filter(data, cutoff, fs, order=1, mode='high'): def butter_pass_filter(data, cutoff, fs, order=1, mode='high'):
""" """
......
File added
...@@ -77,15 +77,15 @@ def get_csv(csv_folder, slash="\\"): ...@@ -77,15 +77,15 @@ def get_csv(csv_folder, slash="\\"):
------- -------
data_20_21 : DATAFRAME data_20_21 : DATAFRAME
Contains the inforamtion inside the .CSVs. Contains the inforamtion inside the .CSVs.
audio_paths : LIST sorted_names : LIST
Names corresponding to audio files in the first column of the .CSVs. Names corresponding to audio files in the first column of the .CSVs.
""" """
csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')] csv_names = [a for a in os.listdir(csv_folder) if a.endswith('.csv')]
# import data # import data
data = import_csv(csv_names[0], csv_folder, slash=slash) data = import_csv(csv_names[0], csv_folder, separator=slash)
for i in range(1,len(csv_names)): for i in range(1,len(csv_names)):
data = data + import_csv(csv_names[i], csv_folder, slash=slash)[1:] data = data + import_csv(csv_names[i], csv_folder, separator=slash)[1:]
data_frame = pd.DataFrame(data=data[:][1:], columns=data[:][0]) data_frame = pd.DataFrame(data=data[:][1:], columns=data[:][0])
# change dtype for selected columns # change dtype for selected columns
...@@ -98,7 +98,18 @@ def get_csv(csv_folder, slash="\\"): ...@@ -98,7 +98,18 @@ def get_csv(csv_folder, slash="\\"):
if filename.endswith(".wav"): if filename.endswith(".wav"):
audio_names = np.append(audio_names, filename.replace('/', slash)) audio_names = np.append(audio_names, filename.replace('/', slash))
return data_frame, audio_names # sort audio files names
# We want to sort it by year
years = np.unique([path[4:8] for path in audio_names])
sorted_names = np.array([], dtype='<U49')
# and inside each year, sort it by date.
for year in years:
idx_to_sort = np.where(np.array([path[4:8] for path in audio_names]) == year)[0]
temp_path = np.copy(audio_names[idx_to_sort])
temp_path.sort()
sorted_names = np.append(temp_path, sorted_names)
return data_frame, sorted_names
#%% Trajectories algorithms #%% Trajectories algorithms
def get_local_maxima(spectrogram, spectrogram2, hardness, threshold=10e-5): def get_local_maxima(spectrogram, spectrogram2, hardness, threshold=10e-5):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment