Skip to content

Commit

Permalink
Merge pull request #415 from kahst/undo-review
Browse files Browse the repository at this point in the history
Undo review
  • Loading branch information
max-mauermann authored Aug 27, 2024
2 parents acc25a7 + 5762857 commit 6042440
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 51 deletions.
161 changes: 110 additions & 51 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1629,16 +1629,17 @@ def create_log_plot(positives, negatives, fig_num=None):
p_colors = ["blue", "purple", "orange", "green"]

for target_p, p_color, threshold in zip(target_ps, p_colors, thresholds):
ax.vlines(
threshold,
0,
target_p,
color=p_color,
linestyle="--",
linewidth=0.5,
label=f"p={target_p:.2f} treshold>={threshold:.2f}",
)
ax.hlines(target_p, 0, threshold, color=p_color, linestyle="--", linewidth=0.5)
if threshold <= 1:
ax.vlines(
threshold,
0,
target_p,
color=p_color,
linestyle="--",
linewidth=0.5,
label=f"p={target_p:.2f} threshold>={threshold:.2f}",
)
ax.hlines(target_p, 0, threshold, color=p_color, linestyle="--", linewidth=0.5)

ax.plot(Xs, Ys, color="red")
ax.scatter(thresholds, target_ps, color=p_colors, marker="x")
Expand All @@ -1656,12 +1657,13 @@ def create_log_plot(positives, negatives, fig_num=None):
review_state = gr.State(
{
"input_directory": "",
"spcies_list": [],
"species_list": [],
"current_species": "",
"files": [],
"current": 0,
POSITIVE_LABEL_DIR: [],
NEGATIVE_LABEL_DIR: [],
"skipped": [],
"history": [],
}
)

Expand All @@ -1677,13 +1679,17 @@ def create_log_plot(positives, negatives, fig_num=None):
loc.localize("review-tab-file-matrix-neg-header"),
],
interactive=False,
elem_id="segments-results-grid",
)

with gr.Column() as review_item_col:
with gr.Row():
spectrogram_image = gr.Plot(label=loc.localize("review-tab-spectrogram-plot-label"))

