-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample9.py
149 lines (114 loc) · 4.55 KB
/
sample9.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
import ctypes
import random
from timeit import default_timer as timer
import numpy as np
from adabox.plot_tools import plot_rectangles, plot_rectangles_only_lines
from adabox.tools import Rectangle
def slice_rectangle(rec_to_slice, slices_arg):
a = rec_to_slice[1] - rec_to_slice[0]
b = rec_to_slice[3] - rec_to_slice[2]
if a > b:
side = a
min_coord = 0
max_coord = 1
else:
side = b
min_coord = 2
max_coord = 3
sep = int((side / slices_arg))
reference = rec_to_slice[min_coord]
sliced_recs = []
for i in range(slices_arg - 1):
nr = rec_to_slice.copy()
nr[min_coord] = reference
nr[max_coord] = reference + sep - 1
reference = reference + sep
sliced_recs.append(nr)
nr = rec_to_slice.copy()
nr[min_coord] = reference
nr[max_coord] = rec_to_slice[max_coord]
sliced_recs.append(nr)
sliced_areas = list(map(lambda r: ((r[1] - r[0]) * (r[3] - r[2])), sliced_recs))
sliced_ab_ratio = list(map(lambda r: ((r[1] - r[0]) / (r[3] - r[2])), sliced_recs))
return sliced_recs, sliced_areas, sliced_ab_ratio
def find_a_rectangle(point, data_binary_matrix, so_lib):
c_int_p = ctypes.POINTER(ctypes.c_int)
c_double_p = ctypes.POINTER(ctypes.c_double)
data_matrix_ptr = data_binary_matrix.ctypes.data_as(c_int_p)
out = np.array([0, 0, 0, 0]).astype(np.intc)
out_ptr = out.ctypes.data_as(c_int_p)
area = np.array([0]).astype(np.intc)
area_ptr = area.ctypes.data_as(c_int_p)
ab_ratio = np.array([0]).astype(np.float64)
ab_ratio_ptr = ab_ratio.ctypes.data_as(c_double_p)
idx_var = int(point[0])
idj_var = int(point[1])
m = data_binary_matrix.shape[0]
n = data_binary_matrix.shape[1]
so_lib.find_largest_rectangle(idx_var, idj_var, m, n, data_matrix_ptr, out_ptr, area_ptr, ab_ratio_ptr)
return out, area, ab_ratio
def remove_rectangle_from_matrix(rec_to_remove, data_binary_matrix):
x1 = rec_to_remove[0]
y1 = rec_to_remove[1]
x2 = rec_to_remove[2]
y2 = rec_to_remove[3]
data_binary_matrix[x2:y2 + 1, x1:y1 + 1] = 0
def find_rectangles_and_filter_the_best(random_points_arg, data_matrix_arg, lib_arg):
results = []
for rp in random_points_arg:
rec_out, rec_area_out, ab_ratio_out = find_a_rectangle(rp, data_matrix_arg, lib_arg)
results.append([rec_out, rec_area_out, ab_ratio_out])
# conditions
results_array_area = np.array(results, dtype=object)[:, 1]
result = results[results_array_area.argmax()]
return result[0], result[1], result[2]
so_file = "/Users/kolibri/PycharmProjects/adaptive-boxes/adabox/decomposition/cpp/getters_completed.so"
getters_so_lib = ctypes.CDLL(so_file)
# Input Path
in_path = '/Users/kolibri/PycharmProjects/adaptive-boxes/sample_data/humboldt_binary_matrix.csv'
# Load Demo data with columns [x_position y_position flag]
data_matrix = np.loadtxt(in_path, delimiter=",")
data_matrix = data_matrix.astype(np.intc)
total_area = data_matrix.sum()
n_gpus = 20
max_area = total_area / n_gpus
# Plot demo data
# plt.imshow(np.flip(data_matrix, axis=0), cmap='magma', interpolation='nearest')
# search rectangle
coords = np.argwhere(data_matrix == 1)
recs = []
areas = []
ab_ratios = []
start = timer()
while coords.shape[0] != 0:
n_searches = 100
random_points = random.choices(coords, k=n_searches)
rec, rec_area, ab_ratio = find_rectangles_and_filter_the_best(random_points, data_matrix, getters_so_lib)
remove_rectangle_from_matrix(rec, data_matrix)
coords = np.argwhere(data_matrix == 1)
if rec_area[0] >= max_area:
slices = int(np.ceil(rec_area / max_area)[0])
s_recs, s_areas, s_ab_ratios = slice_rectangle(rec, slices)
recs.extend(s_recs)
areas.extend(s_areas)
ab_ratios.extend(s_ab_ratios)
else:
recs.append(rec)
areas.append(rec_area)
ab_ratios.append(ab_ratio)
end = timer()
print("elapsed time " + str(end - start) + "seconds")
# Plotting
rectangles_list = list(map(lambda x: Rectangle(x[0], x[1], x[2], x[3]), recs))
plot_rectangles(rectangles_list, 1)
plot_rectangles_only_lines(rectangles_list, 1)
# Export data
best_set = rectangles_list
array_to_save = np.zeros(shape=[len(best_set), 4])
for x in range(len(best_set)):
array_to_save[x, 0] = best_set[x].x1
array_to_save[x, 1] = best_set[x].x2
array_to_save[x, 2] = best_set[x].y1
array_to_save[x, 3] = best_set[x].y2
np.savetxt('/Users/kolibri/PycharmProjects/adaptive-boxes/adabox/decomposition/samples/decomposition_n_20.csv',
np.array(recs), fmt='%s', delimiter=',')