Matplotlib profiling of MBC.

This commit is contained in:
Casper 2018-10-21 16:59:17 +02:00
parent d4b79674ae
commit b6dadc7789
No known key found for this signature in database
GPG Key ID: B1156723DB3BDDA8
3 changed files with 85 additions and 63 deletions

View File

@ -14,7 +14,6 @@ def sidedness(slope: float, intersection: float, p3: Point, flipper: callable, e
return Side.BELOW
@Profiler("solving 1D LP")
def solve_1dlp(c, constraints):
c1, c2 = c
((a1, a2), b) = constraints[-1]
@ -36,7 +35,7 @@ def solve_1dlp(c, constraints):
return interval[1], q - (p * interval[1])
@Profiler("solving 2D LP")
@Profiler("solving LP")
def solve_2dlp(c, constraints):
c1, c2 = c
x1 = -10_000 if c1 > 0 else 10_000
@ -88,7 +87,8 @@ def mbc_ch(points: Set[Point], flipper: callable, extra_prune=False, shuffle=Tru
pr = {p for p in points if p.x >= med_x}
# Shuffle
constraints = [((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]
with Profiler("flipping constraints"):
constraints = [((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]
if shuffle:
with Profiler("shuffling constraints"):
random.shuffle(constraints)

File diff suppressed because one or more lines are too long

View File

@ -132,3 +132,67 @@ class Side(Enum):
ON = auto()
ABOVE = auto()
BELOW = auto()
def stacked_bar(data, series_labels, category_labels=None,
show_values=False, value_format="{}", y_label=None,
grid=True, reverse=False):
"""
Plots a stacked bar chart with the data and labels provided (https://stackoverflow.com/a/50205834).
Keyword arguments:
data -- 2-dimensional numpy array or nested list
containing data for each series in rows
series_labels -- list of series labels (these appear in
the legend)
category_labels -- list of category labels (these appear
on the x-axis)
show_values -- If True then numeric value labels will
be shown on each bar
value_format -- Format string for numeric value labels
(default is "{}")
y_label -- Label for y-axis (str)
grid -- If True display grid
reverse -- If True reverse the order that the
series are displayed (left-to-right
or right-to-left)
"""
ny = len(data[0])
ind = list(range(ny))
axes = []
cum_size = np.zeros(ny)
data = np.array(data)
if reverse:
data = np.flip(data, axis=1)
category_labels = reversed(category_labels)
for i, row_data in enumerate(data):
axes.append(plt.bar(ind, row_data, bottom=cum_size,
label=series_labels[i]))
cum_size += row_data
if category_labels:
plt.xticks(ind, category_labels)
if y_label:
plt.ylabel(y_label)
plt.legend()
if grid:
plt.grid()
if show_values:
for axis in axes:
for bar in axis:
w, h = bar.get_width(), bar.get_height()
if h != 0:
plt.text(bar.get_x() + w/2, bar.get_y() + h/2,
value_format.format(h), ha="center",
va="center")
plt.show()