Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed python3 import pathes #48

Merged
merged 22 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
setup(name='thunderfish',
version='0.5.0', # see http://semver.org/
packages=find_packages(exclude=['contrib', 'doc', 'tests*']),
entry_points={
'console_scripts': [
'thunderfish = thunderfish.thunderfish:main',
'fishfinder = thunderfish.fishfinder:main',
]},
description='Algorithms and scripts for analyzing recordings of e-fish electric fields.',
author='Jan Benda, Juan F. Sehuanes, Till Raab, Joerg Henninger, Jan Grewe, Fabian Sinz',
requires=['numpy', 'matplotlib', 'audioio']
Expand Down
4 changes: 2 additions & 2 deletions tests/test_peakdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_detect_peaks():
n = pt_indices[0]
data[0:n] = 0.1 + 0.9 * np.arange(0.0, n) / n
up = False
for i in xrange(0, len(pt_indices) - 1):
for i in range(len(pt_indices) - 1):
n = pt_indices[i + 1] - pt_indices[i]
if up:
data[pt_indices[i]:pt_indices[i + 1]] = np.arange(0.0, n) / n
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_detect_dynamic_peaks():
n = pt_indices[0]
data[0:n] = 0.1 + 0.9 * np.arange(0.0, n) / n
up = False
for i in xrange(0, len(pt_indices) - 1):
for i in range(len(pt_indices) - 1):
n = pt_indices[i + 1] - pt_indices[i]
if up:
data[pt_indices[i]:pt_indices[i + 1]] = np.arange(0.0, n) / n
Expand Down
1 change: 0 additions & 1 deletion thunderfish/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
__all__ = ['dataloader', 'configfile', 'peakdetection', 'bestwindow', 'powerspectrum', 'harmonicgroups', 'checkpulse', 'consistentfishes', 'eodanalysis', 'fakefish']
from thunderfish import *
14 changes: 7 additions & 7 deletions thunderfish/bestwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import warnings
import numpy as np
import peakdetection as pkd
from .peakdetection import percentile_threshold, detect_peaks, trim_to_peak


def clip_amplitudes(data, win_indices, min_fac=2.0, nbins=20,
Expand Down Expand Up @@ -225,11 +225,11 @@ def best_window_indices(data, samplerate, single=True, win_size=1., win_shift=0.
return 0, 0

# threshold for peak detection:
threshold = pkd.percentile_threshold(data, samplerate, win_shift,
th_factor=th_factor, percentile=percentile)
threshold = percentile_threshold(data, samplerate, win_shift,
th_factor=th_factor, percentile=percentile)

# detect large peaks and troughs:
peak_idx, trough_idx = pkd.detect_peaks(data, threshold)
peak_idx, trough_idx = detect_peaks(data, threshold)
if len(peak_idx) == 0 or len(trough_idx) == 0:
warnings.warn('best_window(): no peaks or troughs detected')
return 0, 0
Expand All @@ -246,7 +246,7 @@ def best_window_indices(data, samplerate, single=True, win_size=1., win_shift=0.
# indices of peaks and troughs inside analysis window:
pinx = (peak_idx >= wtinx) & (peak_idx <= wtinx + win_size_indices)
tinx = (trough_idx >= wtinx) & (trough_idx <= wtinx + win_size_indices)
p_idx, t_idx = pkd.trim_to_peak(peak_idx[pinx], trough_idx[tinx])
p_idx, t_idx = trim_to_peak(peak_idx[pinx], trough_idx[tinx])
# interval statistics:
ipis = np.diff(p_idx)
itis = np.diff(t_idx)
Expand Down Expand Up @@ -496,10 +496,10 @@ def best_window_args(cfg):
title = "test sines"
data += 0.01 * np.random.randn(len(data))
else:
import dataloader as dl
from .dataloader import load_data

print("load %s ..." % sys.argv[1])
data, rate, unit = dl.load_data(sys.argv[1], 0)
data, rate, unit = load_data(sys.argv[1], 0)
title = sys.argv[1]

# determine clipping amplitudes:
Expand Down
14 changes: 7 additions & 7 deletions thunderfish/checkpulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"""

import numpy as np
import peakdetection as pkd
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from .peakdetection import percentile_threshold, detect_peaks, trim_to_peak


def check_pulse_width(data, samplerate, win_size=0.5, th_factor=0.8, percentile=0.1,
Expand Down Expand Up @@ -49,7 +49,7 @@ def ratio(peak_idx, trough_idx):
:return: peak-ratio (float). The median of (peak-trough) / (peak-peak)
:return: r_tr (array). The distribution of (peak-trough) / (peak-peak)
"""
peaks, troughs = pkd.trim_to_peak(peak_idx, trough_idx)
peaks, troughs = trim_to_peak(peak_idx, trough_idx)

# get times of peaks and troughs, pk_times need to be floats!
pk_times = peaks / float(samplerate) # Actually there is no need to divide by samplerate.
Expand All @@ -70,11 +70,11 @@ def ratio(peak_idx, trough_idx):
print('Analyzing Fish-Type...')

# threshold for peak detection:
threshold = pkd.percentile_threshold(data, samplerate, win_size,
th_factor=th_factor, percentile=percentile)
threshold = percentile_threshold(data, samplerate, win_size,
th_factor=th_factor, percentile=percentile)

# detect large peaks and troughs:
peak_idx, trough_idx = pkd.detect_peaks(data, threshold)
peak_idx, trough_idx = detect_peaks(data, threshold)

pr_pvt, pvt_dist = ratio(peak_idx, trough_idx)
pr_tvp, tvp_dist = ratio(trough_idx, peak_idx)
Expand Down Expand Up @@ -275,11 +275,11 @@ def plot_psd_proportion(freqs, power, proportions, percentiles, pulse_fish,
elif sys.argv[1] == '-t':
data = ff.generate_triphasic_pulses(80.0, rate, 8.0)
else: # load data given by the user
import dataloader as dl
from .dataloader import load_data

file_path = sys.argv[1]
print("loading %s ...\n" % file_path)
rawdata, rate, unit = dl.load_data(sys.argv[1], 0)
rawdata, rate, unit = load_data(sys.argv[1], 0)
data, _ = bw.best_window(rawdata, rate)

# draw figure with subplots:
Expand Down
28 changes: 14 additions & 14 deletions thunderfish/chirp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import numpy as np
import matplotlib.pyplot as plt
import harmonicgroups as hg
import powerspectrum as ps
import peakdetection as pkd
from .harmonicgroups import harmonic_groups
from .powerspectrum import spectrogram
from .peakdetection import std_threshold, detect_peaks, trim_to_peak


def clean_chirps(chirp_time_idx, power, power_window=100):
Expand Down Expand Up @@ -61,7 +61,7 @@ def chirp_detection(spectrum, freqs, time, fishlist, min_power= 0.005, freq_tole
chirp_time = np.array([])

for enu, fundamental in enumerate(fundamentals):
print fundamental
print(fundamental)
# extract power of only the part of the spectrum that has to be analysied for each fundamental and get the peak
# power of every piont in time.
power = np.max(spectrum[(freqs >= fundamental - freq_tolerance) & (freqs <= fundamental + freq_tolerance)], axis=0)
Expand All @@ -71,9 +71,9 @@ def chirp_detection(spectrum, freqs, time, fishlist, min_power= 0.005, freq_tole
power_diff = np.diff(power)

# peakdetection in the power_diff to detect drops in power indicating chrips
threshold = pkd.std_threshold(power_diff)
peaks, troughs = pkd.detect_peaks(power_diff, threshold)
troughs, peaks = pkd.trim_to_peak(troughs, peaks) # reversed troughs and peaks in output and input to get trim_to_troughs
threshold = std_threshold(power_diff)
peaks, troughs = detect_peaks(power_diff, threshold)
troughs, peaks = trim_to_peak(troughs, peaks) # reversed troughs and peaks in output and input to get trim_to_troughs

# exclude peaks and troughs with to much time diff to be a chirp
peaks = peaks[(troughs - peaks) < chirp_th]
Expand Down Expand Up @@ -134,11 +134,11 @@ def chirp_analysis(data, samplerate, cfg):
:param cfg:(dict) HAS TO BE REMOVED !!!!
:param min_power: (float) minimal power of the fish fundamental to include this fish in chirp detection.
"""
spectrum, freqs, time = ps.spectrogram(data, samplerate, fresolution=2., overlap_frac=0.95)
spectrum, freqs, time = spectrogram(data, samplerate, fresolution=2., overlap_frac=0.95)

power = np.mean(spectrum, axis=1) # spectrum[:, t0:t1] to only let spectrum of certain time....

fishlist = hg.harmonic_groups(freqs, power, cfg)[0]
fishlist = harmonic_groups(freqs, power, cfg)[0]

chirp_time = chirp_detection(spectrum, freqs, time, fishlist, plot_data_func=chirp_detection_plot)
print chirp_time
Expand All @@ -153,14 +153,14 @@ def chirp_analysis(data, samplerate, cfg):
# '2016_04_27__downstream_stonewall_at_pool' made in colombia, 2016.
###
import sys
import dataloader as dl
import config_tools as ct
from dataloader import load_data
from config_tools import get_config_dict

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not "from .config_tools import get_config_dict" in this case ?
is the dot not required here ? and if so, why ?


cfg = ct.get_config_dict()
cfg = get_config_dict()

audio_file = sys.argv[1]
raw_data, samplerate, unit = dl.load_data(audio_file, channel=0)
raw_data, samplerate, unit = load_data(audio_file, channel=0)

chirp_time = chirp_analysis(raw_data, samplerate, cfg)

# power = np.mean(spectrum[:, t:t + nffts_per_psd], axis=1)
# power = np.mean(spectrum[:, t:t + nffts_per_psd], axis=1)
62 changes: 0 additions & 62 deletions thunderfish/config_tools.py

This file was deleted.

4 changes: 2 additions & 2 deletions thunderfish/consistentfishes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import numpy as np
import harmonicgroups as hg
from .harmonicgroups import extract_fundamental_freqs


def find_consistency(fundamentals, df_th=1.):
Expand Down Expand Up @@ -115,7 +115,7 @@ def consistent_fishes(fishlists, verbose=0, plot_data_func=None, **kwargs):
if verbose >= 1:
print('Finding consistent fishes out of %0.f fishlists ...' % len(fishlists))

fundamentals = hg.extract_fundamental_freqs(fishlists)
fundamentals = extract_fundamental_freqs(fishlists)

consistent_fundamentals, index = find_consistency(fundamentals)

Expand Down
6 changes: 0 additions & 6 deletions thunderfish/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ def load_data(filepath, channel=0, verbose=0):
samplerate (float): the sampling rate of the data in Hz
unit (string): the unit of the data
"""
# check types:
if not isinstance(filepath, basestring):
raise NameError('load_data(): input argument filepath must be a string!')
if not isinstance(channel, int):
raise NameError('load_data(): input argument channel must be an int!')

# check values:
data = np.array([])
samplerate = 0.0
Expand Down
20 changes: 10 additions & 10 deletions thunderfish/eodanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import numpy as np
import peakdetection as pkd
from .peakdetection import percentile_threshold, detect_peaks, snippets


def eod_waveform(data, samplerate, th_factor=0.8, percentile=0.1, start=None, stop=None):
Expand All @@ -26,10 +26,10 @@ def eod_waveform(data, samplerate, th_factor=0.8, percentile=0.1, start=None, st
:return eod_times (1-D array) Times of EOD peaks in seconds.
"""
# threshold for peak detection:
threshold = pkd.percentile_threshold(data, th_factor=th_factor, percentile=percentile)
threshold = percentile_threshold(data, th_factor=th_factor, percentile=percentile)

# detect peaks:
eod_idx, _ = pkd.detect_peaks(data, threshold)
eod_idx, _ = detect_peaks(data, threshold)

# eod times:
eod_times = eod_idx / samplerate
Expand All @@ -46,7 +46,7 @@ def eod_waveform(data, samplerate, th_factor=0.8, percentile=0.1, start=None, st
stop_inx = int(stop * samplerate)

# extract snippets:
eod_snippets = pkd.snippets(data, eod_idx, start_inx, stop_inx)
eod_snippets = snippets(data, eod_idx, start_inx, stop_inx)

# mean and std of snippets:
mean_eod = np.mean(eod_snippets, axis=0)
Expand Down Expand Up @@ -76,10 +76,10 @@ def eod_waveform_plot(time, mean_eod, std_eod, ax, unit='a.u.'):

if __name__ == '__main__':
import sys
import fakefish as ff
import dataloader as dl
import bestwindow as bw
import matplotlib.pyplot as plt
from .fakefish import generate_biphasic_pulses
from .dataloader import load_data
from .bestwindow import best_window

print('Analysis of EOD waveforms.')
print('')
Expand All @@ -90,11 +90,11 @@ def eod_waveform_plot(time, mean_eod, std_eod, ax, unit='a.u.'):
# data:
if len(sys.argv) <= 1:
samplerate = 44100.0
data = ff.generate_biphasic_pulses(80.0, samplerate, 4.0, noise_std=0.05)
data = generate_biphasic_pulses(80.0, samplerate, 4.0, noise_std=0.05)
unit = 'mV'
else:
rawdata, samplerate, unit = dl.load_data(sys.argv[1])
data, _ = bw.best_window(rawdata, samplerate)
rawdata, samplerate, unit = load_data(sys.argv[1])
data, _ = best_window(rawdata, samplerate)

# analyse EOD:
mean_eod, std_eod, time, eod_times = eod_waveform(data, samplerate,
Expand Down
Loading