-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfeatures_popup.py
More file actions
837 lines (752 loc) · 30.7 KB
/
features_popup.py
File metadata and controls
837 lines (752 loc) · 30.7 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# Copyright (c) 2021-2026 The University of Texas Southwestern Medical Center.
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted for academic and research use only (subject to the
# limitations in the disclaimer below) provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Standard library imports
import tkinter as tk
from tkinter import messagebox
import inspect
import ast
import os
import platform
# Third party imports
from PIL import ImageTk
# Local application imports
from navigate.view.popups.feature_list_popup import FeatureIcon, FeatureConfigPopup
from navigate.view.custom_widgets.ArrowLabel import ArrowLabel
from navigate.view.theme import get_theme_color
from navigate.controller.sub_controllers.gui import GUIController
from navigate.tools.image import create_arrow_image
from navigate.tools.file_functions import load_yaml_file, save_yaml_file
from navigate.model.features.feature_related_functions import (
convert_str_to_feature_list,
convert_feature_list_to_str,
)
from navigate.model.features import feature_related_functions
from navigate.config.config import get_navigate_path
class FeaturePopupController(GUIController):
"""Controller for feature list popup"""
def __init__(self, view, parent_controller, feature_list_id=0):
"""Initialize the controller
Parameters
----------
view : navigate.view.popups.feature_list_popup.FeatureListPopup
The view of the controller
parent_controller : navigate.controller.main_controller.MainController
The parent controller
feature_list_id : int, optional
The id of the feature list, by default 0
"""
super().__init__(view, parent_controller)
#: int: The id of the feature in the feature list.
self.feature_list_id = feature_list_id
#: list: The list of feature names.
self.features = []
#: list: The list of feature structure.
self.feature_structure = []
self.feature_list_graph_controller = FeatureListGraphController(
self.view.feature_view_frame,
self.view.inputs["content"],
self.view.buttons["preview"],
)
if "add" in self.view.buttons:
self.view.buttons["add"].configure(command=self.add_feature_list)
self.view.popup.protocol("WM_DELETE_WINDOW", self.exit_func)
self.view.buttons["cancel"].configure(command=self.exit_func)
elif "confirm" in self.view.buttons:
self.view.buttons["confirm"].configure(command=self.update_feature_list)
self.view.popup.protocol("WM_DELETE_WINDOW", self.cancel_acquisition)
self.view.buttons["cancel"].configure(command=self.cancel_acquisition)
# Dismiss popup.
self.view.popup.bind("<Escape>", self.exit_func)
def populate_feature_list(self, feature_list_id):
"""Populate the feature list
Parameters
----------
feature_list_id : int
The id of the feature list
"""
self.feature_list_id = feature_list_id
feature_list_content = self.parent_controller.model.get_feature_list(
feature_list_id
)
self.view.inputs["feature_list_name"].set(
self.parent_controller.menu_controller.feature_list_names[feature_list_id]
)
self.view.inputs["feature_list_name"].widget["state"] = "disabled"
self.feature_list_graph_controller.update(feature_list_content)
def add_feature_list(self):
"""Add the feature list"""
if not self.verify_feature_list():
return
content = self.view.inputs["content"].get("1.0", "end-1c")
feature_list_content = "".join(content.split("\n"))
feature_list_name = self.view.inputs["feature_list_name"].get()
if not feature_list_name:
messagebox.showerror(
title="Feature List Error",
message="Please enter a name for this feature list!",
)
return
if not self.parent_controller.menu_controller.add_feature_list(
feature_list_name, feature_list_content
):
messagebox.showerror(
title="Feature List Error",
message="Please enter a new list name! "
"The one you entered has been exist!",
)
else:
self.exit_func()
def update_feature_list(self):
"""Update the feature list"""
if not self.verify_feature_list():
return
content = self.view.inputs["content"].get("1.0", "end-1c")
feature_list_content = "".join(content.split("\n"))
self.parent_controller.execute(
"load_feature", self.feature_list_id, feature_list_content
)
# save to yaml file
feature_lists_path = get_navigate_path() + "/feature_lists"
feature_list_name = self.view.inputs["feature_list_name"].get()
feature_list_config = load_yaml_file(
os.path.join(
feature_lists_path, f"{'_'.join(feature_list_name.split(' '))}.yml"
)
)
if feature_list_config and feature_list_config["module_name"] is None:
save_yaml_file(
feature_lists_path,
{
"module_name": None,
"feature_list_name": feature_list_name,
"feature_list": feature_list_content,
},
f"{'_'.join(feature_list_name.split(' '))}.yml",
)
#: bool: Whether the acquisition should start.
self.start_acquisiton_flag = True
self.close_child_popups()
self.view.popup.dismiss()
def verify_feature_list(self):
"""Verify the feature list
Returns
-------
feature_list : list
The feature list
"""
content = self.view.inputs["content"].get("1.0", "end-1c")
return verify_feature_list(content)
def exit_func(self, *args):
"""Exit the popup"""
self.close_child_popups()
self.view.popup.dismiss()
delattr(self.parent_controller, "features_popup_controller")
def cancel_acquisition(self):
"""Cancel the acquisition"""
self.start_acquisiton_flag = False
self.close_child_popups()
self.view.popup.dismiss()
def close_child_popups(self):
"""Close child config popups"""
for popup in self.feature_list_graph_controller.child_popups:
popup.popup.dismiss()
class FeatureListGraphController:
def __init__(
self, feature_list_view, feature_content_view, preview_btn, child_popups=None
):
"""Initialize feature list window
Parameters
----------
feature_list_view : frame
feature list graph view
feature_content_view : text
feature list content
preview_btn : button
preview button
child_popups : list
list of child config popup windows
"""
self.feature_list_view = feature_list_view
self.feature_content_view = feature_content_view
self.preview_btn = preview_btn
self.feature_list = None
self.features = []
self.feature_structure = []
self.feature_list_graph_controllers_true = {}
self.feature_list_graph_controllers_false = {}
# get all feature names
#: list: The list of feature names.
self.feature_names = []
temp = dir(feature_related_functions)
for t in temp:
if inspect.isclass(getattr(feature_related_functions, t)):
self.feature_names.append(t)
# event
self.preview_btn.configure(command=self.draw_feature_list_graph)
# popups
self.child_popups = [] if child_popups is None else child_popups
def update(self, feature_list_content):
"""Update feature list window
Parameters
----------
feature_list_content : list
The feature list
"""
self.feature_list = None
self.features = []
self.feature_structure = []
self.feature_list_graph_controllers_true = {}
self.feature_list_graph_controllers_false = {}
if type(feature_list_content) == list:
feature_list_content = convert_feature_list_to_str(feature_list_content)
self.feature_content_view.delete("1.0", tk.END)
self.feature_content_view.insert("1.0", feature_list_content)
self.draw_feature_list_graph(new_list_flag=True)
def flatten_feature_list(self, feature_list):
"""Flatten the feature list
Parameters
----------
feature_list : list
The feature list
"""
for temp in feature_list:
if type(temp) is dict:
self.features.append(temp)
self.feature_structure.append(len(self.features) - 1)
elif temp == "break" or temp == "continue":
self.features.append(temp)
self.feature_structure.append(len(self.features) - 1)
else:
self.feature_structure.append("(")
self.flatten_feature_list(temp)
self.feature_structure.append(")")
def get_feature_content(self):
"""Get feature list content
Returns
-------
content : str
feature list content
"""
content = self.feature_content_view.get("1.0", "end-1c")
return content
def update_feature_content(self):
"""Update feature content"""
self.feature_content_view.delete("1.0", tk.END)
self.feature_content_view.insert("1.0", self.build_feature_list_text())
def draw_feature_list_graph(self, new_list_flag=True):
"""Draw the feature list graph
Parameters
----------
new_list_flag : bool, optional
Whether the feature list is new, by default True
"""
if new_list_flag:
feature_list = verify_feature_list(self.get_feature_content())
if not feature_list:
return
# flatten feature list
self.features = []
self.feature_structure = []
self.flatten_feature_list(feature_list)
self.feature_structure.pop()
for child in self.feature_list_view.winfo_children():
child.destroy()
l = len(self.features) - 1 # noqa
feature_icon_width = 228
al_width = 104
for i, feature in enumerate(self.features):
if type(feature) == str:
btn = FeatureIcon(self.feature_list_view, feature)
flag = False
else:
flag = True
is_decistion_btn = "true" in feature or "false" in feature
btn = FeatureIcon(
self.feature_list_view,
feature["name"].__name__,
set_bg=is_decistion_btn,
)
# left click
btn.bind("<Button-1>", self.show_config_popup(i))
btn.grid(row=0, column=i * 2, sticky="", pady=(30, 0))
btn["width"] = 20
# Right-Click Bindings
if platform.system() == "Darwin":
btn.bind("<ButtonPress-2>", self.show_menu(i, flag))
else:
btn.bind("<Button-3>", self.show_menu(i, flag))
if i == 0:
self.feature_list_view.update()
feature_icon_width = btn.winfo_width()
if i < l:
al = ArrowLabel(
self.feature_list_view,
xys=[(0, 20), (40, 20)],
direction="right",
image_width=40,
image_height=40,
)
al.grid(row=0, column=i * 2 + 1, sticky="", pady=(30, 0))
if i == 0:
self.feature_list_view.update()
al_width = al.winfo_width()
# draw loop arrows
image_width = feature_icon_width * (l + 1) + al_width * l
image_height = self.calculate_arrow_image_height()
stack = []
arrow_image = None
space = 30
end_pos = 0
for c in self.feature_structure:
if c == "(":
if len(stack) > 0 and type(stack[-1]) is int:
stack.pop()
stack.append("(")
elif c == ")":
# assert there is already at least one '('
p, loops, start_pos, arrow_height = stack[-2]
pre = stack[-1]
if loops - 1 == 0:
stack.pop()
stack.pop()
stack.append(pre)
else:
stack[-2] = (p, loops - 1, start_pos - space, arrow_height)
# draw arrow
arrow_image = create_arrow_image(
xys=[
(end_pos, 0),
(end_pos, arrow_height),
(start_pos, arrow_height),
(start_pos, 0),
],
image_width=image_width,
image_height=image_height,
direction="up",
image=arrow_image,
)
# update arrow height
for i in range(len(stack)):
if type(stack[i]) == tuple:
p, loops, start_pos, arrow_height = stack[i]
stack[i] = (p, loops, start_pos, arrow_height + 20)
elif type(c) is int:
if len(stack) > 0 and type(stack[-1]) is int:
stack[-1] = c
else:
loops = 0
while len(stack) > 0 and stack[-1] == "(":
loops += 1
stack.pop()
if loops > 0:
stack.append(
(
c,
loops,
c * (feature_icon_width + al_width)
+ feature_icon_width // 2
+ (loops // 2) * space,
50,
)
)
else:
stack.append(c)
end_pos = c * (feature_icon_width + al_width) + feature_icon_width // 2
if arrow_image:
#: ImageTk.PhotoImage: The image of the feature list graph.
self.image = ImageTk.PhotoImage(
arrow_image, master=self.feature_list_view
)
al = tk.Label(
self.feature_list_view,
image=self.image,
bg=get_theme_color("panel_bg", "#1a212b"),
borderwidth=0,
highlightthickness=0,
)
al.grid(row=1, column=0, columnspan=2 * l + 1, sticky="ew")
def show_config_popup(self, idx):
"""Show the feature configuration popup
Parameters
----------
idx : int
The index of the feature
Returns
-------
func : function
The function to show the feature configuration popup
"""
feature = self.features[idx]
def func(event):
"""The function to show the feature configuration popup"""
# load feature parameter setting
feature_config_path = (
f"{get_navigate_path()}/"
f"feature_lists/feature_parameter_setting"
f"/{feature['name'].__name__}.yml"
)
feature_parameter_config = None
if os.path.exists(feature_config_path):
feature_parameter_config = load_yaml_file(feature_config_path)
spec = inspect.getfullargspec(feature["name"])
if spec.defaults:
args_value = list(spec.defaults)
else:
args_value = spec.defaults
# if there is any parameters
if args_value is not None and "args" in feature:
for i, a in enumerate(feature["args"]):
if i >= len(args_value):
break
args_value[i] = a
kwargs = {}
if "true" in feature:
kwargs["true"] = True
if "false" in feature:
kwargs["false"] = True
popup = FeatureConfigPopup(
self.feature_list_view,
features=self.feature_names,
feature_name=feature["name"].__name__,
args_name=spec.args[2:],
args_value=args_value,
title="Feature Parameters",
parameter_config=feature_parameter_config,
**kwargs,
)
popup.feature_name_widget.widget.bind(
"<<ComboboxSelected>>", lambda event: refresh_parameters(popup)
)
popup.popup.protocol(
"WM_DELETE_WINDOW", lambda: update_feature_parameters(popup)
)
if "true" in feature:
self.feature_list_graph_controllers_true[idx] = (
FeatureListGraphController(
popup.feature_list_true_frame.feature_view_frame,
popup.feature_list_true_frame.content,
popup.preview_btn_true,
self.child_popups,
)
)
self.feature_list_graph_controllers_true[idx].update(feature["true"])
if "false" in feature:
self.feature_list_graph_controllers_false[idx] = (
FeatureListGraphController(
popup.feature_list_false_frame.feature_view_frame,
popup.feature_list_false_frame.content,
popup.preview_btn_false,
self.child_popups,
)
)
self.feature_list_graph_controllers_false[idx].update(feature["false"])
# save the popup reference
self.child_popups.append(popup)
def refresh_parameters(popup):
"""Refresh the feature parameters
Parameters
----------
popup : navigate.view.popups.feature_list_popup.FeatureConfigPopup
The feature configuration popup
"""
feature_name = popup.feature_name_widget.get()
new_feature = getattr(feature_related_functions, feature_name)
# load feature parameter setting
feature_config_path = (
f"{get_navigate_path()}/"
f"feature_lists/feature_parameter_setting"
f"/{new_feature.__name__}.yml"
)
feature_parameter_config = None
if os.path.exists(feature_config_path):
feature_parameter_config = load_yaml_file(feature_config_path)
spec = inspect.getfullargspec(new_feature)
popup.build_widgets(spec.args[2:], spec.defaults, feature_parameter_config)
def update_feature_parameters(popup):
"""Update the feature parameters
Parameters
----------
popup : navigate.view.popups.feature_list_popup.FeatureConfigPopup
The feature configuration popup
"""
widgets = popup.get_widgets()
feature_name = popup.feature_name_widget.get()
feature["name"] = getattr(feature_related_functions, feature_name)
# if new feature doesn't have any parameters
if "args" in feature and len(widgets) == 0:
del feature["args"]
if len(widgets) > 0:
feature["args"] = [w.get() for w in widgets]
for i, a in enumerate(feature["args"]):
if a == "True":
feature["args"][i] = True
elif a == "False":
feature["args"][i] = False
elif popup.inputs_type[i] is float:
try:
feature["args"][i] = float(a)
except ValueError:
feature["args"][i] = a
elif popup.inputs_type[i] is dict:
try:
feature["args"][i] = ast.literal_eval(a.replace("'", '"'))
except (SyntaxError, ValueError):
spec = inspect.getfullargspec(feature["name"])
arg_name = spec.args[i + 2]
messagebox.showerror(
title="Upate Feature Parameter Error",
message=f"The argument {arg_name} has something wrong!\n"
"Please make sure you input a correct value!",
)
return
elif a == "None":
feature["args"][i] = None
if "true" in feature:
feature["true"] = convert_str_to_feature_list(
self.feature_list_graph_controllers_true[idx].get_feature_content()
)
if "false" in feature:
feature["false"] = convert_str_to_feature_list(
self.feature_list_graph_controllers_false[idx].get_feature_content()
)
# update text
self.update_feature_content()
popup.popup.dismiss()
self.draw_feature_list_graph(False)
return func
def show_menu(self, idx, flag=True):
"""Show the popup menu
Parameters
----------
idx : int
The index of the feature
flag : bool, optional
Whether the feature is a decision node, by default True
Returns
-------
func : function
The function to show the popup menu
"""
def func(event):
"""The function to show the popup menu"""
popup_menu = tk.Menu(self.feature_list_view, tearoff=0)
popup_menu.add_command(label="Delete", command=lambda: delete_feature(idx))
if flag:
popup_menu.add_command(
label="Insert Before", command=lambda: insert_before(idx)
)
popup_menu.add_command(
label="Insert After", command=lambda: insert_after(idx)
)
popup_menu.post(event.x_root, event.y_root)
def delete_feature(idx):
"""Delete the feature
Parameters
----------
idx : int
The index of the feature
"""
del self.features[idx]
i = self.feature_structure.index(idx)
del self.feature_structure[i]
for _, c in enumerate(self.feature_structure):
if type(c) == int and c > idx:
self.feature_structure[_] -= 1
# delete ()
pre_count = 0
for _ in range(i - 1, -1, -1):
if self.feature_structure[_] == "(":
del self.feature_structure[_]
pre_count += 1
else:
break
stack = []
if (
pre_count == 0
and i < len(self.feature_structure)
and self.feature_structure[i] == ")"
):
del self.feature_structure[i]
for _ in range(i - 1, -1, -1):
if self.feature_structure[_] == ")":
stack.append(_)
elif self.feature_structure[_] == "(":
if len(stack) > 0:
stack.pop()
else:
del self.feature_structure[_]
break
else:
i = i - pre_count
while pre_count > 0:
if self.feature_structure[i] == "(":
stack.append(i)
i += 1
elif self.feature_structure[i] == ")":
if len(stack) > 0:
stack.pop()
i += 1
else:
del self.feature_structure[i]
pre_count -= 1
else:
i += 1
if i >= len(self.feature_structure):
break
# update text
self.update_feature_content()
self.draw_feature_list_graph(False)
def insert_before(idx):
"""Insert the feature before the current feature
Parameters
----------
idx : int
The index of the feature
"""
self.features.insert(idx, dict(self.features[idx]))
i = self.feature_structure.index(idx)
for _, c in enumerate(self.feature_structure):
if type(c) == int and c >= idx:
self.feature_structure[_] = c + 1
self.feature_structure.insert(i, idx)
# update text
self.update_feature_content()
self.draw_feature_list_graph(False)
def insert_after(idx):
"""Insert the feature after the current feature
Parameters
----------
idx : int
The index of the feature
"""
self.features.insert(
idx,
(
dict(self.features[idx])
if type(self.features[idx]) == dict
else self.features[idx]
),
)
i = self.feature_structure.index(idx)
for _, c in enumerate(self.feature_structure):
if type(c) == int and c > idx:
self.feature_structure[_] = c + 1
self.feature_structure.insert(i + 1, idx + 1)
# update text
self.update_feature_content()
self.draw_feature_list_graph(False)
return func
def calculate_arrow_image_height(self):
"""Calculate the height of the arrow image
Returns
-------
image_height : int
The height of the arrow image
"""
image_height = 0
h = 0
for c in self.feature_structure:
if c == "(":
h += 20
image_height = max(h, image_height)
elif c == ")":
h -= 20
return image_height + 100
def build_feature_list_text(self):
"""Build the feature list text
Returns
-------
content : str
The feature list text
"""
content = "["
for c in self.feature_structure:
if c == "(":
content += "("
continue
elif c == ")":
content += ")"
else:
feature = self.features[c]
content += "{" + f'"name": {feature["name"].__name__}, '
if "args" in feature:
arg_str = ""
for a in feature["args"]:
if a is None:
arg_str += "None"
elif type(a) is bool:
arg_str += str(a)
elif type(a) is int or type(a) is float:
arg_str += str(a)
elif type(a) is dict:
arg_str += str(a)
else:
try:
float(a)
arg_str += a
except (ValueError, TypeError):
arg_str += f'"{a}"'
arg_str += ","
content += f'"args": ({arg_str}),'
# "true"
if "true" in feature:
content += (
f'"true": {convert_feature_list_to_str(feature["true"])},'
)
# "false"
if "false" in feature:
content += (
f'"false": {convert_feature_list_to_str(feature["false"])},'
)
content += "}"
content += ","
content += "]"
return content
def verify_feature_list(content):
"""Verify if feature list is valid
Parameters
----------
content : str
The feature list content
Returns
-------
feature list : list
feature list
"""
feature_list_content = "".join(content.split("\n"))
if feature_list_content in ["break", '"break"', "'break'"]:
return ["break"]
if feature_list_content in ["continue", '"continue"', "'continue'"]:
return ["continue"]
feature_list = convert_str_to_feature_list(feature_list_content)
if feature_list is None:
messagebox.showerror(
title="Feature List Error",
message="There is something wrong for this feature "
"list, please verify there is no "
"spelling error!",
)
return feature_list