From 3c2dd4c34641f178094246b35c36e239f68d9b3e Mon Sep 17 00:00:00 2001 From: Sehuanes Date: Thu, 30 Jun 2016 17:22:36 +0200 Subject: [PATCH 01/13] updated fakefish.py docu. removed seaborn stuff from Auxiliary --- thunderfish/Auxiliary.py | 12 ------------ thunderfish/fakefish.py | 8 ++++---- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/thunderfish/Auxiliary.py b/thunderfish/Auxiliary.py index 6dba42f4..8f00e525 100644 --- a/thunderfish/Auxiliary.py +++ b/thunderfish/Auxiliary.py @@ -51,9 +51,6 @@ def plot_dfs_histogram(dfs_array, binwidth='FD'): q75, q25 = np.percentile(abs(dfs_array), [75, 25]) inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") fig, ax = plt.subplots(figsize=(15. / inch_factor, 10. / inch_factor)) if binwidth == 'FD': @@ -68,7 +65,6 @@ def plot_dfs_histogram(dfs_array, binwidth='FD'): ax.set_xlabel('Possible Beat-Frequencies [Hz]', fontsize=14) ax.tick_params(axis='both', which='major', labelsize=12) ax.set_title('Distribution of Beat-Frequencies', fontsize=16) - sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() fig.savefig('figures/histo_of_dfs.pdf') plt.close() @@ -77,9 +73,6 @@ def plot_dfs_histogram(dfs_array, binwidth='FD'): def draw_bwin_analysis_plot(filename, t_trace, eod_trace, no_of_peaks, cvs, mean_ampl): fs = 20 inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") fig = plt.figure(figsize=(40. / inch_factor, 35. / inch_factor), num='Fish No. ' + filename[-10:-8]) @@ -147,7 +140,6 @@ def draw_bwin_in_plot(ax, filename, t_trace, eod_trace, start_bwin, len_bwin, pk if enu > 0: axis.set_xlim((0, 25)) - sns.despine(ax=axis, offset=10) plt.tight_layout() if not savefig: @@ -226,9 +218,6 @@ def create_outp_folder(filepath, out_path='.'): def beat_plot(beat_data, beat_time): inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") fs = 16 fig, ax = plt.subplots(figsize=(45. / inch_factor, 20. / inch_factor)) @@ -238,7 +227,6 @@ def beat_plot(beat_data, beat_time): ax.set_title('2Hz Beat', fontsize=fs + 2) ax.tick_params(axis='both', which='major', labelsize=fs - 2) - sns.despine(fig=fig, ax=ax, offset=10) fig.tight_layout() plt.show() diff --git a/thunderfish/fakefish.py b/thunderfish/fakefish.py index f76dd58b..bbea3385 100644 --- a/thunderfish/fakefish.py +++ b/thunderfish/fakefish.py @@ -29,18 +29,18 @@ def generate_wavefish(freq, samplerate, time_len=20., harmonics=3): def generate_pulsefish(freq, samplerate, time_len=20., noise_fac=0.1, pk_std=0.001, pk_amplitude=1., tr_std=0.001, tr_amplitude=0.5, - tr_time=0.005): + tr_time=0.002): """ :param freq: (float). Frequency of the fish in Hz. :param samplerate: (float). Sampling Rate in Hz :param time_len: (float). Length of the recording in sec. :param noise_fac: (float). Factor by which random gaussian distributed noise is inserted. - :param pk_std: (float). std of the positive part of the pulse in sec. + :param pk_std: (float). std of the positive part of the pulse in seconds. :param pk_amplitude: (float). Factor for regulating the positive part of the pulse. - :param tr_std: (float). std of the negative part of the pulse in sec. + :param tr_std: (float). std of the negative part of the pulse in seconds. :param tr_amplitude: (float). Factor for regulating the negative part of the pulse. - :param tr_time: + :param tr_time: (float). Time distance between peak and trough of the pulse in seconds. :return: (array). Data with pulses at the given frequency. """ From 44726168ef5422fc0dbd7ad2d645cb64de7278f6 Mon Sep 17 00:00:00 2001 From: Sehuanes Date: Thu, 30 Jun 2016 17:36:18 +0200 Subject: [PATCH 02/13] removed create_plots.py --- thunderfish/create_plots.py | 116 ------------------------------------ 1 file changed, 116 deletions(-) delete mode 100644 thunderfish/create_plots.py diff --git a/thunderfish/create_plots.py b/thunderfish/create_plots.py deleted file mode 100644 index d18dfd7f..00000000 --- a/thunderfish/create_plots.py +++ /dev/null @@ -1,116 +0,0 @@ -__author__ = 'raab' -from Auxiliary import * -import sys -import glob -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -import os - - -def load_data(glob_ls): - """ - loads all .npy data (numpy.array) and converts it to a dictionary using part from filenames as keys. - - :param glob_ls: list of .npy files - :return: data (list of frequencies) - """ - print '\nloading data ...' - # load data as a dictionary using either pulsefish or wavefish as key and the np.array as value. - data = {curr_file.split('_')[-1].split('.npy')[0]: np.load(curr_file) for curr_file in glob_ls} - print 'data loaded successfully\n' - return data - - -def create_histo(data): - """ - gets a list of data and creates an histogram of this data. - - :param data: dictionary with fishtype as keys and np.array with EOD-Frequencies as values. - """ - print 'creating histogramm ...' - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig, ax = plt.subplots(figsize=(15. / inch_factor, 10. / inch_factor)) - colors = ['salmon', 'cornflowerblue'] - - for enu, curr_fishtype in enumerate(data.keys()): - hist, bins = np.histogram(data[curr_fishtype], bins=len(data[curr_fishtype]) // 4) - width = 0.7 * (bins[1] - bins[0]) - center = (bins[:-1] + bins[1:]) / 2 - ax.bar(center, hist, align='center', width=width, alpha=0.8, facecolor=colors[enu], label=curr_fishtype) - - ax.set_ylabel('Counts', fontsize=14) - ax.set_xlabel('Frequency [Hz]', fontsize=14) - ax.set_xticks(np.arange(0, max(np.hstack(data.values())) + 100, 250)) - ax.tick_params(axis='both', which='major', labelsize=12) - ax.set_title('Distribution of EOD-Frequencies', fontsize=16) - ax.legend(frameon=False, loc='best', fontsize=12) - sns.despine(fig=fig, ax=ax, offset=10) - fig.tight_layout() - fig.savefig('figures/histo_of_eod_freqs.pdf') - plt.close() - - -def fishtype_barplot(data): - """ This function creates a bar plot showing the distribution of wave-fishes vs. pulse-fishes. - - :param data: dictionary with fish-type as keys and array of EODfs as values. - """ - - # Read the keys of the dictionary and use them to get the count of pulse- and wave-type fishes. - keys = np.array(data.keys()) - bool_wave = np.array(['wave' in e for e in keys], dtype=bool) - bool_pulse = np.array(['puls' in e for e in keys], dtype=bool) - - count_wave = len(data[keys[bool_wave][0]]) - count_pulse = len(data[keys[bool_pulse][0]]) - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig, ax = plt.subplots(figsize=(10. / inch_factor, 10. / inch_factor)) - width = 0.5 - ax.bar(1 - width / 2., count_wave, width=width, facecolor='cornflowerblue', alpha=0.8) - ax.bar(2 - width / 2., count_pulse, width=width, facecolor='salmon', alpha=0.8) - ax.set_xticks([1, 2]) - ax.set_xticklabels(['Wave-type', 'Pulse-type']) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.set_ylabel('Number of Fishes', fontsize=14) - ax.set_title('Distribution of Fish-types', fontsize=16) - sns.despine(fig=fig, ax=ax, offset=10) - fig.tight_layout() - fig.savefig('figures/fishtype_barplot.pdf') - plt.close() - - -def main(): - # Load data - npy_files = glob.glob('*.npy') - data = load_data(npy_files) - - # create histogram of EOD-frequencies - create_histo(data) - - # create histogram of all possible beat-frequencies - wave_file = glob.glob('*wave*.npy') - wave_freqs = np.load(wave_file[0]) - dfs = df_histogram(wave_freqs) - plot_dfs_histogram(dfs) - - # Plot barplot with fish-type distribution - fishtype_barplot(data) - - print 'code finished' - if len(sys.argv) == 3: - response = raw_input('Do you want to create a .pdf file with the data and the figures processed ? [y/n]') - if response == 'y': - os.system('python create_tex.py %s.npy %s.npy' % (sys.argv[1], sys.argv[2])) - - -if __name__ == '__main__': - main() From f9e82f3cb2f6b7fc848f044a1c5a855f27bfe64a Mon Sep 17 00:00:00 2001 From: Sehuanes Date: Thu, 30 Jun 2016 17:36:40 +0200 Subject: [PATCH 03/13] removed create_plots2.py --- thunderfish/create_plots2.py | 119 ----------------------------------- 1 file changed, 119 deletions(-) delete mode 100644 thunderfish/create_plots2.py diff --git a/thunderfish/create_plots2.py b/thunderfish/create_plots2.py deleted file mode 100644 index 8f14e3a6..00000000 --- a/thunderfish/create_plots2.py +++ /dev/null @@ -1,119 +0,0 @@ -__author__ = 'raab' -from Auxiliary import * -import sys -import glob -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -import os - - -def load_data(glob_ls): - """ - loads all .npy data (numpy.array) and converts it to a dictionary using part from filenames as keys. - - :param glob_ls: list of .npy files - :return: data (list of frequencies) - """ - print '\nloading data ...' - # load data as a dictionary using either pulsefish or wavefish as key and the np.array as value. - data = {curr_file.split('_')[-1].split('.npy')[0]: np.load(curr_file) for curr_file in glob_ls} - print 'data loaded successfully\n' - return data - - -def create_histo(data): - """ - gets a list of data and creates an histogram of this data. - - :param data: dictionary with fishtype as keys and np.array with EOD-Frequencies as values. - """ - print 'creating histogramm ...' - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig, ax = plt.subplots(figsize=(15. / inch_factor, 10. / inch_factor)) - colors = ['salmon', 'cornflowerblue'] - - for enu, curr_fishtype in enumerate(data.keys()): - if len(data[curr_fishtype]) >= 4: - hist, bins = np.histogram(data[curr_fishtype], bins=len(data[curr_fishtype]) // 4) - width = 0.7 * (bins[1] - bins[0]) - center = (bins[:-1] + bins[1:]) / 2 - ax.bar(center, hist, align='center', width=width, alpha=0.8, facecolor=colors[enu], label=curr_fishtype) - - ax.set_ylabel('Counts', fontsize=14) - ax.set_xlabel('Frequency [Hz]', fontsize=14) - ax.set_xticks(np.arange(0, max(np.hstack(data.values())) + 100, 250)) - ax.tick_params(axis='both', which='major', labelsize=12) - ax.set_title('Distribution of EOD-Frequencies', fontsize=16) - ax.legend(frameon=False, loc='best', fontsize=12) - sns.despine(fig=fig, ax=ax, offset=10) - fig.tight_layout() - fig.savefig('figures/histo_of_eod_freqs.pdf') - plt.close() - - -def fishtype_barplot(data): - """ This function creates a bar plot showing the distribution of wave-fishes vs. pulse-fishes. - - :param data: dictionary with fish-type as keys and array of EODfs as values. - """ - - # Read the keys of the dictionary and use them to get the count of pulse- and wave-type fishes. - keys = np.array(data.keys()) - bool_wave = np.array(['wave' in e for e in keys], dtype=bool) - bool_pulse = np.array(['puls' in e for e in keys], dtype=bool) - - count_wave = len(data[keys[bool_wave][0]]) - count_pulse = len(data[keys[bool_pulse][0]]) - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig, ax = plt.subplots(figsize=(10. / inch_factor, 10. / inch_factor)) - width = 0.5 - ax.bar(1 - width / 2., count_wave, width=width, facecolor='cornflowerblue', alpha=0.8) - ax.bar(2 - width / 2., count_pulse, width=width, facecolor='salmon', alpha=0.8) - ax.set_xticks([1, 2]) - ax.set_xticklabels(['Wave-type', 'Pulse-type']) - ax.tick_params(axis='both', which='major', labelsize=14) - ax.set_ylabel('Number of Fishes', fontsize=14) - ax.set_title('Distribution of Fish-types', fontsize=16) - sns.despine(fig=fig, ax=ax, offset=10) - fig.tight_layout() - fig.savefig('figures/fishtype_barplot.pdf') - plt.close() - - -def main(): - # Load data - wave_file = sys.argv[1] - - # npy_files = glob.glob('*.npy') - # data = load_data(npy_files) - - # create histogram of EOD-frequencies - create_histo(data) - - # create histogram of all possible beat-frequencies - wave_file = ('fish_wave.npy') - wave_freqs = np.load(wave_file[0]) - dfs = df_histogram(wave_freqs) - plot_dfs_histogram(dfs) - - # Plot barplot with fish-type distribution - fishtype_barplot(data) - - print 'code finished' - # if len(sys.argv) == 3: - # response = raw_input('Do you want to create a .pdf file with the data and the figures processed ? [y/n]') - # if response == 'y': - # os.system('python create_tex.py %s.npy %s.npy' % (sys.argv[1], sys.argv[2])) - - -if __name__ == '__main__': - main() From c0bc7bd95ceb5d0a9178831368bcb4088a67e6dd Mon Sep 17 00:00:00 2001 From: Sehuanes Date: Thu, 30 Jun 2016 17:37:25 +0200 Subject: [PATCH 04/13] removed create_tex.py --- thunderfish/create_tex.py | 189 -------------------------------------- 1 file changed, 189 deletions(-) delete mode 100644 thunderfish/create_tex.py diff --git a/thunderfish/create_tex.py b/thunderfish/create_tex.py deleted file mode 100644 index d752c304..00000000 --- a/thunderfish/create_tex.py +++ /dev/null @@ -1,189 +0,0 @@ -__author__ = 'raab' - - -def description_of_code(): - """ - Builds a .pdf data. - - Arg 1: this script it self - Arg 2: .npy data from wavefish - Arg 3: .npy data from pulsefish - Arg 4: .pdf figure with distribution of wave and pulsefish - Arg 5: .pdf figure with distribution of eod frequenies - Arg 6: .pdf figure with distribution of beat frequencies - :return: - """ - - -import numpy as np -import sys -import os - - -def build_tex_pdf(wavefish, pulsefish): - response = raw_input('What shall the Title of the .pdf be ?') - filename = response - - tf = open('%s.tex' % filename, 'w') - tf.write('\\documentclass[a4paper,12pt,pdflatex]{article}\n') - tf.write('\\usepackage{graphics}\n') - tf.write('\n') - tf.write('\\begin{document}\n') - tf.write('\\section*{%s}\n' % filename) - - response = raw_input('Do you want to enter extra Data (loation, temp, cond. etc ? yes[y] or no[n]') - if response == "y": - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - # tf.write( 'Question & Answer \\\\ \\hline \n' ) - tf.write('\\\\\n') - - Country = raw_input('What country ?') - tf.write('Country: & %s\\\\\n' % Country) - tf.write('\\\\\n') - - Location = raw_input('What location ?') - tf.write('Location: & %s\\\\\n' % Location) - tf.write('\\\\\n') - - Date = raw_input('What date ?') - tf.write('Date: & %s\\\\\n' % Date) - tf.write('\\\\\n') - - Water_conductivity = raw_input('What water conductivity ?') - tf.write('Water condutivity: & %s\\\\\n' % Water_conductivity) - tf.write('\\\\\n') - - Water_temp = raw_input('What water temperatur ?') - tf.write('Water temperature: & %s\\\\\n' % Water_temp) - tf.write('\\\\\n') - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\\\\n') - tf.write('\\\\\n') - annotations = raw_input('Any further annotations ?') - tf.write('Annotations:\\\\\n') - tf.write('%s\\\\\n' % annotations) - - tf.write('\n') - tf.write('\n') - tf.write('\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[3]) - tf.write('\\includegraphics{figures/fishtype_barplot.pdf}\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\pagebreak\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[4]) - tf.write('\\includegraphics{figures/histo_of_eod_freqs.pdf}\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[5]) - tf.write('\\includegraphics{figures/histo_of_dfs.pdf}\n') - tf.write('\\pagebreak\n') - tf.write('\\section*{Wavefishes list}\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - for i in np.arange(len(wavefish)): - help_var = i + 1 - if help_var % 35 == 0: - tf.write('%s & %s \\\\\n' % (i + 1, wavefish[i])) - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - else: - tf.write('%s & %s \\\\\n' % (i + 1, wavefish[i])) - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - # tf.write( '\\pagebreak\n' ) - tf.write('\n') - tf.write('\\section*{Pulsefishes list}\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - for j in np.arange(len(pulsefish)): - help_var2 = j + 1 - if help_var2 % 35 == 0: - tf.write('%s & %s \\\\\n' % (j + 1, pulsefish[j])) - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - else: - tf.write('%s & %s \\\\\n' % (j + 1, pulsefish[j])) - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\n') - tf.write('\n') - tf.write('\\end{document}\n') - tf.close() - os.system('pdflatex %s' % filename) - os.remove('%s.aux' % filename) - os.remove('%s.log' % filename) - os.remove('%s.tex' % filename) - - -def load_npy_convert_list(): - wavefish = np.load('%s' % sys.argv[1]) - wavefish = wavefish.tolist() - for i in np.arange(len(wavefish)): - wavefish[i] = "%.2f" % wavefish[i] - - pulsefish = np.load('%s' % sys.argv[2]) - pulsefish = pulsefish.tolist() - for j in np.arange(len(pulsefish)): - pulsefish[j] = "%.2f" % pulsefish[j] - - return wavefish, pulsefish - - -def clean_up(): - response = raw_input('Shall I deleate some of the figures and .npy files used ? [y/n]') - if response is 'y': - respose = raw_input('Shall I deleate the .npy files? [y/n]') - if response is 'y': - os.remove('%s' % sys.argv[1]) - os.remove('%s' % sys.argv[2]) - - response = raw_input('Shall I deleate the figures? [y/n]') - if response is 'y': - os.remove('figures/fishtype_barplot.pdf') - os.remove('figures/histo_of_eod_freqs.pdf') - os.remove('figures/histo_of_dfs.pdf') - elif response is 'n': - print 'Builded .pdf file successfully' - else: - clean_up() - - -def main(): - print '### Lets make a .tex file ###' - wavefish, pulsefish = load_npy_convert_list() - build_tex_pdf(wavefish, pulsefish) - clean_up() - - -if __name__ == '__main__': - main() From 9dc50ef1996a687101a84f5e90752197f5ae40b3 Mon Sep 17 00:00:00 2001 From: Sehuanes Date: Thu, 30 Jun 2016 17:37:49 +0200 Subject: [PATCH 05/13] removed create_tex2.py --- thunderfish/create_tex2.py | 213 ------------------------------------- 1 file changed, 213 deletions(-) delete mode 100644 thunderfish/create_tex2.py diff --git a/thunderfish/create_tex2.py b/thunderfish/create_tex2.py deleted file mode 100644 index eec661d3..00000000 --- a/thunderfish/create_tex2.py +++ /dev/null @@ -1,213 +0,0 @@ -__author__ = 'raab' - - -def description_of_code(): - """ - Builds a .pdf data. - - Arg 1: this script it self - Arg 2: .npy data from wavefish - Arg 3: .npy data from pulsefish - Arg 4: .pdf figure with distribution of wave and pulsefish - Arg 5: .pdf figure with distribution of eod frequenies - Arg 6: .pdf figure with distribution of beat frequencies - :return: - """ - - -import numpy as np -import sys -import os -import glob - - -def build_tex_pdf(wavefish, pulsefish): - response = raw_input('What shall the Title of the .pdf be ?') - filename = response - - tf = open('%s.tex' % filename, 'w') - tf.write('\\documentclass[a4paper,12pt,pdflatex]{article}\n') - tf.write('\\usepackage{graphics}\n') - tf.write('\n') - tf.write('\\begin{document}\n') - tf.write('\\section*{%s}\n' % filename) - - response = raw_input('Do you want to enter extra Data (loation, temp, cond. etc ? yes[y] or no[n]') - if response == "y": - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - # tf.write( 'Question & Answer \\\\ \\hline \n' ) - tf.write('\\\\\n') - - Country = raw_input('What country ?') - tf.write('Country: & %s\\\\\n' % Country) - tf.write('\\\\\n') - - Location = raw_input('What location ?') - tf.write('Location: & %s\\\\\n' % Location) - tf.write('\\\\\n') - - Date = raw_input('What date ?') - tf.write('Date: & %s\\\\\n' % Date) - tf.write('\\\\\n') - - Water_conductivity = raw_input('What water conductivity ?') - tf.write('Water condutivity: & %s\\\\\n' % Water_conductivity) - tf.write('\\\\\n') - - Water_temp = raw_input('What water temperatur ?') - tf.write('Water temperature: & %s\\\\\n' % Water_temp) - tf.write('\\\\\n') - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\\\\n') - tf.write('\\\\\n') - annotations = raw_input('Any further annotations ?') - tf.write('Annotations:\\\\\n') - tf.write('%s\\\\\n' % annotations) - - tf.write('\n') - tf.write('\n') - tf.write('\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[3]) - if len(wavefish) > 0 and len(pulsefish) > 0: - tf.write('\\includegraphics{figures/fishtype_barplot.pdf}\n') - tf.write('\n') - tf.write('\\pagebreak\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[4]) - - tf.write('\\includegraphics{figures/histo_of_eod_freqs.pdf}\n') - # tf.write( '\\includegraphics{%s}\n' %sys.argv[5]) - # tf.write( '\\includegraphics{figures/histo_of_dfs.pdf}\n' ) ### verschieben - tf.write('\\pagebreak\n') - - # for loop looking throught figures/; for every file add best window and EOD plot - for file in np.arange(len(glob.glob('figures/PSD_best_window*.pdf'))) + 1: - # best window - tf.write('\\includegraphics{figures/PSD_best_window%.0f.pdf}\n' % file) - # tf.write( '\\pagebreak\n') - - # eod - if os.path.exists('figures/EOD%.0f.pdf' % file): - tf.write('\\includegraphics{figures/EOD%.0f.pdf}\n' % file) - tf.write('\\pagebreak\n') - if os.path.exists('figures/pulse-EOD%.0f.pdf' % file): - tf.write('\\includegraphics{figures/pulse-EOD%.0f.pdf}\n' % file) - tf.write('\\pagebreak\n') - # tf.write( '\\pagebreak\n') - - tf.write('\\section*{Wavefishes list}\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - for i in np.arange(len(wavefish)): - help_var = i + 1 - if help_var % 35 == 0: - tf.write('%s & %s \\\\\n' % (i + 1, wavefish[i])) - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - else: - tf.write('%s & %s \\\\\n' % (i + 1, wavefish[i])) - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - # tf.write( '\\pagebreak\n' ) - tf.write('\n') - tf.write('\\section*{Pulsefishes list}\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - for j in np.arange(len(pulsefish)): - help_var2 = j + 1 - if help_var2 % 35 == 0: - tf.write('%s & %s \\\\\n' % (j + 1, pulsefish[j])) - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\\begin{tabular}[t]{rr}\n') - tf.write('\\hline\n') - tf.write('No. & freq [Hz] \\\\ \\hline \n') - - else: - tf.write('%s & %s \\\\\n' % (j + 1, pulsefish[j])) - - tf.write('\\hline\n') - tf.write('\\end{tabular}\n') - tf.write('\n') - tf.write('\n') - tf.write('\\end{document}\n') - tf.close() - os.system('pdflatex %s' % filename) - os.remove('%s.aux' % filename) - os.remove('%s.log' % filename) - os.remove('%s.tex' % filename) - - -def load_npy_convert_list(): - wave_frequencies = 'fish_wave.npy' - pulse_frequencies = 'fish_pulse.npy' - - if not os.path.exists(wave_frequencies): - np.save(wave_frequencies, np.array([])) - - wavefish = np.load(wave_frequencies) - wavefish = wavefish.tolist() - for i in np.arange(len(wavefish)): - wavefish[i] = "%.2f" % wavefish[i] - - if not os.path.exists(pulse_frequencies): - np.save(pulse_frequencies, np.array([])) - pulsefish = np.load(pulse_frequencies) - pulsefish = pulsefish.tolist() - for j in np.arange(len(pulsefish)): - pulsefish[j] = "%.2f" % pulsefish[j] - - return wavefish, pulsefish - - -def clean_up(): - response = raw_input('Shall I deleate some of the figures and .npy files used ? [y/n]') - if response is 'y': - respose = raw_input('Shall I deleate the .npy files? [y/n]') - if response is 'y': - os.remove('fish_wave.npy') - os.remove('fish_pulse.npy') - - response = raw_input('Shall I deleate the figures? [y/n]') - if response is 'y': - os.remove('figures/fishtype_barplot.pdf') - os.remove('figures/histo_of_eod_freqs.pdf') - os.remove('figures/histo_of_dfs.pdf') - elif response is 'n': - print 'Builded .pdf file successfully' - else: - clean_up() - - -def main(): - print '### Lets make a .tex file ###' - wavefish, pulsefish = load_npy_convert_list() - build_tex_pdf(wavefish, pulsefish) - clean_up() - - -if __name__ == '__main__': - main() From 3f619a05d82efa21e91ebc78435884abb622d336 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:21:27 +0200 Subject: [PATCH 06/13] sorting_tools.py removed --- thunderfish/sorting_tools.py | 98 ------------------------------------ 1 file changed, 98 deletions(-) delete mode 100644 thunderfish/sorting_tools.py diff --git a/thunderfish/sorting_tools.py b/thunderfish/sorting_tools.py deleted file mode 100644 index 9b7441cf..00000000 --- a/thunderfish/sorting_tools.py +++ /dev/null @@ -1,98 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -import scipy.stats as sps -import os - - -def filter_fishes(fishlists): - ################################################################### - # extract fishes which are consistent for different resolutions - fundamentals = [[] for i in fishlists] - for i in np.arange(len(fishlists)): - for j in np.arange(len(fishlists[i])): - fundamentals[i].append(fishlists[i][j][0][0]) - consistent_fish_help = [1 for k in fundamentals[0]] - - for i in np.arange(len(fundamentals[0])): - for j in np.arange(len(fundamentals) - 1): - for k in np.arange(len(fundamentals[j + 1])): - if fundamentals[0][i] < fundamentals[j + 1][k] + 2 and fundamentals[0][i] > fundamentals[j + 1][k] - 2: - consistent_fish_help[i] += 1 - index_of_valid_fish = [] - for i in np.arange(len(consistent_fish_help)): - if consistent_fish_help[i] == len(fishlists): - index_of_valid_fish.append(i) - - fishlist = [] - for i in index_of_valid_fish: - fishlist.append(fishlists[0][i]) - - return fishlist - - -def wave_or_pulse_psd(power, freqs, data, rate, fresolution, create_dataset=False, category='wave'): - freq_steps = int(125 / fresolution) - proportions = [] - mean_powers = [] - # power_db = 10.0 * np.log10(power) - for i in np.arange((1500 / fresolution) // freq_steps): # does all of this till the frequency of 3k Hz - power_db = 10.0 * np.log10(power[i * freq_steps:i * freq_steps + freq_steps]) - power_db_p25_75 = [] - power_db_p99 = np.percentile(power_db, 99) - power_db_p1 = np.percentile(power_db, 1) - power_db_p25 = np.percentile(power_db, 25) - power_db_p75 = np.percentile(power_db, 75) - - ### proportion psd (idea by Jan) - proportions.append((power_db_p75 - power_db_p25) / ( - power_db_p99 - power_db_p1)) # value between 0 and 1; pulse psds have much bigger values than wave psds - ### - mean_proportions = np.mean(proportions) - if np.mean(proportions) < 0.27: - psd_type = 'wave' - else: - psd_type = 'pulse' - - if create_dataset: - if category is 'wave': - if not os.path.exists('wave_PSD_algor.npy'): - np.save('wave_PSD_algor.npy', np.array([])) - wave_prop = np.load('wave_PSD_algor.npy') - wave_prop = wave_prop.tolist() - wave_prop.append(np.mean(proportions)) - wave_prop = np.asarray(wave_prop) - np.save('wave_PSD_algor.npy', wave_prop) - - elif category is 'pulse': - if not os.path.exists('pulse_PSD_algor.npy'): - np.save('pulse_PSD_algor.npy', np.array([])) - pulse_prop = np.load('pulse_PSD_algor.npy') - pulse_prop = pulse_prop.tolist() - pulse_prop.append(np.mean(proportions)) - pulse_prop = np.asarray(pulse_prop) - np.save('pulse_PSD_algor.npy', pulse_prop) - - else: - print("unknown fish category: %s" % category) - quit() - - return psd_type, mean_proportions - - -def save_fundamentals(fishlist, output_folder): - if not os.path.exists(output_folder): - os.makedirs(output_folder) - - file_name = output_folder + 'fish_wave.npy' - if not os.path.exists(file_name): - np.save(file_name, np.array([])) - fundamentals = np.load(file_name) - fundamentals = fundamentals.tolist() - - for fish in np.arange(len(fishlist)): - fundamentals.append(fishlist[fish][0][0]) - - fundamentals = np.asarray(fundamentals) - np.save(file_name, fundamentals) - - print('current fundamental frequencies are: ', fundamentals) From 41eeaa26766f9fb31c3afe835892c6c66e646d59 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:22:04 +0200 Subject: [PATCH 07/13] mat_meth_fig.py removed --- thunderfish/mat_meth_fig.py | 266 ------------------------------------ 1 file changed, 266 deletions(-) delete mode 100644 thunderfish/mat_meth_fig.py diff --git a/thunderfish/mat_meth_fig.py b/thunderfish/mat_meth_fig.py deleted file mode 100644 index d2027594..00000000 --- a/thunderfish/mat_meth_fig.py +++ /dev/null @@ -1,266 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -import pandas as pd -import random -import scipy.stats as sps -from harmonicgroups import * - - -def load_data(): - wave_psd_prop = np.load('wave_PSD_algor.npy') - pulse_psd_prop = np.load('pulse_PSD_algor.npy') - multi_wave_psd_prop = np.load('multi_wave_PSD_algor.npy') - - wave_p2v_prop = np.load('wave_p2v_algor.npy') - pulse_p2v_prop = np.load('pulse_p2v_algor.npy') - multi_wave_p2v_prop = np.load('multi_wave_p2v_algor.npy') - - wave_hist_algor = np.load('wave_hist_algor.npy') - pulse_hist_algor = np.load('pulse_hist_algor.npy') - multi_wave_hist_algor = np.load('multi_wave_hist_algor.npy') - - mixed_psd_prop = np.load('mixed_PSD_algor.npy') - mixed_p2v_prop = np.load('mixed_p2v_algor.npy') - mixed_hist_algor = np.load('mixed_hist_algor.npy') - - return wave_psd_prop, pulse_psd_prop, multi_wave_psd_prop, wave_p2v_prop, pulse_p2v_prop, multi_wave_p2v_prop, \ - wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor, mixed_psd_prop, mixed_p2v_prop, mixed_hist_algor - - -def plot_data(wave_psd_prop, pulse_psd_prop, multi_wave_psd_prop, wave_p2v_prop, pulse_p2v_prop, - multi_wave_p2v_prop, wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor, mixed_psd_prop, - mixed_p2v_prop, mixed_hist_algor): - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fs = 20 - - fig1, ax = plt.subplots(figsize=(40. / inch_factor, 25. / inch_factor)) - ax.plot(wave_psd_prop, wave_p2v_prop, 'o', color='royalblue', label='Wave-type', alpha=0.7, markersize=20, mec='k', - mew=3) - ax.plot(pulse_psd_prop, pulse_p2v_prop, 'o', color='firebrick', label='Pulse-type', alpha=0.7, markersize=20, - mec='k', mew=3) - ax.plot(mixed_psd_prop, mixed_p2v_prop, 'o', color='darkorange', label='Mixed', alpha=0.7, markersize=17, mec='k', - mew=3) - ax.tick_params(axis='both', which='major', labelsize=fs - 2) - - plt.legend(fontsize=fs - 2) - # ax.set_xlabel('P / ' + r'$\rho$', fontsize=fs) - ax.set_xlabel(r'$\langle \rho/P \rangle$', fontsize=fs) - ax.set_ylabel(r'$\langle \tau/T \rangle$', fontsize=fs) - ax.set_title('Relation between\n' + r'$\langle \rho/P \rangle$' + ' and ' + r'$\langle \tau/T \rangle$', - fontsize=fs + 2) - ax.set_ylim([0, 0.5]) - - sns.despine(fig=fig1, ax=ax, offset=10) - fig1.tight_layout() - # plt.draw() - - -def plot_psd_algo(wave_prop, pulse_prop, multi_wave_prop, mixed_prop): - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fs = 20 - - fig2 = plt.figure(figsize=(40. / inch_factor, 20. / inch_factor)) - ax1 = fig2.add_subplot(2, 3, (1, 4)) - dafr = pd.DataFrame([wave_prop, pulse_prop]) # turn - dafr = dafr.transpose() - dafr.columns = ['Wave', 'Pulse'] - sns.violinplot(data=dafr, ax=ax1, inner=None, linewidth=2, color='k', palette=("royalblue", "firebrick"), - scale="width") - ax1.set_ylabel(r'$\langle \rho/P \rangle$', fontsize=fs) - ax1.set_xlabel('EOD-type', fontsize=fs) - ax1.set_title(r'$\langle \rho/P \rangle$' + ' for different\nEOD-types', fontsize=fs + 2) - ax1.tick_params(axis='both', which='major', labelsize=fs - 2) - ax1.set_ylim([0, 0.6]) - - pulse_psd_data = np.load('pulse_psd_data.npy') - ax2 = fig2.add_subplot(2, 3, (2, 3)) - ax2.axis([0, 1500, min(pulse_psd_data[1][:len(pulse_psd_data[0][pulse_psd_data[0] < 1500])]), - max(pulse_psd_data[1]) + 10]) - ax2.plot(pulse_psd_data[0], pulse_psd_data[1], lw=2, color='firebrick', alpha=0.9) - ax2.set_xlabel('Frequency [Hz]', fontsize=fs) - ax2.set_ylabel('Power [dB]', fontsize=fs) - ax2.set_title('Pulsefish PSD\n', fontsize=fs + 2) - ax2.tick_params(axis='both', which='major', labelsize=fs - 2) - - ################################### - fres = pulse_psd_data[0][-1] / len(pulse_psd_data[0]) - freq_steps = int(125 / fres) - - p1 = [] - p99 = [] - p25 = [] - p75 = [] - for i in np.arange((1500 / fres) // freq_steps): # does all of this till the frequency of 3k Hz - bdata = pulse_psd_data[1][i * freq_steps:i * freq_steps + freq_steps - 1] - p1.append(np.percentile(bdata, 1)) - p99.append(np.percentile(bdata, 99)) - p25.append(np.percentile(bdata, 25)) - p75.append(np.percentile(bdata, 75)) - x_freqs = [freq_steps / 2 * fres + i * freq_steps * fres for i in np.arange(len(p1))] - - ax2.plot(x_freqs, p1, '-', color='green') - ax2.plot(x_freqs, p25, '-', color='darkorange') - ax2.plot(x_freqs, p75, '-', color='darkorange') - ax2.plot(x_freqs, p99, '-', color='green') - - ax2.plot([x_freqs[3], x_freqs[3]], [p1[3], p99[3]], '-', color='k', lw=5, label='P') - ax2.plot([x_freqs[4], x_freqs[4]], [p25[4], p75[4]], '-', color='royalblue', lw=5, label=r'$ \rho $') - plt.legend(bbox_to_anchor=(1, 1.2), ncol=2) - - wave_psd_data = np.load('wave_psd_data.npy') - ax3 = fig2.add_subplot(2, 3, (5, 6)) - ax3.axis( - [0, 1500, min(wave_psd_data[1][:len(wave_psd_data[0][wave_psd_data[0] < 1500])]), max(wave_psd_data[1]) + 10]) - ax3.plot(wave_psd_data[0], wave_psd_data[1], lw=2, color='royalblue', alpha=0.9) - ax3.set_xlabel('Frequency [Hz]', fontsize=fs) - ax3.set_ylabel('Power [dB]', fontsize=fs) - ax3.set_title('Wavefish PSD\n', fontsize=fs + 2) - ax3.tick_params(axis='both', which='major', labelsize=fs - 2) - - sns.despine(fig=fig2, ax=[ax1, ax2, ax3], offset=10) - fig2.tight_layout() - # plt.draw() - - -def accept_peak_heights(times, data, peak_inx, index, trough_inx, min_inx, threshold, check_conditions): - return [times[peak_inx], data[peak_inx]] - - -def plot_p2v_algo(wave_p2v_prop, pulse_p2v_prop, multi_wave_p2v_prop): - samplerate = 44100 ### really hard code - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fs = 20 - - fig3 = plt.figure(figsize=(40. / inch_factor, 20. / inch_factor)) - - ax1 = fig3.add_subplot(2, 3, (1, 4)) - dafr2 = pd.DataFrame([wave_p2v_prop, pulse_p2v_prop]) - dafr2 = dafr2.transpose() - dafr2.columns = ['Wave', 'Pulse'] - sns.violinplot(data=dafr2, ax=ax1, inner=None, linewidth=2, color='k', palette=("royalblue", "firebrick"), - scale="width") - ax1.set_ylabel(r'$\langle \tau/T \rangle$', fontsize=fs) - ax1.set_xlabel('EOD-type', fontsize=fs) - ax1.set_title(r'$\langle \tau/T \rangle$' + ' for different\nEOD-types', fontsize=fs + 2) - ax1.tick_params(axis='both', which='major', labelsize=fs - 2) - ax1.set_ylim([0, 0.5]) - - pulse_trace_data = np.load('pulse_trace_data.npy') - time = np.arange(3000) * 1.0 / samplerate * 1000 - data = pulse_trace_data[500:3500] / 2.0 ** 15 - peaks = detect_peaks(time, data, 0.25 * (np.max(data) - np.min(data)), accept_peak_heights) - troughs = detect_peaks(time, -data, 0.25 * (np.max(data) - np.min(data)), accept_peak_heights) - ax2 = fig3.add_subplot(2, 3, (2, 3)) - ax2.plot(peaks[:, 0], peaks[:, 1], 'go', markersize=15, alpha=0.7) - ax2.plot(troughs[:, 0], -troughs[:, 1], 'o', color='darkorange', markersize=15, alpha=0.7) - - ax2.plot(time, data, lw=2, color='firebrick', alpha=0.9) - ax2.set_xlim([0, 60]) - ax2.set_ylim([-1, 1]) - ax2.set_xlabel('Time [ms]', fontsize=fs) - ax2.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax2.set_title('Pulsefish EODs\n', fontsize=fs + 2) - ax2.tick_params(axis='both', which='major', labelsize=fs - 2) - - wave_trace_data = np.load('wave_trace_data.npy') - time = np.arange(500) * 1.0 / samplerate * 1000 - data = wave_trace_data[:500] / 2.0 ** 15 - peaks = detect_peaks(time, data, 0.75 * (np.max(data) - np.min(data)), accept_peak_heights) - troughs = detect_peaks(time, -data, 0.75 * (np.max(data) - np.min(data)), accept_peak_heights) - ax3 = fig3.add_subplot(2, 3, (5, 6)) - ax3.plot(peaks[:, 0], peaks[:, 1], 'go', markersize=15, alpha=0.7) - ax3.plot(troughs[:, 0], -troughs[:, 1], 'o', color='darkorange', markersize=15, alpha=0.7) - - # ax3.annotate("", xy= (peaks[1][0], peaks[1][1]), xytext=(peaks[0][0], peaks[0][1]), arrowprops={"facecolor": 'red', "shrink": 0.05}) - # ax3.annotate('T', xy=(1.55, 0.042), xycoords='data', xytext=(5, 0), textcoords='offset points') - - # ax3.annotate("", xy= (troughs[1][0], -troughs[1][1]), xytext=(peaks[0][0], -troughs[1][1]), arrowprops={"facecolor": 'k', "shrink": 0.05}) - # ax3.annotate(r'$\tau$', xy=(1.2, -0.04), xycoords='data', xytext=(5, 0), textcoords='offset points') - - ax3.plot([peaks[0][0], peaks[1][0]], [peaks[0][1], peaks[0][1]], '-', color='k', label='T', lw=5) - ax3.plot([peaks[0][0], troughs[1][0]], [-troughs[1][1], -troughs[1][1]], '-', color='royalblue', label=r'$\tau$', - lw=5) - plt.legend(bbox_to_anchor=(1, 1.2), ncol=2) - - ax3.plot(np.arange(500) * 1.0 / samplerate * 1000, wave_trace_data[:500] / 2.0 ** 15, lw=2, color='royalblue', - alpha=0.9) - ax3.set_xlim([0, 11]) - ax3.set_ylim([-0.06, 0.06]) - ax3.set_xlabel('Time [ms]', fontsize=fs) - ax3.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax3.set_title('Wavefish EODs\n', fontsize=fs + 2) - ax3.tick_params(axis='both', which='major', labelsize=fs - 2) - - sns.despine(fig=fig3, ax=[ax1, ax2, ax3], offset=10) - fig3.tight_layout() - # plt.draw() - - -def plot_hist_algo(wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor): - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - # sns.set_style("ticks") - - fig4 = plt.figure(figsize=(35. / inch_factor, 20. / inch_factor)) - ax1 = fig4.add_subplot(2, 3, (1, 4)) - dafr = pd.DataFrame([wave_hist_algor, multi_wave_hist_algor, pulse_hist_algor]) # turn - dafr = dafr.transpose() - dafr.columns = ['wave', 'multi-wave', 'pulse'] - sns.violinplot(data=dafr, ax=ax1, col=("blue", "green", "red")) - ax1.set_ylabel('psd_proportion') - ax1.set_xlabel('EOD-type') - ax1.set_title('Fishsorting based on PSD') - - wave_psd_data = np.load('wave_psd_data.npy') - wave_hist_data = wave_psd_data[1][:len(wave_psd_data[0][wave_psd_data[0] < 1500])] - ax3 = fig4.add_subplot(2, 3, (2, 5)) - n, bin, patch = ax3.hist(wave_hist_data, 50, color='blue', alpha=0.7, normed=True) - # ax3.set_ylim([0, max(n)+10]) - ax3.set_ylabel('counts in histogram bin') - ax3.set_xlabel('amplitude of PSD') - ax3.set_title('Histogram of pulsefish PSD') - - pulse_psd_data = np.load('pulse_psd_data.npy') - pulse_hist_data = pulse_psd_data[1][:len(pulse_psd_data[0][pulse_psd_data[0] < 1500])] - ax2 = fig4.add_subplot(2, 3, (3, 6)) - ax2.hist(pulse_hist_data, 50, color='red', alpha=0.7, normed=True) - # ax2.set_ylim([0, max(n)+10]) - ax2.set_ylabel('counts in histogram bin') - ax2.set_xlabel('amplitude of PSD') - ax2.set_title('Histogram of pulsefish PSD') - - fig4.tight_layout() - - -def main(): - wave_psd_prop, pulse_psd_prop, multi_wave_psd_prop, wave_p2v_prop, pulse_p2v_prop, multi_wave_p2v_prop, \ - wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor, mixed_psd_prop, mixed_p2v_prop, \ - mixed_hist_algor = load_data() - - plot_data(wave_psd_prop, pulse_psd_prop, multi_wave_psd_prop, wave_p2v_prop, pulse_p2v_prop, multi_wave_p2v_prop, - wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor, mixed_psd_prop, mixed_p2v_prop, - mixed_hist_algor) - - plot_psd_algo(wave_psd_prop, pulse_psd_prop, multi_wave_psd_prop, mixed_psd_prop) - - plot_p2v_algo(wave_p2v_prop, pulse_p2v_prop, multi_wave_p2v_prop) - - # plot_hist_algo(wave_hist_algor, pulse_hist_algor, multi_wave_hist_algor) - - plt.show() - - -if __name__ == '__main__': - main() From 137758084bca7a3c19e61c5a17fff7604151c07e Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:26:36 +0200 Subject: [PATCH 08/13] freq_power_analyse.py removed --- thunderfish/freq_power_analyse.py | 48 ------------------------------- 1 file changed, 48 deletions(-) delete mode 100644 thunderfish/freq_power_analyse.py diff --git a/thunderfish/freq_power_analyse.py b/thunderfish/freq_power_analyse.py deleted file mode 100644 index 681bb62f..00000000 --- a/thunderfish/freq_power_analyse.py +++ /dev/null @@ -1,48 +0,0 @@ -__author__ = 'raab' -import numpy as np -import matplotlib.pyplot as plt -import scipy.stats as sp - -pulse_diffs = np.load('pulse_diffs.npy') -# pulse_diffs = pulse_diffs.tolist() - -wave_diffs = np.load('wave_diffs.npy') -# wave_diffs = wave_diffs.tolist() - -pulse_prop = np.load('pulse_proportions.npy') -# pulse_prop = pulse_prop.tolist() - -wave_prop = np.load('wave_proportions.npy') - -pulse_trace_prop = np.load('pulse_trace_proportions.npy') - -wave_trace_prop = np.load('wave_trace_proportions.npy') - -wd = [1 for i in np.arange(len(wave_diffs))] -pd = [1 for j in np.arange(len(pulse_diffs))] -wp = [1 for k in np.arange(len(wave_prop))] -pp = [1 for l in np.arange(len(pulse_prop))] -wt = [1 for m in np.arange(len(wave_trace_prop))] -pt = [1 for n in np.arange(len(pulse_trace_prop))] - -fig, ax = plt.subplots(facecolor='white', nrows=1, ncols=3) -ax[0].set_title('difference (p90-p10)') -ax[0].plot(wd, wave_diffs, '.') -ax[0].plot(pd, pulse_diffs, '.', color='r') - -ax[1].set_title('proportion ((p75-p25)/(p99-p1))') -ax[1].plot(wp, wave_prop, '.') -ax[1].plot(pp, pulse_prop, '.', color='r') - -ax[2].set_title('trace proportions') -ax[2].plot(wt, wave_trace_prop, '.') -ax[2].plot(pt, pulse_trace_prop, '.', color='r') - -plt.show() - -fig, ax = plt.subplots(facecolor='white') -plt.xlabel('difference (p90-p10)') -plt.ylabel('proportion ((p75-p25)/(p99-p1))') -ax.plot(wave_diffs, wave_trace_prop, '.') -ax.plot(pulse_diffs, pulse_trace_prop, '.', color='r') -plt.show() From 706a2e0f097f14141cd9a57effbbedc6f1d210b5 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:27:08 +0200 Subject: [PATCH 09/13] FishTracker.py removed --- thunderfish/FishTracker.py | 528 ------------------------------------- 1 file changed, 528 deletions(-) delete mode 100644 thunderfish/FishTracker.py diff --git a/thunderfish/FishTracker.py b/thunderfish/FishTracker.py deleted file mode 100644 index 75311316..00000000 --- a/thunderfish/FishTracker.py +++ /dev/null @@ -1,528 +0,0 @@ -import os -import glob -import matplotlib.pyplot as plt -# import seaborn as sns -import matplotlib.mlab as ml -from harmonicgroups import * -import sorting_tools as st - - -class FishTracker: - def __init__(self, filename, samplingrate): - self.rate = samplingrate - self.tstart = 0 - self.fish_freqs_dict = {} - self.datasize = 200.0 # seconds - self.step = 0.5 - self.fresolution = 0.5 - self.twindow = 8.0 - self.fishes = {} - self.filename = filename - - def processdata(self, data, fish_type, bwin, win_width, config_dict, - test_longfile=False): # , rate, fish_freqs_dict, tstart, datasize, step ) : - """ - gets sound data. - builds a powerspectrum over 8 sec. these 8 seconds shift through the sound data in steps of 0.5 sec. - - calls function: harmonic_groups witch returns an array (fishlist) containing information for each fish. - Example: np.array([fund_freq_fish1, power], [harmonic1_fish1, power], ...), - np.array([fund_freq_fish2, power], [harmonic1_fish2, power], ...), ... - - calls function: puls_or_wave. This function gets the array fishlist. (see above) - Analysis the data and discriminates between puls and wavefish. - returns lists containing the fundamental frequencies for either wave- or pulsfish. - - finally: updates a global dictionary (self.fish_freqs_dict) witch gets the time variable (as key) - and the list of WAVE-fishes. - - updates a global dictionary (self.pulsfish_freqs_dict) witch gets the time variable (as key) - and the list of PULS-fishes. - """ - - test_resolutions = [self.fresolution, self.fresolution * 2.0, self.fresolution * 4.0] - all_nfft = [] - for i in np.arange(len(test_resolutions)): - nfft = int(np.round(2 ** (np.floor(np.log(self.rate / test_resolutions[i]) / np.log(2.0)) + 1.0))) - if nfft < 16: - nfft = 16 - all_nfft.append(nfft) - - tw = int(np.round(self.twindow * self.rate)) - minw = all_nfft[0] * (config_dict['minPSDAverages'][0] + 1) / 2 - if tw < minw: - tw = minw - - self.datasize = len(data) / self.rate - - # running Power spectrum in best window (8s) - fishlists_dif_fresolution = [] # gets fishlists from 3 different psd(powerspectrum) - for i in np.arange(len(all_nfft)): - power, freqs = ml.psd(data[(bwin * self.rate):(bwin * 1.0 * self.rate + win_width * 1.0 * self.rate)], - NFFT=all_nfft[i], noverlap=all_nfft[i] / 2, Fs=self.rate, - detrend=ml.detrend_mean) - if i == 0: - power_fres1 = power - freqs_fres1 = freqs - - fishlist, _, mains, allpeaks, peaks, lowth, highth, center = harmonic_groups(freqs, power, config_dict) - - # power to dB - for fish in np.arange(len(fishlist)): - for harmonic in np.arange(len(fishlist[fish])): - fishlist[fish][harmonic][-1] = 10.0 * np.log10(fishlist[fish][harmonic][-1]) - - fishlists_dif_fresolution.append(fishlist) - - fishlist = st.filter_fishes(fishlists_dif_fresolution) - - if fish_type is 'pulse': - for i in np.arange(len(fishlist))[::-1]: - if fishlist[i][0][0] < 150: - fishlist.pop(i) - - psd_type, mean_proportions = st.wave_or_pulse_psd(power_fres1, freqs_fres1, - data[(bwin * self.rate):( - bwin * self.rate + win_width * self.rate)], self.rate, - self.fresolution) - - wave_ls = [fishlist[fish][0][0] for fish in np.arange(len(fishlist))] - - config_dict['lowThreshold'][0] = lowth - config_dict['highThreshold'][0] = highth - - temp_dict = {bwin: wave_ls} - - self.fish_freqs_dict.update(temp_dict) - - # Pulsefish processing - # if psd_type is 'pulse' and fish_type is 'pulse': - # print 'HERE WE HAVE TO BUILD IN THE PULSEFISH ANALYSIS' - return power_fres1, freqs_fres1, psd_type, fish_type, fishlist, mean_proportions - - def sort_my_wavefish(self): - """ - this function works with the global dictionary self.fish_freq_dict and assigns the frequencies to fishes. - - - finally: updates a global dict (self.fishes) witch contains the fishes and the frequencies belonging to it. - Example: {[1: np.NaN np.NaN 174.3 175.0 np.NaN ...], - [2: np.NaN 180.7 np.NaN 181.9 np.NaN ...], ... - np.NaN --> fish was not available at this mesuing point. - this dict therefore carries the time variable. - - polt: x-axis: fishno.; y-axis: frequencies - """ - - dict_times = self.fish_freqs_dict.keys() - - for k, t in enumerate(sorted(self.fish_freqs_dict)): - - if t is dict_times[0]: - for i in np.arange(len(self.fish_freqs_dict[t])): - print 'Phase II; ', 'point in time is: ', t, 'secondes.', 'wavefish no.:', i - temp_fish = {len(self.fishes) + 1: [self.fish_freqs_dict[t][i]]} - self.fishes.update(temp_fish) - else: - for i in np.arange(len(self.fish_freqs_dict[t])): - print 'Phase II; ', 'point in time is: ', t, 'secondes.', 'wavefish no.:', i - help_v = 0 - new_freq = self.fish_freqs_dict[t][i] - - for j in self.fishes.keys(): - for p in np.arange(len(self.fishes[j])) + 1: - if self.fishes[j][-p] is not np.nan: - index_last_nan = -p - break - if self.fishes[j][index_last_nan] - 0.5 < new_freq <= self.fishes[j][index_last_nan] + 0.5 and \ - help_v == 0: - self.fishes[j].append(new_freq) - help_v += 1 - - if help_v is 0: - temp_fish = {len(self.fishes) + 1: []} - for l in np.arange(k): - temp_fish[len(self.fishes) + 1].append(np.NaN) - temp_fish[len(self.fishes) + 1].append(self.fish_freqs_dict[t][i]) - self.fishes.update(temp_fish) - elif help_v >= 2: - print "added frequency to more than one fish. reduce tolerance!!!" - break - for m in self.fishes.keys(): - if len(self.fishes[m]) < k + 1: - self.fishes[m].append(np.nan) - - def get_data(self): - data = np.zeros(np.ceil(self.rate * self.datasize), dtype=" 4: - ind = np.argsort([fishlist[fish][0][1] for fish in np.arange(len(fishlist))])[-4:] - # print ind - else: - ind = np.argsort([fishlist[fish][0][1] for fish in np.arange(len(fishlist))]) - # print ind - - plot_path = output_path + 'figures/' - if not os.path.exists(plot_path): - os.makedirs(plot_path) - # start plot cosmetic with seaborn - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - - plot_w = 26. - plot_h = 15. - fs = 16 # fontsize of the axis labels - inch_factor = 2.54 - - fig_all = plt.figure(figsize=(plot_w * 2 / inch_factor, plot_h * 2 / inch_factor)) - ax1_all = fig_all.add_subplot(2, 6, (7, 12)) - - power_dB = 10.0 * np.log10(power) - ax1_all.axis([0, 3000, min(power_dB[:len(freqs[freqs < 3000])]), max(power_dB) + 10]) - ax1_all.plot(freqs, power_dB, lw=2, color='royalblue', alpha=0.7) - - if save_data_for_plot: - np.save('wave_psd_data.npy', np.array([freqs, power_dB])) - - if fish_type is 'wave' or psd_type is 'wave': - color = ['red', 'blue', 'green', 'cornflowerblue'] - for color_no, fish in enumerate(ind): - for harmonic in np.arange(len(fishlist[fish])): - if harmonic == 0: - ax1_all.plot(fishlist[fish][harmonic][0], fishlist[fish][harmonic][1], 'o', - color=color[color_no], - label=('%.2f Hz' % fishlist[fish][0][0])) - else: - ax1_all.plot(fishlist[fish][harmonic][0], fishlist[fish][harmonic][1], 'o', - color=color[color_no]) - - ax1_all.tick_params(axis='both', which='major', labelsize=fs - 2) - ax1_all.legend(loc='upper right') - ax1_all.set_xlabel('Frequency [Hz]', fontsize=fs) - ax1_all.set_ylabel('Power [dB SPL]', fontsize=fs) - ax1_all.set_title('PSD of best window', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax1_all, offset=10) - - # ax2_all = fig_all.add_subplot(2, 6, (4, 6)) - ##################################################################################################### - if only_psd_plot: - fs = 18 - figx, axx = plt.subplots(figsize=(40. / inch_factor, 20. / inch_factor)) - - # power_dB = 10.0 * np.log10(power) - axx.axis([0, 2500, min(power_dB[:len(freqs[freqs < 3000])]), max(power_dB) + 10]) - axx.plot(freqs, power_dB, lw=2, color='royalblue', alpha=0.7) - - if fish_type is 'wave' or psd_type is 'wave': - color = ['red', 'blue', 'green', 'cornflowerblue'] - for color_no, fish in enumerate(ind): - for harmonic in np.arange(len(fishlist[fish])): - if harmonic == 0: - axx.plot(fishlist[fish][harmonic][0], fishlist[fish][harmonic][1], 'o', - color=color[color_no], - label=('%.2f Hz' % fishlist[fish][0][0])) - else: - axx.plot(fishlist[fish][harmonic][0], fishlist[fish][harmonic][1], 'o', - color=color[color_no]) - - axx.tick_params(axis='both', which='major', labelsize=fs - 2) - axx.legend(loc='upper right') - axx.set_xlabel('Frequency [Hz]', fontsize=fs) - axx.set_ylabel('Power [dB SPL]', fontsize=fs) - axx.set_title('PSD of best window', fontsize=fs + 2) - sns.despine(fig=figx, ax=axx, offset=10) - figx.tight_layout() - - plt.show(figx) - quit() - - ##################################################################################################### - if fish_type is 'wave' and psd_type is 'wave': - ax2_all = fig_all.add_subplot(2, 6, (4, 6)) - if len(fishlist) == 1: - eod_plot_tw = 0.030 - mean_wave_data = [] - std_wave_data = [] - - datapoints_p_wave = np.median([len(wave_data[i]) for i in wave_data.keys()]) - - for i in wave_data.keys(): - if len(wave_data[i]) != int(datapoints_p_wave): - # print i - del wave_data[i] - - for k in np.arange(int(datapoints_p_wave)): - - try: - tmp_mu = np.mean([wave_data[wave][k] for wave in sorted(wave_data.keys())]) - tmp_std = np.std([wave_data[wave][k] for wave in sorted(wave_data.keys())], ddof=1) - except IndexError: - print('Warning! Something seems odd when calculating sound-trace average. ' - 'Proceeding with fingers crossed...\n') - continue - - mean_wave_data.append(tmp_mu) - std_wave_data.append(tmp_std) - - up_std = [mean_wave_data[i] + std_wave_data[i] for i in range(len(mean_wave_data))] - bottom_std = [mean_wave_data[i] - std_wave_data[i] for i in range(len(mean_wave_data))] - plot_time = ((np.arange(len(mean_wave_data)) * 1.0 / self.rate) - eod_plot_tw / 2) * 1000 # s to ms - - ax2_all.set_xlim([-1.5 / fishlist[0][0][0] * 1000, 1.5 / fishlist[0][0][0] * 1000]) - ax2_all = fig_all.add_subplot(2, 6, (4, 6)) - ax2_all.plot(plot_time, mean_wave_data, lw=2, color='firebrick', alpha=0.7, label='mean EOD') - ax2_all.fill_between(plot_time, up_std, bottom_std, color='grey', alpha=0.3) - ax2_all.tick_params(axis='both', which='major', labelsize=fs - 2) - ax2_all.set_xlabel('Time [ms]', fontsize=fs) - ax2_all.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax2_all.set_title('Mean wave-EOD', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax2_all, offset=10) - - else: - # ax2_all = fig_all.add_subplot(2, 6, (4, 6)) - # fig3, ax3 = plt.subplots(figsize=(plot_w / inch_factor, plot_h / inch_factor)) - # ToDo: Brasil data doesnt work properly: Line 260 Index out of bounds - eod_wtimes = (np.arange( - len(data[ - (bwin * self.rate):(bwin * self.rate + round(self.rate * 1.0 / fishlist[ind[-1]][0][0] * 4))]) - ) * 1.0 / self.rate) * 1000.0 - eod_wampls = data[ - (bwin * self.rate):( - bwin * self.rate + round(self.rate * 1.0 / fishlist[ind[-1]][0][0] * 4))] - - # WHEN SINGLEWAVEFISH (len(fishlist) =1) gemittelter plot; anderenfall continue - ax2_all.set_xlim([eod_wtimes[0], eod_wtimes[-1]]) - ax2_all.plot(eod_wtimes, eod_wampls, lw=2, color='firebrick', alpha=0.7) - ax2_all.tick_params(axis='both', which='major', labelsize=fs - 2) - ax2_all.set_xlabel('Time [ms]', fontsize=fs) - ax2_all.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax2_all.set_title('EOD-Waveform', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax2_all, offset=10) - - if fish_type is 'pulse' or psd_type is 'pulse': - eod_plot_tw = 0.006 - mean_pulse_data = [] - std_pulse_data = [] - - datapoints_p_pulse = np.median([len(pulse_data[i]) for i in pulse_data.keys()]) - - for i in pulse_data.keys(): - if len(pulse_data[i]) != int(datapoints_p_pulse): - # print i - del pulse_data[i] - - for k in np.arange(int(datapoints_p_pulse)): - - try: - tmp_mu = np.mean([pulse_data[pulse][k] for pulse in sorted(pulse_data.keys())]) - tmp_std = np.std([pulse_data[pulse][k] for pulse in sorted(pulse_data.keys())], ddof=1) - except IndexError: - print('Warning! Something seems odd when calculating sound-trace average. ' - 'Proceeding with fingers crossed...\n') - continue - - mean_pulse_data.append(tmp_mu) - std_pulse_data.append(tmp_std) - - up_std = [mean_pulse_data[i] + std_pulse_data[i] for i in range(len(mean_pulse_data))] - bottom_std = [mean_pulse_data[i] - std_pulse_data[i] for i in range(len(mean_pulse_data))] - - # get time for plot - plot_time = ((np.arange(len(mean_pulse_data)) * 1.0 / self.rate) - eod_plot_tw / 2) * 1000 # s to ms - if fish_type is 'pulse' and psd_type is 'pulse': - ax2_all = fig_all.add_subplot(2, 6, (4, 6)) - ax2_all.plot(plot_time, mean_pulse_data, lw=2, color='firebrick', alpha=0.7, label='mean EOD') - ax2_all.fill_between(plot_time, up_std, bottom_std, color='grey', alpha=0.3) - # ax2_all.plot(plot_time, up_std, lw=1, color='red', alpha=0.7, label='std EOD') - # ax2_all.plot(plot_time, bottom_std, lw=1, color='red', alpha=0.7) - ax2_all.tick_params(axis='both', which='major', labelsize=fs - 2) - ax2_all.set_xlabel('Time [ms]', fontsize=fs) - ax2_all.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax2_all.set_title('Mean pulse-EOD', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax2_all, offset=10) - else: - ax3 = fig_all.add_subplot(2, 6, (3, 4)) - ax3.plot(plot_time, mean_pulse_data, lw=2, color='firebrick', alpha=0.7, label='mean EOD') - ax3.fill_between(plot_time, up_std, bottom_std, color='grey', alpha=0.3) - # ax3.plot(plot_time, up_std, lw=1, color='red', alpha=0.7, label='std EOD') - # ax3.plot(plot_time, bottom_std, lw=1, color='red', alpha=0.7) - ax3.tick_params(axis='both', which='major', labelsize=fs - 2) - # plt.legend(loc='upper right') - ax3.set_xlabel('Time [ms]', fontsize=fs) - ax3.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax3.set_title('EOD-Waveform pulse', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax3, offset=10) - # fig3.tight_layout() - # fig3.savefig(plot_path + 'pulse-EOD%.0f.pdf' % (len(glob.glob(plot_path + 'pulse-EOD*.pdf')) + 1)) - # plt.close(fig3) - ax5 = fig_all.add_subplot(2, 6, (5, 6)) - eod_wtimes = (np.arange( - len(data[ - (bwin * self.rate):(bwin * self.rate + round(self.rate * 1.0 / fishlist[ind[-1]][0][0] * 4))]) - ) * 1.0 / self.rate) * 1000.0 - eod_wampls = data[ - (bwin * self.rate):( - bwin * self.rate + round(self.rate * 1.0 / fishlist[ind[-1]][0][0] * 4))] - - # WHEN SINGLEWAVEFISH (len(fishlist) =1) gemittelter plot; anderenfall continue - ax5.set_xlim([eod_wtimes[0], eod_wtimes[-1]]) - ax5.plot(eod_wtimes, eod_wampls, lw=2, color='firebrick', alpha=0.7) - ax5.tick_params(axis='both', which='major', labelsize=fs - 2) - ax5.set_xlabel('Time [ms]', fontsize=fs) - ax5.set_ylabel('Amplitude [a.u.]', fontsize=fs) - ax5.set_title('EOD-Waveform wave', fontsize=fs + 2) - sns.despine(fig=fig_all, ax=ax5, offset=10) - - if psd_type is fish_type: - text_ax = fig_all.add_subplot(2, 6, (1, 3)) - else: - text_ax = fig_all.add_subplot(2, 6, (1, 2)) - text_ax.text(-0.1, 0.9, 'Filename:', fontsize=fs) - text_ax.text(0.5, 0.9, '%s' % self.filename, fontsize=fs) - - text_ax.text(-0.1, 0.8, 'File duration:', fontsize=fs) - text_ax.text(0.5, 0.8, '%.2f s' % (len(data) / self.rate), fontsize=fs) - - text_ax.text(-0.1, 0.7, 'Best window:', fontsize=fs) - text_ax.text(0.5, 0.7, '%.2f s - %.2f s' % (bwin, bwin + win_width), fontsize=fs) - - text_ax.text(-0.1, 0.6, 'Shown EODf:', fontsize=fs) - text_ax.text(-0.1, 0.5, 'Fish-Type:', fontsize=fs) - - if fish_type is not 'pulse' or psd_type is not 'pulse': - text_ax.text(0.5, 0.6, '%.2f Hz' % fishlist[ind[-1]][0][0], fontsize=fs) - else: - text_ax.text(0.5, 0.6, '%.2f Hz' % pulse_freq, fontsize=fs) - text_ax.text(0.5, 0.5, '%s' % fish_type + ' (%.2f ; th = 0.1)' % r_value, fontsize=fs) - - text_ax.text(-0.1, 0.4, 'PSD-Type:', fontsize=fs) - text_ax.text(0.5, 0.4, '%s' % psd_type + ' (%.2f ; th = 0.27)' % mean_proportions, fontsize=fs) - - # text_ax.text(-0.1, 0.3, 'No. of wave-fishes:', fontsize=fs) - # text_ax.text(0.5, 0.3, '%.0f' % len(fishlist), fontsize=fs) - - sns.despine(fig=fig_all, ax=text_ax, offset=10) - plt.axis('off') - - fig_all.tight_layout() - fig_all.savefig(plot_path + '%s.pdf' % self.filename[:-4]) - plt.close(fig_all) - - def pulse_sorting(self, bwin, win_width, data, fish_type, psd_type): - # load data and time (0-8s) of bestwindow - bw_data = data[(bwin * self.rate):(bwin * self.rate + win_width * self.rate)] - time = np.arange(len(bw_data)) * 1.0 / self.rate - - # get time of data exceeding the threshold - - if fish_type is 'wave' and psd_type is 'pulse': - threshold = np.percentile(bw_data, 90) - else: - threshold = max(bw_data) - ((max(bw_data) - np.mean(bw_data)) / 2) - - th_time = [] - for i in np.arange(len(bw_data) - 1): - if bw_data[i + 1] > threshold and bw_data[i] <= threshold: - th_time.append(time[i + 1]) - - # pulse frequency PROBLEMATISCH BEI MEHREREN PULSEFISHEN !!! - pulse_freq = len(th_time) / win_width - print('') - print('Pulse-frequency:', pulse_freq) - print('') - - pulse_frequencies = 'fish_pulse.npy' - if not os.path.exists(pulse_frequencies): - np.save(pulse_frequencies, np.array([])) - pulse_ls = np.load(pulse_frequencies) - pulse_ls = pulse_ls.tolist() - - pulse_ls.append(pulse_freq) - - pulse_ls = np.asarray(pulse_ls) - np.save(pulse_frequencies, pulse_ls) - - # for each detected pulse/exceeding-time (count in pulse_data.keys()) - # save a window of the data arround this time - pulse_data = {} - eod_plot_tw = 0.006 # seconds shown in plot - for i in np.arange(len(th_time)): - plot_data = bw_data[(th_time[i] - eod_plot_tw / 2) * self.rate: (th_time[ - i] - eod_plot_tw / 2) * self.rate + eod_plot_tw * self.rate] - temp_dict = {len(pulse_data) + 1: plot_data} - pulse_data.update(temp_dict) - - return pulse_data, pulse_freq - - def wave_sorting(self, bwin, win_width, data): - bw_data = data[(bwin * self.rate):(bwin * self.rate + win_width * self.rate)] - time = np.arange(len(bw_data)) * 1.0 / self.rate - - # threshold = max(bw_data) - ((max(bw_data) - np.mean(bw_data)) / 2) - threshold = np.percentile(bw_data, 75) - th_time = [] - for i in np.arange(len(bw_data) - 1): - if bw_data[i + 1] > threshold and bw_data[i] <= threshold: - th_time.append(time[i + 1]) - - wave_data = {} - # eod_plot_tw = 0.006 # seconds shown in plot - eod_plot_tw = 0.030 - for i in np.arange(len(th_time)): - plot_data = bw_data[(th_time[i] - eod_plot_tw / 2) * self.rate: (th_time[ - i] - eod_plot_tw / 2) * self.rate + eod_plot_tw * self.rate] - temp_dict = {len(wave_data) + 1: plot_data} - wave_data.update(temp_dict) - return wave_data From f4800165d4900bd2ec63303b4e0ffd3b54607e89 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:33:39 +0200 Subject: [PATCH 10/13] FishRecording.py removed. May Fabian's masterpiece of code rest in Gits' hands. --- thunderfish/FishRecording.py | 402 ----------------------------------- 1 file changed, 402 deletions(-) delete mode 100644 thunderfish/FishRecording.py diff --git a/thunderfish/FishRecording.py b/thunderfish/FishRecording.py deleted file mode 100644 index c3f52122..00000000 --- a/thunderfish/FishRecording.py +++ /dev/null @@ -1,402 +0,0 @@ -""" -Created on Fri March 21 14:40:04 2014 - -@author: Juan Felipe Sehuanes -""" -import os - -import Auxiliary as aux -import numpy as np -# import seaborn as sns -from scipy import stats -import matplotlib.pyplot as plt -from matplotlib import mlab -import peakdetection as pd - - -def needs_roi(func): - def new_func(*args, **kwargs): - if args[0].roi is None: - args[0].detect_best_window() - return func(*args, **kwargs) - - return new_func - - -class FishRecording: - def __init__(self, wavfile): - self._wavfile = wavfile - self._time, self._eod, self._sample_rate = aux.load_trace(wavfile) - self._eod_peak_idx = None - self._eod_trough_idx = None - self.roi = None - - def filtr_data(self, filter_fac=5.5): - fund_freq = self.fund_freq() - filtered_data = aux.butter_lowpass_filter(self._eod, fund_freq * filter_fac, self._sample_rate) - return filtered_data - - def detect_peak_and_trough_indices(self, peak_threshold=None, norm_window=.1): - """This function finds the indices of peaks and troughs of each EOD-cycle in the recording - - :param peak_threshold: This is the threshold to be used for the peak detection - :param norm_window: - :return: two arrays. The first contains the peak indices and the second contains the trough indices. - """ - if self._eod_peak_idx is None or self._eod_trough_idx is None: - - w = np.ones(self._sample_rate * norm_window) - w[:] /= len(w) - eod2 = np.sqrt( - np.correlate(self._eod ** 2., w, mode='same') - np.correlate(self._eod, w, mode='same') ** 2.) - eod2 = self._eod / eod2 - if peak_threshold is None: - peak_threshold = np.percentile(np.abs(eod2), 99.9) - np.percentile(np.abs(eod2), 70) - # The Threshold is 1.5 times the standard deviation of the eod - - # _, self._eod_peak_idx, _, self._eod_trough_idx = aux.peakdet(eod2, peak_threshold) - self._eod_peak_idx, self._eod_trough_idx = pd.detect_peaks(eod2, peak_threshold) - # refine by matching troughs and peaks - everything = list(self.peak_trough_iterator()) - _, _, self._eod_peak_idx, _, _, self._eod_trough_idx = map(lambda x: np.asarray(x), zip(*everything)) - - return self._eod_peak_idx, self._eod_trough_idx - - def forget_peaks(self): - self._eod_peak_idx = None - self._eod_trough_idx = None - pass - - def peak_trough_iterator(self): - pidx, tidx = self.detect_peak_and_trough_indices() - - t = np.hstack((self._time[pidx], self._time[tidx])) - y = np.hstack((self._time[pidx] * 0 + 1, self._time[tidx] * 0 - 1)) - all_idx = np.hstack((pidx, tidx)) - a = np.hstack((self._eod[pidx], self._eod[tidx])) - idx = np.argsort(t) - pt = y[idx] - t = t[idx] - a = a[idx] - all_idx = all_idx[idx] - - total_length = len(t) - hit_upper_limit = False - hit_lower_limit = False - for i in np.where(pt == 1)[0]: - t_peak = t[i] - a_peak = a[i] - - k_next = i - k_prev = i - while pt[k_next] > 0: - k_next += 1 - if k_next == total_length: - hit_upper_limit = True - break - if hit_upper_limit: - break - - t_next_trough = t[k_next] - - while pt[k_prev] > 0: - k_prev -= 1 - if k_prev < 0: - hit_lower_limit = True - break - if hit_lower_limit: - hit_lower_limit = False - continue - - t_prev_trough = t[k_prev] - trough_idx = None - if np.abs(t_next_trough - t_peak) < np.abs(t_prev_trough - t_peak): - t_trough = t_next_trough - a_trough = a[k_next] - trough_idx = k_next - else: - t_trough = t_prev_trough - a_trough = a[k_prev] - trough_idx = k_prev - - yield t_peak, a_peak, all_idx[i], t_trough, a_trough, all_idx[trough_idx] - - def detect_best_window(self, win_size=8., win_shift=0.2, plot_debug=False, ax=False, savefig=False): - """ This function detects the best window of the file to be analyzed. The core mechanism is in the - best_window_algorithm function. For plot debug, call this function in "main" with argument plot_debug=True - - :param win_size: float. Size in seconds of the best window in seconds. - :param win_shift: float. Size in seconds between windows. Default is 0.2 seconds. - :param plot_debug: boolean. use True to plot filter parameters (and thresholds) for detecting best window - :param ax: axes of the debugging plots. - :return: two floats. The first float marks the start of the best window and the second the defined window-size. - """ - filename = self._wavfile - p_idx, t_idx = self.detect_peak_and_trough_indices() - peak_time, peak_ampl, trough_time, trough_ampl = self._time[p_idx], self._eod[p_idx], \ - self._time[t_idx], self._eod[t_idx] - # peaks and troughs here refer to those found in each eod-cycle. For each cycle there should be one peak and - # one trough if the detect_peak_indices function worked fine. - my_times = peak_time[peak_time <= peak_time[-1] - win_size] # Upper window-boundaries solution - my_times = np.arange(0.0, peak_time[-1] - win_size, win_shift) - cvs = np.empty(len(my_times)) - no_of_peaks = np.empty(len(my_times)) - mean_ampl = np.empty(len(my_times)) - - for i, curr_t in enumerate(my_times): - # This for-loop goes through each eod-cycle. Isn't this too much? It considerably makes the code slow. - window_idx = (peak_time >= curr_t) & (peak_time <= curr_t + win_size) - # the last line makes a window from curr_t and adds 8. seconds to it. Lower window-boundaries solution. - p2t_ampl = peak_ampl[window_idx] - trough_ampl[window_idx] - cvs[i] = np.std(p2t_ampl, ddof=1) / np.mean(p2t_ampl) - mean_ampl[i] = np.mean(p2t_ampl) - no_of_peaks[i] = len(p2t_ampl) - - if plot_debug: - ax = aux.draw_bwin_analysis_plot(filename, self._time, self._eod, no_of_peaks, cvs, mean_ampl) - - # ToDo: Need to find a way to plot ax5!! It's not as easy as it seems... - # ToDo: WOULD BE GREAT TO DO ALL THE PLOTTING IN EXTRA FUNCTION IN AUXILIARY!!! - - bwin_bool_array = self.best_window_algorithm(no_of_peaks, mean_ampl, cvs, plot_debug=plot_debug, axs=ax, - win_shift=win_shift) - bwin_bool_inx = np.where(bwin_bool_array)[0][0] # Gets the index of the best window out of all windows. - entire_time_idx = self._eod_peak_idx[bwin_bool_inx] - bwin = my_times[bwin_bool_inx] - - self.roi = (entire_time_idx, entire_time_idx + int(self._sample_rate / 2.)) - - # plotting the best window in ax[5] - if plot_debug and len(ax) > 0: - aux.draw_bwin_in_plot(ax, filename, self._time, self._eod, bwin, win_size, p_idx, t_idx, - savefig=savefig) - - return bwin, win_size - - def best_window_algorithm(self, peak_no, mean_amplitudes, cvs, pks_th=0.15, ampls_percentile_th=85., - cvs_percentile_th=15., plot_debug=False, axs=None, win_shift=0.2): - - """This is the algorithm that chooses the best window. It first filters out the windows that have a siginificant - different amount of peaks compared to the stats.mode peak number of all windows. Secondly, it filters out - windows that have a higher coefficient of variation than a certain percentile of the distribution of cvs. - From those windows that get through both filters, the one with the highest peak-to-trough-amplitude - (that is not clipped!) is chosen as the best window. We assume clipping as amplitudes above 85% percentile of - the distribution of peak to trough amplitude. - - :param cvs_percentile_th: threshold of how much amplitude-variance (covariance coefficient) of the signal - is allowed. Default is 10%. - :param peak_no: array with number of peaks - :param mean_amplitudes: array with mean peak-to-trough-amplitudes - :param cvs: array with covariance coefficients - :param pks_th: threshold for number-of-peaks-filter - :param ampls_percentile_th: choose a percentile threshold to avoid clipping. Default is 85 - :param plot_debug: boolean for showing plot-debugging. - :param axs: axis of plot debugging. - :param win_shift: float. Size in seconds between windows. Default is 0.2 seconds. - :return: boolean array with a single True element. This is the Index of the best window out of all windows. - """ - # First filter: Stable # of detected peaks - pk_mode = stats.mode(peak_no) - tot_pks = max(peak_no) - min(peak_no) - lower = peak_no >= pk_mode[0][0] - tot_pks * pks_th - upper = peak_no <= pk_mode[0][0] + tot_pks * pks_th - valid_pks = lower * upper - - # Second filter: Low variance in the amplitude - cvs[np.isnan(cvs)] = np.median(cvs) # Set a huge number where NaN to avoid NaN!! - cov_th = np.percentile(cvs, cvs_percentile_th) - valid_cv = cvs < cov_th - - # Third filter: From the remaining windows, choose the one with the highest p2t_amplitude that's not clipped. - - # replace the median ampl where 0's - ampl_means = np.where(mean_amplitudes == 0., np.median(mean_amplitudes), mean_amplitudes) - tot_ampls = max(ampl_means) - min(ampl_means) - ampls_th = np.percentile(ampl_means, ampls_percentile_th) - valid_ampls = ampl_means <= ampls_th - - valid_windows = valid_pks * valid_cv * valid_ampls - - # If there is no best window, run the algorithm again with more flexible threshodlds. - if not True in valid_windows and cvs_percentile_th == 100. and ampls_percentile_th == 0.: - print('\nWARNING. The recording %s seems to be of very bad quality for analysis. ' - 'Skipping recording...\n' % self._wavfile) - os.remove(self._wavfile) - quit() - - elif not True in valid_windows: - print('\nNo best window found. Rerunning best_window_algorithm with more flexible arguments.\n') - if cvs_percentile_th <= 95.: - cvs_percentile_th += 5. - if ampls_percentile_th >= 5.: - ampls_percentile_th -= 5. - return self.best_window_algorithm(peak_no, mean_amplitudes, cvs, - ampls_percentile_th=ampls_percentile_th, - cvs_percentile_th=cvs_percentile_th, plot_debug=plot_debug, - win_shift=win_shift) - # This return is a Recursion! Need to return the value in the embeded function, otherwise the root_function - # will not return anything! - - else: - max_ampl_window = ampl_means == np.max( - ampl_means[valid_windows]) # Boolean array with a single True element - - best_window = valid_windows * max_ampl_window - - if plot_debug: - windows = np.arange(len(peak_no)) * win_shift - up_th = np.ones(len(windows)) * pk_mode[0][0] + tot_pks * pks_th - down_th = np.ones(len(windows)) * pk_mode[0][0] - tot_pks * pks_th - axs[1].fill_between(windows, y1=down_th, y2=up_th, color='forestgreen', alpha=0.4, edgecolor='k', lw=1) - - cvs_th_array = np.ones(len(windows)) * cov_th - axs[2].fill_between(windows, y1=np.zeros(len(windows)), y2=cvs_th_array, color='forestgreen', - alpha=0.4, edgecolor='k', lw=1) - - clipping_lim = np.ones(len(windows)) * axs[3].get_ylim()[-1] - clipping_th = np.ones(len(windows)) * ampls_th - axs[3].fill_between(windows, y1=clipping_th, y2=clipping_lim, - color='tomato', alpha=0.6, edgecolor='k', lw=1) - axs[3].plot(windows[best_window], ampl_means[best_window], 'o', ms=25, mec='black', mew=3, - color='purple', alpha=0.8) - - return best_window - - @property - @needs_roi - def w_time(self): - assert self.roi is not None, "Must detect window first" - return self._time[self.roi[0]:self.roi[1]] - - @property - @needs_roi - def w_eod(self): - assert self.roi is not None, "Must detect window first" - return self._eod[self.roi[0]:self.roi[1]] - - @property - @needs_roi - def w_pt(self): - assert self.roi is not None, "Must detect window first" - trough_t = self._time[self._eod_trough_idx] - trough_eod = self._eod[self._eod_trough_idx] - peak_t = self._time[self._eod_peak_idx] - peak_eod = self._eod[self._eod_peak_idx] - - start, end = self._time[self.roi[0]], self._time[self.roi[1]] - - idx_t = (trough_t >= start) & (trough_t <= end) - idx_p = (peak_t >= start) & (peak_t <= end) - idx = idx_t & idx_p - return peak_t[idx], trough_t[idx], peak_eod[idx], trough_eod[idx] - - @property - @needs_roi - def fund_freq(self): - - wpeak_t, _, wpeak_eod, _ = self.w_pt - - inter_peak_interv = np.diff(wpeak_t) - - hp = np.histogram(inter_peak_interv, bins=max(int(len(inter_peak_interv) / 10), 10)) - # bins=np.arange(min(inter_peak_interv), max(inter_peak_interv), 0.00001)) - - fund_freq = 1. / hp[1][np.argmax(hp[0])] - return fund_freq - - def plot_peaks_troughs(self, mod_filename): - plot_t = self.w_time - plot_eod = self.w_eod - wpeak_t, wtrough_t, wpeak_eod, wtrough_eod = self.w_pt - - # filtr_eod = self.filtr_data - fig = plt.figure(figsize=(7, 8), num='Fish No. ' + mod_filename[-10:-8]) - ax = fig.add_subplot(1, 1, 1) - - # ax.plot(self._time, filtr_eod, color='blue', alpha=0.8, lw=2.) - ax.plot(plot_t, plot_eod, color='blue', alpha=0.8, lw=2.) - ax.plot(wpeak_t, wpeak_eod, 'ok', mfc='crimson') - ax.plot(wtrough_t, wtrough_eod, 'ok', mfc='lime') - - pass - - def type_detector(self, thres=0.1): - - pk_t, tr_t, _, _ = self.w_pt - pk_2_pk = np.diff(pk_t) - pk_2_tr = np.abs(pk_t - tr_t) - - min_n = np.min([len(pk_2_pk), len(pk_2_tr)]) - - r_tr = pk_2_tr[:min_n] / pk_2_pk[:min_n] - print np.min(r_tr), np.max(r_tr) - r_tr[r_tr > 0.5] = 1 - r_tr[r_tr > 0.5] - r_value = np.median(r_tr) - # r_value = np.median(pk_2_tr[:min_n] / pk_2_pk[:min_n]) - - return 'pulse' if r_value < thres else 'wave', r_value - - def plot_spectogram(self, ax): - - fu_freq = self.fund_freq - nwindowpoints = 4096 - noverlap = nwindowpoints / 2 - - Pxx, freqs, t = mlab.specgram(self.w_eod, NFFT=nwindowpoints, Fs=self._sample_rate, - window=np.hamming(nwindowpoints), - noverlap=noverlap) - ax.plot(freqs, np.mean(Pxx, axis=1), label='Fundamental Frequency: %.1f Hz' % fu_freq) - # ax.set_xlim(0, fund_freq*filtr) # Power Spec Xlim is 4 times the fundamental freq of the fish - ax.set_xlim(0, 4000) - ax.set_ylabel('Power Spectrum [dB]') - ax.set_xlabel('Frequency [Hz]') - ax.set_yscale('log') - ax.legend(frameon=False, loc='best', prop={'size': 11}) - - pass - - def plot_eodform(self, ax, filtr): - - fu_freq = self.fund_freq - low_pass = fu_freq * filtr - if self.type_detector() == 'pulse': - low_pass = 3000. - - f_eod = aux.butter_lowpass_filter(self.w_eod, low_pass, self._sample_rate) - pt, tt, pe, te = self.w_pt - plot_inxs = (self.w_time >= tt[1]) & (self.w_time <= tt[3]) - - ax.plot(1000. * (self.w_time[plot_inxs] - self.w_time[0]), f_eod[plot_inxs], color='green', lw=2) - ax.set_ylabel('Amplitude [au]') # "au" stands for arbitrary unit - ax.set_xlabel('Time [ms]') - - pass - - def plot_wavenvelope(self, ax, w_start, w_end): - - """ This function plots the envelope of the recording. - - :param ax: The axis in which you wish to plot. - :param w_start: Start of the best window. - :param w_end: End of the best window. - """ - window_size = int(0.05 * self._sample_rate) # 0.050 are 50 milliseconds for the envelope window! - w = 1.0 * np.ones(window_size) / window_size - envelope = (np.sqrt((np.correlate(self._eod ** 2, w, mode='same') - - np.correlate(self._eod, w, mode='same') ** 2)).ravel()) * np.sqrt(2.) - upper_bound = np.max(envelope) + np.percentile(envelope, 1) - ax.fill_between(self._time[::500], y1=-envelope[::500], y2=envelope[::500], color='purple', alpha=0.5) - ax.plot((w_start, w_start), (-upper_bound, upper_bound), 'k--', linewidth=2) - ax.plot((w_end, w_end), (-upper_bound, upper_bound), 'k--', linewidth=2) - ax.text((w_start + w_end) / 2., upper_bound - np.percentile(envelope, 10), 'Analysis Window', - rotation='horizontal', horizontalalignment='center', verticalalignment='center', fontsize=14) - - ax.set_ylim(-upper_bound, upper_bound) - ax.set_xlabel('Time [s]', fontsize=16) - ax.set_ylabel('Signal Amplitude [au]', fontsize=16) - ax.tick_params(axis='both', which='major', labelsize=14) - - pass - - def __str__(self): - return """Fish trace from %s with %i sample points""" % (self._wavfile, self._eod.size) From ccb3a24b87a91db37b2ab38868dbe1d669c038dd Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:34:18 +0200 Subject: [PATCH 11/13] df_and_envelope_tools_till.py removed. --- thunderfish/df_and_envelope_tools_till.py | 179 ---------------------- 1 file changed, 179 deletions(-) delete mode 100644 thunderfish/df_and_envelope_tools_till.py diff --git a/thunderfish/df_and_envelope_tools_till.py b/thunderfish/df_and_envelope_tools_till.py deleted file mode 100644 index 2d359a5e..00000000 --- a/thunderfish/df_and_envelope_tools_till.py +++ /dev/null @@ -1,179 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns -import sys - - -def get_diff_freqs(freqs_array): - """ This Function takes an array of fundamental frequencies and calculates all possible - difference-frequencies between all combinations. - :rtype : array with all possible dfs - :param freqs_array: array of fish fundamental frequencies. - """ - all_diffs = np.hstack([freqs_array - e for e in freqs_array]) - ret = all_diffs[all_diffs != 0.] - return ret - - -def plot_freq_histogram(m1, m2, m3, m4, s1, s2, s3, s4, x_label, x_limits, binwidth, savefig=False, - save_path_and_name='./histo_fig.pdf'): - """ Plots a histogram of frequencies. - :param freqs_array: array-like. list of frequencies. - :param x_label: string. Sets the label of x-axis - :param bins: float. select the number of bins. Exception: string 'FD' for Freedman-Diaconis rule (default) - :param savefig: boolean. True if you want to save the figure. - :param save_path_and_name: string. path + fig-name where you want to save the file. Default is './histo_fig.pdf'. - Don't forget the '.pdf' at the end of the save_path_and_name string!! - """ - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig = plt.figure(figsize=(40. / inch_factor, 40. / inch_factor)) - - ax1 = fig.add_subplot(4, 2, 1) - ax1.hist(m1, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax1.set_xlim(x_limits) - ax1.set_ylabel('n', fontsize=18) - ax1.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax1.tick_params(axis='both', which='major', labelsize=16) - ax1.set_title('\nDay 1: large Group', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax2 = fig.add_subplot(4, 2, 3) - ax2.hist(m2, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax2.set_xlim(x_limits) - ax2.set_ylabel('n', fontsize=18) - ax2.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax2.tick_params(axis='both', which='major', labelsize=16) - ax2.set_title('\nDay 2: large Group', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax3 = fig.add_subplot(4, 2, 5) - ax3.hist(m3, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax3.set_xlim(x_limits) - ax3.set_ylabel('n', fontsize=18) - ax3.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax3.tick_params(axis='both', which='major', labelsize=16) - ax3.set_title('\nDay 3: large Group', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax4 = fig.add_subplot(4, 2, 7) - ax4.hist(m4, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax4.set_xlim(x_limits) - ax4.set_ylabel('n', fontsize=18) - ax4.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax4.tick_params(axis='both', which='major', labelsize=16) - ax4.set_title('\nDay 4: large Group', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax5 = fig.add_subplot(4, 2, 2) - ax5.hist(s1, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax5.set_xlim(x_limits) - ax5.set_ylabel('n', fontsize=18) - ax5.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax5.tick_params(axis='both', which='major', labelsize=16) - ax5.set_title('\nDay1: single', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax6 = fig.add_subplot(4, 2, 4) - ax6.hist(s2, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax6.set_xlim(x_limits) - ax6.set_ylabel('n', fontsize=18) - ax6.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax6.tick_params(axis='both', which='major', labelsize=16) - ax6.set_title('\nDay 2: single', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax7 = fig.add_subplot(4, 2, 6) - ax7.hist(s3, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax7.set_xlim(x_limits) - ax7.set_ylabel('n', fontsize=18) - ax7.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax7.tick_params(axis='both', which='major', labelsize=16) - ax7.set_title('\nDay 3: single', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - ax8 = fig.add_subplot(4, 2, 8) - ax8.hist(s4, bins=range(x_limits[0], x_limits[1] + 100, binwidth), color='firebrick', alpha=0.8) - ax8.set_xlim(x_limits) - ax8.set_ylabel('n', fontsize=18) - ax8.set_xlabel(x_label + ' [Hz]', fontsize=18) - ax8.tick_params(axis='both', which='major', labelsize=16) - ax8.set_title('\nDay 4: single', fontsize=20) - # sns.despine(fig=fig, ax=ax1, offset=10) - fig.tight_layout() - - sns.despine(fig=fig, ax=[ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8]) - ## DESPINE !!! - if savefig: - fig.savefig(save_path_and_name) - plt.close() - - -if __name__ == '__main__': - # Test the plot_freq_histogram() function - # file_path = sys.argv[1] - # freqs = np.load(file_path) - # freqs = np.unique(freqs) - - m1 = np.load('20140521_RioCanita/multi/fish_wave.npy') - m1 = np.unique(m1) - m2 = np.load('20140522_RioCanita/multi/fish_wave.npy') - m1 = np.unique(m2) - m3 = np.load('20140523_RioCanita/multi/fish_wave.npy') - m1 = np.unique(m3) - m4 = np.load('20140524_RioCanita/multi/fish_wave.npy') - m1 = np.unique(m4) - s1 = np.load('20140521_RioCanita/single/fish_wave.npy') - m1 = np.unique(s1) - s2 = np.load('20140522_RioCanita/single/fish_wave.npy') - m1 = np.unique(s2) - s3 = np.load('20140523_RioCanita/single/fish_wave.npy') - m1 = np.unique(s3) - s4 = np.load('20140524_RioCanita/single/fish_wave.npy') - m1 = np.unique(s4) - - df_m1 = get_diff_freqs(m1) - df_m1 = np.unique(df_m1) - - df_m2 = get_diff_freqs(m2) - df_m2 = np.unique(df_m2) - - df_m3 = get_diff_freqs(m3) - df_m3 = np.unique(df_m3) - - df_m4 = get_diff_freqs(m4) - df_m4 = np.unique(df_m4) - - df_s1 = get_diff_freqs(s1) - df_s1 = np.unique(df_s1) - - df_s2 = get_diff_freqs(s2) - df_s2 = np.unique(df_s2) - - df_s3 = get_diff_freqs(s3) - df_s3 = np.unique(df_s3) - - df_s4 = get_diff_freqs(s4) - df_s4 = np.unique(df_s4) - - # envelopes = get_diff_freqs(dfs) - # envelopes = np.unique(envelopes) - - # bins=range(min(data), max(data) + binwidth, binwidth) - - plot_freq_histogram(m1, m2, m3, m4, s1, s2, s3, s4, 'Fundamental EOD-Frequencies', binwidth=10, x_limits=[0, 1000]) - plot_freq_histogram(df_m1, df_m2, df_m3, df_m4, df_s1, df_s2, df_s3, df_s4, 'Beat Frequencies', binwidth=20, - x_limits=[-1000, 1000]) - # plot_freq_histogram(envelopes, 'Envelope Frequencies', binwidth= 20, x_limits=[-1000, 1000]) - - plt.show() From 1b2d50a6e480ec4082299984d4db479edb158008 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 12:34:52 +0200 Subject: [PATCH 12/13] df_and_envelope_tools.py removed. Thunderfish directory looks better now. Commiting before merging with Jan's updates on fakefish.py --- thunderfish/df_and_envelope_tools.py | 71 ---------------------------- 1 file changed, 71 deletions(-) delete mode 100644 thunderfish/df_and_envelope_tools.py diff --git a/thunderfish/df_and_envelope_tools.py b/thunderfish/df_and_envelope_tools.py deleted file mode 100644 index 9d52d022..00000000 --- a/thunderfish/df_and_envelope_tools.py +++ /dev/null @@ -1,71 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -import seaborn as sns - - -def get_diff_freqs(freqs_array): - """ This Function takes an array of fundamental frequencies and calculates all possible - difference-frequencies between all combinations. - - :rtype : array with all possible dfs - :param freqs_array: array of fish fundamental frequencies. - """ - all_diffs = np.hstack([freqs_array - e for e in freqs_array]) - ret = all_diffs[all_diffs != 0.] - return ret - - -def plot_freq_histogram(freqs_array, x_label, bins='FD', savefig=False, save_path_and_name='./histo_fig.pdf'): - """ Plots a histogram of frequencies. - - :param freqs_array: array-like. list of frequencies. - :param x_label: string. Sets the label of x-axis - :param bins: float. select the number of bins. Exception: string 'FD' for Freedman-Diaconis rule (default) - :param savefig: boolean. True if you want to save the figure. - :param save_path_and_name: string. path + fig-name where you want to save the file. Default is './histo_fig.pdf'. - Don't forget the '.pdf' at the end of the save_path_and_name string!! - """ - q75, q25 = np.percentile(abs(freqs_array), [75, 25]) - - inch_factor = 2.54 - sns.set_context("poster") - sns.axes_style('white') - sns.set_style("ticks") - fig, ax = plt.subplots(figsize=(20. / inch_factor, 15. / inch_factor)) - - if bins == 'FD': - ax.hist(freqs_array, bins=int(2 * (q75 - q25) * len(freqs_array) ** (-1. / 3.)), - facecolor='cornflowerblue', alpha=0.8, lw=1.5) # Freedman-Diaconis rule for binwidth - else: - ax.hist(freqs_array, bins=bins, color='cornflowerblue', alpha=0.8) - - # Plot Cosmetics - - ax.set_ylabel('Counts', fontsize=16) - ax.set_xlabel(x_label + ' [Hz]', fontsize=14) - ax.tick_params(axis='both', which='major', labelsize=12) - ax.set_title('Distribution of %s' % x_label, fontsize=16) - sns.despine(fig=fig, ax=ax, offset=10) - fig.tight_layout() - if savefig: - fig.savefig(save_path_and_name) - plt.close() - - -if __name__ == '__main__': - # Test the plot_freq_histogram() function - file_path = 'data/recordings_cano_rubiano_RAW/fish_wave.npy' - freqs = np.load(file_path) - freqs = np.unique(freqs) - - dfs = get_diff_freqs(freqs) - dfs = np.unique(dfs) - - envelopes = get_diff_freqs(dfs) - envelopes = np.unique(envelopes) - - plot_freq_histogram(freqs, 'Fundamental EOD-Frequencies') - plot_freq_histogram(dfs, 'Beat Frequencies', bins=2000) - plot_freq_histogram(envelopes, 'Envelope Frequencies', bins=500) - - plt.show() From 2ee61345eb679d7097c5d3c0fed62ba082fcab70 Mon Sep 17 00:00:00 2001 From: jfsehuanes Date: Fri, 1 Jul 2016 13:08:47 +0200 Subject: [PATCH 13/13] test_best_window.py removed. Travis should be happy now. Ready for pull-request! --- thunderfish/test_best_window.py | 47 --------------------------------- 1 file changed, 47 deletions(-) delete mode 100644 thunderfish/test_best_window.py diff --git a/thunderfish/test_best_window.py b/thunderfish/test_best_window.py deleted file mode 100644 index fb7400a4..00000000 --- a/thunderfish/test_best_window.py +++ /dev/null @@ -1,47 +0,0 @@ -__author__ = 'juan' - -# imports - -import sys -import os - -import matplotlib.pyplot as plt - -from FishRecording import * -from Auxiliary import * - -if __name__ == '__main__': - - if not 3 >= len(sys.argv) >= 2: # only if len(sys.argv) == 2 or 3, the codes continues running. - print("\nI need an audiofile as first argument. Please give me the path + filename as argument! " - "Then you can tell me the folder name you wish me to save the figures in, as a second argument " - "(don't use any '/'). Only of you like though.\n") - sys.exit(2) - # Load, convert and process the audiofile - audio_file = sys.argv[1] - modfile = conv_to_single_ch_audio(audio_file) - fish = FishRecording(modfile) - - # detect best window - w_start, w_width = fish.detect_best_window() - - # define save path and create the plots with the best window - if len(sys.argv) == 3: - folder_name = sys.argv[2] - savepath = './control_figures/best_window_detection/' + folder_name + '/' - else: - savepath = './control_figures/best_window_detection/' - - if not os.path.exists(savepath): - os.mkdir(savepath) - - recording_name = '_'.join(modfile.split('.')[0].split('_')[:2]) + '.pdf' - - fig, ax = plt.subplots(figsize=(12, 8)) - fish.plot_wavenvelope(ax, w_start, w_start + w_width) - ax.set_title(recording_name, fontsize=18) - fig.tight_layout() - fig.savefig(savepath + recording_name) - plt.close() - os.remove(modfile) - sys.exit(2)