Skip to content

Commit

Permalink
Merge pull request #732 from analysiscenter/weighted_sampler
Browse files Browse the repository at this point in the history
Add weighted sampler
  • Loading branch information
SergeyTsimfer authored Jan 15, 2024
2 parents bec7bb9 + b49070d commit 78ca0b0
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions batchflow/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def __and__(self, other):
result of the multiplication.
"""
if isinstance(other, (float, int)):
self.weight *= other
return self
return WeightedSampler(self, self.weight * other)

return AndSampler(self, other)

Expand Down Expand Up @@ -346,6 +345,20 @@ def sample(self, size):

return np.concatenate(samples)[:size]

class WeightedSampler(Sampler):
""" Class for implementing `&` (weighting) operation on a number and a `Sampler` instance.
"""
def __init__(self, base, weight, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bases = [base]
self.weight = weight

def sample(self, size):
""" Sampling procedure of a product of the number and the sampler instance. Check out the docstring of
`Sampler.sample` for more info.
"""
return self.bases[0].sample(size)


class BaseOperationSampler(Sampler):
""" Base class for implementing all arithmetic operations on `Sampler`-instances.
Expand Down

0 comments on commit 78ca0b0

Please sign in to comment.