ornithoscope / info.py
gdamms's picture
More datasets
14aae6f
raw
history blame
3.35 kB
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))