with gr.Column():
with gr.Row():
skip_btn = gr.Button(loc.localize("review-tab-skip-button-label"))
undo_btn = gr.Button(loc.localize("review-tab-undo-button-label"))
positive_btn = gr.Button(loc.localize("review-tab-pos-button-label"))
negative_btn = gr.Button(loc.localize("review-tab-neg-button-label"))
review_audio = gr.Audio(
Expand All @@ -1696,11 +1702,44 @@ def create_log_plot(positives, negatives, fig_num=None):
no_samles_label = gr.Label(loc.localize("review-tab-no-files-label"), visible=False)
species_regression_plot = gr.Plot(label=loc.localize("review-tab-regression-plot-label"))

def next_review(next_review_state: dict, target_dir: str = None):
current_file = next_review_state["files"][next_review_state["current"]]

def update_values(next_review_state, skip_plot=False):
update_dict = {review_state: next_review_state}

if not skip_plot:
update_dict |= {
species_regression_plot: create_log_plot(
next_review_state[POSITIVE_LABEL_DIR], next_review_state[NEGATIVE_LABEL_DIR], 2
),
}

if next_review_state["files"]:
next_file = next_review_state["files"][0]
update_dict |= {
review_audio: gr.Audio(next_file, label=os.path.basename(next_file)),
spectrogram_image: utils.spectrogram_from_file(next_file),
}
else:
update_dict |= {
no_samles_label: gr.Label(visible=True),
review_item_col: gr.Column(visible=False),
}

update_dict |= {
file_count_matrix: [
[
len(next_review_state["files"]) + len(next_review_state["skipped"]),
len(next_review_state[POSITIVE_LABEL_DIR]),
len(next_review_state[NEGATIVE_LABEL_DIR]),
],
],
undo_btn: gr.Button(interactive=bool(next_review_state["history"])),
}

return update_dict

def next_review(next_review_state: dict, target_dir: str = None):
current_file = next_review_state["files"][0]

if target_dir:
selected_dir = os.path.join(
next_review_state["input_directory"], next_review_state["current_species"], target_dir
Expand All @@ -1716,44 +1755,13 @@ def next_review(next_review_state: dict, target_dir: str = None):
next_review_state[target_dir] += [current_file]
next_review_state["files"].remove(current_file)

update_dict |= {
species_regression_plot: create_log_plot(
next_review_state[POSITIVE_LABEL_DIR], next_review_state[NEGATIVE_LABEL_DIR], 2
),
}

if not next_review_state["files"]:
update_dict |= {
no_samles_label: gr.Label(visible=True),
review_item_col: gr.Column(visible=False),
}
else:
next_file = next_review_state["files"][next_review_state["current"]]
update_dict |= {
review_audio: gr.Audio(next_file, label=os.path.basename(next_file)),
spectrogram_image: utils.spectrogram_from_file(next_file),
}

update_dict |= {
file_count_matrix: [
[
len(next_review_state["files"]),
len(next_review_state[POSITIVE_LABEL_DIR]),
len(next_review_state[NEGATIVE_LABEL_DIR]),
],
],
}
next_review_state["history"].append((current_file, target_dir))
else:
if next_review_state["current"] + 1 < len(next_review_state["files"]):
next_review_state["current"] += 1
next_file = next_review_state["files"][next_review_state["current"]]

update_dict |= {
review_audio: gr.Audio(next_file, label=os.path.basename(next_file)),
spectrogram_image: utils.spectrogram_from_file(next_file),
}
next_review_state["skipped"].append(current_file)
next_review_state["files"].remove(current_file)
next_review_state["history"].append((current_file, None))

return update_dict
return update_values(next_review_state)

def select_subdir(new_value: str, next_review_state: dict):
if new_value != next_review_state["current_species"]:
Expand Down Expand Up @@ -1781,6 +1789,9 @@ def start_review(next_review_state):
return {review_state: next_review_state}

def update_review(next_review_state: dict, selected_species: str = None):
next_review_state["history"] = []
next_review_state["skipped"] = []

if selected_species:
next_review_state["current_species"] = selected_species
else:
Expand All @@ -1799,6 +1810,7 @@ def update_review(next_review_state: dict, selected_species: str = None):
update_dict = {
review_col: gr.Column(visible=True),
review_state: next_review_state,
undo_btn: gr.Button(interactive=bool(next_review_state["history"])),
file_count_matrix: [
[
len(next_review_state["files"]),
Expand Down Expand Up @@ -1830,6 +1842,37 @@ def update_review(next_review_state: dict, selected_species: str = None):

return update_dict

def undo_review(next_review_state):
if next_review_state["history"]:
last_file, last_dir = next_review_state["history"].pop()

if last_dir:
os.rename(
os.path.join(
next_review_state["input_directory"],
next_review_state["current_species"],
last_dir,
os.path.basename(last_file),
),
os.path.join(
next_review_state["input_directory"],
next_review_state["current_species"],
os.path.basename(last_file),
),
)

next_review_state[last_dir].remove(last_file)
else:
next_review_state["skipped"].remove(last_file)

next_review_state["files"].insert(0, last_file)

return update_values(next_review_state, skip_plot=not last_dir)
return {
review_state: next_review_state,
undo_btn: gr.Button(interactive=bool(next_review_state["history"])),
}

def toggle_autoplay(value):
return gr.Audio(autoplay=value)

Expand All @@ -1845,6 +1888,7 @@ def toggle_autoplay(value):
review_state,
file_count_matrix,
species_regression_plot,
undo_btn,
]

species_dropdown.change(
Expand All @@ -1862,6 +1906,7 @@ def toggle_autoplay(value):
no_samles_label,
file_count_matrix,
species_regression_plot,
undo_btn,
]

positive_btn.click(
Expand All @@ -1878,6 +1923,20 @@ def toggle_autoplay(value):
show_progress=True,
)

skip_btn.click(
next_review,
inputs=review_state,
outputs=review_btn_output,
show_progress=True,
)

undo_btn.click(
undo_review,
inputs=review_state,
outputs=review_btn_output,
show_progress=True,
)

select_directory_btn.click(
start_review,
inputs=review_state,
Expand Down
2 changes: 2 additions & 0 deletions lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"review-tab-regression-plot-y-label-false": "Falsch",
"review-tab-regression-plot-y-label-true": "Wahr",
"review-tab-autoplay-checkbox-label": "autom. Abspielen",
"review-tab-skip-button-label": "Überspringen",
"review-tab-undo-button-label": "Rückgängig",
"species-tab-title": "Arten",
"species-tab-select-output-directory-button-label": "Wählen Sie das Ausgabeverzeichnis",
"species-tab-filename-textbox-label": "Name der Datei, wenn nicht angegeben, wird 'species_list.txt' verwendet.",
Expand Down
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"review-tab-regression-plot-y-label-false": "False",
"review-tab-regression-plot-y-label-true": "True",
"review-tab-autoplay-checkbox-label": "Autoplay",
"review-tab-skip-button-label": "Skip",
"review-tab-undo-button-label": "Undo",
"species-tab-title": "Species",
"species-tab-select-output-directory-button-label": "Select output directory",
"species-tab-filename-textbox-label": "Name of the file, if not specified 'species_list.txt' will be used.",
Expand Down

0 comments on commit 6042440

Please sign in to comment.