|
a |
|
b/dataprocess/xmlopt.py |
|
|
1 |
''' |
|
|
2 |
Created by WangQL |
|
|
3 |
|
|
|
4 |
6/27/2018 |
|
|
5 |
|
|
|
6 |
operation for xml files |
|
|
7 |
get structure of xml files from LIDC |
|
|
8 |
''' |
|
|
9 |
try: |
|
|
10 |
import xml.etree.cElementTree as ET |
|
|
11 |
except ImportError: |
|
|
12 |
import xml.etree.ElementTree as ET |
|
|
13 |
import sys,os |
|
|
14 |
import matplotlib.pyplot as plt |
|
|
15 |
import scipy.misc |
|
|
16 |
import numpy as np |
|
|
17 |
|
|
|
18 |
def getEdgeMap(file, zloc, nodule_list): |
|
|
19 |
|
|
|
20 |
tree = ET.parse(file) |
|
|
21 |
root = tree.getroot() |
|
|
22 |
|
|
|
23 |
try: |
|
|
24 |
rangelist = [] |
|
|
25 |
Img=np.zeros((512,512)) |
|
|
26 |
|
|
|
27 |
for onenoduleid in nodule_list: |
|
|
28 |
rangelist, Img = getEdgeMap_id(root, onenoduleid, zloc) |
|
|
29 |
if rangelist != None: |
|
|
30 |
break |
|
|
31 |
|
|
|
32 |
# scipy.misc.imsave(str(j) + '.png', Img) |
|
|
33 |
# print('flag: ', flag) |
|
|
34 |
signtemp = False |
|
|
35 |
|
|
|
36 |
lnglist = [] |
|
|
37 |
latlist = [] |
|
|
38 |
for i in range(len(rangelist)-1): |
|
|
39 |
lnglist.append(rangelist[i][0]) |
|
|
40 |
latlist.append(rangelist[i][1]) |
|
|
41 |
|
|
|
42 |
# print(nodule_list[0]) |
|
|
43 |
# print(lnglist) |
|
|
44 |
# print(latlist) |
|
|
45 |
maxlng = max(lnglist) |
|
|
46 |
minlng = min(lnglist) |
|
|
47 |
maxlat = max(latlist) |
|
|
48 |
minlat = min(latlist) |
|
|
49 |
|
|
|
50 |
for a in range(minlng, maxlng): |
|
|
51 |
for b in range(minlat, maxlat): |
|
|
52 |
point = [a, b] |
|
|
53 |
if isPointinPolygon(point, rangelist, maxlng, minlng, maxlat, minlat): |
|
|
54 |
Img[b][a] = 1 |
|
|
55 |
except: |
|
|
56 |
signtemp = True |
|
|
57 |
|
|
|
58 |
return Img, signtemp |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
def getEdgeMap_id(root, nodule_list, zloc): |
|
|
62 |
flag = True |
|
|
63 |
j=0 |
|
|
64 |
coo2=[] |
|
|
65 |
sign = 0 |
|
|
66 |
|
|
|
67 |
rangelist = [] |
|
|
68 |
flag = True |
|
|
69 |
Img=np.zeros((512,512)) |
|
|
70 |
for ResponseHeader in root: |
|
|
71 |
# print('child') |
|
|
72 |
# print(ResponseHeader.tag) |
|
|
73 |
if not flag: |
|
|
74 |
break |
|
|
75 |
for readingSession in ResponseHeader: |
|
|
76 |
if not flag: |
|
|
77 |
break |
|
|
78 |
# print(' ',readingSession.tag) |
|
|
79 |
for unblindedReadNodule in readingSession: |
|
|
80 |
if not flag: |
|
|
81 |
break |
|
|
82 |
# print(' ',unblindedReadNodule.tag) |
|
|
83 |
if unblindedReadNodule.tag == '{http://www.nih.gov}noduleID': |
|
|
84 |
id = unblindedReadNodule.text.strip() |
|
|
85 |
try: |
|
|
86 |
input_id = int(nodule_list) |
|
|
87 |
id = int(id) |
|
|
88 |
except: |
|
|
89 |
input_id = str(nodule_list) |
|
|
90 |
id = str(id) |
|
|
91 |
# print(id_i, id) |
|
|
92 |
if id == input_id: |
|
|
93 |
# print('here here') |
|
|
94 |
for unblindedReadNodule in readingSession: |
|
|
95 |
if unblindedReadNodule.tag=='{http://www.nih.gov}roi': |
|
|
96 |
coo=[] |
|
|
97 |
hoo=[] |
|
|
98 |
hoo1=[] |
|
|
99 |
j+=1 |
|
|
100 |
for roi in unblindedReadNodule: |
|
|
101 |
if not flag: |
|
|
102 |
break |
|
|
103 |
if roi.tag == '{http://www.nih.gov}imageZposition': |
|
|
104 |
if abs(float(zloc) - float(roi.text)) < 0.001: |
|
|
105 |
# existsign = True |
|
|
106 |
# print("float(zloc)") |
|
|
107 |
# print(float(roi.text)) |
|
|
108 |
for roi in unblindedReadNodule: |
|
|
109 |
if roi.tag=='{http://www.nih.gov}edgeMap': |
|
|
110 |
n=0 |
|
|
111 |
coo1=[] |
|
|
112 |
for edgeMap in roi: |
|
|
113 |
if n: |
|
|
114 |
hoo.append(int(edgeMap.text)) |
|
|
115 |
else: |
|
|
116 |
hoo1.append(int(edgeMap.text)) |
|
|
117 |
n=1 |
|
|
118 |
coo1.append(int(edgeMap.text)) |
|
|
119 |
coo.append(coo1) |
|
|
120 |
coo2.append(coo1) |
|
|
121 |
for i in range(len(coo)): |
|
|
122 |
Img[coo[i][1]][coo[i][0]]=1 |
|
|
123 |
flag = False |
|
|
124 |
rangelist = coo |
|
|
125 |
return rangelist, Img |
|
|
126 |
|
|
|
127 |
def isPointinPolygon(point, rangelist, maxlng, minlng, maxlat, minlat): #[[0,0],[1,1],[0,1],[0,0]] [1,0.8] |
|
|
128 |
|
|
|
129 |
if (point[0] > maxlng or point[0] < minlng or |
|
|
130 |
point[1] > maxlat or point[1] < minlat): |
|
|
131 |
return False |
|
|
132 |
count = 0 |
|
|
133 |
point1 = rangelist[0] |
|
|
134 |
for i in range(1, len(rangelist)): |
|
|
135 |
point2 = rangelist[i] |
|
|
136 |
if (point[0] == point1[0] and point[1] == point1[1]) or (point[0] == point2[0] and point[1] == point2[1]): |
|
|
137 |
return False |
|
|
138 |
if (point1[1] < point[1] and point2[1] >= point[1]) or (point1[1] >= point[1] and point2[1] < point[1]): |
|
|
139 |
point12lng = point2[0] - (point2[1] - point[1]) * (point2[0] - point1[0])/(point2[1] - point1[1]) |
|
|
140 |
if (point12lng == point[0]): |
|
|
141 |
return False |
|
|
142 |
if (point12lng < point[0]): |
|
|
143 |
count +=1 |
|
|
144 |
point1 = point2 |
|
|
145 |
if count%2 == 0: |
|
|
146 |
return False |
|
|
147 |
else: |
|
|
148 |
return True |