-
Notifications
You must be signed in to change notification settings - Fork 2
/
results.py
113 lines (89 loc) · 3.34 KB
/
results.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from config import ANATOMICAL_LANDMARKS, CVM_STAGES
import time
import json
import os
def format_landmarks(
ceph_id: str,
landmarks: list,
) -> dict:
data = dict()
data["ceph_id"] = ceph_id
data["landmarks"] = []
for index, landmark in enumerate(ANATOMICAL_LANDMARKS.items()):
landmark_id = landmark[0]
info = landmark[1]
data["landmarks"].append(
{
"landmark_id": landmark_id,
"title": info["title"],
"symbol": info["symbol"],
"value": {
"x": round(landmarks[index][0]),
"y": round(landmarks[index][1])
}
}
)
data["dataset_name"] = "CEPHA29: Cephalometric Landmark Detection Dataset"
data["dataset_split"] = "Validation"
data["created_at"] = time.strftime("%Y-%m-%d %H:%M:%S.000000")
return data
def format_cvm_stage(
ceph_id: str,
cvm_stage: int
) -> dict:
data = dict()
data["ceph_id"] = ceph_id
key = [key for key, value in CVM_STAGES.items() if value["value"] == cvm_stage][0]
data["cvm_stage"] = {
"id": key,
"title": CVM_STAGES[key]["title"],
"value": CVM_STAGES[key]["value"]
}
data["dataset_name"] = "CEPHA29: Cephalometric Landmark Detection Dataset"
data["dataset_split"] = "Validation"
data["created_at"] = time.strftime("%Y-%m-%d %H:%M:%S.000000")
return data
def save_landmarks_results(
folder_path: str,
ceph_id: str,
landmarks: list
) -> None:
"""
Saves the predicted values of landmarks in .json format.
Args:
folder_path: Relative path of the directory where to save the results.
e.g. `./cephalometric-landmarks-detection/results/valid/Cephalometric Landmarks`
ceph_id: Name/ID of the cephalogram without extension.
e.g. `cks2ip8fq2a0o0yufhfuo7clg`
landmarks: List/Numpy array of shape (29, 2) of predicted landmarks.
"""
data = format_landmarks(ceph_id, landmarks)
file_name = ceph_id + "." + "json"
with open(os.path.join(folder_path, file_name), "w") as file:
json.dump(data, file)
def save_cvm_results(
folder_path: str,
ceph_id: str,
cvm_stage: int
) -> None:
"""
Saves the predicted values of landmarks in .json format.
Args:
folder_path: Relative path of the directory where to save the results.
e.g. `./cephalometric-landmarks-detection/results/valid/CVM Stages`
ceph_id: Name/ID of the cephalogram without extension.
e.g. `cks2ip8fq2a0o0yufhfuo7clg`
cvm_stage: Predicted CVM Stage.
e.g. `1 or 2 or 5`
"""
data = format_cvm_stage(ceph_id, cvm_stage)
file_name = ceph_id + "." + "json"
with open(os.path.join(folder_path, file_name), "w") as file:
json.dump(data, file)
if __name__ == "__main__":
import numpy as np
import os
landmarks = np.random.randint(0, 2000, size=(29, 2)) # Your Model's Predicted Landmarks
cvm_stage = np.random.randint(0, 5) # Your Model's predicted CVM Stage
save_landmarks_results(folder_path="./results/valid/Cephalometric Landmarks", ceph_id="cks2ip8fq2a0o0yufhfuo7clg", landmarks=landmarks)
save_cvm_results(folder_path="./results/valid/CVM Stages", ceph_id="cks2ip8fq2a0o0yufhfuo7clg", cvm_stage=cvm_stage)