From 32ce52decda1a9578e2d5eaaba2e9f3582d68d29 Mon Sep 17 00:00:00 2001 From: healthonrails Date: Thu, 19 Dec 2024 16:46:55 -0500 Subject: [PATCH] Refactor to update YAML configuration - Load and update with pinned flags. - Append new flags to the section. - Initialize as an empty dictionary. - Save updated configuration back to . - Automatically call on initialization. --- annolid/gui/app.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/annolid/gui/app.py b/annolid/gui/app.py index 2f03348..c54dfc9 100644 --- a/annolid/gui/app.py +++ b/annolid/gui/app.py @@ -65,6 +65,7 @@ import csv import os.path as osp import time +import yaml import html import shutil import sys @@ -185,7 +186,7 @@ def __init__(self, self.auto_recovery_missing_instances = False self.compute_optical_flow = True self.behaviors = None - self.pinned_flags = None + self.pinned_flags = {} # Create progress bar self.progress_bar = QtWidgets.QProgressBar() self.progress_bar.setRange(0, 100) @@ -226,6 +227,8 @@ def __init__(self, self.flag_widget.flagsSaved.connect(self.handle_flags_saved) self.flag_widget.rowSelected.connect(self.handle_row_selected) + self.handle_flags_saved() + self.setCentralWidget(scrollArea) self.createPolygonSAMMode = action( @@ -579,8 +582,30 @@ def __init__(self, self.populateModeActions() - def handle_flags_saved(self, flags): - self.loadFlags(flags) + def handle_flags_saved(self, flags={}): + default_config = self.here.parent.resolve() / 'configs' / 'behaviors.yaml' + + # Load the existing configuration from the YAML file + try: + with open(default_config, 'r') as file: + config_data = yaml.safe_load(file) or {} + except FileNotFoundError: + config_data = {} + + # Append the pinned flags to the existing configuration + if 'pinned_flags' not in config_data: + config_data['pinned_flags'] = [] + config_data['pinned_flags'].extend(flags) # Append the new flags + pinned_flags = { + pinned_flag: False for pinned_flag in config_data['pinned_flags']} + + # Save the updated configuration back to the YAML file + with open(default_config, 'w') as file: + yaml.dump(config_data, file, default_flow_style=False) + + # Update the pinned_flags attribute and load the flags + self.pinned_flags = pinned_flags + self.loadFlags(pinned_flags) def handle_row_selected(self, flag_name: str): self.event_type = flag_name