-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerator.py
More file actions
64 lines (46 loc) · 1.95 KB
/
generator.py
File metadata and controls
64 lines (46 loc) · 1.95 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
from math import floor, log2
from random import randint, gauss
from tqdm import tqdm
import click
def generate_batch(cols, rows):
"""
Given the number of columns and rows, returns a random pixel array with 2 + (rows * cols) elements
The first two elements are the image dimensions as specified in the document
"""
def clamp(value):
return max(min(floor(value), 255), 0)
return [cols, rows] + [clamp(gauss(127, 30)) for _ in range(rows * cols)]
def solve_batch(batch):
"""
Given a list with a structure like
[cols, rows, PIXEL_1, ..., PIXEL_(ROWS*COLS)]
returns a list of pixels equalized with the given algorithm
"""
image = batch[2:]
min_pixel_value, max_pixel_value = min(image), max(image)
delta_value = max_pixel_value - min_pixel_value
shift_level = 8 - floor(log2(delta_value + 1))
def equalize(pixel):
temp_pixel = (pixel - min_pixel_value) << shift_level
return min(255, temp_pixel)
return [equalize(x) for x in image]
def generate_ram(cols, rows):
"""
Generates ram values for a random test case
"""
batch = generate_batch(cols, rows)
return [cols*rows] + batch + solve_batch(batch)
@click.command()
@click.option('--size', default=100, show_default=True, help='Number of tests to generate')
@click.option('--limit', default=128, show_default=True, help='Maximum row/col size')
def main(size, limit):
with open('ram_content.txt', 'w') as ram, open('test_values.txt', 'w') as readable:
for i in tqdm(range(size), desc='Generating tests', dynamic_ncols=True):
cols, rows = randint(1, limit), randint(1, limit)
test = generate_ram(cols, rows)
for value in test:
ram.write(f'{value}\n')
written_ram = ' '.join([str(v) for v in test])
readable.write(f'{i}) {test[1]} cols, {test[2]} rows: {test[0]} pixels \t\t RAM: {written_ram}\n')
if __name__ == '__main__':
main()