-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstatistics.py
More file actions
360 lines (334 loc) · 18.5 KB
/
statistics.py
File metadata and controls
360 lines (334 loc) · 18.5 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""
Make statistics on Chinese and English tables and draw corresponding histograms.
include:table number,table row,table column,cell number,cell start row,cell end row
cell start column,cell end column,cell rowspan,cell colspan.
Besides, can also calculate the maximum, minimum, mean, and standard deviation
of each parameter
"""
import json
import os
import glob
from tqdm import tqdm
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
if __name__ == '__main__':
path = "X:/LAB/dataset/CurveTabSet/chinese_no-line/TSR_TCR_annotation/"
path_2 = "X:/LAB/dataset/CurveTabSet/english_no-line/TSR_TCR_annotation/"
a = glob.glob(path + "*.json")
a_2 = glob.glob(path_2 + "*.json")
b = 0
b_2 = 0
table_nums = [] # 表格数量
row_nums = [] # 行数
col_nums = [] # 列数
cell_nums = [] # 单元格数
start_row = []
end_row = []
start_column = []
end_column = []
rows_nums = [] # 跨行数
cols_nums = [] # 跨列数
table_nums_2 = [] # 表格数量
row_nums_2 = [] # 行数
col_nums_2 = [] # 列数
cell_nums_2 = [] # 单元格数
start_row_2 = []
end_row_2 = []
start_column_2 = []
end_column_2 = []
rows_nums_2 = [] # 跨行数
cols_nums_2 = [] # 跨列数
print("中文图片数量:" + str(len(a)))
print("英文图片数量:" + str(len(a_2)))
for name in tqdm(a, desc="chinese_statistics"):
f_name, ext = os.path.splitext(name)
base_name = os.path.basename(f_name)
img_name = base_name + ".json"
try:
table_num = 1
with open(path + img_name, encoding='utf-8') as file:
dic = json.load(file)
k = len(dic["shapes"]) # 获取单元格数目
for i in range(0, k):
start_row.append(int(dic["shapes"][i]["label"].split('-')[0]))
end_row.append(
int(dic["shapes"][i]["label"].split('-')[0]) + int(dic["shapes"][i]["label"].split('-')[2]) - 1)
start_column.append(int(dic["shapes"][i]["label"].split('-')[1]))
end_column.append(
int(dic["shapes"][i]["label"].split('-')[1]) + int(dic["shapes"][i]["label"].split('-')[3]) - 1)
rows_nums.append(int(dic["shapes"][i]["label"].split('-')[2]))
cols_nums.append(int(dic["shapes"][i]["label"].split('-')[3]))
polygons = dic["shapes"]
polygons_groupBy_groupID = {}
for poly_ in polygons:
key_ = poly_["group_id"]
if polygons_groupBy_groupID.get(key_) is None:
polygons_groupBy_groupID[key_] = []
polygons_groupBy_groupID[key_].append(poly_)
table_nums.append(len(polygons_groupBy_groupID)) # 表格数
for group_id, polygons in polygons_groupBy_groupID.items():
cell_nums.append(len(polygons))
row_num = 1
col_num = 1
for polygon_ in polygons:
if int(polygon_["label"].split('-')[0]) > row_num:
row_num = int(polygon_["label"].split('-')[0])
if int(polygon_["label"].split('-')[1]) > col_num:
col_num = int(polygon_["label"].split('-')[1])
row_nums.append(row_num)
col_nums.append(col_num)
b += 1
except Exception:
print(base_name)
for name in tqdm(a_2, desc="english_statistics"):
f_name, ext = os.path.splitext(name)
base_name = os.path.basename(f_name)
img_name = base_name + ".json"
try:
table_num = 1
with open(path_2 + img_name, encoding='utf-8') as file:
dic = json.load(file)
k = len(dic["shapes"]) # 获取单元格数目
for i in range(0, k):
start_row_2.append(int(dic["shapes"][i]["label"].split('-')[0]))
end_row_2.append(
int(dic["shapes"][i]["label"].split('-')[0]) + int(dic["shapes"][i]["label"].split('-')[2]) - 1)
start_column_2.append(int(dic["shapes"][i]["label"].split('-')[1]))
end_column_2.append(
int(dic["shapes"][i]["label"].split('-')[1]) + int(dic["shapes"][i]["label"].split('-')[3]) - 1)
rows_nums.append(int(dic["shapes"][i]["label"].split('-')[2]))
cols_nums.append(int(dic["shapes"][i]["label"].split('-')[3]))
polygons = dic["shapes"]
polygons_groupBy_groupID = {}
for poly_ in polygons:
key_ = poly_["group_id"]
if polygons_groupBy_groupID.get(key_) is None:
polygons_groupBy_groupID[key_] = []
polygons_groupBy_groupID[key_].append(poly_)
table_nums.append(len(polygons_groupBy_groupID)) # 表格数
for group_id, polygons in polygons_groupBy_groupID.items():
cell_nums.append(len(polygons))
row_num = 1
col_num = 1
for polygon_ in polygons:
if int(polygon_["label"].split('-')[0]) > row_num:
row_num = int(polygon_["label"].split('-')[0])
if int(polygon_["label"].split('-')[1]) > col_num:
col_num = int(polygon_["label"].split('-')[1])
row_nums_2.append(row_num)
col_nums_2.append(col_num)
b_2 += 1
except Exception:
print(base_name)
print("中文表格数量:" + str(b))
print("英文表格数量:" + str(b_2))
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['figure.figsize'] = (12, 12)
mpl.rcParams["font.size"] = 65
######################### 表格数 #######5################
plt.hist(table_nums, bins=7,
rwidth=0.8,
range=(1, 8),
align='left', alpha=0.5, stacked=True, log=True)
# for i in range(len(n1)):
# plt.text(bins[i], n1[i] * 1.01, int(n1[i]), fontsize=12, horizontalalignment="center")
# for i in range(len(n2)):
# plt.text(bins[i], n2[i] * 1.01, int(n2[i]), fontsize=12, horizontalalignment="center")
plt.xlabel('table number')
plt.ylabel('number of picture')
plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
plt.yticks([10, 100, 1000, 10000])
plt.xticks(range(8))
ax = plt.gca() # 获得坐标轴的句柄
ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文表格数:%f %f %f %f" % (max(table_nums), min(table_nums), np.mean(table_nums), np.std(table_nums, ddof=1)))
# print("英文表格数:%f %f %f %f" % (
# max(table_nums_2), min(table_nums_2), np.mean(table_nums_2), np.std(table_nums_2, ddof=1)))
plt.tight_layout() # 解决字体遮挡
plt.savefig("table number.png", bbox_inches="tight")
plt.show()
######################### 行数 #######################
# plt.hist([row_nums, row_nums_2], bins=7, rwidth=0.8,
# range=(1, 52),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('row number')
# plt.ylabel('number of table')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,10,20,30,40,50])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文行数:%f %f %f %f" % (max(row_nums), min(row_nums), np.mean(row_nums), np.std(row_nums, ddof=1)))
# print("英文行数:%f %f %f %f" % (
# max(row_nums_2), min(row_nums_2), np.mean(row_nums_2), np.std(row_nums_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("table row.png", bbox_inches="tight")
# plt.show()
######################### 列数 #######################
# plt.hist([col_nums, col_nums_2], bins=7, rwidth=0.8,
# range=(1, 28),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('column number')
# plt.ylabel('number of table')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,5,10,15,20,25])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文列数:%f %f %f %f" % (max(col_nums), min(col_nums), np.mean(col_nums), np.std(col_nums, ddof=1)))
# print("英文列数:%f %f %f %f" % (
# max(col_nums_2), min(col_nums_2), np.mean(col_nums_2), np.std(col_nums_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("table column.png", bbox_inches="tight")
# plt.show()
######################### 单元格数 #######################
# plt.hist(cell_nums, bins=7, rwidth=0.8,
# range=(1, 351),
# align='left', alpha=0.5,stacked=True, log=True)
# plt.xlabel('cell number')
# plt.ylabel('number of table')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks([10,100,1000,10000])
# plt.xticks([1,100,200,300])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# # print("中文单元格数:%f %f %f %f" % (max(cell_nums), min(cell_nums), np.mean(cell_nums), np.std(cell_nums, ddof=1)))
# # print("英文单元格数:%f %f %f %f" % (
# # max(cell_nums_2), min(cell_nums_2), np.mean(cell_nums_2), np.std(cell_nums_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell number.png", bbox_inches="tight")
# plt.show()
######################### 起始行 #######################
# plt.hist([start_row, start_row_2], bins=7, rwidth=0.8,
# range=(1, 52),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('start row number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,10,20,30,40,50])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文起始行:%f %f %f %f" % (max(start_row), min(start_row), np.mean(start_row), np.std(start_row, ddof=1)))
# print("英文起始行:%f %f %f %f" % (
# max(start_row_2), min(start_row_2), np.mean(start_row_2), np.std(start_row_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell start row.png", bbox_inches="tight")
# plt.show()
######################### 结束行 #######################
# plt.hist([end_row, end_row_2], bins=7, rwidth=0.8,
# range=(1, 52),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('end row number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,10,20,30,40,50])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文结束行:%f %f %f %f" % (max(end_row), min(end_row), np.mean(end_row), np.std(end_row, ddof=1)))
# print("英文结束行:%f %f %f %f" % (
# max(end_row_2), min(end_row_2), np.mean(end_row_2), np.std(end_row_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell end row.png", bbox_inches="tight")
# plt.show()
######################### 起始列 #######################
# plt.hist([start_column, start_column_2], bins=7, rwidth=0.8,
# range=(1, 28),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('start column number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,5,10,15,20,25])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文起始列:%f %f %f %f" % (
# max(start_column), min(start_column), np.mean(start_column), np.std(start_column, ddof=1)))
# print("英文起始列:%f %f %f %f" % (
# max(start_column_2), min(start_column_2), np.mean(start_column_2), np.std(start_column_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell start column.png", bbox_inches="tight")
# plt.show()
######################### 结束列 #######################
# plt.hist([end_column, end_column_2], bins=7, rwidth=0.8,
# range=(1, 28),
# align='left', alpha=0.5, label=['chinese', 'english'], stacked=True, log=True)
# plt.xlabel('end column number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,5,10,15,20,25])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# print("中文结束列:%f %f %f %f" % (max(end_column), min(end_column), np.mean(end_column), np.std(end_column, ddof=1)))
# print("英文结束列:%f %f %f %f" % (
# max(end_column_2), min(end_column_2), np.mean(end_column_2), np.std(end_column_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell end column.png", bbox_inches="tight")
# plt.show()
######################### 跨行 #######################
# plt.hist(rows_nums, bins=7, rwidth=0.8,
# range=(1, 42),
# align='left', alpha=0.5, stacked=True, log=True)
# plt.xlabel('rowspan number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks()
# plt.xticks([1,10,20,30,40])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# # print("中文跨行:%f %f %f %f" % (max(rows_nums), min(rows_nums), np.mean(rows_nums), np.std(rows_nums, ddof=1)))
# # print("英文跨行:%f %f %f %f" % (
# # max(rows_nums_2), min(rows_nums_2), np.mean(rows_nums_2), np.std(rows_nums_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell rowspan.png", bbox_inches="tight")
# plt.show()
######################### 跨列 #######################
# plt.hist(cols_nums, bins=7, rwidth=0.8,
# range=(1, 25),
# align='left', alpha=0.5, stacked=True, log=True)
# plt.xlabel('colspan number')
# plt.ylabel('number of cell')
# plt.legend(loc='upper right', frameon=False, borderpad=-0.4, labelspacing=-0.1, handlelength=1, handletextpad=0.1)
# plt.yticks([10,1000,100000])
# plt.xticks([1,5,10,15,20])
# ax = plt.gca() # 获得坐标轴的句柄
# ax.spines['bottom'].set_linewidth(3) ###设置底部坐标轴的粗细
# ax.spines['left'].set_linewidth(3) ####设置左边坐标轴的粗细
# ax.spines['right'].set_linewidth(3) ###设置右边坐标轴的粗细
# ax.spines['top'].set_linewidth(3) ####设置上部坐标轴的粗细
# # print("中文跨列:%f %f %f %f" % (max(cols_nums), min(cols_nums), np.mean(cols_nums), np.std(cols_nums, ddof=1)))
# # print("英文跨列:%f %f %f %f" % (
# # max(cols_nums_2), min(cols_nums_2), np.mean(cols_nums_2), np.std(cols_nums_2, ddof=1)))
# plt.tight_layout() # 解决字体遮挡
# plt.savefig("cell colspan.png", bbox_inches="tight")
# plt.show()