Skip to content

Commit caa6164

Browse files
committed
fix classify and detect for EPG audio read plugin
1 parent a6803e4 commit caa6164

6 files changed

Lines changed: 42 additions & 53 deletions

File tree

src/classify

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import importlib
6060
from lib import log_nvidia_smi_output, select_GPUs
6161

6262
import tifffile
63-
from lib import compute_background
63+
from lib import compute_background, load_audio_read_plugin
6464

6565
FLAGS = None
6666

@@ -79,15 +79,8 @@ def main():
7979
sys.path.append(os.path.dirname(FLAGS.video_findfile))
8080
video_findfile = importlib.import_module(os.path.basename(FLAGS.video_findfile)).video_findfile
8181

82-
sys.path.append(os.path.dirname(FLAGS.audio_read_plugin))
83-
audio_read_module = importlib.import_module(os.path.basename(FLAGS.audio_read_plugin))
84-
def audio_read(wav_path, start_tic=None, stop_tic=None):
85-
return audio_read_module.audio_read(wav_path, start_tic, stop_tic,
86-
**FLAGS.audio_read_plugin_kwargs)
87-
def audio_read_rec2ch(wavfile):
88-
return audio_read_module.audio_read_rec2ch(wavfile, **FLAGS.audio_read_plugin_kwargs)
89-
def audio_read_strip_rec(wavfile):
90-
return audio_read_module.audio_read_strip_rec(wavfile, **FLAGS.audio_read_plugin_kwargs)
82+
load_audio_read_plugin(FLAGS.audio_read_plugin, FLAGS.audio_read_plugin_kwargs)
83+
from lib import audio_read, trim_ext
9184

9285
sys.path.append(os.path.dirname(FLAGS.video_read_plugin))
9386
video_read_module = importlib.import_module(os.path.basename(FLAGS.video_read_plugin))
@@ -249,12 +242,7 @@ def main():
249242
else:
250243
adjusted_probability = probability_matrix[:,ch]
251244
waveform = adjusted_probability*np.iinfo(np.int16).max
252-
if len(audio_read_rec2ch(audio_read_strip_rec(FLAGS.wav))) > 1:
253-
tmp = FLAGS.wav.split('-')
254-
withext, rec = '-'.join(tmp[:-1]), tmp[-1]
255-
withoutext = os.path.splitext(withext)[0]+'-'+rec
256-
else:
257-
withoutext = os.path.splitext(FLAGS.wav)[0]
245+
withoutext = trim_ext(FLAGS.wav)
258246
filename = withoutext+'-'+labels[ch]+'.wav'
259247
wavfile.write(filename, int(sample_rate), waveform.astype('int16'))
260248

src/detect-plugin.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import argparse
1212
import os
13-
import importlib
1413
import sys
1514
import csv
1615
import socket
@@ -64,11 +63,8 @@ def main():
6463
for key in sorted(flags.keys()):
6564
print('%s = %s' % (key, flags[key]))
6665

67-
sys.path.append(os.path.dirname(FLAGS.audio_read_plugin))
68-
audio_read_module = importlib.import_module(os.path.basename(FLAGS.audio_read_plugin))
69-
def audio_read(wav_path, start_tic=None, stop_tic=None):
70-
return audio_read_module.audio_read(wav_path, start_tic, stop_tic,
71-
**FLAGS.audio_read_plugin_kwargs)
66+
load_audio_read_plugin(FLAGS.audio_read_plugin, FLAGS.audio_read_plugin_kwargs)
67+
from lib import audio_read, trim_ext
7268

7369
hyperparameter1 = int(FLAGS.parameters["my-simple-textbox"])
7470

@@ -79,7 +75,7 @@ def audio_read(wav_path, start_tic=None, stop_tic=None):
7975
amplitude = scipy.signal.medfilt(song)
8076

8177
basename = os.path.basename(FLAGS.filename)
82-
with open(os.path.splitext(FLAGS.filename)[0]+'-detected.csv', 'w') as fid:
78+
with open(trim_ext(FLAGS.filename)+'-detected.csv', 'w') as fid:
8379
csvwriter = csv.writer(fid, lineterminator='\n')
8480
for i in range(1,len(amplitude)-1):
8581
if amplitude[i] > hyperparameter1:

src/gui/controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,10 @@ def pbfile_succeeded(pbfile, reftime):
937937
return recent_file_exists(pbfile, reftime, True)
938938

