-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
314 lines (286 loc) · 10.9 KB
/
functions.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
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
import numpy as np
from math import ceil
from time import time
from copy import deepcopy
from termcolor import colored
from itertools import groupby
from numpy.random import choice, randint
import matplotlib.pyplot as plt
from PIL import Image, ImageColor
import matplotlib.patches as mpatch
from ortools.linear_solver import pywraplp
import constants as cst
###############################################################################
# Auxiliary
###############################################################################
def runLength(s_list):
# https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-75.php
grp = groupby(s_list)
runL = tuple([tuple([len(list(group)), key]) for key, group in grp])
return runL
def flatten(t):
# https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists
return [item for sublist in t for item in sublist]
def isNotebook():
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True
elif shell == 'TerminalInteractiveShell':
return False
else:
return False
except NameError:
return False
def isInt(element):
try:
int(element)
return True
except ValueError:
return False
###############################################################################
# Data Preprocessing
###############################################################################
def genScrambleDicts(pDict, threshold=200):
(colDict, colDeDict, pVectors) = (
deepcopy(pDict['colorMapper']),
deepcopy(pDict['colorDeMapper']),
deepcopy(pDict['runLengthVectors'])
)
# Get quantities and splits
pSums = {i: sum(pVectors[i]) for i in pVectors.keys()}
ixsMax = max(list(colDeDict.keys()))
ovrLen = {ix: pSums[ix] for ix in list(pSums.keys())}
ixsNeed = {ix: ceil(pSums[ix]/threshold) for ix in list(ovrLen.keys())}
# Generate scrambler dictionaries
scrambler = {}
cKey = ixsMax
for ix in list(ixsNeed.keys()):
cKeyNeed = ixsNeed[ix]
if cKeyNeed > 1:
scrambler[ix] = [ix]
for ksNeed in range(cKeyNeed-1):
cKey = cKey + 1
colDeDict[cKey] = colDeDict[ix]
scrambler[ix] = scrambler[ix]+[cKey]
else:
scrambler[ix] = [ix]
colDict = {ix: tuple(set(scrambler[v]+[v])) for (ix, v) in colDict.items()}
return (colDict, colDeDict, scrambler)
def scramblePixDictUniform(pixDict, scrambler):
pixDict = tuple([
tuple([choice(scrambler[c], 1)[0] for c in row])
for row in pixDict
])
return pixDict
def pixRowSection(x, l):
for i, y in enumerate(l):
if y > x:
return i-1
return i
def scramblePixRowLengthColor(
pixArray,
scrambler, scramblerIx,
intRange=(-3, 3)
):
for (row, _) in enumerate(pixArray):
pixRow = pixArray[row]
# Generate a random shift for the intervals
(cids, clen) = (scrambler[scramblerIx], len(scrambler[scramblerIx]))
totalCount = list(pixRow).count(scramblerIx)
csamps = randint(intRange[0], intRange[1], clen)
# Get the regular intervals over replacements
pixRowL = len(pixRow)
pixDelta = [i*totalCount//clen for i in range(clen)]
# Generate shifted intervals
intervals = (csamps + pixDelta)
# Replace color in row
cCount = 0
for (ix, _) in enumerate(pixRow):
if pixRow[ix] == scramblerIx:
cCount = cCount+1
pixRow[ix] = cids[pixRowSection(cCount, intervals)]
return pixArray
def scramblePixDictLength(pixDict, scrambler, intRange=(-3, 3)):
pixArray = np.asarray(pixDict)
for cid in list(scrambler.keys()):
scramblePixRowLengthColor(pixArray, scrambler, cid, intRange=intRange)
return tuple([tuple(row) for row in pixArray])
def getVectorCounts(pixDict, dictVals):
runL = [runLength(i) for i in pixDict]
# Get flattened vectors counts ------------------------------------------------
pVectors = {}
for dix in dictVals:
pValPerRow = [[c if i == dix else 0 for (c, i) in row] for row in runL]
pValPerRow = [[c for (c, i) in row if i==dix] for row in runL]
pValPerRow = flatten(pValPerRow)
pVectors[dix] = pValPerRow
pLengths = {i: len(pVectors[i]) for i in pVectors.keys()}
return (pVectors, pLengths)
###############################################################################
# Image Preprocessing
###############################################################################
def rgbToHex(rgb):
# https://www.codespeedy.com/convert-rgb-to-hex-color-code-in-python/
return '%02x%02x%02x' % rgb
def paletteReshape(colorPalette):
# Hex to entries
rgbTuples = [ImageColor.getrgb(i) for i in colorPalette]
pal = [item for sublist in rgbTuples for item in sublist]
entries = int(len(pal)/3)
# Palette swatch
palette = pal # + [0,]*(256-entries)*3
resnp = np.arange(entries, dtype=np.uint8).reshape(entries, 1)
resim = Image.fromarray(resnp, mode='P')
resim.putpalette(palette)
# Return
return (len(pal), resim)
def quantizeImage(img, colorsNumber=255, colorPalette=None, method=0, dither=False):
if colorPalette is None:
img = img.quantize(colorsNumber, method=method, dither=dither)
else:
img = img.quantize(
palette=colorPalette, method=method, dither=dither
)
return img
###############################################################################
# Optimization
# https://developers.google.com/optimization/bin/multiple_knapsack
###############################################################################
def genSolverData(gaps, blocks, values=cst.LARGE_FIRST_BLOCK_VALUES):
data = {}
data['weights'] = blocks
data['values'] = [values[i] for i in blocks]
assert len(data['weights']) == len(data['values'])
data['num_items'] = len(data['weights'])
data['all_items'] = range(data['num_items'])
data['bin_capacities'] = gaps
data['num_bins'] = len(data['bin_capacities'])
data['all_bins'] = range(data['num_bins'])
return data
def genSolverXVector(data, solver):
# x[i, b] = 1 if item i is packed in bin b
x = {}
for i in data['all_items']:
for b in data['all_bins']:
x[i, b] = solver.BoolVar(f'x_{i}_{b}')
return (x, solver)
def setSolverConstraints(data, x, solver):
# Each item is assigned to at most one bin.
for i in data['all_items']:
solver.Add(sum(x[i, b] for b in data['all_bins']) <= 1)
# The amount packed in each bin cannot exceed its capacity.
for b in data['all_bins']:
solver.Add(
sum(x[i, b] * data['weights'][i]
for i in data['all_items']) <= data['bin_capacities'][b])
return (x, solver)
def setSolverObjective(data, x, solver):
objective = solver.Objective()
for i in data['all_items']:
for b in data['all_bins']:
objective.SetCoefficient(x[i, b], data['values'][i])
objective.SetMaximization()
return (x, solver, objective)
def convertSolution(data, x, blocks):
blocksAtBins = []
for b in data['all_bins']:
elementsInBin = []
for i in data['all_items']:
if x[i, b].solution_value() > 0:
elementsInBin.append(blocks[i])
blocksAtBins.append(elementsInBin)
return blocksAtBins
def solveColor(
gaps, blocks,
values=cst.LARGE_FIRST_BLOCK_VALUES, verbose=True
):
# Start timer and generate data structure for solver ----------------------
tic = time()
data = genSolverData(gaps, blocks, values=values)
# Setup problem -----------------------------------------------------------
solver = pywraplp.Solver.CreateSolver('SCIP')
(x, solver) = genSolverXVector(data, solver)
(x, solver) = setSolverConstraints(data, x, solver)
(x, solver, objective) = setSolverObjective(data, x, solver)
# Solve problem -----------------------------------------------------------
status = solver.Solve()
# Assemble and return solution --------------------------------------------
solution = convertSolution(data, x, blocks)
# Timing ------------------------------------------------------------------
toc = time()
rTime = (toc-tic)/60
# print(solution)
msg = lambda rTime, gLen, gNum : f"[{rTime:06.2f} mins for {gLen:04d} elements with {gNum:04d} length]"
if verbose:
(gLen, gNum) = (len(gaps), sum(gaps))
if (gNum==sum(flatten(solution))):
pCol = 'green'
else:
pCol = 'red'
# Print summary to terminal -------------------------------------------
print(colored(msg(rTime, gLen, gNum), pCol))
outDict = solution
return outDict
###############################################################################
# BOM Swatch
###############################################################################
def genColorCounts(
imgPalette, width, height, imgSize, upscale=1,
fontdict = {'family':'monospace', 'weight':'normal', 'size':30},
xlim = (0, 1.25)
):
pal = imgPalette
blocks = sum([sum(i.values()) for i in pal.values()])
# Create canvas
fig = plt.gcf()
DPI = fig.get_dpi()
ax = fig.add_axes([0, 0, 1, 1])
fig.set_size_inches(width/float(DPI), height/float(DPI))
# Setting up groups
n_groups = 1
n_rows = len(pal)//n_groups+1
# Generate swatch with count
for (j, cdt) in enumerate(sorted(list(pal.keys()))):
(wr, hr) = (.25, 1)
(color, count) = (cdt, pal[cdt])
rgb = [i/255 for i in color]
# Color rows
col_shift = (j//n_rows)*3
y_pos = (j%(n_rows))*hr
# Print rectangle and text
hshift = .05
ax.add_patch(mpatch.Rectangle(
(hshift+col_shift, y_pos), wr, hr, color=rgb, ec='k', lw=4
))
colorText = rgbToHex(color).upper()
ax.text(
hshift+wr*1.1+col_shift, y_pos+hr/2,
f' {colorText} {count} ',
color='k', va='center', ha='left', fontdict=fontdict
)
# Add pixel size and total count
pxSize = [int(i/upscale) for i in imgSize]
y_pos = ((0)%(n_rows))*hr
ax.text(
hshift, y_pos-hr/2,
f'Size: {pxSize[0]}x{pxSize[1]}',
color='k', va='center', ha='left', fontdict=fontdict
)
y_pos = ((j+1)%(n_rows))*hr
ax.text(
hshift, y_pos+hr/2,
f'Total: {blocks} blocks',
color='k', va='center', ha='left', fontdict=fontdict
)
# Clean up the axes
ax.set_xlim(xlim[0], xlim[1]*n_groups)
ax.set_ylim((n_rows), -1)
ax.axis('off')
# Return figure
return (fig, ax)
def hConcat(im1, im2):
dst = Image.new('RGB', (im1.width + im2.width, im1.height))
dst.paste(im1, (0, 0))
dst.paste(im2, (im1.width, 0))
return dst