Skip to content

Commit

Permalink
catch exceptions for saving files
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Colangelo committed Dec 31, 2024
1 parent 496ef19 commit 18011ee
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
85 changes: 48 additions & 37 deletions src/digest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,45 +983,56 @@ def save_reports(self):
save_directory, str(digest_model.model_name) + "_reports"
)

os.makedirs(save_directory, exist_ok=True)
try:
os.makedirs(save_directory, exist_ok=True)

# Save the node histogram image
node_histogram = current_tab.ui.opHistogramChart.grab()
node_histogram.save(
os.path.join(save_directory, f"{model_name}_histogram.png"), "PNG"
)
# Save the node histogram image
node_histogram = current_tab.ui.opHistogramChart.grab()
node_histogram.save(
os.path.join(save_directory, f"{model_name}_histogram.png"), "PNG"
)

# Save csv of node type counts
node_type_filepath = os.path.join(
save_directory, f"{model_name}_node_type_counts.csv"
)
digest_model.save_node_type_counts_csv_report(node_type_filepath)

# Save (copy) the similarity image
png_file_path = self.model_similarity_thread[
digest_model.unique_id
].png_filepath
png_save_path = os.path.join(save_directory, f"{model_name}_heatmap.png")
if png_file_path and os.path.exists(png_file_path):
shutil.copy(png_file_path, png_save_path)

# Save the text report
txt_report_filepath = os.path.join(save_directory, f"{model_name}_report.txt")
digest_model.save_text_report(txt_report_filepath)

# Save the yaml report
yaml_report_filepath = os.path.join(save_directory, f"{model_name}_report.yaml")
digest_model.save_yaml_report(yaml_report_filepath)

# Save the node list
nodes_report_filepath = os.path.join(save_directory, f"{model_name}_nodes.csv")
self.save_nodes_csv(nodes_report_filepath, False)

self.status_dialog = StatusDialog(
f"Saved reports to: \n{os.path.abspath(save_directory)}",
"Successfully saved reports!",
)
self.status_dialog.show()
# Save csv of node type counts
node_type_filepath = os.path.join(
save_directory, f"{model_name}_node_type_counts.csv"
)
digest_model.save_node_type_counts_csv_report(node_type_filepath)

# Save (copy) the similarity image
png_file_path = self.model_similarity_thread[
digest_model.unique_id
].png_filepath
png_save_path = os.path.join(save_directory, f"{model_name}_heatmap.png")
if png_file_path and os.path.exists(png_file_path):
shutil.copy(png_file_path, png_save_path)

# Save the text report
txt_report_filepath = os.path.join(
save_directory, f"{model_name}_report.txt"
)
digest_model.save_text_report(txt_report_filepath)

# Save the yaml report
yaml_report_filepath = os.path.join(
save_directory, f"{model_name}_report.yaml"
)
digest_model.save_yaml_report(yaml_report_filepath)

# Save the node list
nodes_report_filepath = os.path.join(
save_directory, f"{model_name}_nodes.csv"
)

self.save_nodes_csv(nodes_report_filepath, False)
except Exception as exception: # pylint: disable=broad-exception-caught
self.status_dialog = StatusDialog(f"{exception}")
self.status_dialog.show()
else:
self.status_dialog = StatusDialog(
f"Saved reports to: \n{os.path.abspath(save_directory)}",
"Successfully saved reports!",
)
self.status_dialog.show()

def on_dialog_closed(self):
self.infoDialog = None
Expand Down
13 changes: 9 additions & 4 deletions src/digest/model_class/digest_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ def save_nodes_csv_report(node_data: NodeData, filepath: str) -> None:
flattened_data.append(row)

fieldnames = fieldnames + input_fieldnames + output_fieldnames
with open(filepath, "w", encoding="utf-8", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, lineterminator="\n")
writer.writeheader()
writer.writerows(flattened_data)
try:
with open(filepath, "w", encoding="utf-8", newline="") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, lineterminator="\n")
writer.writeheader()
writer.writerows(flattened_data)
except PermissionError as exception:
raise PermissionError(
f"Saving reports to {filepath} failed with error {exception}"
)


def save_node_type_counts_csv_report(
Expand Down
7 changes: 5 additions & 2 deletions src/digest/multi_model_selection_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def update_list_view_items(self):

def set_directory(self, directory: str):
"""
Recursively searches a directory for onnx models.
Recursively searches a directory for onnx models and yaml report files.
"""

if not os.path.exists(directory):
Expand Down Expand Up @@ -237,9 +237,12 @@ def set_directory(self, directory: str):
break
try:
models_loaded += 1
if os.path.splitext(filepath)[-1] == ".onnx":
extension = os.path.splitext(filepath)[-1]
if extension == ".onnx":
model = onnx.load(filepath, load_external_data=False)
serialized_models_paths[model.SerializeToString()].append(filepath)
elif extension == ".yaml":
pass
dialog_msg = (
"Warning: System RAM has exceeded the threshold of "
f"{memory_limit_percentage}%. No further models will be loaded. "
Expand Down

0 comments on commit 18011ee

Please sign in to comment.