-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
108 lines (85 loc) · 2.71 KB
/
helper.py
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
import logging
from pprint import pprint
import numpy as np
from collections import defaultdict
from typing import Dict, Any, Tuple
import numpy as np
import matplotlib.pyplot as plt
from twimage import Point, heatmap, matrix2dict, get_shape
import settings
_log = logging.getLogger(__name__)
def create_blocks():
# block = np.ones((3, 3))
# block[0, 0] = 0
# block[2, 1] = 0
# block = np.array(
# [
# [1, 1, 0],
# [0, 1, 1],
# [0, 1, 0],
# ]
# )
block = np.array(
[
[1, 0],
[1, 0],
[1, 1],
[0, 1],
]
)
print(matrix2dict(block))
plt.imshow(block)
plt.show()
class Ok(object):
"""Location where a block can be put within grid
(k,i,j)
"""
def __init__(self, config: Dict) -> None:
super().__init__()
self.config = config
self.dimension = config['dimension']
self.blocks = config['blocks']
# self.block_shapes = config['block_shapes']
self.ok = defaultdict(lambda: 0)
self.create_ok()
def create_ok(self) -> None:
for k, block in self.blocks.items():
for i in range(1, self.dimension + 1):
n, m = get_shape(block)
if n > self.dimension + 1 - i:
continue
for j in range(1, self.dimension + 1):
if m > self.dimension + 1 - j:
continue
self.ok[k, i, j] = 1
_ = None
_log.info(f"Oks created: {len(self.ok)}")
class Cover(object):
"""Location where a block can be put within grid
(k,i,j, ii, jj)
"""
def __init__(self, config: Dict) -> None:
super().__init__()
self.config = config
self.ok = Ok(config=config).ok
self.dimension = config['dimension']
self.blocks = config['blocks']
# self.block_shapes = config['block_shapes']
self.cover = defaultdict(lambda: 0)
self.create_cover()
def create_cover(self) -> None:
for (k, i, j) in self.ok:
block = self.blocks[k]
for (ii, jj), v in block.items():
if v == 0:
continue
self.cover[k, i, j, i + ii, j + jj] = 1
_log.info(f"Covers created: {len(self.cover)}")
if __name__ == '__main__':
log_fmt = r'%(asctime)-15s %(levelname)s %(name)s %(funcName)s:%(lineno)d %(message)s'
logging.basicConfig(format=log_fmt, level=logging.DEBUG)
logging.getLogger('matplotlib').setLevel(logging.INFO)
create_blocks()
# name = 'test'
# # ok = Ok(config=getattr(settings, name))
# cover = Cover(config=getattr(settings, name))