Mock Samplable Set

Contains a MockSamplableSet class that mimicks the behavior of github.com/gstonge/SamplableSet but is less efficient.

class epipack.mock_samplable_set.MockSamplableSet(min_weight, max_weight, weighted_elements=[], cpp_type='int')[source]

Bases: object

A set of items that can be sampled with probability proportional to a corresponding item weight.

Mimicks the behavior of github.com/gstonge/SamplableSet without being as efficient.

Works similar to Python's set, with __getitem__, __setitem__, __delitem__, __iter__, __len__, __contains__.

Parameters
  • min_weight (float) -- minimum possible weight

  • max_weight (float) -- maximum possible weight

  • weighted_elements (list, default = []) -- list of 2-tuples, first entry an item, second entry a weight

  • cpp_type (str, default = 'int') -- The type of the items.

min_weight

minimum possible weight

Type

float

max_weight

maximum possible weight

Type

float

items

list of items in this set

Type

numpy.ndarray

weights

list of corresponding weights

Type

numpy.ndarray

clear()[source]

Reset the set. Not implemented yet.

sample()[source]

Random sample from the set, sampled with probability proportional to items' weight.

Returns

  • item (cpp_type) -- An item from the set

  • weight (float) -- The weight of the item

total_weight()[source]

Obtain the total weight of the set

epipack.mock_samplable_set.choice(arr, p)[source]

Returns a random element from arr with probability given in array p. If arr is not an iterable, the function returns the index of the chosen element.