Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reformat #66

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 44 additions & 35 deletions src/locpix/scripts/img_seg/membrane_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
import os
import numpy as np
from locpix.preprocessing import datastruc

# from locpix.visualise.performance import plot_pr_curve , generate_binary_conf_matrix
import locpix.evaluate.metrics as metrics
from sklearn.metrics import precision_recall_curve, auc
import polars as pl
from datetime import datetime

# import matplotlib.pyplot as plt
# from locpix.visualise import vis_img
import argparse
from locpix.scripts.img_seg import membrane_performance_config
import json
import time


def main():

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -186,40 +189,41 @@ def main():
# output_seg_imgs_test = os.path.join(
# project_folder,
# f"membrane_performance/{method}/membrane/seg_images/test/{fold}",
#)
# )
# output_seg_imgs_val = os.path.join(
# project_folder,
# f"membrane_performance/{method}/membrane/seg_images/val/{fold}",
#)
#output_train_pr = os.path.join(
# )
# output_train_pr = os.path.join(
# project_folder,
# f"membrane_performance/{method}/membrane/train_pr/{fold}",
#)
#output_test_pr = os.path.join(
# project_folder, f"membrane_performance/{method}/membrane/test_pr/{fold}"
#)
#output_val_pr = os.path.join(
# )
# output_test_pr = os.path.join(
# project_folder, f"membrane_performance/{method}/membrane/test_pr/
# {fold}"
# )
# output_val_pr = os.path.join(
# project_folder, f"membrane_performance/{method}/membrane/val_pr/{fold}"
#)
# )
output_metrics = os.path.join(
project_folder, f"membrane_performance/{method}/membrane/metrics/{fold}"
)
#output_conf_matrix = os.path.join(
# output_conf_matrix = os.path.join(
# project_folder,
# f"membrane_performance/{method}/membrane/conf_matrix/{fold}",
#)
# )

# create output folders
folders = [
output_df_folder_test,
output_df_folder_val,
# output_seg_imgs_val,
# output_seg_imgs_test,
#output_train_pr,
#output_val_pr,
#output_test_pr,
# output_train_pr,
# output_val_pr,
# output_test_pr,
output_metrics,
#output_conf_matrix,
# output_conf_matrix,
]

for folder in folders:
Expand Down Expand Up @@ -270,9 +274,9 @@ def main():
pr, rec, pr_threshold = precision_recall_curve(
gt_list, prob_list, pos_label=1
)
baseline = len(gt[gt == 1]) / len(gt)
baseline = len(gt_list[gt_list == 1]) / len(gt_list)

# pr, recall saved for train
# pr, recall saved for train
save_loc = os.path.join(output_metrics, f"train_{date}.txt")
lines = ["Overall results", "-----------"]
lines.append(f"prcurve_pr: {list(pr)}")
Expand All @@ -282,7 +286,7 @@ def main():
f.writelines("\n".join(lines))

# plot pr curve
#save_loc = os.path.join(output_train_pr, "_curve.pkl")
# save_loc = os.path.join(output_train_pr, "_curve.pkl")
# plot_pr_curve(
# ax_train,
# method.capitalize(),
Expand Down Expand Up @@ -417,7 +421,7 @@ def main():
baseline = len(gt[gt == 1]) / len(gt)

# plot pr curve
#save_loc = os.path.join(output_test_pr, "_curve.pkl")
# save_loc = os.path.join(output_test_pr, "_curve.pkl")
# plot_pr_curve(
# ax_test,
# method.capitalize(),
Expand All @@ -430,14 +434,16 @@ def main():
# # pickle=True,
# )
pr_auc = auc(rec, pr)
add_metrics = {"pr_auc": pr_auc,
"prcurve_pr": list(pr),
"prcurve_rec": list(rec),
"prcurve_baseline": baseline}
add_metrics = {
"pr_auc": pr_auc,
"prcurve_pr": list(pr),
"prcurve_rec": list(rec),
"prcurve_baseline": baseline,
}

# metric calculations based on final prediction
save_loc = os.path.join(output_metrics, f"test_{date}.txt")
agg_results = metrics.aggregated_metrics(
_ = metrics.aggregated_metrics(
output_df_folder_test,
save_loc,
gt_label_map,
Expand Down Expand Up @@ -549,10 +555,8 @@ def main():
)
baseline = len(gt[gt == 1]) / len(gt)



# plot pr curve
#save_loc = os.path.join(output_val_pr, "_curve.pkl")
# save_loc = os.path.join(output_val_pr, "_curve.pkl")
# plot_pr_curve(
# ax_val,
# method.capitalize(),
Expand All @@ -565,14 +569,16 @@ def main():
# # pickle=True,
# )
pr_auc = auc(rec, pr)
add_metrics = {"pr_auc": pr_auc,
"prcurve_pr": list(pr),
"prcurve_rec": list(rec),
"prcurve_baseline": baseline}
add_metrics = {
"pr_auc": pr_auc,
"prcurve_pr": list(pr),
"prcurve_rec": list(rec),
"prcurve_baseline": baseline,
}

# metric calculations based on final prediction
save_loc = os.path.join(output_metrics, f"val_{date}.txt")
agg_results =metrics.aggregated_metrics(
_ = metrics.aggregated_metrics(
output_df_folder_val,
save_loc,
gt_label_map,
Expand Down Expand Up @@ -602,9 +608,12 @@ def main():
# ax_test.legend([handles_test[idx] for idx in order],
# [methods[idx] for idx in order])

# fig_train.savefig(os.path.join(output_overlay_pr_curves, "_train.png"), dpi=600)
# fig_test.savefig(os.path.join(output_overlay_pr_curves, "_test.png"), dpi=600)
# fig_val.savefig(os.path.join(output_overlay_pr_curves, "_val.png"), dpi=600)
# fig_train.savefig(os.path.join(output_overlay_pr_curves,
# "_train.png"), dpi=600)
# fig_test.savefig(os.path.join(output_overlay_pr_curves,
# "_test.png"), dpi=600)
# fig_val.savefig(os.path.join(output_overlay_pr_curves,
# "_val.png"), dpi=600)

# save yaml file
yaml_save_loc = os.path.join(project_folder, "membrane_performance.yaml")
Expand Down
Loading