forked from planktivore/SPCConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddPredictions.py
More file actions
93 lines (73 loc) · 2.38 KB
/
Copy pathaddPredictions.py
File metadata and controls
93 lines (73 loc) · 2.38 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
import pandas as pd
import sys
import math
import pystache
import numpy as np
import os
import json
# Project Level Imports
from utils.db_utils import to_json_format, loadDB
# Annotation
# getting predicted labels on the static page
def updateDB(db_path, json_path):
# load in database file
entries = loadDB(db_path)
# load prediction data
prediction_df = pd.read_json(json_path)
# create a dict of all labels, and URLs
url_to_label = {}
for i in range(len(prediction_df['machine_labels'])):
data = {}
url = prediction_df['machine_labels'].iloc[i]['image_id']
data['pred'] = prediction_df['machine_labels'].iloc[i]['pred']
data['prob'] = prediction_df['machine_labels'].iloc[i]['prob']
url_to_label[url] = data
# update values in the list
for entry in entries:
filename = entry['url'][13:]
filename = filename[:len(filename) - 5]
filename = filename + ".tif"
entry["pred"] = url_to_label[filename]['pred']
entry["prob_non_proro"] = url_to_label[filename]['prob'][0]
entry["prob_proro"] = url_to_label[filename]['prob'][1]
entry["gtruth"] = url_to_label[filename]['pred']
# write a new db from the db template with the updated list
db_str = json.dumps(entries)
db_str = "roistore = TAFFY(" + db_str + ");"
with open(db_path, "w") as fconv:
fconv.write(db_str)
# Visualisation
# go through prediction data and count the classes
def count_pred(json_path):
prediction_df = pd.read_json(json_path)
num_pred_1 = 0
num_pred_0 = 0
for i in range(len(prediction_df['machine_labels'])):
if prediction_df['machine_labels'].iloc[i]['pred'] == 1:
num_pred_1 += 1
else:
num_pred_0 += 1
return (num_pred_0, num_pred_1)
# re-render the html with the data
def update_preds(html_path, json_path):
context = {}
template = ""
with open(html_path,"r") as fconv:
template = fconv.read()
context['num_pred_0'], context['num_pred_1'] = count_pred(json_path)
# render the html page and save to disk
page = pystache.render(template,context)
with open(html_path,"w") as fconv:
fconv.write(page)
# Entry point
if __name__ == '__main__':
if len(sys.argv) <= 3:
print ("Please input the path to the current html, the predictions json, and path to the db, aborting.")
elif len(sys.argv) <= 4:
html_path = sys.argv[1]
json_path = sys.argv[2]
db_path = sys.argv[3]
update_preds(html_path, json_path)
updateDB(db_path, json_path)
else:
print("too many arguments, aborting")