Skip to content
Snippets Groups Projects
Commit 3ea89a17 authored by Loïc Lehnhoff's avatar Loïc Lehnhoff
Browse files

Fixed duplication of x-coordinates (time)

Added popup save window before closing
parent 5db537c4
No related branches found
No related tags found
No related merge requests found
audio_examples/SCW1807_20200713_064545.wav audio_examples/SCW1807_20200713_064545.wav
line_clicker/__pycache__
__pycache__
##### IMPORTATIONS ##### ##### IMPORTATIONS #####
import os import os
import sys
import argparse import argparse
from argparse import RawTextHelpFormatter from argparse import RawTextHelpFormatter
...@@ -36,8 +37,9 @@ def fetch_inputs(): ...@@ -36,8 +37,9 @@ def fetch_inputs():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
formatter_class=RawTextHelpFormatter, formatter_class=RawTextHelpFormatter,
description= description=
("This script requires LIBRARIES." ("This script requires libraries that can be installed with: \n"
"\nAnd it does things, TO BE DESCRIBED.") "'$ pip install -r requirements.txt'"
"Read README.md to get an overview of this software.")
) )
parser.add_argument( parser.add_argument(
...@@ -122,7 +124,7 @@ def fetch_inputs(): ...@@ -122,7 +124,7 @@ def fetch_inputs():
f"\nInputError: Max number of contours cannot exceed 50, got {contour}.") f"\nInputError: Max number of contours cannot exceed 50, got {contour}.")
except Exception as e: except Exception as e:
print(e) print(e)
exit() sys.exit(1)
return (explore, contour, resampl, outputs, modify_file, from_wav) return (explore, contour, resampl, outputs, modify_file, from_wav)
......
...@@ -6,9 +6,6 @@ import numpy as np ...@@ -6,9 +6,6 @@ import numpy as np
from librosa import load, amplitude_to_db, stft, pcen from librosa import load, amplitude_to_db, stft, pcen
from scipy.signal import resample from scipy.signal import resample
from line_clicker.line_clicker import to_curve
##### FUNCTIONS ##### ##### FUNCTIONS #####
def save_dict(dictionary, folder, name): def save_dict(dictionary, folder, name):
""" """
......
...@@ -82,6 +82,56 @@ class FileExplorer(object): ...@@ -82,6 +82,56 @@ class FileExplorer(object):
('All files', '*.*') ('All files', '*.*')
)) ))
class Popup(object):
"""
A Class that opens a popup asking if the user wants to save its work
before leaving.
Parameters
----------
options : list
The list of options that will show in popup.
prompt : str
The prompt that will be shown on top of the buttons.
"""
def __init__(self, options, prompt):
self.options = options
self.prompt = prompt
self.popup_window() # start function auto
def popup_window(self):
"""
Creates the windown and the layout for buttons/text.
"""
self.root = Toplevel()
Label(self.root, text=self.prompt).grid(row=0, columnspan=len(self.options))
for i, option in enumerate(self.options):
Button(
self.root,
text=option,
command=lambda x=option: self.button_pressed(event=x)
).grid(row=1, column=i)
self.root.mainloop()
def button_pressed(self, event):
"""
Saves the button that is pressed to self.pressed.
Then shuts down the window.
Parameters
----------
event : str
The text from the selected option.
"""
self.pressed = event
self.root.quit()
self.root.destroy()
class App(object): class App(object):
""" """
A Class to construct an contours annotation tool for audio data. A Class to construct an contours annotation tool for audio data.
...@@ -243,8 +293,21 @@ class App(object): ...@@ -243,8 +293,21 @@ class App(object):
self.figure.canvas.mpl_disconnect(self.klicker.key_press) self.figure.canvas.mpl_disconnect(self.klicker.key_press)
self.root.bind('<Key>', self.get_key_pressed) self.root.bind('<Key>', self.get_key_pressed)
# just to be sure
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
self.root.mainloop() self.root.mainloop()
def on_close(self):
save = Popup(prompt="Save and exit?", options=["Yes", "No", "Cancel"]).pressed
if save == "Yes":
self._quit()
if save == "No":
self.root.quit()
self.root.destroy()
else:
pass
def bspline_activation(self): def bspline_activation(self):
""" """
Activates/deactivates the visualisation of lines as curves. Activates/deactivates the visualisation of lines as curves.
...@@ -554,7 +617,7 @@ class App(object): ...@@ -554,7 +617,7 @@ class App(object):
os.path.basename(self.WAVEFILE)[:-4]+"-contours.json") os.path.basename(self.WAVEFILE)[:-4]+"-contours.json")
self.WAVEFILE = new_wavefile self.WAVEFILE = new_wavefile
# display loading scree # display loading screen
self.loading_screen.grid(row=1, column=1, rowspan=14) self.loading_screen.grid(row=1, column=1, rowspan=14)
self.canvas.get_tk_widget().destroy() self.canvas.get_tk_widget().destroy()
......
File added
File added
...@@ -364,8 +364,8 @@ class clicker(object): ...@@ -364,8 +364,8 @@ class clicker(object):
""" """
if self.legend_labels[self.current_line] in list(self.coords.keys()): if self.legend_labels[self.current_line] in list(self.coords.keys()):
# does this point already exists ? # does this point already exists ?
if [x,y] in self.coords[self.legend_labels[self.current_line]]: if x in np.array(self.coords[self.legend_labels[self.current_line]])[:,0]:
warnings.warn("This point already exists!", UserWarning, stacklevel=2) warnings.warn("Cannot place two points at the same timestamp!", UserWarning, stacklevel=2)
else: else:
# where should it be inserted ? # where should it be inserted ?
here = np.where(np.array(self.coords[self.legend_labels[self.current_line]])[:,0] > x)[0] here = np.where(np.array(self.coords[self.legend_labels[self.current_line]])[:,0] > x)[0]
...@@ -592,6 +592,11 @@ class clicker(object): ...@@ -592,6 +592,11 @@ class clicker(object):
if (self.currently_pressed and if (self.currently_pressed and
event.xdata != None and event.xdata != None and
event.ydata != None): event.ydata != None):
# does this point already exists ?
if event.xdata in np.array(self.coords[self.legend_labels[self.current_line]])[:,0]:
warnings.warn("Cannot place two points at the same timestamp!", UserWarning, stacklevel=2)
else:
# update coords # update coords
current_lines = self.figure_lines[self.current_line].get_data() current_lines = self.figure_lines[self.current_line].get_data()
current_lines[0][self.index] = event.xdata current_lines[0][self.index] = event.xdata
......
...@@ -101,8 +101,8 @@ ...@@ -101,8 +101,8 @@
9632.580796877977 9632.580796877977
], ],
[ [
1.1769355956020835, 1.1794070626717919,
9171.762083884514 9479.66604006841
], ],
[ [
1.2234946472488883, 1.2234946472488883,
...@@ -187,28 +187,32 @@ ...@@ -187,28 +187,32 @@
15162.405352799564 15162.405352799564
], ],
[ [
1.446383724281465, 1.4413724760470132,
16496.35425883328 16287.542250441644
], ],
[ [
1.44676171875, 1.4490057835548567,
17700.91851851852 19078.485781877196
], ],
[ [
1.44676171875, 1.4515679818356721,
19269.57037037037 19196.707818930037
], ],
[ [
1.4532515624999998, 1.4733184911158894,
19254.340740740743 18661.618655692724
], ],
[ [
1.4662312499999999, 1.4937878542675378,
19117.274074074077 17396.273242381794
], ],
[ [
1.4857007812499998, 1.507527807781656,
18751.762962962963 16956.60405592277
],
[
1.5294099559708072,
16899.255901167242
] ]
], ],
"Line3": [ "Line3": [
......
...@@ -126,10 +126,13 @@ class Results(object): ...@@ -126,10 +126,13 @@ class Results(object):
for idx, key in enumerate(list(self.coords.keys())): for idx, key in enumerate(list(self.coords.keys())):
if mode=="curves": if mode=="curves":
if len(np.array(self.coords[key])[:,0])>2:
cx, cy = to_curve( cx, cy = to_curve(
np.array(self.coords[key])[:,0], np.array(self.coords[key])[:,0],
np.array(self.coords[key])[:,1], np.array(self.coords[key])[:,1],
kind="quadratic") kind="quadratic")
else:
cx, cy = np.array(self.coords[key])[:,0], np.array(self.coords[key])[:,1]
ax.plot(cx, cy, color=self.colors[idx%len(self.colors)]) ax.plot(cx, cy, color=self.colors[idx%len(self.colors)])
else: else:
...@@ -150,10 +153,10 @@ class Results(object): ...@@ -150,10 +153,10 @@ class Results(object):
fig, ax = self.display_image(img) fig, ax = self.display_image(img)
for idx, key in enumerate(list(self.coords.keys())): for idx, key in enumerate(list(self.coords.keys())):
min_min = (min(np.array(annot_data.coords[key])[:,0]), min_min = (min(np.array(self.coords[key])[:,0]),
min(np.array(annot_data.coords[key])[:,1])) min(np.array(self.coords[key])[:,1]))
max_max = (max(np.array(annot_data.coords[key])[:,0]), max_max = (max(np.array(self.coords[key])[:,0]),
max(np.array(annot_data.coords[key])[:,1])) max(np.array(self.coords[key])[:,1]))
min_min = (min_min[0]-tol*min_min[0], min_min[1]-tol*min_min[1]) min_min = (min_min[0]-tol*min_min[0], min_min[1]-tol*min_min[1])
max_max = (max_max[0]+tol*max_max[0], max_max[1]+tol*max_max[1]) max_max = (max_max[0]+tol*max_max[0], max_max[1]+tol*max_max[1])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment