-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvocalpy_gui.py
More file actions
275 lines (248 loc) · 11.8 KB
/
Copy pathvocalpy_gui.py
File metadata and controls
275 lines (248 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from PIL import Image
import numpy as np
# import matplotlib.pyplot as plt
import webbrowser
from pyforms.basewidget import BaseWidget
from pyforms.controls import ControlDir
from pyforms.controls import ControlLabel
from pyforms.controls import ControlCombo
from pyforms.controls import ControlButton
from pyforms.controls import ControlImage
from pyforms.controls import ControlNumber
from pyforms.controls import ControlProgress
from pyforms.controls import ControlCheckBox
from pyforms.controls import ControlCheckBoxList
class VocalPy(BaseWidget):
def __init__(self, *args, **kwargs):
super().__init__("VocalPy")
# Definition of the forms fields
# tab: choose audios
self._dir_load_directory = ControlDir(" directory path")
self._checkboxlist_fileList = ControlCheckBoxList(" select files")
self._checkbox_searchSubDirs = ControlCheckBox("search subdirectories ")
self._button_select_all = ControlButton("select all")
self._button_select_none = ControlButton("unselect all")
self._button_select_clear = ControlButton("clear list")
self._button_more_info = ControlButton("http://dietrich-lab.org/vocalpy")
# # actions
self._dir_load_directory.changed_event = self.__directorySelectionEvent
self._checkbox_searchSubDirs.changed_event = self.__directorySelectionEvent
self._button_select_all.value = self.__selectAllCheckbox
self._button_select_none.value = self.__selectNoneCheckbox
self._button_select_clear.value = self.__selectClearCheckbox
self._button_more_info.value = self.__openURLButton
# tab: select animal pipeline
self._combo_animal_dropdown = ControlCombo("choose animal pipeline")
self._combo_animal_dropdown.add_item("Mouse", "mouse")
self._combo_animal_dropdown.add_item("Rat", "rat")
self._combo_animal_dropdown.add_item("Guinea pig", "guineapig")
self._number_lo_freq_cutoff = ControlNumber("lower frequency cutoff (Hz)", default=45000, minimum=0, maximum=10000000)
self._number_hi_freq_cutoff = ControlNumber(
"higher frequency cutoff (Hz)", default=125000, minimum=0, maximum=10000000
)
# # actions
self._combo_animal_dropdown.changed_event = self.__changeFrequencyAnimal
# tab: optional settings
self._number_thread_count = ControlNumber("number of threads for parallelization", default=2, minimum=1, maximum=40)
self._checkbox_save_spectrograms = ControlCheckBox("save vocal spectrogram images", default=True)
self._checkbox_save_segmentation = ControlCheckBox("save vocal segmentation images", default=False)
self._checkbox_save_overlay = ControlCheckBox("save vocal segmentation overlay images", default=False)
self._checkbox_save_plots = ControlCheckBox("generate sample plots", default=True)
# # actions
# tab: run analysis
self._button_run_analysis = ControlButton("run!")
self._label_run_analysis = ControlLabel("#")
self._progress_bar = ControlProgress("Processing audio files")
# # actions
self._progress_bar.min = 0
self._progress_bar.max = self.__numberOfAudios()
self._button_run_analysis.value = self.__runVocalPy
# tab: inspect vocalizations
self._dir_load_vocalpy_file = ControlDir(" choose an audio output directory")
self._label_vocal_ID = ControlLabel("#")
self._label_vocal_start = ControlLabel("#")
self._label_vocal_end = ControlLabel("#")
self._label_vocal_duration = ControlLabel("#")
self._label_vocal_min_freq = ControlLabel("#")
self._label_vocal_max_freq = ControlLabel("#")
self._label_vocal_avg_freq = ControlLabel("#")
self._label_vocal_bandwidth = ControlLabel("#")
self._label_vocal_current_num = ControlLabel("#")
self._label_vocal_total_num = ControlLabel("#")
self._label_vocal_class = ControlLabel("#")
self._button_change_vocal = ControlButton("Change")
self._button_previous_vocal = ControlButton("Previous")
self._button_next_vocal = ControlButton("Next")
self._image_plot_vocal = ControlImage("vocalization")
self._button_save_changes = ControlButton("save changes")
self._label_save_changes = ControlLabel("when you are done, select save changes")
# # actions
self._dir_load_vocalpy_file.changed_event = self.__loadVocalPyObject
self._button_next_vocal.value = self.__loadNextVocal
self._button_previous_vocal.value = self.__loadPreviousVocal
self._button_save_changes.value = self.__updateVocalPyObject
# Define the event called before showing the image in the player
# self._player.process_frame_event = self.__process_frame
# Define the organization of the Form Controls
self.set_margin(10)
self._formset = [
{
"a:choose audios": [
"",
(" ", "h3: step 1: choose audio files", " "),
("_dir_load_directory", "_checkbox_searchSubDirs"),
("_checkboxlist_fileList"),
(" ", "_button_select_all", "_button_select_none", "_button_select_clear", " "),
(" ", "h3: for more information, visit:", " "),
(" ", "_button_more_info", " "),
" ",
],
"b:animal pipeline": [
"",
(" ", "h3: step 2: select animal pipeline", " "),
(" ", "_combo_animal_dropdown", " "),
(" ", "_number_lo_freq_cutoff", " "),
(" ", "_number_hi_freq_cutoff", " "),
" ",
],
"c:optional settings": [
"",
(" ", "h3: step 3: select optional settings", " "),
(" ", "_number_thread_count", " "),
(" ", "_checkbox_save_spectrograms", " "),
(" ", "_checkbox_save_segmentation", " "),
(" ", "_checkbox_save_overlay", " "),
(" ", "_checkbox_save_plots", " "),
" ",
],
"d:run analysis": [
"",
(" ", "h3: step 4: run analysis", " "),
(" ", "_button_run_analysis", " "),
(" ", "_progress_bar", " "),
(" ", "_label_run_analysis", " "),
" ",
],
"e:inspect vocalizations": [
"",
(" ", "h3: step 5: inspect vocalizations", " "),
("_dir_load_vocalpy_file"),
(
" ",
"filename:",
"_label_vocal_ID",
"start:",
"_label_vocal_start",
"end:",
"_label_vocal_end",
"duration:",
"_label_vocal_duration",
" ",
),
(
" ",
"frequency |",
"minimum:",
"_label_vocal_min_freq",
"maximum:",
"_label_vocal_max_freq",
"average:",
"_label_vocal_avg_freq",
"bandwidth:",
"_label_vocal_bandwidth",
" ",
),
(" ", "label:", "_label_vocal_class", "_button_change_vocal", " "),
(
" ",
"_button_previous_vocal",
"_label_vocal_current_num",
"/",
"_label_vocal_total_num",
"_button_next_vocal",
" ",
),
("_image_plot_vocal"),
(" ", "_label_save_changes", " "),
(" ", "_button_save_changes", " "),
],
}
]
# save session before closing
self.before_close_event()
def __directorySelectionEvent(self):
"""
When the directory is selected instanciate the video in the player
"""
from vocalpy.utils.io import parse_input_path
self._checkboxlist_fileList.clear()
search_subdir = self._checkbox_searchSubDirs.value
filelist = parse_input_path(self._dir_load_directory.value, search_subdir)
if filelist == -1 or not filelist:
self._checkboxlist_fileList += 'no audios found, select a different directory or use "search subdirectories"'
else:
for file in filelist:
self._checkboxlist_fileList += (file, True)
def __selectAllCheckbox(self):
"""
Do some processing to the frame and return the result frame
"""
items = self._checkboxlist_fileList.items
self._checkboxlist_fileList.clear()
for item in items:
self._checkboxlist_fileList += (item[0], True)
def __selectNoneCheckbox(self):
"""
Do some processing to the frame and return the result frame
"""
items = self._checkboxlist_fileList.items
self._checkboxlist_fileList.clear()
for item in items:
self._checkboxlist_fileList += (item[0], False)
def __selectClearCheckbox(self):
"""
Do some processing to the frame and return the result frame
"""
self._checkboxlist_fileList.clear()
def __openURLButton(self):
url = "http://dietrich-lab.org/vocalpy"
webbrowser.open(url)
def __changeFrequencyAnimal(self):
selected_animal = self._combo_animal_dropdown.value
if selected_animal == "mouse":
self._number_lo_freq_cutoff.value = 45000
self._number_hi_freq_cutoff.value = 125000
elif selected_animal == "rat":
self._number_lo_freq_cutoff.value = 20000
self._number_hi_freq_cutoff.value = 125000
elif selected_animal == "guineapig":
self._number_lo_freq_cutoff.value = 100
self._number_hi_freq_cutoff.value = 20000
def __numberOfAudios(self):
return 10 ** 6
def __loadVocalPyObject(self):
img = Image.open(
"/Users/gustavo/Documents/git/vocalpy/archive/audios/1795_Agrp-Trpv1_1st_Stage_outputs/spectrogram/1_8.png"
).convert("L")
self._image_plot_vocal.value = np.pad(np.asarray(img), 75)
def __loadNextVocal(self):
img = Image.open(
"/Users/gustavo/Documents/git/vocalpy/archive/audios/1795_Agrp-Trpv1_1st_Stage_outputs/spectrogram/1_9.png"
).convert("L")
self._image_plot_vocal.value = np.pad(np.asarray(img), 75)
def __loadPreviousVocal(self):
img = Image.open(
"/Users/gustavo/Documents/git/vocalpy/archive/audios/1795_Agrp-Trpv1_1st_Stage_outputs/spectrogram/1_10.png"
).convert("L")
self._image_plot_vocal.value = np.pad(np.asarray(img), 75)
def __updateVocalPyObject(self):
self._label_save_changes.value = "saving changes, please wait..."
def __runVocalPy(self):
self._label_run_analysis.value = "analysis running, check terminal for detailed logging information..."
for i in range(0, 10 ** 6):
self._progress_bar.value = i
self._progress_bar.value = self._progress_bar.max
self._label_run_analysis.value = '!!!!!! analysis complete (optional: go to "inspect vocalizations") !!!!!!'
if __name__ == "__main__":
from pyforms import start_app
start_app(VocalPy, geometry=(100, 100, 650, 700))