Skip to content

Commit

Permalink
Fix bug related to error when user delete or rename the predefined fo…
Browse files Browse the repository at this point in the history
…lder containing all FIT_MODELs
  • Loading branch information
h0anle committed Sep 26, 2024
1 parent 298d0af commit 24e2151
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
82 changes: 48 additions & 34 deletions app/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,33 @@ def __init__(self, graph_id=None):
# Set layout margins to 0 to remove extra space
self.graph_layout.setContentsMargins(0, 0, 0, 0)
self.graph_layout.setSpacing(0)

def save(self, fname=None):
""" Save Graph object to serialization. Save it if a fname is given """
# List of keys to exclude from serialization
excluded_keys = ['figure', 'canvas', 'setLayout', 'graph_layout',
'some_signal_instance']

dict_graph = {}
for key, val in vars(self).items():
if key not in excluded_keys and not callable(val):
try:
json.dumps(val)
dict_graph[key] = val
except TypeError:
continue

if fname is not None:
with open(fname, 'w') as f:
json.dump(dict_graph, f, indent=4)

return dict_graph

def set_attributes(self, attributes_dict):
"""Set attributes of the Graph object from a given dictionary."""
for key, value in attributes_dict.items():
if hasattr(self, key):
setattr(self, key, value)

def clear_layout(self, layout):
"""Clears all widgets and layouts from the specified layout."""
Expand Down Expand Up @@ -2026,33 +2053,7 @@ def _plot_tertiary_axis(self, df):
self.ax3.set_ylabel(self.y3label, color='green')
self.ax3.tick_params(axis='y', colors='green')

def save(self, fname=None):
""" Save Graph object to serialization. Save it if a fname is given """
# List of keys to exclude from serialization
excluded_keys = ['figure', 'canvas', 'setLayout', 'graph_layout',
'some_signal_instance']

dict_graph = {}
for key, val in vars(self).items():
if key not in excluded_keys and not callable(val):
try:
json.dumps(val)
dict_graph[key] = val
except TypeError:
continue

if fname is not None:
with open(fname, 'w') as f:
json.dump(dict_graph, f, indent=4)

return dict_graph

def set_attributes(self, attributes_dict):
"""Set attributes of the Graph object from a given dictionary."""
for key, value in attributes_dict.items():
if hasattr(self, key):
setattr(self, key, value)



class FitModelManager:
"""
Expand All @@ -2067,8 +2068,7 @@ class FitModelManager:

def __init__(self, settings):
self.settings = settings
self.default_model_folder = self.settings.value("default_model_folder",
"")
self.default_model_folder = self.settings.value("default_model_folder","")
self.available_models = []
if self.default_model_folder:
self.scan_models()
Expand All @@ -2089,14 +2089,28 @@ def scan_models(self):
Scan the default folder and populate the available_models list.
This method scans the default_model_folder for files with the '.json'
extension
and updates the available_models list accordingly.
extension and updates the available_models list accordingly.
"""
self.available_models = []

if self.default_model_folder:
for file_name in os.listdir(self.default_model_folder):
if file_name.endswith('.json'):
self.available_models.append(file_name)
if not os.path.exists(self.default_model_folder):
# Folder is specified but does not exist
msg= f"Default 'Fit_models' folder '{self.default_model_folder}' not found. Please specify another one."
show_alert(msg)
# Reset the default model folder to empty
self.default_model_folder = ""
self.settings.setValue("default_model_folder", "")
return # Exit the method since the folder is missing

# Scan the folder for JSON files if it exists
try:
for file_name in os.listdir(self.default_model_folder):
if file_name.endswith('.json'):
self.available_models.append(file_name)
except Exception as e:
print(f"Error scanning the folder '{self.default_model_folder}': {e}")


def get_available_models(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion app/visualisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,12 @@ def load(self, file_path):
sub_window.show()

self.plot_action()

self.filter.upd_filter_listbox()
self.add_graph_list_to_combobox()

except Exception as e:
show_alert(f"Error loading work: {e}")
print(f"Error loading work: {e}")

def delete_graph(self, graph_id):
"""Delete the specified graph from the plots dictionary by graph_id"""
Expand Down

0 comments on commit 24e2151

Please sign in to comment.