from lib.File import File
from matplotlib import pyplot as plt
from data.loader import get_kasios_obs, get_obs, map_path
import numpy as np
from math import sqrt
import pandas as pd
def get_spectrums(df):
###
### Generate the spectres for all files within the dataframe given (maintains the df index)
###
N = df.shape[0]
data = {} # Initialisation
for i, (index, row) in enumerate(df.iterrows()):
# Progression
if i % 50 == 0:
print("Computing spectrums... ({0:.0%}) ".format(i/N), end="\r")
# Reading the file
f = File(row["song"])
spectre, freq = f.getNormalizedSpectre()
if spectre is None:
# Sometimes the spectre cannot be compute because there is too much noise
continue
# Adding the spectre
data[index] = spectre
# Building output dataframe
X = pd.DataFrame.from_dict(data, columns = freq, orient = "index")
X = X.reindex(df.index, fill_value = 0)
print("All spectrums have been generated. ")
return X
def merge_spectres(spectres):
###
### Merge multiple spectres to one and normalize it.
###
m_spectres = spectres.sum(axis = 0)
m_spectres = (m_spectres - m_spectres.mean())/m_spectres.std()
return m_spectres
# Plot only one species
def plot_species(species, spectres, df):
# Parameter for the species
species_spectres = spectres.loc[df.English_name == species]
species_color = df.loc[df.English_name == species].color.iloc[0]
plt.title(species)
plt.plot(merge_spectres(species_spectres), color = species_color) # Merge all spectres together
plt.xlabel("Fréquence (Hz)")
# Loads files and makes conversion if necessary
df_all = get_obs(songs = True)
df_kasios = get_kasios_obs(songs = True)
# X = get_spectrums(df_all) # Can be very long
# X.to_pickle("data/all_birds_freq.pickle") # Save to cache
X = pd.read_pickle("data/all_birds_freq.pickle") # Quicker to use cache ;)
X.head()
#X_kasios = get_spectres(df_kasios) # Can be very long
#X_kasios.to_pickle("data/kasios_birds_freq.pickle")
X_kasios = pd.read_pickle("data/kasios_birds_freq.pickle") # Quicker to use cache ;)
X_kasios.head()
df_all.English_name.unique()
species = df_all.English_name.unique()
plt.figure(figsize=(15,15))
plt.suptitle('Average spectrum of species', fontsize=25)
for i, specie in enumerate(species):
plt.subplot(5,4,i+1)
plot_species(specie, X, df_all)
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
N = 6
X_blue = X.loc[(df_all.English_name == "Rose-crested Blue Pipit")
& (df_all['Vocalization_type'] == 'song')].sample(N)
df_blue = df_all.loc[X_blue.index]
flim = 4000
plt.figure(figsize=(15,10))
plt.suptitle('Random Blue Pipit "song" spectrums', fontsize=22)
for i in range(N):
obs = df_blue.iloc[i]
plt.subplot(3, 2, i+1)
plt.plot(X_blue.iloc[i])
plt.axvline(flim, color="red")
plt.title("{} n°{}".format(obs.English_name, obs.name))
plt.show()
N = 6
X_blue = X.loc[(df_all.English_name == "Rose-crested Blue Pipit")
& (df_all['Vocalization_type'] == 'call')].sample(N)
df_blue = df_all.loc[X_blue.index]
flim = 4000
plt.figure(figsize=(15,10))
plt.suptitle('Random Blue Pipit "call" spectrums', fontsize=22)
for i in range(N):
obs = df_blue.iloc[i]
plt.subplot(3, 2, i+1)
plt.plot(X_blue.iloc[i])
plt.axvline(flim, color="red")
plt.title("{} n°{}".format(obs.English_name, obs.name))
plt.show()
plt.figure(figsize=(15,40))
for i in range(X_kasios.shape[0]):
plt.subplot(10,2,i+1)
plt.plot(X_kasios.iloc[i])
plt.title("Kasios n°{}".format(i+1))
plt.axvline(flim, color="red")
plt.show()
obs_id = 160293
obs = df_all.loc[obs_id]
f = File(obs.song)
plt.plot(f.data)
f.plotAmp()
spectre, freq = f.getNormalizedSpectre()
plt.plot(freq, spectre)