Removed dependency on numpy.

This commit is contained in:
Michael Whittaker 2021-02-07 12:03:31 -08:00
parent 5f284d6661
commit a4dfc6f751
2 changed files with 19 additions and 17 deletions

View file

@ -8,8 +8,8 @@ import collections
import datetime import datetime
import itertools import itertools
import math import math
import numpy as np
import pulp import pulp
import random
T = TypeVar('T') T = TypeVar('T')
@ -623,12 +623,12 @@ class Strategy(Generic[T]):
return self.qs.nodes() return self.qs.nodes()
def get_read_quorum(self) -> Set[T]: def get_read_quorum(self) -> Set[T]:
return set(np.random.choice(list(self.sigma_r.keys()), return set(random.choices(list(self.sigma_r.keys()),
p=list(self.sigma_r.values()))) weights=list(self.sigma_r.values()))[0])
def get_write_quorum(self) -> Set[T]: def get_write_quorum(self) -> Set[T]:
return set(np.random.choice(list(self.sigma_w.keys()), return set(random.choices(list(self.sigma_w.keys()),
p=list(self.sigma_w.values()))) weights=list(self.sigma_w.values()))[0])
def load(self, def load(self,
read_fraction: Optional[Distribution] = None, read_fraction: Optional[Distribution] = None,

View file

@ -8,7 +8,6 @@ from typing import Dict, FrozenSet, List, Optional, Set, Tuple, TypeVar
import collections import collections
import matplotlib import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np
T = TypeVar('T') T = TypeVar('T')
@ -125,22 +124,23 @@ def _plot_node_load_on(ax: plt.Axes,
x_index = {x: i for (i, x) in enumerate(x_list)} x_index = {x: i for (i, x) in enumerate(x_list)}
x_ticks = list(range(len(x_list))) x_ticks = list(range(len(x_list)))
def one_hot(quorum: FrozenSet[T]) -> np.array: def one_hot(quorum: FrozenSet[T]) -> List[float]:
bar_heights = np.zeros(len(x_list)) bar_heights = [0.0] * len(x_list)
for x in quorum: for x in quorum:
bar_heights[x_index[x]] = 1 bar_heights[x_index[x]] = 1.
return bar_heights return bar_heights
width = 0.8 width = 0.8
def plot_quorums(sigma: Dict[FrozenSet[T], float], def plot_quorums(sigma: Dict[FrozenSet[T], float],
fraction: float, fraction: float,
bottoms: np.array, bottoms: List[float],
capacities: np.array, capacities: List[float],
cmap: matplotlib.colors.Colormap): cmap: matplotlib.colors.Colormap):
for (i, (quorum, weight)) in enumerate(sigma.items()): for (i, (quorum, weight)) in enumerate(sigma.items()):
bar_heights = scale * fraction * weight * one_hot(quorum) bar_heights = [scale * fraction * weight * x for x in one_hot(quorum)]
if scale_by_node_capacity: if scale_by_node_capacity:
bar_heights /= capacities for j in range(len(bar_heights)):
bar_heights[j] /= capacities[j]
ax.bar(x_ticks, ax.bar(x_ticks,
bar_heights, bar_heights,
@ -153,14 +153,16 @@ def _plot_node_load_on(ax: plt.Axes,
if bar_height != 0: if bar_height != 0:
ax.text(x_ticks[j], bottom + bar_height / 2, text, ax.text(x_ticks[j], bottom + bar_height / 2, text,
ha='center', va='center') ha='center', va='center')
bottoms += bar_heights
for j in range(len(bar_heights)):
bottoms[j] += bar_heights[j]
# Plot the quorums. # Plot the quorums.
fr = sum(weight * fr for (fr, weight) in d.items()) fr = sum(weight * fr for (fr, weight) in d.items())
fw = 1 - fr fw = 1 - fr
read_capacities = np.array([node.read_capacity for node in nodes]) read_capacities = [node.read_capacity for node in nodes]
write_capacities = np.array([node.write_capacity for node in nodes]) write_capacities = [node.write_capacity for node in nodes]
bottoms = np.zeros(len(x_list)) bottoms = [0.0] * len(x_list)
plot_quorums(sigma.sigma_r, fr, bottoms, read_capacities, plot_quorums(sigma.sigma_r, fr, bottoms, read_capacities,
matplotlib.cm.get_cmap('Reds')) matplotlib.cm.get_cmap('Reds'))
plot_quorums(sigma.sigma_w, fw, bottoms, plot_quorums(sigma.sigma_w, fw, bottoms,