Skip to content

Commit

Permalink
Removed dependency on numpy.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhittaker committed Feb 7, 2021
1 parent 5f284d6 commit a4dfc6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
10 changes: 5 additions & 5 deletions quoracle/quorum_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import datetime
import itertools
import math
import numpy as np
import pulp
import random


T = TypeVar('T')
Expand Down Expand Up @@ -623,12 +623,12 @@ def nodes(self) -> Set[Node[T]]:
return self.qs.nodes()

def get_read_quorum(self) -> Set[T]:
return set(np.random.choice(list(self.sigma_r.keys()),
p=list(self.sigma_r.values())))
return set(random.choices(list(self.sigma_r.keys()),
weights=list(self.sigma_r.values()))[0])

def get_write_quorum(self) -> Set[T]:
return set(np.random.choice(list(self.sigma_w.keys()),
p=list(self.sigma_w.values())))
return set(random.choices(list(self.sigma_w.keys()),
weights=list(self.sigma_w.values()))[0])

def load(self,
read_fraction: Optional[Distribution] = None,
Expand Down
26 changes: 14 additions & 12 deletions quoracle/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import collections
import matplotlib
import matplotlib.pyplot as plt
import numpy as np


T = TypeVar('T')
Expand Down Expand Up @@ -125,22 +124,23 @@ def _plot_node_load_on(ax: plt.Axes,
x_index = {x: i for (i, x) in enumerate(x_list)}
x_ticks = list(range(len(x_list)))

def one_hot(quorum: FrozenSet[T]) -> np.array:
bar_heights = np.zeros(len(x_list))
def one_hot(quorum: FrozenSet[T]) -> List[float]:
bar_heights = [0.0] * len(x_list)
for x in quorum:
bar_heights[x_index[x]] = 1
bar_heights[x_index[x]] = 1.
return bar_heights

width = 0.8
def plot_quorums(sigma: Dict[FrozenSet[T], float],
fraction: float,
bottoms: np.array,
capacities: np.array,
bottoms: List[float],
capacities: List[float],
cmap: matplotlib.colors.Colormap):
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:
bar_heights /= capacities
for j in range(len(bar_heights)):
bar_heights[j] /= capacities[j]

ax.bar(x_ticks,
bar_heights,
Expand All @@ -153,14 +153,16 @@ def plot_quorums(sigma: Dict[FrozenSet[T], float],
if bar_height != 0:
ax.text(x_ticks[j], bottom + bar_height / 2, text,
ha='center', va='center')
bottoms += bar_heights

for j in range(len(bar_heights)):
bottoms[j] += bar_heights[j]

# Plot the quorums.
fr = sum(weight * fr for (fr, weight) in d.items())
fw = 1 - fr
read_capacities = np.array([node.read_capacity for node in nodes])
write_capacities = np.array([node.write_capacity for node in nodes])
bottoms = np.zeros(len(x_list))
read_capacities = [node.read_capacity for node in nodes]
write_capacities = [node.write_capacity for node in nodes]
bottoms = [0.0] * len(x_list)
plot_quorums(sigma.sigma_r, fr, bottoms, read_capacities,
matplotlib.cm.get_cmap('Reds'))
plot_quorums(sigma.sigma_w, fw, bottoms,
Expand Down

0 comments on commit a4dfc6f

Please sign in to comment.