Skip to content

Commit 53f8377

Browse files
authored
Refactor / simplify CM.py (#82)
Refactor / simplify CM.py
1 parent b4706b1 commit 53f8377

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

src/ccompass/CCMPS.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,11 +1558,11 @@ def run(self):
15581558
)
15591559

15601560
elif event == "-marker_manage-":
1561-
from .CM import CM_exec
1561+
from .class_manager_dialog import show_class_manager_dialog
15621562

15631563
if check_markers(self.model.marker_sets):
1564-
self.model.marker_conv = CM_exec(
1565-
self.model.marker_sets, self.model.marker_conv
1564+
self.model.marker_conv = show_class_manager_dialog(
1565+
self.model.marker_conv
15661566
)
15671567
else:
15681568
messagebox.showerror(
Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
1-
"""Class manager."""
1+
"""Class manager dialog.
2+
3+
Dialog window for selecting and mapping annotations to classes.
4+
"""
25

36
import FreeSimpleGUI as sg
47
import numpy as np
58
import pandas as pd
69

710

8-
def refresh_conversion(conversion, values):
11+
def _refresh_conversion(
12+
conversion: dict[str, str | float], values: dict
13+
) -> dict[str, str | float]:
14+
"""Refresh the conversion dictionary with the new values from the GUI."""
915
for o in conversion:
10-
if values["--" + o + "--"] == True:
11-
conversion[o] = values["--" + o + "_class--"]
12-
elif values["--" + o + "--"] == False:
13-
conversion[o] = np.nan
16+
conversion[o] = (
17+
values[f"--{o}_class--"] if values[f"--{o}--"] else np.nan
18+
)
1419
return conversion
1520

1621

17-
def CM_exec(markers, marker_conv):
18-
conv_old = marker_conv
19-
20-
num_names = 0
21-
for name in marker_conv:
22-
if not pd.isnull(marker_conv[name]):
23-
num_names += 1
24-
22+
def _create_class_manager_window(
23+
marker_conv: dict[str, str | float], num_names: int
24+
) -> sg.Window:
25+
"""Create the class manager window."""
2526
layout_column = [
2627
[sg.Text("Annotation", size=(25, 1)), sg.Text("Class", size=(20, 1))],
27-
[
28-
sg.Text(
29-
"--------------------------------------------------------------------------------"
30-
)
31-
],
28+
[sg.Text("-" * 80)],
3229
*[
3330
[
3431
sg.Checkbox(
3532
o,
3633
default=not pd.isnull(marker_conv[o]),
3734
enable_events=True,
3835
size=(20, 5),
39-
key="--" + o + "--",
36+
key=f"--{o}--",
4037
),
4138
sg.InputText(
4239
str(marker_conv[o]),
4340
visible=not pd.isnull(marker_conv[o]),
4441
size=(20, 5),
45-
key="--" + o + "_class--",
42+
key=f"--{o}_class--",
4643
),
4744
]
4845
for o in marker_conv
@@ -68,11 +65,11 @@ def CM_exec(markers, marker_conv):
6865
sg.Column(
6966
layout=[
7067
[
71-
sg.Text("initial annotations: "),
68+
sg.Text("Initial annotations: "),
7269
sg.Text(str(len(marker_conv))),
7370
],
7471
[
75-
sg.Text("used annotations: "),
72+
sg.Text("Used annotations: "),
7673
sg.Text(str(num_names), key="-num_anno-"),
7774
],
7875
[
@@ -99,30 +96,40 @@ def CM_exec(markers, marker_conv):
9996
]
10097
]
10198

102-
window_CM = sg.Window("Classes", layout_CM, size=(600, 400))
99+
return sg.Window("Classes", layout_CM, size=(600, 400))
100+
101+
102+
def show_class_manager_dialog(
103+
marker_conv: dict[str, str | float],
104+
) -> dict[str, str | float]:
105+
"""Show the class managar dialog."""
106+
conv_old = marker_conv
107+
num_names = sum(not pd.isnull(v) for v in marker_conv.values())
108+
109+
window = _create_class_manager_window(marker_conv, num_names)
103110

104111
while True:
105-
event_CM, values_CM = window_CM.read()
112+
event, values = window.read()
113+
114+
if event == sg.WIN_CLOSED or event == "--cancel--":
115+
marker_conv = conv_old
116+
break
117+
106118
for k in marker_conv:
107-
if event_CM == "--" + k + "--":
108-
window_CM["--" + k + "_class--"].Update(
109-
visible=values_CM["--" + k + "--"]
110-
)
111-
if values_CM["--" + k + "--"] == True:
112-
window_CM["--" + k + "_class--"].Update(value=k)
119+
# checkbox clicked?
120+
if event == f"--{k}--":
121+
window[f"--{k}_class--"].Update(visible=values[f"--{k}--"])
122+
if values[f"--{k}--"]:
123+
window[f"--{k}_class--"].Update(value=k)
113124
num_names += 1
114-
window_CM["-num_anno-"].Update(value=str(num_names))
115-
elif values_CM["--" + k + "--"] == False:
116-
window_CM["--" + k + "_class--"].Update(value=False)
125+
else:
126+
window[f"--{k}_class--"].Update(value=False)
117127
num_names -= 1
118-
window_CM["-num_anno-"].Update(value=str(num_names))
128+
window["-num_anno-"].Update(value=str(num_names))
119129

120-
if event_CM == sg.WIN_CLOSED or event_CM == "--cancel--":
121-
marker_conv = conv_old
122-
break
123-
if event_CM == "--accept--":
124-
marker_conv = refresh_conversion(marker_conv, values_CM)
130+
if event == "--accept--":
131+
marker_conv = _refresh_conversion(marker_conv, values)
125132
break
126133

127-
window_CM.close()
134+
window.close()
128135
return marker_conv

0 commit comments

Comments
 (0)