939939
def detect_succeeded(wavfile, reftime):
940-
logfile = wavfile[:-4]+'-detect.log'
940+
logfile = M.trim_ext(wavfile)+'-detect.log'
941941
if not logfile_succeeded(logfile, reftime):
942942
return False
943-
csvfile = wavfile[:-4]+'-detected.csv'
943+
csvfile = M.trim_ext(wavfile)+'-detected.csv'
944944
if not csvfile_succeeded(csvfile, reftime):
945945
return False
946946
return True
@@ -955,7 +955,7 @@ async def detect_actuate():
955955
async def _detect_actuate(i, wavfiles, threads, results):
956956
wavfile = wavfiles.pop(0)
957957
currtime = time.time()
958-
logfile = os.path.splitext(wavfile)[0]+'-detect.log'
958+
logfile = M.trim_ext(wavfile)+'-detect.log'
959959
jobid = generic_actuate(M.detect_plugin+".py", logfile, \
960960
M.detect_where,
961961
M.detect_ncpu_cores,

src/gui/model.py

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import importlib
1616

1717
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
18-
from lib import get_srcrepobindirs
18+
from lib import get_srcrepobindirs, load_audio_read_plugin
1919

2020
def parse_model_file(modelstr):
2121
filepath, filename = os.path.split(modelstr)
@@ -83,15 +83,6 @@ def isused(sound):
8383
return np.where([x['file']==sound['file'] and x['ticks']==sound['ticks'] \
8484
for x in used_sounds])[0]
8585

86-
def trim_ext(wavfile):
87-
if len(audio_read_rec2ch(audio_read_strip_rec(wavfile))) > 1:
88-
tmp = wavfile.split('-')
89-
withext, rec = '-'.join(tmp[:-1]), tmp[-1]
90-
withoutext = os.path.splitext(withext)[0]+'-'+rec
91-
else:
92-
withoutext = os.path.splitext(wavfile)[0]
93-
return withoutext
94-
9586
def save_annotations():
9687
global nrecent_annotations
9788
if nrecent_annotations>0:
@@ -259,7 +250,7 @@ def init(_bokeh_document, _configuration_file, _use_aitch):
259250
global context_width_sec0, context_offset_sec0
260251
global xcluster, ycluster, zcluster, ndcluster, tic2pix_max, snippet_width_pix, ilayer, ispecies, iword, inohyphen, ikind, nlayers, layers, species, words, nohyphens, kinds, used_labels, snippets_gap_sec, snippets_tic, snippets_gap_tic, snippets_decimate_by, snippets_pix, snippets_gap_pix, context_decimate_by, context_width_tic, context_offset_tic, context_sound, isnippet, xsnippet, ysnippet, file_nframes, context_midpoint_tic, ilabel, used_sounds, used_starts_sorted, used_stops, iused_stops_sorted, annotated_sounds, annotated_starts_sorted, annotated_stops, iannotated_stops_sorted, annotated_csvfiles_all, nrecent_annotations, clustered_sounds, clustered_activations, used_recording2firstsound, clustered_starts_sorted, clustered_stops, iclustered_stops_sorted, songexplorer_starttime, history_stack, history_idx, wizard, action, function, statepath, state, file_dialog_root, file_dialog_filter, nearest_sounds, status_ticker_queue, waitfor_job, dfs, remaining_isounds
261252
global user_changed_recording, user_copied_parameters
262-
global audio_read, audio_read_exts, audio_read_rec2ch, audio_read_strip_rec
253+
global audio_read, audio_read_exts, audio_read_rec2ch, audio_read_strip_rec, trim_ext
263254
global video_read, detect_labels, doubleclick_annotation, context_data, context_data_istart, model, video_findfile
264255
global detect_parameters, doubleclick_parameters, model_parameters, cluster_parameters
265256

@@ -276,14 +267,8 @@ def init(_bokeh_document, _configuration_file, _use_aitch):
276267

277268
sys.path.insert(0,srcdir)
278269

279-
sys.path.insert(0,os.path.dirname(audio_read_plugin))
280-
audio_read_module = importlib.import_module(os.path.basename(audio_read_plugin))
281-
audio_read_module.audio_read_init(**audio_read_plugin_kwargs)
282-
def audio_read(wav_path, start_tic=None, stop_tic=None):
283-
return audio_read_module.audio_read(wav_path, start_tic, stop_tic, **audio_read_plugin_kwargs)
284-
def audio_read_exts(): return audio_read_module.audio_read_exts(**audio_read_plugin_kwargs)
285-
def audio_read_rec2ch(wavfile): return audio_read_module.audio_read_rec2ch(wavfile, **audio_read_plugin_kwargs)
286-
def audio_read_strip_rec(recfile): return audio_read_module.audio_read_strip_rec(recfile, **audio_read_plugin_kwargs)
270+
load_audio_read_plugin(audio_read_plugin, audio_read_plugin_kwargs)
271+
from lib import audio_read, audio_read_exts, audio_read_rec2ch, audio_read_strip_rec, trim_ext
287272

288273
sys.path.insert(0,os.path.dirname(video_read_plugin))
289274
video_read_module = importlib.import_module(os.path.basename(video_read_plugin))

src/lib.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,37 @@
2020
from scipy.io import wavfile
2121
import csv
2222
from datetime import datetime
23+
import importlib
2324

2425
import tifffile
2526

2627
import tensorflow as tf
2728
import platform
2829
from subprocess import run, PIPE, STDOUT
2930

31+
def load_audio_read_plugin(audio_read_plugin, audio_read_plugin_kwargs):
32+
sys.path.append(os.path.dirname(audio_read_plugin))
33+
audio_read_module = importlib.import_module(os.path.basename(audio_read_plugin))
34+
global audio_read, audio_read_exts, audio_read_rec2ch, audio_read_strip_rec
35+
def audio_read(wav_path, start_tic=None, stop_tic=None):
36+
return audio_read_module.audio_read(wav_path, start_tic, stop_tic,
37+
**audio_read_plugin_kwargs)
38+
def audio_read_exts():
39+
return audio_read_module.audio_read_exts(**audio_read_plugin_kwargs)
40+
def audio_read_rec2ch(wavfile):
41+
return audio_read_module.audio_read_rec2ch(wavfile, **audio_read_plugin_kwargs)
42+
def audio_read_strip_rec(recfile):
43+
return audio_read_module.audio_read_strip_rec(recfile, **audio_read_plugin_kwargs)
44+
45+
def trim_ext(wavfile):
46+
if len(audio_read_rec2ch(audio_read_strip_rec(wavfile))) > 1:
47+
tmp = wavfile.split('-')
48+
withext, rec = '-'.join(tmp[:-1]), tmp[-1]
49+
withoutext = os.path.splitext(withext)[0]+'-'+rec
50+
else:
51+
withoutext = os.path.splitext(wavfile)[0]
52+
return withoutext
53+
3054
def check_config(configuration_file):
3155
exec(open(configuration_file).read())
3256

src/time-freq-threshold.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import argparse
3131
import os
3232
import numpy as np
33-
import importlib
3433
import skimage
3534
from skimage.morphology import closing, opening
3635
import nitime.utils as utils
@@ -43,7 +42,7 @@
4342
import socket
4443
import json
4544

46-
from lib import combine_events
45+
from lib import combine_events, load_audio_read_plugin
4746

4847
def _frequency_n_callback(M,V,C):
4948
C.time.sleep(0.5)
@@ -96,11 +95,8 @@ def main():
9695
time_scale = FLAGS.time_scale
9796
time_units = FLAGS.time_units
9897

99-
sys.path.append(os.path.dirname(FLAGS.audio_read_plugin))
100-
audio_read_module = importlib.import_module(os.path.basename(FLAGS.audio_read_plugin))
101-
def audio_read(wav_path, start_tic=None, stop_tic=None):
102-
return audio_read_module.audio_read(wav_path, start_tic, stop_tic,
103-
**FLAGS.audio_read_plugin_kwargs)
98+
load_audio_read_plugin(FLAGS.audio_read_plugin, FLAGS.audio_read_plugin_kwargs)
99+
from lib import audio_read, trim_ext
104100

105101
time_sigma_signal, time_sigma_noise = [int(x) for x in FLAGS.parameters['time_sigma'].split(',')]
106102

@@ -229,7 +225,7 @@ def bool2stamp(song_morphed, scale):
229225

230226

231227
basename = os.path.basename(FLAGS.filename)
232-
with open(os.path.splitext(FLAGS.filename)[0]+'-detected.csv', 'w') as fid:
228+
with open(trim_ext(FLAGS.filename)+'-detected.csv', 'w') as fid:
233229
csvwriter = csv.writer(fid, lineterminator='\n')
234230
for i in intervals_time_signal:
235231
csvwriter.writerow([basename,i[1],i[2],'detected','time'+i[3]])

0 commit comments

Comments
 (0)