File size: 3,353 Bytes
14aae6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json


class Table:
    def __init__(self, cells: list[list]):
        self.width = max(len(row) for row in cells)
        self.height = len(cells)
        self.cells = [
            row + [''] * (self.width - len(row))
            for row in cells
        ]

    def __str__(self):
        column_widths = [
            max(
                max(map(len, str(self.cells[row][column]).split('\n')))
                for row in range(self.height)
            )
            for column in range(self.width)
        ]
        line_heights = [
            max(
                len(str(self.cells[row][column]).split('\n'))
                for column in range(self.width)
            )
            for row in range(self.height)
        ]
        cells_str = [
            [
                str(self.cells[row][column]).split('\n')
                for column in range(self.width)
            ]
            for row in range(self.height)
        ]
        for row in range(self.height):
            for column in range(self.width):
                cell_str = cells_str[row][column]
                cell_str += [''] * (line_heights[row] - len(cell_str))
                cells_str[row][column] = cell_str
        lines = []
        for row in range(self.height):
            for line in range(line_heights[row]):
                line_str = ''
                for column in range(self.width):
                    cell_str = cells_str[row][column][line]
                    line_str += cell_str + ' ' * \
                        (column_widths[column] - len(cell_str) + 2)
                lines.append(line_str)
        return '\n'.join(lines)


def analyse(setname):
    global suffs, dir, sets_info
    set_info = {
        'setname': setname,
    }
    for suff in suffs:
        with open(f'{dir}/{setname}_{suff}.json', 'r') as f:
            data = json.load(f)
        counts = {}
        for value in data.values():
            for box in value['boxes']:
                counts[box['label']] = counts.get(box['label'], 0) + 1
        set_info[suff] = {
            'counts': counts,
            'images': len(data),
        }
    sets_info.append(set_info)


suffs = ['train', 'val', 'test']
dir = 'sets'
datasets = sorted([
    name[: -len(f'_{suffs[0]}.json')]
    for name in os.listdir(dir)
    if name.endswith(f'_{suffs[0]}.json')
])
sets_info = []

for setname in datasets:
    analyse(setname)


species = set()
for set_info in sets_info:
    for suff_info in set_info.values():
        if isinstance(suff_info, dict):
            for label in suff_info['counts'].keys():
                species.add(label)
species = sorted(list(species))

for set_info in sets_info:
    for suff_info in set_info.values():
        if isinstance(suff_info, dict):
            for label in species:
                suff_info['counts'][label] = suff_info['counts'].get(
                    label, 0)


def set_table(set_info):
    global suffs, species
    table = [[suff for suff in suffs]]
    for specie in species:
        row = [set_info[suff]['counts'][specie] for suff in suffs]
        table.append(row)
    return Table(table)


table = [
    ['setname'] + [dataset['setname'] for dataset in sets_info],
    ['\n' + '\n'.join(species)] + [set_table(set_info)
                                   for set_info in sets_info],
]

print(Table(table))